MSprite.ts 10 KB

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