Boy.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import EventType from "./EventType";
  2. import MatchPanel from "../Panel/MatchPanel";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class Boy extends cc.Component {
  6. //unique
  7. index:number;
  8. //childNodes
  9. headphoto:cc.Node;
  10. arrow:cc.Node
  11. body:cc.Node;
  12. saw:cc.Node;
  13. fire:cc.Node;
  14. //components
  15. rigidBody:cc.RigidBody;
  16. armatureDisplay:dragonBones.ArmatureDisplay;
  17. sawAnimate:cc.Animation;
  18. //properties
  19. speed:number = 400;
  20. acc:cc.Vec2 = cc.v2(0,0);
  21. angleCorrectSpeed = 150;
  22. color:cc.Color = cc.Color.WHITE;
  23. //flags
  24. attacking:boolean = false;
  25. isAutoAttack:boolean = true;
  26. onGround:number = 0;
  27. dead:boolean = false;
  28. bouncing:boolean = false;
  29. keepingRun:boolean = false;
  30. /**名次 */
  31. rankingNum:number;
  32. onLoad(){
  33. this.node.zIndex = 1;
  34. this.body = this.node.getChildByName('Body');
  35. this.fire = this.body.getChildByName('Fire');
  36. this.saw = this.body.getChildByName('Saw');
  37. this.saw.addComponent(Saw).boy = this;
  38. this.arrow = this.node.getChildByName('Arrow');
  39. this.headphoto = this.node.getChildByName('HeadPhoto');
  40. this.rigidBody = this.node.getComponent(cc.RigidBody);
  41. this.armatureDisplay = this.body.getComponent(dragonBones.ArmatureDisplay);
  42. this.sawAnimate = this.saw.getComponent(cc.Animation);
  43. //init
  44. this.armatureDisplay.timeScale = 2;
  45. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  46. if(this.index==0){
  47. this.inActiveHeadPhoto();
  48. this.activeArrow();
  49. this.scheduleOnce(()=>{
  50. this.inActiveArrow();
  51. this.activeHeadPhoto();
  52. },1.5);
  53. }else{
  54. this.fire.active = false;
  55. this.activeHeadPhoto();
  56. }
  57. }
  58. getIdleName(){
  59. return 'daiji' + BoyColor.getColorWith_(this.index);
  60. }
  61. getMoveName(){
  62. return 'xingzou' + BoyColor.getColorWith_(this.index);
  63. }
  64. getHappName(){
  65. return 'bianlian' + BoyColor.getColorWith_(this.index);
  66. }
  67. addAcc(acc:cc.Vec2){
  68. this.acc = this.acc.add(acc);
  69. }
  70. subAcc(acc:cc.Vec2){
  71. this.acc = this.acc.sub(acc);
  72. }
  73. activeArrow(){
  74. this.arrow.active = true;
  75. this.arrow.getComponent(cc.Sprite).spriteFrame =
  76. (MatchPanel.palyerInfos[this.index].gender==2?
  77. window.rs.sf_arrowPink:
  78. window.rs.sf_arrowBlue
  79. );
  80. this.arrow.runAction(
  81. cc.repeatForever(cc.sequence(
  82. cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,1)),
  83. cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,1)),
  84. ))
  85. );
  86. }
  87. inActiveArrow(){
  88. this.arrow.stopAllActions();
  89. this.arrow.active = false;
  90. }
  91. activeHeadPhoto(){
  92. this.headphoto.active = true;
  93. let avatar = this.headphoto.getChildByName("Mask").getChildByName("Avatar").getComponent(cc.Sprite);
  94. avatar.spriteFrame = MatchPanel.palyerInfos[this.index].avatar;
  95. this.headphoto.runAction(cc.repeatForever(cc.sequence(
  96. cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,1)),
  97. cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,1)),
  98. )));
  99. }
  100. inActiveHeadPhoto(){
  101. this.headphoto.active = false;
  102. }
  103. cancelRun(){
  104. this.keepingRun = false;
  105. }
  106. forward(direction:number){
  107. this.body.scaleX = direction * Math.abs(this.body.scaleX);
  108. if(this.onGround>0){
  109. this.keepingRun = true;
  110. }
  111. // this.scheduleOnce(function(){
  112. // this.keepingRun = false;
  113. // },0.2);
  114. }
  115. move(){
  116. let basicAngle = this.body.scaleX < 0 ? 180 : 0;
  117. let radian = (basicAngle + this.body.angle) / 180 * Math.PI;
  118. this.rigidBody.linearVelocity = cc.v2(
  119. Math.cos(radian) * this.speed,
  120. Math.sin(radian) * this.speed
  121. );
  122. }
  123. jump(){
  124. this.keepingRun = false;
  125. if(this.body.scaleX<0){
  126. this.leftUp();
  127. }else if(this.body.scaleX>0){
  128. this.rightUp();
  129. }
  130. }
  131. up(){
  132. if(this.bouncing)return;
  133. this.keepingRun = false;
  134. this.rigidBody.linearVelocity = cc.v2(0,this.speed*1.5);
  135. }
  136. leftUp(){
  137. if(this.bouncing)return;
  138. this.body.scaleX = -1 * Math.abs(this.body.scaleX);
  139. this.rigidBody.linearVelocity = cc.v2(-this.speed*0.5,this.speed*1.5);
  140. }
  141. rightUp(){
  142. if(this.bouncing)return;
  143. this.body.scaleX = 1 * Math.abs(this.body.scaleX);
  144. this.rigidBody.linearVelocity = cc.v2(this.speed*0.5,this.speed*1.5);
  145. }
  146. attack(){
  147. if(!this.attacking){
  148. this.attacking = true;
  149. this.sawAnimate.play('Saw');
  150. cc.audioEngine.playEffect(window.rs.ac_attack,false);
  151. }
  152. }
  153. autoAttack(){
  154. if(this.isAutoAttack){
  155. return this.isAutoAttack = false;
  156. }else{
  157. return this.isAutoAttack = true;
  158. }
  159. }
  160. die(){
  161. if(this.dead)return;
  162. this.dead = true;
  163. let node = cc.instantiate(window.rs.pf_blood);
  164. node.setPosition(this.node.position);
  165. let armatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
  166. armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE,()=>{
  167. if(armatureDisplay.animationName=='blood1'){
  168. this.armatureDisplay.playAnimation('blood2',1);
  169. }
  170. });
  171. window.gc.bloodGroup.addChild(node);
  172. window.gm.removeBoy(this);
  173. cc.audioEngine.playEffect(window.rs.ac_bu,false);
  174. }
  175. update(dt:number){
  176. if(this.onGround>0){
  177. this.rigidBody.gravityScale = 5;
  178. if(window.gm.isGameOver){
  179. if(this.armatureDisplay.animationName!=this.getHappName()){
  180. this.armatureDisplay.playAnimation(this.getHappName(),0);
  181. }
  182. }else{
  183. if(this.keepingRun){
  184. this.move();
  185. if(this.armatureDisplay.animationName!=this.getMoveName()){
  186. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  187. }
  188. }else{
  189. if(this.armatureDisplay.animationName!=this.getIdleName()){
  190. this.armatureDisplay.playAnimation(this.getIdleName(),0);
  191. }
  192. }
  193. }
  194. }else{
  195. this.rigidBody.gravityScale = 5;
  196. if(this.body.angle<0){
  197. let nextAngle = this.body.angle + this.angleCorrectSpeed*dt;
  198. if(nextAngle>0){
  199. this.body.angle = 0;
  200. }else{
  201. this.body.angle = nextAngle;
  202. }
  203. }else if(this.body.angle>0){
  204. let nextAngle = this.body.angle - this.angleCorrectSpeed*dt;
  205. if(nextAngle<0){
  206. this.body.angle = 0;
  207. }else{
  208. this.body.angle = nextAngle;
  209. }
  210. }
  211. if(this.armatureDisplay.animationName!=this.getMoveName()){
  212. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  213. }
  214. }
  215. this.rigidBody.linearVelocity = this.rigidBody.linearVelocity.add(cc.v2(this.acc.x*dt,this.acc.y*dt));
  216. }
  217. /**
  218. * bounce when a player step on another player
  219. * @param direction 1:up,-1:down
  220. */
  221. bounce(direction:number){
  222. this.bouncing = true;
  223. this.rigidBody.linearVelocity = cc.v2(0,direction*this.speed*1.5);
  224. this.scheduleOnce(()=>{
  225. this.bouncing = false;
  226. },0.1)
  227. }
  228. grounds:cc.Node[] = [];
  229. removeGround(ground:cc.Node){
  230. let arr = [];
  231. this.grounds.forEach((node:cc.Node)=>{
  232. if(ground!=node){
  233. arr.push(node);
  234. }
  235. });
  236. this.grounds = arr;
  237. }
  238. onCollisionEnter(other:cc.Collider){
  239. if(other.node.group==EventType.GROUP_GROUND){
  240. this.onGround++;
  241. this.grounds.push(other.node);
  242. this.body.angle = other.node.angle;
  243. }
  244. if(other.node.group==EventType.GROUP_BOY){
  245. let distance = this.node.y - other.node.y;
  246. if(distance<-25){
  247. this.bounce(-1);
  248. }else if(distance>25){
  249. this.bounce(1);
  250. }
  251. }
  252. }
  253. onCollisionExit(other:cc.Collider){
  254. if(other.node.group==EventType.GROUP_GROUND){
  255. this.onGround--;
  256. this.removeGround(other.node);
  257. if(this.grounds.length>0){
  258. this.body.angle = this.grounds[this.grounds.length-1].angle;
  259. }
  260. }
  261. }
  262. }
  263. class BoyColor {
  264. static colors = ['red','green','blue','yellow'];
  265. static mapColors = [cc.Color.RED,cc.Color.GREEN,cc.Color.BLUE,cc.Color.YELLOW];
  266. static getColorWith_(index:number){
  267. return '_' + BoyColor.colors[index];
  268. }
  269. }
  270. class Saw extends cc.Component{
  271. boy:Boy;
  272. onCollisionStay(other:cc.Collider){
  273. if(other.node==this.boy.node||other.node.group==EventType.GROUP_GROUND)return;
  274. if(other.node.group==EventType.GROUP_SAW){
  275. if(this.boy.autoAttack){
  276. this.boy.attack();
  277. }
  278. return;
  279. }
  280. if(other.node.group==EventType.GROUP_BOY){
  281. if(this.boy.autoAttack){
  282. this.boy.attack();
  283. }
  284. if(this.boy.attacking){
  285. let otherBoy = other.getComponent(Boy);
  286. this.kill(otherBoy);
  287. }
  288. return;
  289. }
  290. }
  291. kill(otherBoy:Boy){
  292. if(otherBoy){
  293. if(this.boy.node.y>=otherBoy.node.y){
  294. if(!this.boy.dead){
  295. otherBoy.die();
  296. }
  297. }
  298. }
  299. }
  300. attackFinish(){
  301. this.boy.attacking = false;
  302. }
  303. }