Lv.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. export enum LvDir {
  2. none = 0,
  3. left = 1,
  4. right = 2,
  5. up = 3,
  6. down = 4,
  7. xCenter = 5,
  8. xOut = 6,
  9. yCenter = 7,
  10. yOut = 8,
  11. }
  12. interface event_protocol {
  13. life_change(life: number): void;
  14. dir_change(dir: LvDir): void;
  15. }
  16. /**关卡*/
  17. export class Lv {
  18. private _lv: number;//关卡数
  19. public get lv(): number { return this._lv };
  20. private _life: number = 3;//生命值
  21. public get life(): number { return this._life };
  22. private _is_win: boolean = false;//连胜
  23. public get is_win(): boolean { return this._is_win };
  24. private _dir: LvDir = LvDir.none;//关卡状态
  25. public get dir(): LvDir { return this._dir };
  26. private _time: number = 0;//时间设置
  27. public get time(): number { return this._time };
  28. public evt = chsdk.get_new_event<event_protocol>();
  29. public _seed: number;
  30. public get seed(): number { return this._seed };
  31. private _relive_count: number = 0;
  32. public get relive_count(): number { return this._relive_count };
  33. public readonly relive_max: number = 5;
  34. public record: { item1: number, item2: number, item3: number, relive: number };
  35. constructor() {
  36. }
  37. public init(lv: number) {
  38. this.record = {
  39. item1: 0, item2: 0, item3: 0, relive: 0,
  40. }
  41. //this._seed = ch.util.getRandomInt(1000, 10000000000) + floor * lv;
  42. this._time = 0;
  43. this._is_win = false;
  44. this._lv = lv;
  45. this._life = 3;
  46. this._relive_count = 0;
  47. this.evt.emit(this.evt.key.life_change, this._life);
  48. switch (this._lv) {
  49. case 1:
  50. case 2:
  51. this._dir = LvDir.none;
  52. break;
  53. case 3:
  54. this._dir = LvDir.left;
  55. break;
  56. case 4:
  57. this._dir = LvDir.up;
  58. break;
  59. case 5:
  60. this._dir = LvDir.right;
  61. break;
  62. case 6:
  63. this._dir = LvDir.down;
  64. break;
  65. case 7:
  66. this._dir = LvDir.xOut;
  67. break;
  68. case 8:
  69. this._dir = LvDir.yOut;
  70. break;
  71. case 9:
  72. this._dir = LvDir.xCenter;
  73. break;
  74. case 10:
  75. this._dir = LvDir.yCenter;
  76. break;
  77. }
  78. }
  79. public change_dir(): LvDir {
  80. switch (this._dir) {
  81. case LvDir.none:
  82. break;
  83. case LvDir.left:
  84. this._dir = LvDir.right;
  85. break;
  86. case LvDir.up:
  87. this._dir = LvDir.down;
  88. break;
  89. case LvDir.right:
  90. this._dir = LvDir.left;
  91. break;
  92. case LvDir.down:
  93. this._dir = LvDir.up;
  94. break;
  95. case LvDir.xOut:
  96. this._dir = LvDir.xCenter;
  97. break;
  98. case LvDir.yOut:
  99. this._dir = LvDir.yCenter;
  100. break;
  101. case LvDir.xCenter:
  102. this._dir = LvDir.xOut;
  103. break;
  104. case LvDir.yCenter:
  105. this._dir = LvDir.yOut;
  106. break;
  107. }
  108. this.evt.emit(this.evt.key.dir_change, this._dir);
  109. return this._dir;
  110. }
  111. /**剩余复活次数*/
  112. public get_relife_count_down(): number {
  113. const count = this.relive_max - this._relive_count;
  114. return count < 0 ? 0 : count;
  115. }
  116. /**复活*/
  117. public relife(): void {
  118. this._life = 3;
  119. this._relive_count++;
  120. this.record.relive++;
  121. this.evt.emit(this.evt.key.life_change, this._life);
  122. }
  123. /**扣血*/
  124. public change_life(n: number): void {
  125. this._life += n;
  126. if (this._life <= 0) {
  127. this._life = 0;
  128. this.lose();
  129. } else if (this._life > 6) {
  130. this._life = 6;
  131. }
  132. this.evt.emit(this.evt.key.life_change, this._life);
  133. }
  134. private lose(): void {
  135. this._is_win = false;
  136. // GameLink.getInst().game_end();
  137. }
  138. public win(): void {
  139. this._is_win = true;
  140. //GameLink.getInst().game_end();
  141. }
  142. public time_run(dt: number): void {
  143. this._time += dt;
  144. }
  145. }