EnemyTrigger.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import FMap from "../map/FMap";
  2. import CUtilTime from "../../../util/CUtilTime";
  3. import FSprite from "../object/FSprite";
  4. import CMath from "../../../util/CMath";
  5. import { GroupType } from "../object/FObject";
  6. const { ccclass, property } = cc._decorator;
  7. /**
  8. * 怪物触发器
  9. */
  10. @ccclass
  11. export default class EnemyTrigger extends cc.Component {
  12. @property({
  13. displayName: '关联表格数据'
  14. })
  15. mId: number = 0;
  16. @property({
  17. type: cc.Prefab,
  18. displayName: '怪物原型',
  19. })
  20. mMonster: cc.Prefab = null;
  21. @property({
  22. displayName: '初始怪物数量'
  23. })
  24. mInitCount: number = 20;
  25. @property({
  26. displayName: '最大存在值',
  27. })
  28. mMaxCount: number = 20;
  29. @property({
  30. displayName: '刷怪周期/秒(-1:不刷新)',
  31. })
  32. mFlushTime: number = 5;
  33. @property({
  34. displayName: '周期刷怪个数',
  35. })
  36. mFlushCount: number = 3;
  37. public mMap: FMap;
  38. /**
  39. * 上次刷新时间
  40. */
  41. private prevTime = 0;
  42. onLoad() {
  43. let mapNode = this.node.parent.parent;
  44. this.mMap = mapNode.getComponent(FMap);
  45. this.flush(this.mInitCount);
  46. this.prevTime = CUtilTime.getNowTime();
  47. if (this.mFlushTime > 0) {
  48. this.schedule(this.addMonster, this.mFlushTime);
  49. }
  50. }
  51. /**
  52. * 刷新怪物
  53. * @param count
  54. */
  55. private flush(count: number) {
  56. for (let i = 0; i < count; i++) {
  57. // let node: cc.Node = cc.instantiate(this.mMonster);
  58. // let sp: FSprite = node.getComponent(FSprite);
  59. // sp.node.group = GroupType.B;
  60. // sp.trigger = this;
  61. // sp.mId = this.mId;
  62. // sp.fData = this.mMap.ff.main.sManage.getMonsterData(this.mId);
  63. // node.setPosition(this.getRandInit());
  64. // this.mMap.addSprite(sp);
  65. }
  66. }
  67. public getRandInit(): cc.Vec2 {
  68. let pos = cc.v2();
  69. pos.x = CMath.getRandom(this.node.x - this.node.width / 2, this.node.x + this.node.width / 2);
  70. pos.y = CMath.getRandom(this.node.y - this.node.height / 2, this.node.y + this.node.height / 2);
  71. return pos;
  72. }
  73. public addMonster() {
  74. //判断数量
  75. // let count = 0;
  76. // let sprites = this.mMap.getSprites();
  77. // for (let i = 0; i < sprites.length; i++) {
  78. // const node = sprites[i];
  79. // let sp = node.getComponent(FSprite);
  80. // if(sp && sp.trigger == this){
  81. // count ++;
  82. // }
  83. // }
  84. // if(count >= this.mMaxCount){
  85. // return;
  86. // }
  87. // this.flush(this.mFlushCount);
  88. }
  89. }