Boy.ts 12 KB

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