MSprite.ts 10 KB

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