Role.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { _decorator, Animation, AnimationClip, AnimationState, BoxCollider2D, Collider2D, Component, instantiate, ITriggerEvent, Node, SpriteFrame, UITransform, Vec2, Vec3 } from 'cc';
  2. import { Tools } from '../Tools/Tools';
  3. import { RoleData } from '../../DataItem/ItemData';
  4. import { resMgr } from '../../Frames/ResourcesMgr';
  5. import { Bullet } from './GameScene/Bullet';
  6. const { ccclass, property } = _decorator;
  7. enum State {
  8. Attack,
  9. Walk,
  10. Idle,
  11. Die
  12. }
  13. @ccclass('Role')
  14. export class Role extends Component {
  15. hp: number = null;
  16. atk: number = null;
  17. atkLength: number = null;
  18. moveSpeed: number = null;
  19. //向左0 向右1
  20. direction: number = 1;
  21. //是否停下
  22. isStop: boolean = true;
  23. //是否可以攻击
  24. isAttacking: boolean = false;
  25. //攻击目标
  26. targetNode: Node = null;
  27. //是否开火
  28. isFire: boolean = false;
  29. private _bullet: Node = null;
  30. private _walkFrames: SpriteFrame[] = [];
  31. private _atkFrames: SpriteFrame[] = [];
  32. private _idleFrames: SpriteFrame[] = [];
  33. private _dieFrames: SpriteFrame[] = [];
  34. private _explodeframes: SpriteFrame[] = [];
  35. private _bulletFrames: SpriteFrame[] = [];
  36. private _animations: Map<string, Animation> = new Map();
  37. private _roleData: RoleData = null;
  38. curState: State = State.Walk;
  39. init(name: string, pos: Vec3, roleDatas: RoleData[], dir?: number) {
  40. this.isStop = false;
  41. this.direction = dir;
  42. let whichData: number = -1;
  43. for (let i = 0; i < roleDatas.length; i++) {
  44. if (!roleDatas[i]) {
  45. console.log(null)
  46. }
  47. if (roleDatas[i].imgName === name) {
  48. this._roleData = roleDatas[i];
  49. whichData = i;
  50. break;
  51. }
  52. }
  53. if (whichData != -1) {
  54. //获取walk精灵帧
  55. const aniCount: number = this._roleData.aniCount;
  56. this._getFrames(aniCount, this._walkFrames, this._roleData.aniImg, 1);
  57. this._collectAni();
  58. //子弹精灵帧
  59. this._getFrames(this._roleData.bulletCount, this._bulletFrames, this._roleData.bulletImg, 1)
  60. //子弹爆炸精灵帧
  61. this._getFrames(this._roleData.bulletCount, this._explodeframes, this._roleData.bulletExplodeImg, 1)
  62. // //获取攻击精灵帧
  63. // const atkCount: number = data[whichData].atkCount;
  64. // this._getFrames(atkCount, this._atkFrames, data[whichData].atkImg, 1);
  65. // //获取待机精灵帧
  66. // const idleCount: number = data[whichData].idleCount;
  67. // this._getFrames(idleCount,this._idleFrames,data[whichData].idleImg, 1);
  68. // //获取死亡精灵帧
  69. // const dieCount: number = data[whichData].dieCount;
  70. // this._getFrames(dieCount,this._dieFrames,data[whichData].dieImg, 1);
  71. //设置基础数据
  72. this._setRoleData(this._roleData);
  73. //this.moveSpeed = this._roleData.moveSpeed;
  74. // let ani = Tools.createAnimation(this._walkFrames, 20, this.node, AnimationClip.WrapMode.Loop);
  75. // ani.name = "a";
  76. this._animations.get("walkAni").play();
  77. }
  78. //位置
  79. this.node.setWorldPosition(pos);
  80. }
  81. private _setRoleData(roleData: RoleData) {
  82. this.hp = roleData.hp;
  83. this.atk = roleData.atk;
  84. this.atkLength = roleData.atkLength + 100;
  85. this.moveSpeed = roleData.moveSpeed + 20;
  86. }
  87. //获取精灵帧组
  88. /*
  89. count -> 精灵帧的数量
  90. imgType -> 图片类型(atk、walk、idle、die)
  91. startIdx -> 索引起始数
  92. */
  93. private _getFrames(count: number, frames: SpriteFrame[], imgType: string, startIdx: number) {
  94. for (let i = startIdx; i <= count; i++) {
  95. if (count > 1) {
  96. frames.push(resMgr.getSpriteFrame(imgType + i));
  97. }
  98. else {
  99. frames.push(resMgr.getSpriteFrame(imgType));
  100. }
  101. }
  102. }
  103. update(deltaTime: number) {
  104. //this._collision(this.targetNode);
  105. if (this.curState == State.Walk) {
  106. //this._animations.get("walkAni").play();
  107. if (!this.isStop) {
  108. this.move(deltaTime);
  109. }
  110. this.stop()
  111. }
  112. else if (this.curState == State.Attack) {
  113. //this._animations.get("atkAni").play();
  114. }
  115. else if (this.curState == State.Die) {
  116. //this._animations.get("dieAni").play();
  117. }
  118. else if (this.curState == State.Idle) {
  119. //this._animations.get("idleAni").play();
  120. }
  121. }
  122. //移动
  123. move(dt: number) {
  124. let x = this.node.getWorldPosition().x;
  125. let y = this.node.getWorldPosition().y;
  126. let z = this.node.getWorldPosition().z;
  127. if (this.direction) {
  128. x = x + this.moveSpeed * dt;
  129. } else {
  130. x = x - this.moveSpeed * dt;
  131. }
  132. this.node.setWorldPosition(x, y, z)
  133. }
  134. stop() {
  135. }
  136. die() {
  137. this.node.destroy();
  138. }
  139. walk() {
  140. }
  141. //将各个动画存储起来
  142. private _collectAni() {
  143. //const walkAni: Animation = this._createAni(this._walkFrames,20, AnimationClip.WrapMode.Loop)
  144. const walkAni: Animation = Tools.createAnimation(this._walkFrames, 20, this.node, AnimationClip.WrapMode.Loop)
  145. walkAni.name = "walkAni";
  146. this._animations.set(walkAni.name, walkAni);
  147. const atkAni: Animation = Tools.createAnimation(this._atkFrames, 20, this.node, AnimationClip.WrapMode.Loop)
  148. atkAni.name = "atkAni";
  149. this._animations.set(atkAni.name, atkAni);
  150. const idleAni: Animation = Tools.createAnimation(this._idleFrames, 20, this.node, AnimationClip.WrapMode.Loop)
  151. idleAni.name = "idleAni";
  152. this._animations.set(idleAni.name, idleAni);
  153. const dieAni: Animation = Tools.createAnimation(this._dieFrames, 20, this.node, AnimationClip.WrapMode.Loop)
  154. dieAni.name = "dieAni";
  155. this._animations.set(dieAni.name, dieAni);
  156. }
  157. //判断是否攻击
  158. getEnemiesAhead(parent: Node) {
  159. for (const node of parent.children) {
  160. const enemyY = node.getWorldPosition().y;
  161. const enemyX = node.getWorldPosition().x;
  162. const y = this.node.getWorldPosition().y;
  163. const x = this.node.getWorldPosition().x;
  164. if (enemyY === y) {
  165. const distance = Math.abs(enemyX - x);
  166. if (distance <= this.atkLength) {
  167. //开始攻击
  168. this.isAttacking = true;
  169. //设置攻击目标
  170. this.targetNode = node;
  171. //停止移动
  172. this.isStop = true;
  173. //开始开火
  174. this.isFire = true;
  175. this.attack(this.targetNode, this.isFire);
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. attack(targetNode: Node, isFire: boolean) {
  182. if (!targetNode) return;
  183. const targetNodeTS = targetNode.getComponent(Role);
  184. if (isFire && targetNodeTS.hp > 0) {
  185. this._createBullet();
  186. this.schedule(() => {
  187. this._createBullet();
  188. }, 3)
  189. }
  190. }
  191. //创建子弹
  192. private _createBullet() {
  193. this._bullet = instantiate(resMgr.getPrefab("Bullet"));
  194. this._bullet.parent = this.node;
  195. const bulletTS = this._bullet.getComponent(Bullet);
  196. bulletTS.direction = this.direction;
  197. bulletTS.bulletFrames = this._bulletFrames;
  198. bulletTS.explodeframes = this._explodeframes;
  199. bulletTS.atkLength = this.atkLength;
  200. bulletTS.targetNode = this.targetNode;
  201. bulletTS.atk = this.atk;
  202. }
  203. }