-
Notifications
You must be signed in to change notification settings - Fork 4
/
enemy.js
57 lines (44 loc) · 1.41 KB
/
enemy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import Phaser from 'phaser';
import Generator from './generator';
import Service from '../../../service';
import SpringMan from '../../actors/spring-man';
import FlyMan from '../../actors/fly-man';
import SpikeBall from '../../actors/spike-ball';
import Cloud from '../../actors/cloud';
export default class EnemyGenerator extends Generator {
constructor(game, bunny, grounds) {
super(game, bunny);
this.grounds = grounds;
this.grounds.signals.generate.add(this.generate, this);
this.score = Service.get('Score');
this.types = [SpringMan, FlyMan, SpikeBall, Cloud];
}
generate(ground) {
const maxChance = 25;
const maxDistance = 100;
const { currentDistance } = this.score;
const currentChance =
this.cubicInOut(currentDistance / maxDistance) * maxChance;
if (!Phaser.Utils.chanceRoll(currentChance)) return null;
const marginLeft = this.game.rnd.between(50, 150);
let x = 0;
let y = 0;
x = ground.x + ground.width + marginLeft;
y = ground.y + this.game.rnd.between(-75, 75);
const TypeClass = this.game.rnd.pick(this.types);
let enemy = this.children.find(
item => item.constructor === TypeClass && !item.alive
);
if (enemy == null) {
enemy = new TypeClass(this.game, x, y);
this.add(enemy);
} else {
enemy.reset(x, y);
}
return enemy;
}
cubicInOut(t) {
if (t > 1) return 1;
return t;
}
}