forked from enriquesanchezb/game_for_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayerState.js
More file actions
160 lines (151 loc) · 5.01 KB
/
playerState.js
File metadata and controls
160 lines (151 loc) · 5.01 KB
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
import { Dust, Fire, Splash } from './particles.js'
const states = {
SITTING: 0,
RUNNING: 1,
JUMPING: 2,
FALLING: 3,
ROLLING: 4,
DIVING: 5,
HIT: 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.frameX = 0;
this.game.player.maxFrame = 4;
this.game.player.frameY = 5;
}
handleInput(input){
if(input.includes('ArrowLeft') || input.includes('ArrowRight')){
this.game.player.setState(states.RUNNING, 1);
} else if (input.includes('Enter')){
this.game.player.setState(states.ROLLING, 2);
}
}
}
export class Running extends State {
constructor(game){
super('RUNNING', game);
}
enter(){
this.game.player.frameX = 0;
this.game.player.maxFrame = 8;
this.game.player.frameY = 3;
}
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('Enter')){
this.game.player.setState(states.ROLLING, 2);
}
}
}
export class Jumping extends State {
constructor(game){
super('JUMPING',game);
}
enter(){
this.game.player.frameX = 0;
if (this.game.player.onGround()) this.game.player.vy -= 30;
this.game.player.maxFrame = 6;
this.game.player.frameY = 1;
}
handleInput(input){
if(this.game.player.vy > this.game.player.weight){
this.game.player.setState(states.FALLING, 1);
} else if (input.includes('Enter')){
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.frameX = 0;
this.game.player.maxFrame = 6;
this.game.player.frameY = 2;
}
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.frameX = 0;
this.game.player.maxFrame = 6;
this.game.player.frameY = 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('Enter') && this.game.player.onGround()){
this.game.player.setState(states.RUNNING, 1);
} else if(!input.includes('Enter') && !this.game.player.onGround()){
this.game.player.setState(states.FALLING, 1);
} else if(input.includes('Enter') && 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.frameX = 0;
this.game.player.maxFrame = 6;
this.game.player.frameY = 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 i = 0; i < 30; i++){
this.game.particles.unshift(new Splash(this.game, this.game.player.x + this.game.player.width *0.5, this.game.player.y+ this.game.player.height));
}
} else if(input.includes('Enter') && this.game.player.onGround()){
this.game.player.setState(states.ROLLING, 2);
}
}
}
export class HIT extends State {
constructor(game){
super('HIT', game);
}
enter(){
this.game.player.frameX = 0;
this.game.player.maxFrame = 10;
this.game.player.frameY = 4;
}
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);
}
}
}