-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmob.ts
78 lines (64 loc) · 3.01 KB
/
mob.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// a "Mobile OBject". The things you can shoot. Balloons, and so on.
class Balloon extends Entity {
constructor(pos: Point) {
super( pos )
this.sprite = new SimpleSprite(ImageResource.BALLOON)
this.sprite.renderPivot = { x: CONST.MOB_BALLOON_RADIUS, y: CONST.MOB_BALLOON_RADIUS}
this.sprite.drawOrder = CONST.LAYER_MOBS
this.collisionRadiusSqare = CONST.MOB_BALLOON_RADIUS * CONST.MOB_BALLOON_RADIUS
this.behaviors.push(new SineMovementBehavior(this, 10, 2.0, "y"))
}
}
class Bird extends Entity {
constructor(pos: Point) {
super( pos )
this.sprite = new AnimatedSprite(ImageResource.BIRD, { x: 182, y:117 })
this.sprite.renderPivot = { x: 77, y: 77}
this.sprite.drawOrder = CONST.LAYER_MOBS
this.collisionRadiusSqare = CONST.MOB_BIRD_RADIUS * CONST.MOB_BIRD_RADIUS
this.behaviors.push(new SineMovementBehavior(this, CONST.MOB_BIRD_MOVE_RANGE, CONST.MOB_BIRD_MOVE_TIME, "x"))
this.behaviors.push(new AnimatedBehavior(CONST.MOB_BIRD_ANIMATION_LENGHT, true))
}
}
class Ufo extends Entity {
constructor(pos: Point) {
super( pos )
this.sprite = new AnimatedSprite(ImageResource.UFO, { x: 131, y:75 })
this.sprite.renderPivot = { x: 65, y: 40}
this.sprite.drawOrder = CONST.LAYER_MOBS
this.collisionRadiusSqare = CONST.MOB_UFO_RADIUS * CONST.MOB_UFO_RADIUS
this.behaviors.push(new AnimatedBehavior(CONST.MOB_UFO_ANIMATION_LENGHT, true))
this.behaviors.push(new SineMovementBehavior(this, CONST.MOB_UFO_MOVE_RANGE_X, CONST.MOB_UFO_MOVE_TIME_X, "x"))
this.behaviors.push(new SineMovementBehavior(this, CONST.MOB_UFO_MOVE_RANGE_Y, CONST.MOB_UFO_MOVE_TIME_Y, "y"))
}
}
class MobLayer {
from: number
range: number
count: number
create: (pos:Point) => Entity
spawn(state:State, start:number) {
for (let i = 0; i < this.count; i++) {
state.addEntity(this.create({
x: Math.random() * CONST.CHUNK_WIDTH + start,
y: Math.pow(Math.random(), 2) * -this.range - this.from
}))
}
}
constructor(create: (pos:Point) => Entity, from: number, to: number, count: number) {
this.create = create
this.from = from
this.range = to - from
this.count = count
}
}
// these are the height levels at which the mobs spawn
const MOB_LAYERS = [
new MobLayer((pos) => new Balloon(pos), 5 * CONST.METER, 15 * CONST.METER, 1),
new MobLayer((pos) => new Balloon(pos), 5 * CONST.METER, 100 * CONST.METER, 10),
new MobLayer((pos) => new Balloon(pos), 5 * CONST.METER, 300 * CONST.METER, 10),
new MobLayer((pos) => new Bird(pos), 100 * CONST.METER, 20 * CONST.METER, 5),
new MobLayer((pos) => new Bird(pos), 100 * CONST.METER, 400 * CONST.METER, 20),
new MobLayer((pos) => new Ufo(pos), 300 * CONST.METER, 150 * CONST.METER, 3),
new MobLayer((pos) => new Ufo(pos), 300 * CONST.METER, 600 * CONST.METER, 20),
]