import EventType from "./EventType"; import RoleDress from "./RoleDress"; const {ccclass, property} = cc._decorator; @ccclass export default class Role extends cc.Component { index:number; //components rigidBody:cc.RigidBody; armatureDisplay:dragonBones.ArmatureDisplay; //skeleton body:cc.Node; saw:cc.Node; //property speed:number = 400; color:cc.Color = cc.Color.WHITE; //flag onGround:number = 0; left:boolean = false; right:boolean = false; up:boolean = false; onLoad(){ this.node.zIndex = 1; this.body = this.node.getChildByName('Body'); this.saw = this.body.getChildByName('Saw'); this.body.addComponent(Body).role = this; this.saw.addComponent(Saw).role = this; this.rigidBody = this.node.getComponent(cc.RigidBody); this.armatureDisplay = this.body.getComponent(dragonBones.ArmatureDisplay); this.node.group = EventType.GROUP_ROLE; this.body.group = EventType.GROUP_SKELETON; this.saw.group = EventType.GROUP_SKELETON; } start(){ RoleDress.changeColor(this.armatureDisplay.armature(), this.index); } update(){ if(this.onGround>0){ this.rigidBody.gravityScale = 0; if(this.left){ this.body.scaleX = -Math.abs(this.body.scaleX); let radian = (180 + this.body.angle) / 180 * Math.PI; this.rigidBody.linearVelocity = cc.v2( Math.cos(radian) * this.speed, Math.sin(radian) * this.speed ); } if(this.right){ this.body.scaleX = Math.abs(this.body.scaleX); let radian = this.body.angle / 180 * Math.PI; this.rigidBody.linearVelocity = cc.v2( Math.cos(radian) * this.speed, Math.sin(radian) * this.speed ); } if(this.up){ this.rigidBody.linearVelocity = cc.v2( this.rigidBody.linearVelocity.x, this.speed * 1.5 ); } if(!this.left&&!this.right&&!this.up){ this.rigidBody.linearVelocity = cc.v2(0,0); if(this.armatureDisplay.animationName!='idle'+(this.saw.active?'2':'')){ this.armatureDisplay.playAnimation('idle'+(this.saw.active?'2':''),0); } }else{ if(this.armatureDisplay.animationName!='move'+(this.saw.active?'2':'')){ this.armatureDisplay.playAnimation('move'+(this.saw.active?'2':''),0); } } }else{ this.rigidBody.gravityScale = 5; if(this.body.angle<0){ let nextAngle = this.body.angle + 3; if(nextAngle>0){ this.body.angle = 0; }else{ this.body.angle = nextAngle; } }else if(this.body.angle>0){ let nextAngle = this.body.angle - 3; if(nextAngle<0){ this.body.angle = 0; }else{ this.body.angle = nextAngle; } } } } onCollisionEnter(other:cc.Collider){ if(other.node.group==EventType.GROUP_GROUND){ this.onGround++; } } onCollisionStay(other:cc.Collider){ if(other.node.group==EventType.GROUP_GROUND){ this.body.angle = other.node.angle; } } onCollisionExit(other:cc.Collider){ if(other.node.group==EventType.GROUP_GROUND){ this.onGround--; } } bounce(direction:number){ this.up = true; this.scheduleOnce(()=>{ this.up = false; },0.05); this.rigidBody.linearVelocity = cc.v2( direction*800, 1200 ); } pickUpSaw():boolean{ if(this.saw.active){ return false; }else{ this.saw.active = true; return true; } } loseSaw(){ if(this.saw.active){ this.saw.active = false; window.controller.createSaw( this.node.position.add(cc.v2(0,80)), cc.v2(Math.random()<0.5?-100:100,1000), 300 ); } } die(){ let node = new cc.Node(); node.addComponent(cc.Sprite).spriteFrame = window.resource.sf_Blood; node.setPosition(this.node.position); node.anchorY = 0; node.scale = 0; window.map.node.addChild(node); node.runAction(cc.scaleTo(0.1,0.5)); window.controller.destroyRole(this); } activeDeltaGreen(){ let myDelta = this.body.getChildByName('DeltaGreen'); myDelta.runAction(cc.repeatForever(cc.sequence( cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,-1)), cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,-1)) ))); myDelta.active = true; } } class Body extends cc.Component{ role:Role; onCollisionEnter(other:cc.Collider){ if(other.node.name=='Saw'&&other.node!=this.role.saw){ this.role.loseSaw(); this.role.die(); } } } class Saw extends cc.Component{ role:Role; onCollisionEnter(other:cc.Collider){ if(other.node.name=='Saw'){ this.role.loseSaw(); if(this.role.node.x