-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.js
171 lines (162 loc) · 4.45 KB
/
states.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Dust, Fire, Splash } from "./particle.js";
const states = {
SITTING: 0,
RUNNING: 1,
JUMPING: 2,
FALLING: 3,
ROLLING: 4,
DIVING: 5,
DIZZY: 6,
};
class State {
constructor(state, game) {
this.state = state;
this.game = game;
}
}
export class Sitting extends State {
constructor(game) {
super("SITTING", game);
}
enter() {
this.game.player.frameY = 5;
this.game.player.maxFrames = 4;
}
handleInput(input) {
if (input.includes("ArrowLeft") || input.includes("ArrowRight"))
this.game.player.setState(states.RUNNING, 1);
else if (input.includes(" ")) this.game.player.setState(states.ROLLING, 2);
}
}
export class Running extends State {
constructor(game) {
super("RUNNING", game);
}
enter() {
this.game.player.frameY = 3;
this.game.player.maxFrames = 8;
}
handleInput(input) {
this.game.particles.unshift(
new Dust(
this.game,
this.game.player.x + this.game.player.width * 0.5,
this.game.player.y + this.game.player.height
)
);
if (input.includes("ArrowDown"))
this.game.player.setState(states.SITTING, 0);
else if (input.includes("ArrowUp"))
this.game.player.setState(states.JUMPING, 1);
else if (input.includes(" ")) this.game.player.setState(states.ROLLING, 2);
}
}
export class Jumping extends State {
constructor(game) {
super("JUMPING", game);
}
enter() {
this.game.player.frameY = 1;
this.game.player.maxFrames = 6;
if (this.game.player.onGround()) this.game.player.vy = -27;
}
handleInput(input) {
if (!this.game.player.onGround() && this.game.player.vy >= 0)
this.game.player.setState(states.FALLING, 1);
else if (input.includes(" ")) this.game.player.setState(states.ROLLING, 2);
else if (input.includes("ArrowDown"))
this.game.player.setState(states.DIVING, 0);
}
}
export class Falling extends State {
constructor(game) {
super("FALLING", game);
}
enter() {
this.game.player.frameY = 2;
this.game.player.maxFrames = 6;
}
handleInput(input) {
if (this.game.player.onGround())
this.game.player.setState(states.RUNNING, 1);
else if (input.includes("ArrowDown"))
this.game.player.setState(states.DIVING, 0);
}
}
export class Rolling extends State {
constructor(game) {
super("ROLLING", game);
}
enter() {
this.game.player.frameY = 6;
this.game.player.maxFrames = 6;
}
handleInput(input) {
this.game.particles.unshift(
new Fire(
this.game,
this.game.player.x + this.game.player.width * 0.5,
this.game.player.y + this.game.player.height * 0.5
)
);
if (!input.includes(" ") && this.game.player.onGround())
this.game.player.setState(states.RUNNING, 1);
else if (!input.includes(" ") && !this.game.player.onGround())
this.game.player.setState(states.FALLING, 1);
else if (
input.includes(" ") &&
input.includes("ArrowUp") &&
this.game.player.onGround()
) {
this.game.player.vy = -27;
} else if (input.includes("ArrowDown") && !this.game.player.onGround())
this.game.player.setState(states.DIVING, 0);
}
}
export class Diving extends State {
constructor(game) {
super("DIVING", game);
}
enter() {
this.game.player.frameY = 6;
this.game.player.maxFrames = 6;
this.game.player.vy = 15;
}
handleInput(input) {
this.game.particles.unshift(
new Fire(
this.game,
this.game.player.x + this.game.player.width * 0.5,
this.game.player.y + this.game.player.height * 0.5
)
);
if (this.game.player.onGround()) {
this.game.player.setState(states.RUNNING, 1);
for (let index = 0; index < 30; index++) {
this.game.particles.unshift(
new Splash(
this.game,
this.game.player.x + this.game.player.width * 0.5,
this.game.player.y
)
);
}
} else if (input.includes(" ") && this.game.player.onGround())
this.game.player.setState(states.ROLLING, 2);
}
}
export class Dizzy extends State {
constructor(game) {
super("Dizzy", game);
}
enter() {
this.game.player.frameY = 4;
this.game.player.maxFrames = 10;
}
handleInput(input) {
if (this.game.player.frameX >= 10 && this.game.player.onGround()) {
this.game.player.setState(states.RUNNING, 1);
} else if (this.game.player.frameX >= 10 && !this.game.player.onGround())
this.game.player.setState(states.FALLING, 1);
}
}