ani.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { cocosz } from "../Framework/CocosZ";
  2. import GameDate from "./gameDate";
  3. import Weapon from "./weapon";
  4. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  5. @ccclass
  6. @executeInEditMode
  7. @playOnFocus
  8. export default class Ani extends cc.Component {
  9. @property({ displayName: "间隔帧数" })
  10. times: number = 10;
  11. @property({ type: [cc.Node], displayName: "动画数组" })
  12. aniNodeArr: cc.Node[] = [];
  13. @property()
  14. _btnAdd: boolean = false;
  15. get btnAdd() { return this._btnAdd; }
  16. @property({ displayName: "添加动画" })
  17. set btnAdd(v) {
  18. this.aniNodeArr = [];
  19. this.node.children.forEach((n) => { this.aniNodeArr.push(n); })
  20. }
  21. @property({ displayName: "停止动画" })
  22. _btnStop: boolean = false;
  23. get btnStop() { return this._btnStop; }
  24. @property
  25. set btnStop(v) {
  26. this._btnStop = false;
  27. this.stopAni();
  28. this.aniNodeArr.forEach((node: cc.Node) => { node.opacity = 255; }, null)
  29. }
  30. @property({ displayName: "待机动画" })
  31. _btnDaiji: boolean = false;
  32. get btnDaiji() { return this._btnDaiji; }
  33. @property
  34. set btnDaiji(v) {
  35. this._btnDaiji = false;
  36. this.addAni("daiji", false);
  37. }
  38. @property({ displayName: "跑步动画" })
  39. _btnRun: boolean = false;
  40. get btnRun() { return this._btnRun; }
  41. @property
  42. set btnRun(v) {
  43. this._btnRun = false;
  44. this.addAni("run", false);
  45. }
  46. @property({ displayName: "攻击动画" })
  47. _btnAtk: boolean = false;
  48. get btnAtk() { return this._btnAtk; }
  49. @property
  50. set btnAtk(v) {
  51. this._btnAtk = false;
  52. this.addAni("atk", false);
  53. }
  54. @property({ displayName: "攻击动画2" })
  55. _btnAtk2: boolean = false;
  56. get btnAtk2() { return this._btnAtk2; }
  57. @property
  58. set btnAtk2(v) {
  59. this._btnAtk2 = false;
  60. this.addAni("atk2", false);
  61. }
  62. @property({ displayName: "死亡动画2" })
  63. _btnDeath: boolean = false;
  64. get btnDeath() { return this._btnAtk2; }
  65. @property
  66. set btnDeath(v) {
  67. this._btnDeath = false;
  68. this.addAni("death", false);
  69. }
  70. private handLeft: cc.Node = null;
  71. private body: cc.Node = null;
  72. private leg: cc.Node = null;
  73. private handRight: cc.Node = null;
  74. private head: cc.Node = null;
  75. private weapon: cc.Node = null;
  76. onLoad() {
  77. this.handLeft = cc.find("handLeft", this.node);
  78. this.body = cc.find("body", this.node);
  79. this.leg = cc.find("leg", this.node);
  80. this.handRight = cc.find("handRight", this.node);
  81. this.head = cc.find("head", this.node);
  82. this.weapon = cc.find("weapon", this.node);
  83. }
  84. start() { }
  85. public aniNode: cc.Node = null;//动画节点
  86. private _t: number = 0;//计时
  87. private _index: number = 0;//动画帧
  88. private _isLoop: boolean = false;//是否循环
  89. private _aniArr: any[] = [];//动画数组
  90. /**
  91. * 设置动画
  92. * @param name 动画名
  93. * @param isLoop 是否循环
  94. * @param time 动画间隔
  95. */
  96. setAni(name: string, isLoop: boolean = false) {
  97. // 清空动画数组
  98. this._aniArr = [];
  99. // 播放动画
  100. this.playAni(name, isLoop);
  101. }
  102. /**
  103. * 添加动画
  104. * @param name 动画名
  105. * @param isLoop 是否循环
  106. * @param time 动画间隔
  107. */
  108. addAni(name: string, isLoop: boolean) {
  109. if (this.aniNode) {
  110. this._aniArr.push({
  111. name: name,
  112. isLoop: isLoop,
  113. })
  114. } else {
  115. // 播放动画
  116. this.playAni(name, isLoop);
  117. }
  118. }
  119. /** 停止动画 */
  120. public stopAni() {
  121. // 清空动画数组
  122. this._aniArr = [];
  123. // 停止动画
  124. this.aniNode = null;
  125. }
  126. /** 是否包含动画 */
  127. public includeAni(name: string) {
  128. let r = false;
  129. this.aniNodeArr.forEach(n => {
  130. if (n.name == name)
  131. r = true;
  132. })
  133. return r;
  134. }
  135. /** 是否正在播放 */
  136. public isAni(name?: string) {
  137. if (this.aniNode && ((!name) || (name && this.aniNode.name.includes(name)))) {
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. private playAni(name: string, isLoop: boolean = false) {
  144. this.aniNode = null;
  145. this._index = 0;
  146. this._t = 0;
  147. // 隐藏其它动画
  148. this.aniNodeArr.forEach(child => {
  149. if (child.name == name) {
  150. // child.active = true;
  151. child.active = true;
  152. child.opacity = 255;
  153. this.aniNode = child;
  154. } else {
  155. // child.active = false;
  156. child.active = true;
  157. child.opacity = 0;
  158. }
  159. })
  160. // 循环播放
  161. this._isLoop = isLoop;
  162. // 输出
  163. if (this.aniNode) {
  164. // cc.log("播放动画:", name);
  165. } else {
  166. cc.log("动画不存在:" + name)
  167. }
  168. }
  169. protected lateUpdate(): void {
  170. if (this.aniNode && this.aniNode.isValid) {
  171. if (this._t % this.times == 0) {
  172. this._t = 0
  173. // 切换图片
  174. this.aniNode.children.forEach((child, index) => {
  175. if (index == this._index) {
  176. child.opacity = 255;
  177. } else {
  178. child.opacity = 0;
  179. }
  180. })
  181. // 动画帧++
  182. this._index++;
  183. // 判断动画
  184. if (this._index >= this.aniNode.childrenCount) {
  185. if (this._isLoop) {
  186. // 循环的当前动画
  187. this._index = 0;
  188. } else if (this._aniArr.length > 0) {
  189. // 下一个动画
  190. let json = this._aniArr.shift();
  191. this.playAni(json.name, json.isLoop);
  192. } else {
  193. // 结束动画
  194. this.aniNode = null;
  195. this._index = 0;
  196. }
  197. }
  198. }
  199. // 帧自增
  200. this._t++;
  201. }
  202. }
  203. setSkinById(id: number) {
  204. this.setSkinByName((id + 1).toString());
  205. this.setGh(id);
  206. }
  207. setSkinByName(name: string) {
  208. this.head.children.forEach((child) => {
  209. if (child.name == name) {
  210. child.active = true;
  211. child.opacity = 255;
  212. } else {
  213. // child.active = false;
  214. child.active = true;
  215. child.opacity = 0;
  216. }
  217. });
  218. }
  219. setWeaponById(id: number) {
  220. this.setWeaponByName(Weapon.WeaponName[id]);
  221. this.setSD(id);
  222. }
  223. setWeaponByName(name: string) {
  224. if (GameDate.Weapon[name].atkRange > 300) {
  225. this.weapon.active = true;
  226. this.handLeft.active = false;
  227. this.handRight.active = false;
  228. const pre = cocosz.resMgr.getRes(`weapon_${name}`, cc.Prefab);
  229. if (pre) {
  230. let rangeWeapon = cc.instantiate(pre);
  231. rangeWeapon.removeComponent(Weapon);
  232. this.weapon.children[0] && this.weapon.children[0].isValid && this.weapon.children[0].destroy();
  233. this.weapon.addChild(rangeWeapon);
  234. }
  235. } else {
  236. this.weapon.active = false;
  237. this.handLeft.active = true;
  238. this.handRight.active = true;
  239. const pre = cocosz.resMgr.getRes(`weapon_${name}`, cc.Prefab);
  240. if (pre) {
  241. let meleeWeapon: cc.Node = cc.instantiate(pre);
  242. meleeWeapon.removeComponent(Weapon);
  243. this.handRight.children[0] && this.handRight.children[0].isValid && this.handRight.children[0].destroy();
  244. this.handRight.addChild(meleeWeapon);
  245. }
  246. }
  247. }
  248. /** 显示光环 */
  249. setGh(id: number) {
  250. let gh = this.node.getChildByName("gh");
  251. if (gh && gh.isValid) {
  252. let info = cocosz.dataMgr.getSkinInfo(id);
  253. if (info) {
  254. let ghAni = gh.getComponent(sp.Skeleton);
  255. gh.active = true;
  256. if (ghAni && ghAni.isValid) {
  257. let personLevel = info.Level;
  258. if (personLevel > 0) {
  259. gh.color = cc.Color.WHITE;
  260. let arr = ["", "y", "p", "r"];
  261. ghAni.setSkin(arr[Math.ceil(personLevel / 2)]);
  262. ghAni.setAnimation(0, "animation", true);
  263. } else {
  264. gh.color = cc.Color.BLACK;
  265. ghAni.setSkin("r");
  266. ghAni.setAnimation(0, "animation", true);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. /** 显示闪电 */
  273. setSD(id) {
  274. let info = cocosz.dataMgr.getGunInfo(id);
  275. this.node.walk((child) => {
  276. if (child.name == "sd") {
  277. if (info.Level > 0) {
  278. child.active = true;
  279. let spAni = child.getComponent(sp.Skeleton);
  280. if (spAni) {
  281. let arr = ["", "y", "p", "r"];
  282. spAni && spAni.setSkin(arr[info.Level]);
  283. }
  284. } else {
  285. child.active = false;
  286. }
  287. }
  288. }, null);
  289. }
  290. }