PlayerController.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import ConfigManager from "../../../engines/configs/ConfigManager";
  8. import MathUtils from "../../../engines/utils/MathUtils";
  9. import ConfigKeys from "../../models/ConfigKeys";
  10. import GameModel from "../../models/GameModel";
  11. import GameController from "./GameController";
  12. const {ccclass, property} = cc._decorator;
  13. @ccclass
  14. export default class PlayerController extends cc.Component {
  15. width:number=0;
  16. height:number=0;
  17. isLeft:boolean=false;
  18. isRight:boolean=false;
  19. isSprint:boolean=false;
  20. speed:cc.Vec3;
  21. leftSpeed:cc.Vec3;
  22. rightSpeed:cc.Vec3;
  23. dropSpeed:cc.Vec3;
  24. sprintSpeed:cc.Vec3;
  25. private helfWidth:number;
  26. private helfHeight:number;
  27. /**
  28. * 起始点
  29. */
  30. private startPos:cc.Vec3;
  31. onLoad () {
  32. }
  33. start () {
  34. this.width=GameController.single.carConfig.width;
  35. this.height=GameController.single.carConfig.height;
  36. this.startPos=new cc.Vec3();
  37. this.startPos.x=5;
  38. this.startPos.y=3;
  39. this.startPos.z=GameController.single.levelConfig.startPos;
  40. this.helfWidth=this.width*0.5;
  41. this.helfHeight=this.height*0,5;
  42. this.speed=new cc.Vec3();
  43. this.speed.z=GameController.single.carConfig.speed;
  44. this.leftSpeed=new cc.Vec3();
  45. this.leftSpeed.x=-GameController.single.carConfig.turnSpeed;
  46. this.rightSpeed=new cc.Vec3();
  47. this.rightSpeed.x=GameController.single.carConfig.turnSpeed;
  48. this.sprintSpeed=new cc.Vec3();
  49. this.sprintSpeed.z=GameController.single.carConfig.sprintSpeed;
  50. this.dropSpeed=new cc.Vec3(0,-60,0);
  51. }
  52. update (dt:number) {
  53. let currentPos:cc.Vec3=this.node.position;
  54. currentPos.x=this.__logicPosition.x;
  55. currentPos.y=this.__logicPosition.y;
  56. currentPos.z=-this.__logicPosition.z;
  57. this.node.position=currentPos;
  58. }
  59. private __logicPosition:cc.Vec3=new cc.Vec3();
  60. get logicPosition():cc.Vec3{
  61. return this.__logicPosition;
  62. }
  63. set logicPosition(value:cc.Vec3){
  64. this.__logicPosition=value;
  65. }
  66. private __rect:cc.Rect=new cc.Rect();
  67. /**
  68. * 矩形
  69. */
  70. get rect():cc.Rect{
  71. this.__rect.x=this.logicPosition.x-this.helfWidth;
  72. this.__rect.y=this.logicPosition.z-this.helfHeight;
  73. this.__rect.width=this.width;
  74. this.__rect.height=this.height;
  75. return this.__rect;
  76. }
  77. private __gridView:cc.Vec3[];
  78. /**
  79. * 将碰撞盒子化为九宫格九个点
  80. */
  81. get gridView():cc.Vec3[]{
  82. let point:cc.Vec3;
  83. if(this.__gridView==null){
  84. this.__gridView=[];
  85. for (let index = 0; index < 9; index++) {
  86. point=new cc.Vec3();
  87. this.__gridView.push(point);
  88. }
  89. }
  90. let rt:cc.Rect=this.rect;
  91. let w1:number=rt.width/3;
  92. let h1:number=rt.height/3;
  93. let index:number=0;
  94. for (let xIndex = 0; xIndex < 3; xIndex++) {
  95. for (let yIndex = 0; yIndex < 3; yIndex++) {
  96. index=xIndex*3+yIndex;
  97. point=this.__gridView[index];
  98. point.x=rt.x+w1*xIndex;
  99. point.y=0;
  100. point.z=rt.y+h1*yIndex;
  101. }
  102. }
  103. return this.__gridView;
  104. }
  105. Reset():void{
  106. this.logicPosition.x=this.startPos.x;
  107. this.logicPosition.y=this.startPos.y;
  108. this.logicPosition.z=this.startPos.z;
  109. this.isLeft=false;
  110. this.isRight=false;
  111. this.isSprint=false;
  112. }
  113. }