123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { GameManager } from "../Manager/GameManager";
- import {GameMain} from "./GameMain";
- import { _decorator, Component, Node,Collider2D, Contact2DType,IPhysics2DContact,PhysicsSystem2D, RigidBody2D, Vec2, Vec3} from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('Role')
- export class Role extends Component {
- @property(GameMain)
- main:GameMain = null;//获取main脚本
- moveSpeed=0;
- index:number=0;
- isInEnd=false;
- initPos=Vec3.ZERO;
- start () {
- // 注册单个碰撞体的回调函数
- let collider = this.getComponent(Collider2D);
- if (collider) {
- console.log('onBeginContact Role');
- collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
-
- }
- this.moveSpeed=(GameManager.getInstance().curLevel-1)*0.2;
- this.initPos=this.node.position;
- }
-
- onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // 只在两个碰撞体开始接触时被调用一次
- console.log('onBeginContact='+otherCollider.node.name);
- //
- if(otherCollider.tag==0)
- {
-
- if(this.node.name=="Role1")
- {
- this.index = 0;
- }
- else if(this.node.name=="Role2")
- {
- this.index = 1;
- }
- else if(this.node.name=="Role3")
- {
- this.index = 2;
- }
- else if(this.node.name=="Role4")
- {
- this.index = 3;
- }
- this.main.GameOver(this.index);//游戏结束
- this.resetStartPostion();
- this.main.RestoreBtn();
- //this.node.active = false;
- }
- else if(otherCollider.tag==1)
- {
- this.isInEnd=true;
- this.main.Success();
- this.resetStartPostion();
- }
- else if(otherCollider.tag==3){
- this.main.RestoreBtn();
- }
-
- }
- protected update(dt: number): void {
- if(!GameManager.getInstance().isStartGame)
- return;
- if(this.main.hitList[this.index]==-1){
- let xAxis = this.node.position.x;
- this.node.setPosition(xAxis+2.2+this.moveSpeed,this.node.position.y)
- }
- }
- resetStartPostion()
- {
- let minPosX= this.main.nodeList[0].position.x;
- for (let i = 1; i < 4; i++) {
- if(this.main.nodeList[i].position.x<minPosX)
- {
- minPosX=this.main.nodeList[i].position.x;
- }
- }
- if(GameManager.getInstance().curLevel<5)
- {
- this.node.setPosition(minPosX-600,this.initPos.y);
- }else{
- this.node.setPosition(minPosX-400,this.initPos.y);
- }
-
- console.log('resetStartPostion');
- }
-
- }
|