MSprite.ts 9.9 KB

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