Role.ts 8.0 KB

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