barrage.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import { EConfigConst } from "../cfg/ConfigDef";
  2. import { BulletItem } from "../cfg/bulletItem";
  3. import { ConfigMgr } from "../cfg/configMgr";
  4. import { Utils } from "../common/utils";
  5. import { MonsterModel } from "../monster/monsterModel";
  6. import monsterbullet from "../monster/monsterbullet";
  7. import bullet from "../player/bullet";
  8. import { ResMgr } from "../res/resMgr";
  9. import { ResRelease } from "../res/resRelease";
  10. import DelayStop from "../update/delayStop";
  11. import { GameSysLogic } from "../update/gameSysLogic";
  12. import Move from "../update/move";
  13. import MoveTime from "../update/moveTime";
  14. import { ZooMgr } from "../zoo/zooMgr";
  15. /**
  16. * 1 扇形弹幕: 开始角度(相对于向上), 角度,数量
  17. * 2 旋转飞行: 半径
  18. * 3 直线: 开始角度
  19. * 4 直线后炸开: 时间,开始角度,数量
  20. */
  21. enum EBulletZ {
  22. monster,
  23. player,
  24. }
  25. interface IMonsterBullet{
  26. worldPos: cc.Vec2;
  27. dirction: cc.Vec2;
  28. atk: number;
  29. zooid?: number;
  30. }
  31. export class Barrage extends GameSysLogic{
  32. layer: cc.Node;
  33. static skillNum = 1;
  34. private static _inst: Barrage;
  35. public static get inst(): Barrage {
  36. if (this._inst == null) {
  37. this._inst = new Barrage();
  38. }
  39. return this._inst;
  40. }
  41. async createPlayerBullet(worldPos: cc.Vec2){
  42. switch(Barrage.skillNum){
  43. case 1:
  44. this.createPlayerBullet1(worldPos);
  45. break;
  46. case 2:
  47. this.createPlayerBullet2(worldPos);
  48. break;
  49. case 3:
  50. this.createPlayerBullet3(worldPos);
  51. break;
  52. case 4:
  53. this.createPlayerBullet4(worldPos);
  54. break;
  55. }
  56. }
  57. createMonsterBulletByZooid(monsterZooid: number, id: string){
  58. let bulletItem = ConfigMgr.inst.getCfgClassById(EConfigConst.bullet, id, BulletItem);
  59. let monsterNode = ZooMgr.inst.get(monsterZooid).node;
  60. let model = monsterNode.getComponent(MonsterModel);
  61. let worldPos = monsterNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
  62. let dirction = monsterNode.convertToWorldSpaceAR(cc.v2(0,-1));
  63. dirction = dirction.sub(worldPos).normalize();
  64. let data: IMonsterBullet = {
  65. worldPos,
  66. dirction,
  67. atk: model.atk,
  68. zooid: monsterZooid,
  69. };
  70. this.createMonsterBullet(data, bulletItem);
  71. }
  72. createMonsterBullet(data: IMonsterBullet, bulletItem: BulletItem) {
  73. switch (Utils.intDefault(bulletItem.data.travel, 1)) {
  74. case 1:
  75. this.createBulletById1(data, bulletItem);
  76. break;
  77. case 2:
  78. this.createBulletById2(data, bulletItem);
  79. break;
  80. case 3:
  81. this.createBulletById3(data, bulletItem);
  82. break;
  83. case 4:
  84. this.createBulletById4(data, bulletItem);
  85. break;
  86. }
  87. }
  88. // 直线
  89. async createBulletById1(data: IMonsterBullet, bulletData: BulletItem){
  90. let bulletNode = await ResMgr.inst.loadPrefab('monster/monsterBullet', this.layer);
  91. let worldPos = data.worldPos;
  92. let dirction = data.dirction;
  93. let bulletCpt = bulletNode.getComponent(monsterbullet);
  94. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1) / 100 * data.atk;
  95. bulletCpt.monsterZooid = data.zooid;
  96. bulletCpt.bulletId = bulletData.id;
  97. bulletNode.zIndex = EBulletZ.monster;
  98. let moveCpt = Utils.getCpt(Move, bulletNode);
  99. moveCpt.dirction = dirction;
  100. moveCpt.speed = Utils.intDefault(bulletData.data.speed, 500);
  101. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  102. bulletNode.setPosition(localPos);
  103. let endJson = Utils.strToJson(bulletData.data.end);
  104. if(endJson[0]== 2) {
  105. let delayCpt = bulletNode.addComponent(DelayStop)
  106. delayCpt.time = endJson[1] / 100;
  107. delayCpt.call = ()=>{
  108. bulletNode.destroy();
  109. }
  110. }
  111. }
  112. // 直线
  113. async createPlayerBullet1(worldPos: cc.Vec2){
  114. let bulletNode = await ResMgr.inst.loadPrefab('monster/bullet', this.layer);
  115. bulletNode.zIndex = EBulletZ.player;
  116. let moveCpt = Utils.getCpt(Move, bulletNode);
  117. moveCpt.dirction = cc.v2(0,1);
  118. moveCpt.speed = 500;
  119. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  120. bulletNode.setPosition(localPos);
  121. }
  122. // 扇形弹幕
  123. async createBulletById2(data: IMonsterBullet, bulletData: BulletItem){
  124. let worldPos = data.worldPos;
  125. let dirction = data.dirction;
  126. let paramJson = Utils.strToJson(bulletData.data.param);
  127. let num = paramJson[2];
  128. let angle = paramJson[1];
  129. let leftVec2 = dirction.rotateSelf(Utils.radian(angle/2));
  130. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  131. for (let index = 0; index < num; index++) {
  132. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  133. let bulletCpt = bulletNode.getComponent(monsterbullet);
  134. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  135. bulletCpt.monsterZooid = data.zooid;
  136. bulletCpt.bulletId = bulletData.id;
  137. bulletNode.zIndex = EBulletZ.monster;
  138. let moveCpt = Utils.getCpt(Move, bulletNode);
  139. if(angle == 360){
  140. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  141. }
  142. else{
  143. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num-1) * index));
  144. }
  145. moveCpt.speed = Utils.intDefault(bulletData.data.speed, 500);
  146. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  147. bulletNode.setPosition(localPos);
  148. let endJson = Utils.strToJson(bulletData.data.end);
  149. if(endJson[0]== 2) {
  150. let delayCpt = bulletNode.addComponent(DelayStop)
  151. delayCpt.time = endJson[1] / 100;
  152. delayCpt.call = ()=>{
  153. bulletNode.destroy();
  154. }
  155. }
  156. }
  157. }
  158. // 扇形弹幕
  159. async createPlayerBullet2(worldPos: cc.Vec2){
  160. let num = 9;
  161. let angle = 60;
  162. let leftVec2 = cc.v2(0,1).rotateSelf(Utils.radian(angle/2));
  163. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  164. for (let index = 0; index < num; index++) {
  165. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  166. bulletNode.zIndex = EBulletZ.player;
  167. let bulletCpt = bulletNode.getComponent(bullet);
  168. let moveCpt = Utils.getCpt(Move, bulletNode);
  169. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num-1) * index));
  170. moveCpt.speed = 500;
  171. bulletCpt.setDirction(moveCpt.dirction);
  172. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  173. bulletNode.setPosition(localPos);
  174. }
  175. }
  176. // 直线炸开
  177. async createBulletById4(data: IMonsterBullet, bulletData: BulletItem){
  178. // monsterZooid: number,
  179. let worldPos = data.worldPos;
  180. let dirction = data.dirction;
  181. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  182. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  183. bulletNode.zIndex = EBulletZ.monster;
  184. let bulletCpt = bulletNode.getComponent(monsterbullet);
  185. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  186. bulletCpt.monsterZooid = data.zooid;
  187. bulletCpt.bulletId = bulletData.id;
  188. let paramJson = Utils.strToJson(bulletData.data.param);
  189. let speed = Utils.intDefault(bulletData.data.speed, 500);
  190. let time = Utils.intDefault(paramJson[1], 100) / 100;
  191. let moveCpt = Utils.getCpt(MoveTime, bulletNode);
  192. moveCpt.dirction = dirction;
  193. moveCpt.speed = speed;
  194. moveCpt.overTime = time;
  195. moveCpt.call = ()=>{
  196. let subWorldPos = bulletNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
  197. let subBulletItem = ConfigMgr.inst.getCfgClassById(EConfigConst.bullet, paramJson[2], BulletItem);
  198. this.createMonsterBullet({
  199. worldPos: subWorldPos,
  200. dirction,
  201. atk: data.atk,
  202. zooid: data.zooid,
  203. }, subBulletItem);
  204. bulletNode.destroy();
  205. // console.log('@@=',bulletNode.getPosition().toString());
  206. }
  207. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  208. bulletNode.setPosition(localPos);
  209. let endJson = Utils.strToJson(bulletData.data.end);
  210. if(endJson[0]== 2) {
  211. let delayCpt = bulletNode.addComponent(DelayStop)
  212. delayCpt.time = endJson[1] / 100;
  213. delayCpt.call = ()=>{
  214. bulletNode.destroy();
  215. }
  216. }
  217. }
  218. // 直线炸开
  219. async createPlayerBullet4(worldPos: cc.Vec2){
  220. let time = 1;
  221. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  222. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  223. bulletNode.zIndex = EBulletZ.player;
  224. let moveCpt = Utils.getCpt(MoveTime, bulletNode);
  225. moveCpt.dirction = cc.v2(0,1);
  226. moveCpt.speed = 500;
  227. moveCpt.overTime = time;
  228. moveCpt.call = ()=>{
  229. let num = 10;
  230. let angle = 360;
  231. let leftVec2 = cc.v2(1,1).normalize();
  232. for (let index = 0; index < num; index++) {
  233. let bulletNode1 = ResMgr.inst.createNode(prefab, this.layer)
  234. bulletNode1.zIndex = EBulletZ.player;
  235. let bulletCpt = bulletNode1.getComponent(bullet);
  236. let moveCpt = Utils.getCpt(Move, bulletNode1);
  237. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  238. moveCpt.speed = 500;
  239. bulletCpt.setDirction(moveCpt.dirction);
  240. bulletNode1.setPosition(bulletNode.getPosition());
  241. // console.log( "@@-", bulletNode1.getPosition().toString());
  242. }
  243. bulletNode.destroy();
  244. // console.log('@@=',bulletNode.getPosition().toString());
  245. }
  246. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  247. bulletNode.setPosition(localPos);
  248. }
  249. // 旋转弹幕 --
  250. async createBulletById3(data: IMonsterBullet, bulletData: BulletItem){
  251. let worldPos = data.worldPos;
  252. let dirction = data.dirction;
  253. let paramJson = Utils.strToJson(bulletData.data.param);
  254. let r = paramJson[1];
  255. let num = paramJson[2];
  256. let leftVec2 = dirction;
  257. let angle = 360;
  258. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  259. let cricleNode = new cc.Node('cricleBullet');
  260. this.layer.addChild(cricleNode);
  261. let moveCpt = Utils.getCpt(Move, cricleNode);
  262. moveCpt.dirction = dirction;
  263. moveCpt.speed = Utils.intDefault(bulletData.data.speed, 500);
  264. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  265. cricleNode.setPosition(localPos);
  266. let t = cc.tween(cricleNode).by(1, {angle: 360}).repeatForever().start();
  267. let isOver = ()=>{
  268. if(cc.isValid(cricleNode, true)){
  269. if(cricleNode.childrenCount <= 0){
  270. t.stop();
  271. cricleNode.destroy();
  272. }
  273. }
  274. }
  275. for (let index = 0; index < num; index++) {
  276. let bulletNode = ResMgr.inst.createNode(prefab, cricleNode)
  277. let bulletCpt = bulletNode.getComponent(monsterbullet);
  278. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  279. bulletCpt.monsterZooid = data.zooid;
  280. bulletCpt.bulletId = bulletData.id;
  281. bulletNode.zIndex = EBulletZ.monster;
  282. let dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  283. bulletNode.setPosition(dirction.mul(r));
  284. bulletCpt.setDirction(dirction.rotate(Utils.radian(90)));
  285. bulletNode.addComponent(ResRelease).call = function(){
  286. isOver();
  287. }
  288. let endJson = Utils.strToJson(bulletData.data.end);
  289. if(endJson[0]== 2) {
  290. let delayCpt = bulletNode.addComponent(DelayStop)
  291. delayCpt.time = endJson[1] / 100;
  292. delayCpt.call = ()=>{
  293. bulletNode.destroy();
  294. }
  295. }
  296. }
  297. }
  298. // 旋转弹幕
  299. async createPlayerBullet3(worldPos: cc.Vec2){
  300. let num = 8;
  301. let r = 40;
  302. let leftVec2 = cc.v2(0,1)
  303. let angle = 360;
  304. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  305. let node = new cc.Node('cricleBullet');
  306. this.layer.addChild(node);
  307. let moveCpt = Utils.getCpt(Move, node);
  308. moveCpt.dirction = cc.v2(0,1);
  309. moveCpt.speed = 500;
  310. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  311. node.setPosition(localPos);
  312. let t = cc.tween(node).by(1, {angle: 360}).repeatForever().start();
  313. let isOver = ()=>{
  314. if(cc.isValid(node, true)){
  315. if(node.childrenCount <= 0){
  316. t.stop();
  317. node.destroy();
  318. }
  319. }
  320. }
  321. for (let index = 0; index < num; index++) {
  322. let bulletNode = ResMgr.inst.createNode(prefab, node)
  323. let bulletCpt = bulletNode.getComponent(bullet);
  324. bulletNode.zIndex = EBulletZ.player;
  325. let dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  326. bulletNode.setPosition(dirction.mul(r));
  327. bulletCpt.setDirction(dirction.rotate(Utils.radian(90)));
  328. bulletNode.addComponent(ResRelease).call = function(){
  329. isOver();
  330. }
  331. }
  332. }
  333. }