barrage.ts 14 KB

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