123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // Learn TypeScript:
- // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
- // Learn Attribute:
- // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
- import { Component, _decorator,Node, Vec2, v2, Vec3, UITransform, Camera, Color, Sprite, math, game } from "cc";
- import { GameEvent } from "../../../Common/Base/GameEventConfig";
- import GameEventManager from "../../../Common/Base/GameEventManager";
- import { GameManager } from "../../../Manager/GameManager";
- import GameMainCtrl from "../../Ctrl/GameMainCtrl";
- import LevelCtrl from "../../Ctrl/LevelCtrl";
- import GameUtils from "../../GameUtils/GameUtils";
- import GameMainLine from "./GameMainLine";
- const { ccclass, property } = _decorator;
- @ccclass
- export default class GameMainPoint extends Component {
- private _indexID = 0
- private _ropeDataList: { rope: GameMainLine, linkPoint: GameMainPoint }[] = [];
- private color:number;
- private canmove:boolean=true;
- resetData(id: number) {
- this._indexID = id
- //绑定人头的移动事件
- this.node.on(Node.EventType.TOUCH_START, this._onTouch, this);
- this.node.on(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
- this.node.on(Node.EventType.TOUCH_END, this._onTouchEnd, this);
- this.node.on(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
- this.canmove=true;
- }
- //给点存线头和线的信息
- addRopeData(data: { rope: GameMainLine, linkPoint: GameMainPoint }) {
- this._ropeDataList.push(data)
- this.setRopeInfo(this.node.getPosition(), data.linkPoint.node.getPosition(), data.rope.node)
- }
- getLineNum() {
- return this._ropeDataList.length
- }
- private _onTouch(event) {
- if(!this.canmove) return;
- // this.resetGesturesData()
- // GameUtils.getInstance().setVisible(this.gesturesOKBtn, false)
- // GameUtils.getInstance().setVisible(this.gesturesRedrawBtn, false)
- }
- public destroySelf(){
- this.canmove=false;
- this.node.off(Node.EventType.TOUCH_START, this._onTouch, this);
- this.node.off(Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
- this.node.off(Node.EventType.TOUCH_END, this._onTouchEnd, this);
- this.node.off(Node.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
-
- this.node.destroy();
- }
- //移动线头,线跟着移动
- private _onTouchMove(event) {
- if(!this.canmove) return;
- var touches = event.getTouches();
- let location=touches[0].getUILocation()
- let pos1=new Vec3(location.x,location.y,0)
- let movePos=this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(pos1);
- // console.log(movePos);
- if(Math.abs(movePos.x)>this.node.parent.getComponent(UITransform).width/2){
- movePos.x=this.node.parent.getComponent(UITransform).width/2*(movePos.x>0?1:-1);
- }
- if(Math.abs(movePos.y)>this.node.parent.getComponent(UITransform).height/2){
- movePos.y=this.node.parent.getComponent(UITransform).height/2*(movePos.y>0?1:-1);
- }
- this.node.setPosition(movePos)
- for (let index = 0; index < this._ropeDataList.length; index++) {
- let ropeData = this._ropeDataList[index]
- this.setRopeInfo(this.node.getPosition(), ropeData.linkPoint.node.getPosition(), ropeData.rope.node)
- }
- }
- //结束移动,判断线与线的相交数量来显示线的颜色
- private _onTouchEnd() {
- if(!this.canmove) return;
- this.resetPointLine()
- if (GameMainCtrl.getInstance().checkIsGameOver()) {//游戏通关
- // let levelData = GameMainCtrl.getInstance().getNowLevelData()
- this.canmove=false;
- GameManager.getInstance().addScore(100);
- //let level=LevelCtrl.getInstance().getPassLevel();
- //LevelCtrl.getInstance().setPassLevel(levelData.level+1>=level?levelData.level+1:level);
- }else{
-
- GameEventManager.getInstance().dispathcGameEvent(GameEvent.MonkeyMove);
- }
- }
- //重置线的颜色
- resetPointLine() {
- for (let index = 0; index < this._ropeDataList.length; index++) {
- let ropeData = this._ropeDataList[index]
- this.setRopeInfo(this.node.getPosition(), ropeData.linkPoint.node.getPosition(), ropeData.rope.node)
- //ropeData.rope
- // cc.log("相交点数", num)
- // ropeData.rope.setColor(num)
- }
- GameMainCtrl.getInstance().resetLineIntersection();
-
- }
- //设置当前绳子的角度,长度和位置
- setRopeInfo(p1: Vec3, p2: Vec3, ropeNode: Node) {
- let dis = GameUtils.getInstance().distance(p1, p2)
- let x = (p1.x + p2.x) / 2
- let y = (p1.y + p2.y) / 2
- let ang = GameUtils.getInstance().angle(p1, p2)
- GameUtils.getInstance().setAngle(ropeNode, ang)
- ropeNode.setPosition(new Vec3(x, y))
- ropeNode.getComponent(UITransform).width = dis
- }
- ResetColorNum(){
- this.color=0;
- }
- AddColorNum(num:number){
- this.color+=num;
- this.setColor();
- }
- //设置点的颜色
- setColor() {
- let allColor = [Color.GREEN, Color.YELLOW, Color.RED]
- this.color = this.color >= 3 ? 2 : this.color
- this.node.children[0].getComponent(Sprite).color = allColor[this.color];
- }
- }
|