barrage.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. dirction = dirction.sub(worldPos).normalize();
  127. let paramJson = Utils.strToJson(bulletData.data.param);
  128. let num = paramJson[2];
  129. let angle = paramJson[1];
  130. let leftVec2 = dirction.rotateSelf(Utils.radian(angle/2));
  131. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  132. for (let index = 0; index < num; index++) {
  133. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  134. let bulletCpt = bulletNode.getComponent(monsterbullet);
  135. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  136. bulletCpt.monsterZooid = data.zooid;
  137. bulletCpt.bulletId = bulletData.id;
  138. bulletNode.zIndex = EBulletZ.monster;
  139. let moveCpt = Utils.getCpt(Move, bulletNode);
  140. if(angle == 360){
  141. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  142. }
  143. else{
  144. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num-1) * index));
  145. }
  146. moveCpt.speed = Utils.intDefault(bulletData.data.speed, 500);
  147. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  148. bulletNode.setPosition(localPos);
  149. let endJson = Utils.strToJson(bulletData.data.end);
  150. if(endJson[0]== 2) {
  151. let delayCpt = bulletNode.addComponent(DelayStop)
  152. delayCpt.time = endJson[1] / 100;
  153. delayCpt.call = ()=>{
  154. bulletNode.destroy();
  155. }
  156. }
  157. }
  158. }
  159. // 扇形弹幕
  160. async createPlayerBullet2(worldPos: cc.Vec2){
  161. let num = 9;
  162. let angle = 60;
  163. let leftVec2 = cc.v2(0,1).rotateSelf(Utils.radian(angle/2));
  164. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  165. for (let index = 0; index < num; index++) {
  166. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  167. bulletNode.zIndex = EBulletZ.player;
  168. let bulletCpt = bulletNode.getComponent(bullet);
  169. let moveCpt = Utils.getCpt(Move, bulletNode);
  170. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num-1) * index));
  171. moveCpt.speed = 500;
  172. bulletCpt.setDirction(moveCpt.dirction);
  173. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  174. bulletNode.setPosition(localPos);
  175. }
  176. }
  177. // 直线炸开
  178. async createBulletById4(data: IMonsterBullet, bulletData: BulletItem){
  179. // monsterZooid: number,
  180. let worldPos = data.worldPos;
  181. let dirction = data.dirction;
  182. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  183. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  184. bulletNode.zIndex = EBulletZ.monster;
  185. let bulletCpt = bulletNode.getComponent(monsterbullet);
  186. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  187. bulletCpt.monsterZooid = data.zooid;
  188. bulletCpt.bulletId = bulletData.id;
  189. let paramJson = Utils.strToJson(bulletData.data.param);
  190. let speed = Utils.intDefault(bulletData.data.speed, 500);
  191. let time = Utils.intDefault(paramJson[1], 100) / 100;
  192. let moveCpt = Utils.getCpt(MoveTime, bulletNode);
  193. moveCpt.dirction = dirction;
  194. moveCpt.speed = speed;
  195. moveCpt.overTime = time;
  196. moveCpt.call = ()=>{
  197. let subWorldPos = bulletNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
  198. let subBulletItem = ConfigMgr.inst.getCfgClassById(EConfigConst.bullet, paramJson[2], BulletItem);
  199. this.createMonsterBullet({
  200. worldPos: subWorldPos,
  201. dirction,
  202. atk: data.atk,
  203. zooid: data.zooid,
  204. }, subBulletItem);
  205. bulletNode.destroy();
  206. console.log('@@=',bulletNode.getPosition().toString());
  207. }
  208. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  209. bulletNode.setPosition(localPos);
  210. let endJson = Utils.strToJson(bulletData.data.end);
  211. if(endJson[0]== 2) {
  212. let delayCpt = bulletNode.addComponent(DelayStop)
  213. delayCpt.time = endJson[1] / 100;
  214. delayCpt.call = ()=>{
  215. bulletNode.destroy();
  216. }
  217. }
  218. }
  219. // 直线炸开
  220. async createPlayerBullet4(worldPos: cc.Vec2){
  221. let time = 1;
  222. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  223. let bulletNode = ResMgr.inst.createNode(prefab, this.layer)
  224. bulletNode.zIndex = EBulletZ.player;
  225. let moveCpt = Utils.getCpt(MoveTime, bulletNode);
  226. moveCpt.dirction = cc.v2(0,1);
  227. moveCpt.speed = 500;
  228. moveCpt.overTime = time;
  229. moveCpt.call = ()=>{
  230. let num = 10;
  231. let angle = 360;
  232. let leftVec2 = cc.v2(1,1).normalize();
  233. for (let index = 0; index < num; index++) {
  234. let bulletNode1 = ResMgr.inst.createNode(prefab, this.layer)
  235. bulletNode1.zIndex = EBulletZ.player;
  236. let bulletCpt = bulletNode1.getComponent(bullet);
  237. let moveCpt = Utils.getCpt(Move, bulletNode1);
  238. moveCpt.dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  239. moveCpt.speed = 500;
  240. bulletCpt.setDirction(moveCpt.dirction);
  241. bulletNode1.setPosition(bulletNode.getPosition());
  242. // console.log( "@@-", bulletNode1.getPosition().toString());
  243. }
  244. bulletNode.destroy();
  245. console.log('@@=',bulletNode.getPosition().toString());
  246. }
  247. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  248. bulletNode.setPosition(localPos);
  249. }
  250. // 旋转弹幕 --
  251. async createBulletById3(data: IMonsterBullet, bulletData: BulletItem){
  252. let worldPos = data.worldPos;
  253. let dirction = data.dirction;
  254. let paramJson = Utils.strToJson(bulletData.data.param);
  255. let r = paramJson[1];
  256. let num = paramJson[2];
  257. let leftVec2 = dirction;
  258. let angle = 360;
  259. let prefab = await ResMgr.inst.getPrefab('monster/monsterBullet');
  260. let cricleNode = new cc.Node('cricleBullet');
  261. this.layer.addChild(cricleNode);
  262. let moveCpt = Utils.getCpt(Move, cricleNode);
  263. moveCpt.dirction = dirction;
  264. moveCpt.speed = Utils.intDefault(bulletData.data.speed, 500);
  265. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  266. cricleNode.setPosition(localPos);
  267. let t = cc.tween(cricleNode).by(1, {angle: 360}).repeatForever().start();
  268. let isOver = ()=>{
  269. if(cc.isValid(cricleNode, true)){
  270. if(cricleNode.childrenCount <= 0){
  271. t.stop();
  272. cricleNode.destroy();
  273. }
  274. }
  275. }
  276. for (let index = 0; index < num; index++) {
  277. let bulletNode = ResMgr.inst.createNode(prefab, cricleNode)
  278. let bulletCpt = bulletNode.getComponent(monsterbullet);
  279. bulletCpt.atk = Utils.intDefault(bulletData.data.effect1)/ 100 * data.atk;
  280. bulletCpt.monsterZooid = data.zooid;
  281. bulletCpt.bulletId = bulletData.id;
  282. bulletNode.zIndex = EBulletZ.monster;
  283. let dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  284. bulletNode.setPosition(dirction.mul(r));
  285. bulletCpt.setDirction(dirction.rotate(Utils.radian(90)));
  286. bulletNode.addComponent(ResRelease).call = function(){
  287. isOver();
  288. }
  289. let endJson = Utils.strToJson(bulletData.data.end);
  290. if(endJson[0]== 2) {
  291. let delayCpt = bulletNode.addComponent(DelayStop)
  292. delayCpt.time = endJson[1] / 100;
  293. delayCpt.call = ()=>{
  294. bulletNode.destroy();
  295. }
  296. }
  297. }
  298. }
  299. // 旋转弹幕
  300. async createPlayerBullet3(worldPos: cc.Vec2){
  301. let num = 8;
  302. let r = 40;
  303. let leftVec2 = cc.v2(0,1)
  304. let angle = 360;
  305. let prefab = await ResMgr.inst.getPrefab('monster/bullet');
  306. let node = new cc.Node('cricleBullet');
  307. this.layer.addChild(node);
  308. let moveCpt = Utils.getCpt(Move, node);
  309. moveCpt.dirction = cc.v2(0,1);
  310. moveCpt.speed = 500;
  311. let localPos = this.layer.convertToNodeSpaceAR(worldPos);
  312. node.setPosition(localPos);
  313. let t = cc.tween(node).by(1, {angle: 360}).repeatForever().start();
  314. let isOver = ()=>{
  315. if(cc.isValid(node, true)){
  316. if(node.childrenCount <= 0){
  317. t.stop();
  318. node.destroy();
  319. }
  320. }
  321. }
  322. for (let index = 0; index < num; index++) {
  323. let bulletNode = ResMgr.inst.createNode(prefab, node)
  324. let bulletCpt = bulletNode.getComponent(bullet);
  325. bulletNode.zIndex = EBulletZ.player;
  326. let dirction = leftVec2.rotate(Utils.radian(-angle / (num) * index));
  327. bulletNode.setPosition(dirction.mul(r));
  328. bulletCpt.setDirction(dirction.rotate(Utils.radian(90)));
  329. bulletNode.addComponent(ResRelease).call = function(){
  330. isOver();
  331. }
  332. }
  333. }
  334. }