Controller.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import Role from "./Role";
  2. import Bot from "./Bot";
  3. import Saw from "./Saw";
  4. import Settlement from "../Panel/Settlement";
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class Controller extends cc.Component {
  8. roles:Role[] = new Array();
  9. saws:cc.Node[] = new Array();
  10. gameOver:boolean = false;
  11. onLoad(){
  12. window.controller = this;
  13. cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
  14. cc.director.getPhysicsManager().enabled = true;
  15. cc.director.getCollisionManager().enabled = true;
  16. // cc.director.getCollisionManager().enabledDebugDraw = true;
  17. }
  18. startGame(){
  19. let playerIndex = Math.floor(Math.random()*4);
  20. let positions = [cc.v2(-450,180),cc.v2(-150,120),cc.v2(150,120),cc.v2(450,180)];
  21. for(let i=0;i<4;i++){
  22. this.createRole(positions.shift(),i,i==playerIndex?false:true);
  23. }
  24. this.showStartCountDownAnimation(this.node,()=>{
  25. this.createSaw(cc.v2(0,250),cc.v2(-1000,0),1000);
  26. this.createSaw(cc.v2(0,250),cc.v2(-700,-700),1000);
  27. this.createSaw(cc.v2(0,250),cc.v2(700,-700),1000);
  28. this.createSaw(cc.v2(0,250),cc.v2(1000,0),1000);
  29. });
  30. cc.find('Canvas/KeyBoardPanel').active = true;
  31. }
  32. createRole(position:cc.Vec2,index:number,isBot:boolean){
  33. let node = cc.instantiate(window.resource.pf_Role);
  34. node.setPosition(position);
  35. let role = node.addComponent(Role);
  36. role.index = index;
  37. window.map.node.addChild(node);
  38. if(!isBot){
  39. role.activeDeltaGreen();
  40. window.playerRole = role;
  41. }
  42. this.roles.push(role);
  43. if(isBot){
  44. node.addComponent(Bot);
  45. }
  46. }
  47. destroyRole(role:Role){
  48. if(this.roles.indexOf(role)>-1){
  49. let ary = new Array();
  50. this.roles.forEach((elem)=>{
  51. if(elem!=role){
  52. ary.push(elem);
  53. }
  54. });
  55. this.roles = ary;
  56. role.node.destroy();
  57. }
  58. if(this.roles.length==1){
  59. let index = this.roles[0].index
  60. setTimeout(()=>{
  61. this.showSettlementPanel(index);
  62. },360);
  63. }
  64. }
  65. showSettlementPanel(index:number){
  66. this.gameOver = true;
  67. let panel = cc.find('Canvas/SettlementPanel');
  68. panel.active = true;
  69. panel.getComponent(Settlement).showWinner(index);
  70. }
  71. createSaw(position:cc.Vec2, linearVelocity:cc.Vec2, angularVelocity:number){
  72. let node = cc.instantiate(window.resource.pf_Saw);
  73. node.setPosition(position);
  74. node.addComponent(Saw);
  75. node.getComponent(cc.RigidBody).linearVelocity = linearVelocity;
  76. node.getComponent(cc.RigidBody).angularVelocity = angularVelocity;
  77. window.map.node.addChild(node);
  78. this.saws.push(node);
  79. }
  80. destroySaw(saw:cc.Node){
  81. if(this.saws.indexOf(saw)>-1){
  82. let ary = new Array();
  83. this.saws.forEach((elem)=>{
  84. if(elem!=saw){
  85. ary.push(elem);
  86. }
  87. });
  88. this.saws = ary;
  89. saw.destroy();
  90. }
  91. }
  92. showStartCountDownAnimation(parent:cc.Node,callback:Function):void{
  93. for(let i=3;i>=0;i--){
  94. this.scheduleOnce(()=>{
  95. let node = new cc.Node();
  96. node.addComponent(cc.Sprite).spriteFrame = window.resource['sf_StartCountDownAnimation'][i];
  97. node.zIndex = 10;
  98. node.setScale(2.5);
  99. node.setPosition(0,150);
  100. parent.addChild(node);
  101. node.runAction(cc.sequence(cc.scaleBy(0.6,0.8).easing(cc.easeBackInOut()),cc.callFunc(()=>{
  102. node.destroy();
  103. if(i==0&&callback){
  104. callback();
  105. }
  106. },this)));
  107. },3-i);
  108. }
  109. }
  110. }