Role.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import EventType from "./EventType";
  2. import RoleDress from "./RoleDress";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class Role extends cc.Component {
  6. index:number;
  7. //components
  8. rigidBody:cc.RigidBody;
  9. armatureDisplay:dragonBones.ArmatureDisplay;
  10. //skeleton
  11. body:cc.Node;
  12. saw:cc.Node;
  13. //property
  14. speed:number = 400;
  15. color:cc.Color = cc.Color.WHITE;
  16. //flag
  17. onGround:number = 0;
  18. left:boolean = false;
  19. right:boolean = false;
  20. up:boolean = false;
  21. onLoad(){
  22. this.node.zIndex = 1;
  23. this.body = this.node.getChildByName('Body');
  24. this.saw = this.body.getChildByName('Saw');
  25. this.body.addComponent(Body).role = this;
  26. this.saw.addComponent(Saw).role = this;
  27. this.rigidBody = this.node.getComponent(cc.RigidBody);
  28. this.armatureDisplay = this.body.getComponent(dragonBones.ArmatureDisplay);
  29. this.node.group = EventType.GROUP_ROLE;
  30. this.body.group = EventType.GROUP_SKELETON;
  31. this.saw.group = EventType.GROUP_SKELETON;
  32. }
  33. start(){
  34. RoleDress.changeColor(this.armatureDisplay.armature(), this.index);
  35. }
  36. update(){
  37. if(this.onGround>0){
  38. this.rigidBody.gravityScale = 0;
  39. if(this.left){
  40. this.body.scaleX = -Math.abs(this.body.scaleX);
  41. let radian = (180 + this.body.angle) / 180 * Math.PI;
  42. this.rigidBody.linearVelocity = cc.v2(
  43. Math.cos(radian) * this.speed,
  44. Math.sin(radian) * this.speed
  45. );
  46. }
  47. if(this.right){
  48. this.body.scaleX = Math.abs(this.body.scaleX);
  49. let radian = this.body.angle / 180 * Math.PI;
  50. this.rigidBody.linearVelocity = cc.v2(
  51. Math.cos(radian) * this.speed,
  52. Math.sin(radian) * this.speed
  53. );
  54. }
  55. if(this.up){
  56. this.rigidBody.linearVelocity = cc.v2(
  57. this.rigidBody.linearVelocity.x,
  58. this.speed * 1.5
  59. );
  60. }
  61. if(!this.left&&!this.right&&!this.up){
  62. this.rigidBody.linearVelocity = cc.v2(0,0);
  63. if(this.armatureDisplay.animationName!='idle'+(this.saw.active?'2':'')){
  64. this.armatureDisplay.playAnimation('idle'+(this.saw.active?'2':''),0);
  65. }
  66. }else{
  67. if(this.armatureDisplay.animationName!='move'+(this.saw.active?'2':'')){
  68. this.armatureDisplay.playAnimation('move'+(this.saw.active?'2':''),0);
  69. }
  70. }
  71. }else{
  72. this.rigidBody.gravityScale = 5;
  73. if(this.body.angle<0){
  74. let nextAngle = this.body.angle + 3;
  75. if(nextAngle>0){
  76. this.body.angle = 0;
  77. }else{
  78. this.body.angle = nextAngle;
  79. }
  80. }else if(this.body.angle>0){
  81. let nextAngle = this.body.angle - 3;
  82. if(nextAngle<0){
  83. this.body.angle = 0;
  84. }else{
  85. this.body.angle = nextAngle;
  86. }
  87. }
  88. }
  89. }
  90. onCollisionEnter(other:cc.Collider){
  91. if(other.node.group==EventType.GROUP_GROUND){
  92. this.onGround++;
  93. }
  94. }
  95. onCollisionStay(other:cc.Collider){
  96. if(other.node.group==EventType.GROUP_GROUND){
  97. this.body.angle = other.node.angle;
  98. }
  99. }
  100. onCollisionExit(other:cc.Collider){
  101. if(other.node.group==EventType.GROUP_GROUND){
  102. this.onGround--;
  103. }
  104. }
  105. bounce(direction:number){
  106. this.up = true;
  107. this.scheduleOnce(()=>{
  108. this.up = false;
  109. },0.05);
  110. this.rigidBody.linearVelocity = cc.v2(
  111. direction*800,
  112. 1200
  113. );
  114. }
  115. pickUpSaw():boolean{
  116. if(this.saw.active){
  117. return false;
  118. }else{
  119. this.saw.active = true;
  120. return true;
  121. }
  122. }
  123. loseSaw(){
  124. if(this.saw.active){
  125. this.saw.active = false;
  126. window.controller.createSaw(
  127. this.node.position.add(cc.v2(0,80)),
  128. cc.v2(Math.random()<0.5?-100:100,1000),
  129. 300
  130. );
  131. }
  132. }
  133. die(){
  134. let node = new cc.Node();
  135. node.addComponent(cc.Sprite).spriteFrame = window.resource.sf_Blood;
  136. node.setPosition(this.node.position);
  137. node.anchorY = 0;
  138. node.scale = 0;
  139. window.map.node.addChild(node);
  140. node.runAction(cc.scaleTo(0.1,0.5));
  141. window.controller.destroyRole(this);
  142. }
  143. activeDeltaGreen(){
  144. let myDelta = this.body.getChildByName('DeltaGreen');
  145. myDelta.runAction(cc.repeatForever(cc.sequence(
  146. cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,-1)),
  147. cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,-1))
  148. )));
  149. myDelta.active = true;
  150. }
  151. }
  152. class Body extends cc.Component{
  153. role:Role;
  154. onCollisionEnter(other:cc.Collider){
  155. if(other.node.name=='Saw'&&other.node!=this.role.saw){
  156. this.role.loseSaw();
  157. this.role.die();
  158. }
  159. }
  160. }
  161. class Saw extends cc.Component{
  162. role:Role;
  163. onCollisionEnter(other:cc.Collider){
  164. if(other.node.name=='Saw'){
  165. this.role.loseSaw();
  166. if(this.role.node.x<other.node.parent.parent.x){
  167. this.role.bounce(-1);
  168. }else{
  169. this.role.bounce(1);
  170. }
  171. // if(this.node.group==EventType.GROUP_SELF){
  172. // window.map.node.runAction(cc.shake(0.3,10,10));
  173. // }
  174. }
  175. }
  176. }