Boy.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import PIFS from "../PIFS";
  2. import MatchView from "../Views/MatchView";
  3. import Bot from "./Bot";
  4. import EventType from "./EventType";
  5. import Utils from "./Utils";
  6. const {ccclass, property} = cc._decorator;
  7. @ccclass
  8. export default class Boy extends cc.Component {
  9. //unique
  10. index:number;
  11. //childNodes
  12. headphoto:cc.Node;
  13. arrow:cc.Node
  14. body:cc.Node;
  15. saw:cc.Node;
  16. fire:cc.Node;
  17. //components
  18. rigidBody:cc.RigidBody;
  19. armatureDisplay:dragonBones.ArmatureDisplay;
  20. sawAnimate:cc.Animation;
  21. //properties
  22. speed:number = 400;
  23. acc:cc.Vec2 = cc.v2(0,0);
  24. angleCorrectSpeed = 150;
  25. color:cc.Color = cc.Color.WHITE;
  26. //flags
  27. attacking:boolean = false;
  28. isAutoAttack:boolean = true;
  29. onGround:number = 0;
  30. dead:boolean = false;
  31. bouncing:boolean = false;
  32. keepingRun:boolean = false;
  33. /**名次 */
  34. rankingNum:number;
  35. onLoad(){
  36. this.node.zIndex = 1;
  37. // this.body = this.node.getChildByName('Body');
  38. this.body = this.node.children[this.index];
  39. this.body.active = true;
  40. this.fire = this.body.getChildByName('Fire');
  41. this.saw = this.body.getChildByName('Saw');
  42. this.saw.addComponent(Saw).boy = this;
  43. this.arrow = this.node.getChildByName('Arrow');
  44. this.headphoto = this.node.getChildByName('HeadPhoto');
  45. this.rigidBody = this.node.getComponent(cc.RigidBody);
  46. this.armatureDisplay = this.body.getComponent(dragonBones.ArmatureDisplay);
  47. this.sawAnimate = this.saw.getComponent(cc.Animation);
  48. //init
  49. this.armatureDisplay.timeScale = 2;
  50. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  51. if(this.index==window.gameSystem.masterId){
  52. this.inActiveHeadPhoto();
  53. this.activeArrow();
  54. this.scheduleOnce(()=>{
  55. this.inActiveArrow();
  56. this.activeHeadPhoto();
  57. },1.5);
  58. }else{
  59. this.fire.active = false;
  60. this.activeHeadPhoto();
  61. }
  62. }
  63. getIdleName(){
  64. return this.attacking ? "attack2" : "idle2";
  65. // return 'daiji' + BoyColor.getColorWith_(this.index);
  66. }
  67. getMoveName(){
  68. return this.attacking ? "attack2" : "move2";
  69. // return 'xingzou' + BoyColor.getColorWith_(this.index);
  70. }
  71. getHappName(){
  72. return "happy";
  73. // return 'bianlian' + BoyColor.getColorWith_(this.index);
  74. }
  75. addAcc(acc:cc.Vec2){
  76. this.acc = this.acc.add(acc);
  77. }
  78. subAcc(acc:cc.Vec2){
  79. this.acc = this.acc.sub(acc);
  80. }
  81. activeArrow(){
  82. this.arrow.active = true;
  83. this.arrow.getComponent(cc.Sprite).spriteFrame =
  84. (PIFS.matchPlayerInfos[this.index].gender==1?
  85. window.rs.sf_arrowPink:
  86. window.rs.sf_arrowBlue
  87. );
  88. this.arrow.runAction(
  89. cc.repeatForever(cc.sequence(
  90. cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,1)),
  91. cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,1)),
  92. ))
  93. );
  94. }
  95. inActiveArrow(){
  96. this.arrow.stopAllActions();
  97. this.arrow.active = false;
  98. }
  99. activeHeadPhoto(){
  100. this.headphoto.active = true;
  101. let avatar = this.headphoto.getChildByName("Mask").getChildByName("Avatar").getComponent(cc.Sprite);
  102. Utils.LoadSpriteFrame(PIFS.matchPlayerInfos[this.index].avatarUrl, (spriteFrame) => {
  103. avatar.spriteFrame = spriteFrame;
  104. });
  105. this.headphoto.runAction(cc.repeatForever(cc.sequence(
  106. cc.spawn(cc.moveBy(0.15,cc.v2(0,15)),cc.scaleTo(0.15,0.9,1)),
  107. cc.spawn(cc.moveBy(0.15,cc.v2(0,-15)),cc.scaleTo(0.15,1,1)),
  108. )));
  109. }
  110. inActiveHeadPhoto(){
  111. this.headphoto.active = false;
  112. }
  113. cancelRun(){
  114. this.keepingRun = false;
  115. }
  116. forward(direction:number){
  117. this.body.scaleX = direction * Math.abs(this.body.scaleX);
  118. if(this.onGround>0){
  119. this.keepingRun = true;
  120. }
  121. }
  122. move(){
  123. let basicAngle = this.body.scaleX < 0 ? 180 : 0;
  124. let radian = (basicAngle + this.body.angle) / 180 * Math.PI;
  125. this.rigidBody.linearVelocity = cc.v2(
  126. Math.cos(radian) * this.speed,
  127. Math.sin(radian) * this.speed
  128. );
  129. }
  130. jump(){
  131. this.keepingRun = false;
  132. if(this.body.scaleX<0){
  133. this.leftUp();
  134. }else if(this.body.scaleX>0){
  135. this.rightUp();
  136. }
  137. }
  138. up(){
  139. if(this.bouncing)return;
  140. this.keepingRun = false;
  141. this.rigidBody.linearVelocity = cc.v2(0,this.speed*1.5);
  142. }
  143. leftUp(){
  144. if(this.bouncing)return;
  145. this.body.scaleX = -1 * Math.abs(this.body.scaleX);
  146. this.rigidBody.linearVelocity = cc.v2(-this.speed*0.5,this.speed*1.5);
  147. }
  148. rightUp(){
  149. if(this.bouncing)return;
  150. this.body.scaleX = 1 * Math.abs(this.body.scaleX);
  151. this.rigidBody.linearVelocity = cc.v2(this.speed*0.5,this.speed*1.5);
  152. }
  153. attack(){
  154. if(!this.attacking){
  155. this.attacking = true;
  156. this.sawAnimate.play('Saw');
  157. cc.audioEngine.playEffect(window.rs.ac_attack,false);
  158. }
  159. }
  160. autoAttack(){
  161. if(this.isAutoAttack){
  162. return this.isAutoAttack = false;
  163. }else{
  164. return this.isAutoAttack = true;
  165. }
  166. }
  167. die(){
  168. if(this.dead)return;
  169. this.dead = true;
  170. let node = cc.instantiate(window.rs.pf_blood);
  171. node.setPosition(this.node.position);
  172. // let armatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
  173. // armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE,()=>{
  174. // if(armatureDisplay.animationName=='blood_1'){
  175. // armatureDisplay.playAnimation('blood_2',1);
  176. // }
  177. // });
  178. window.gcr.bloodGroup.addChild(node);
  179. window.gm.removeBoy(this);
  180. cc.audioEngine.playEffect(window.rs.ac_bu,false);
  181. }
  182. update(dt:number){
  183. if(this.onGround>0){
  184. this.rigidBody.gravityScale = 5;
  185. if(window.gm.isGameOver){
  186. if(this.armatureDisplay.animationName!=this.getHappName()){
  187. this.armatureDisplay.playAnimation(this.getHappName(),0);
  188. }
  189. }else{
  190. if(this.keepingRun){
  191. this.move();
  192. if(this.armatureDisplay.animationName!=this.getMoveName()){
  193. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  194. }
  195. }else{
  196. if(this.armatureDisplay.animationName!=this.getIdleName()){
  197. this.armatureDisplay.playAnimation(this.getIdleName(),0);
  198. }
  199. }
  200. }
  201. }else{
  202. this.rigidBody.gravityScale = 5;
  203. 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. }else if(this.body.angle>0){
  211. let nextAngle = this.body.angle - this.angleCorrectSpeed*dt;
  212. if(nextAngle<0){
  213. this.body.angle = 0;
  214. }else{
  215. this.body.angle = nextAngle;
  216. }
  217. }
  218. if(this.armatureDisplay.animationName!=this.getMoveName()){
  219. this.armatureDisplay.playAnimation(this.getMoveName(),0);
  220. }
  221. }
  222. let boyEntity = window.gameSystem.boys.find(boy => boy.id === this.index);
  223. if (this.isRunOnHost()) {
  224. this.rigidBody.type = cc.RigidBodyType.Dynamic;
  225. this.rigidBody.linearVelocity = this.rigidBody.linearVelocity.add(cc.v2(this.acc.x*dt,this.acc.y*dt));
  226. this.uploadFrame_state();
  227. } else {
  228. this.rigidBody.type = cc.RigidBodyType.Static;
  229. this.rigidBody.linearVelocity = cc.Vec2.ZERO;
  230. this.node.setPosition(this.node.position.lerp(boyEntity.position, 0.33));
  231. this.body.angle = boyEntity.angle;
  232. this.body.scaleX = boyEntity.scaleX;
  233. }
  234. if (boyEntity.dead) {
  235. this.node.setPosition(boyEntity.deadPosition);
  236. this.die();
  237. }
  238. }
  239. /**
  240. * bounce when a player step on another player
  241. * @param direction 1:up,-1:down
  242. */
  243. bounce(direction:number){
  244. this.bouncing = true;
  245. this.rigidBody.linearVelocity = cc.v2(0,direction*this.speed*1.5);
  246. this.scheduleOnce(()=>{
  247. this.bouncing = false;
  248. },0.1)
  249. }
  250. grounds:cc.Node[] = [];
  251. removeGround(ground:cc.Node){
  252. let arr = [];
  253. this.grounds.forEach((node:cc.Node)=>{
  254. if(ground!=node){
  255. arr.push(node);
  256. }
  257. });
  258. this.grounds = arr;
  259. }
  260. onCollisionEnter(other:cc.Collider){
  261. if(other.node.group==EventType.GROUP_GROUND){
  262. this.onGround++;
  263. this.grounds.push(other.node);
  264. if (this.isRunOnHost()) this.body.angle = other.node.angle;
  265. }
  266. if(other.node.group==EventType.GROUP_BOY){
  267. if (this.isRunOnHost()) {
  268. let distance = this.node.y - other.node.y;
  269. if(distance<-25){
  270. this.bounce(-1);
  271. }else if(distance>25){
  272. this.bounce(1);
  273. }
  274. }
  275. }
  276. }
  277. onCollisionExit(other:cc.Collider){
  278. if(other.node.group==EventType.GROUP_GROUND){
  279. this.onGround--;
  280. this.removeGround(other.node);
  281. if (this.isRunOnHost()) {
  282. if(this.grounds.length>0){
  283. this.body.angle = this.grounds[this.grounds.length-1].angle;
  284. }
  285. }
  286. }
  287. }
  288. //======联机API======
  289. /**是否运行在主机上 */
  290. isRunOnHost() {
  291. if (window.gameSystem.masterId === this.index) {
  292. return true;
  293. }
  294. if (this.getComponent(Bot)) return true;
  295. return false;
  296. }
  297. /**上传帧-状态 */
  298. uploadFrame_state() {
  299. window.gm.socketPlayer.cacheInputs({
  300. t: 1,
  301. id: this.index,
  302. x: this.node.x.toFixed(3),
  303. y: this.node.y.toFixed(3),
  304. a: this.body.angle.toFixed(1),
  305. sx: this.body.scaleX.toFixed(3),
  306. });
  307. }
  308. /**上传帧-被杀 */
  309. uploadFrame_beKilled(killerId: number) {
  310. window.gm.socketPlayer.cacheInputs({
  311. t: 2,
  312. id: this.index,
  313. x: this.node.x.toFixed(1),
  314. y: this.node.y.toFixed(1),
  315. ki: killerId
  316. });
  317. }
  318. }
  319. class BoyColor {
  320. static colors = ['red','green','blue','yellow'];
  321. static mapColors = [cc.Color.RED,cc.Color.GREEN,cc.Color.BLUE,cc.Color.YELLOW];
  322. static getColorWith_(index:number){
  323. return '_' + BoyColor.colors[index];
  324. }
  325. }
  326. class Saw extends cc.Component{
  327. boy:Boy;
  328. onCollisionStay(other:cc.Collider){
  329. if(other.node==this.boy.node||other.node.group==EventType.GROUP_GROUND)return;
  330. if(other.node.group==EventType.GROUP_SAW){
  331. if(this.boy.autoAttack){
  332. this.boy.attack();
  333. }
  334. return;
  335. }
  336. if(other.node.group==EventType.GROUP_BOY){
  337. if(this.boy.autoAttack){
  338. this.boy.attack();
  339. }
  340. if(this.boy.attacking){
  341. let otherBoy = other.getComponent(Boy);
  342. this.kill(otherBoy);
  343. }
  344. return;
  345. }
  346. }
  347. kill(otherBoy:Boy){
  348. if(otherBoy){
  349. if(this.boy.node.y>=otherBoy.node.y){
  350. if(!this.boy.dead){
  351. if (otherBoy.isRunOnHost()) otherBoy.uploadFrame_beKilled(this.boy.index);
  352. }
  353. }
  354. }
  355. }
  356. attackFinish(){
  357. this.boy.attacking = false;
  358. }
  359. }