MSprite.ts 9.5 KB

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