MSprite.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import Joystick_mag, { SpeedType } from "../../../joystick/Joystick_mag";
  2. import Joystick from "../../../util/Joystick";
  3. import { FFAttr } from "../../data/FFCalAttr";
  4. import { __SkillData } from "../../data/sdata/SManage";
  5. import BObject from "../bullet/BObject";
  6. import FSprite, { SpriteActionType } from "./FSprite";
  7. import MBomb from "./skill/mainSkill/MBomb";
  8. import MFWind from "./skill/mainSkill/MFWind";
  9. import MMagic from "./skill/mainSkill/MMagic";
  10. import MShotgun from "./skill/mainSkill/MShotgun";
  11. import SkillBase from "./skill/SkillBase";
  12. /**
  13. * 主角
  14. */
  15. const { ccclass, property } = cc._decorator;
  16. @ccclass
  17. export default class MSprite extends FSprite {
  18. /**
  19. * 技能1
  20. * 武器技能
  21. */
  22. public skill1: SkillBase
  23. /**
  24. * 技能2
  25. * 职业技能
  26. */
  27. public skill2: SkillBase
  28. private musicWalkId = -1;
  29. private muiscRunId = -1;
  30. /**
  31. * 移动速度
  32. */
  33. private speedN = 120000;
  34. public start(){
  35. super.start()
  36. this.spineEventListener()
  37. }
  38. public setAttrData(attrData: FFAttr) {
  39. super.setAttrData(attrData)
  40. // attrData.weaponSkill = 1205
  41. let _skillData = this.ff.main.sManage.getSkillById(attrData.weaponSkill)
  42. if (_skillData) {
  43. if (_skillData.type == 1) {//散弹
  44. let skill1: MShotgun = this.node.addComponent(MShotgun) as any
  45. skill1.count = _skillData.value2
  46. skill1.bcount = _skillData.value3
  47. this.loadSkill(_skillData, skill1)
  48. this.skill1 = skill1
  49. } else if (_skillData.type == 2) {//丢炸弹
  50. let skill1: MBomb = this.node.addComponent(MBomb) as any
  51. this.loadSkill(_skillData, skill1)
  52. this.skill1 = skill1
  53. } else if (_skillData.type == 3) {//法术
  54. let skill1 = this.node.addComponent(MMagic) as any
  55. this.loadSkill(_skillData, skill1)
  56. this.skill1 = skill1
  57. } else if (_skillData.type == 4) {//龙卷风
  58. let skill1 = this.node.addComponent(MFWind) as any
  59. this.loadSkill(_skillData, skill1)
  60. this.skill1 = skill1
  61. }
  62. this.skill1._skillData = _skillData
  63. }
  64. }
  65. private loadSkill(_skillData: __SkillData, skill1) {
  66. cc.resources.load('icon/skills/' + _skillData.effect + '/bullet', cc.Prefab, (err, prefab: cc.Prefab) => {
  67. if (err) {
  68. cc.error(err);
  69. } else {
  70. skill1.mBullet = prefab
  71. }
  72. });
  73. }
  74. public setJoystick(mJoystick: Joystick_mag) {
  75. mJoystick.setListen((speedType: SpeedType, moveVec?: cc.Vec2) => {
  76. if (speedType == SpeedType.STOP) {
  77. this.stopMove()
  78. } else {
  79. this.startMove(moveVec)
  80. }
  81. this.playMusic();
  82. });
  83. }
  84. update(dt) {
  85. if (this.gamePause) {
  86. return;
  87. }
  88. if (this.isWalk) {
  89. this.mRigidBody.applyLinearImpulse(
  90. cc.v2(this.moveV2.x * this.speedN * dt, this.moveV2.y * this.speedN * dt),
  91. this.mRigidBody.getWorldCenter(),
  92. true
  93. );
  94. }
  95. }
  96. /**
  97. * @param v2 开始向量移动
  98. */
  99. public startMove(v2: cc.Vec2) {
  100. if (!this.isWalk) {
  101. if (!this.currentShooting) {
  102. this.spine.setAnimation(0, SpriteActionType.move, true)
  103. }
  104. }
  105. this.isWalk = true
  106. this.moveV2 = v2;
  107. //有怪物的时候,面向怪物
  108. let enemy = this.findEnemy(2000);
  109. if(enemy.sprite){
  110. let abs = Math.abs(this.spine.node.scaleX);
  111. if(this.node.x > enemy.sprite.node.x){
  112. this.spine.node.scaleX = -abs;
  113. }else{
  114. this.spine.node.scaleX = abs;
  115. }
  116. }else{
  117. let abs = Math.abs(this.spine.node.scaleX);
  118. if (v2.x <= 0.001 && v2.x >= -0.001) {
  119. } else if (v2.x > 0) {
  120. this.spine.node.scaleX = abs;
  121. } else {
  122. this.spine.node.scaleX = -abs;
  123. }
  124. }
  125. }
  126. /**
  127. * 停止移动
  128. */
  129. public stopMove() {
  130. this.isWalk = false
  131. if (!this.currentShooting) {
  132. this.spine.setAnimation(0, SpriteActionType.stand, true)
  133. }
  134. }
  135. /**
  136. * 设置是否跑
  137. * @param isRuning
  138. */
  139. public setRuning(isRuning: boolean) {
  140. // super.setRuning(isRuning);
  141. this.playMusic();
  142. }
  143. private playMusic() {
  144. if (this.isWalk) {
  145. let setting = this.ff.main.player.setting;
  146. if (setting.music_ON_OFF_2) {
  147. if (this.isRuning) {
  148. if (this.muiscRunId == -1) {
  149. this.muiscRunId = cc.audioEngine.play(this.ff.mRun, true, setting.music_Slider_2);
  150. }
  151. if (this.musicWalkId != -1) {
  152. cc.audioEngine.stop(this.musicWalkId);
  153. this.musicWalkId = -1;
  154. }
  155. } else {
  156. if (this.musicWalkId == -1) {
  157. this.musicWalkId = cc.audioEngine.play(this.ff.mWalk, true, setting.music_Slider_2);
  158. }
  159. if (this.muiscRunId != -1) {
  160. cc.audioEngine.stop(this.muiscRunId);
  161. this.muiscRunId = -1;
  162. }
  163. }
  164. }
  165. } else {
  166. if (this.musicWalkId != -1) {
  167. cc.audioEngine.stop(this.musicWalkId);
  168. this.musicWalkId = -1;
  169. }
  170. if (this.muiscRunId != -1) {
  171. cc.audioEngine.stop(this.muiscRunId);
  172. this.muiscRunId = -1;
  173. }
  174. }
  175. }
  176. public setPause(pause: boolean) {
  177. this.gamePause = pause
  178. if (this.gamePause) {
  179. this.isWalk = false
  180. this.currentShooting = false
  181. this.isShooting = false
  182. this.spine.setAnimation(0, SpriteActionType.stand, true)
  183. }
  184. }
  185. /**
  186. * 当前动作是否处于射击状态
  187. */
  188. private currentShooting = false;
  189. /**
  190. * 设置攻击
  191. * @param isShooting
  192. */
  193. public setShooting(isShooting: boolean) {
  194. // cc.log('isShooting : ',isShooting);
  195. this.isShooting = isShooting;
  196. if (this.isShooting && !this.currentShooting) {
  197. this.currentShooting = true;
  198. this.shooting()
  199. }
  200. }
  201. private shooting() {
  202. this.playAction(SpriteActionType.atk, false, () => {
  203. if (this.isShooting) {
  204. this.shooting()
  205. } else {
  206. this.endShooting()
  207. }
  208. });
  209. }
  210. /**
  211. * 射击结束后
  212. */
  213. private endShooting() {
  214. this.currentShooting = false;
  215. if(this.mWeapon1){
  216. this.mWeapon1.rotation = 180;
  217. }
  218. if (this.isWalk) {
  219. this.spine.setAnimation(0, SpriteActionType.move, true)
  220. } else {
  221. this.spine.setAnimation(0, SpriteActionType.stand, true)
  222. }
  223. }
  224. /**
  225. * 注册帧事件
  226. */
  227. private spineEventListener() {
  228. this.spine.setEventListener((trackEntry, event) => {
  229. if (this.isValid && !this.gamePause) {
  230. // var animationName = trackEntry.animation ? trackEntry.animation.name : "";
  231. // cc.log("[track %s][animation %s] event: %s, %s, %s, %s", trackEntry.trackIndex, animationName, event.data.name, event.intValue, event.floatValue, event.stringValue);
  232. if (this.node.isValid && event.data.name == 'hit') {
  233. let node = cc.instantiate(this.mBullet);
  234. node.group = 'bullet';
  235. let x = this.node.x + this.mAtkSite.worldX
  236. let y = this.node.y + this.mAtkSite.worldY
  237. let pos = cc.v2(x, y);
  238. node.setPosition(pos);
  239. let bObject = node.getComponent(BObject);
  240. bObject.setSprite(this);
  241. let csprite: FSprite = this.findEnemy(2000).sprite;
  242. node.parent = this.map.mSprites;
  243. if (csprite && csprite.isValid) {
  244. bObject.fire(csprite.node);
  245. // this.addSendEffect(bObject.mStartEffect, csprite.node);
  246. } else {//没有目标的时候,向朝向开火
  247. bObject.fireAngleV2(this.moveV2);
  248. // this.addSendEffect(bObject.mStartEffect, null);
  249. }
  250. let angle = Math.atan2(this.moveV2.y, this.moveV2.x);
  251. this.setWeaponAngle(angle);
  252. if (this.fireCallback) {
  253. this.fireCallback();
  254. }
  255. if (this.node == this.ff.mainSprite.node) {
  256. if (this.hitCount > 4) {
  257. this.ff.shockMap();
  258. this.hitCount = 0;
  259. } else {
  260. this.hitCount++;
  261. }
  262. }
  263. this.ff.main.playerEffectByPath('music/magic_1001_fs');
  264. }
  265. }
  266. });
  267. }
  268. }