-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTATE_MANAGEMENT.html
More file actions
209 lines (181 loc) · 5.26 KB
/
STATE_MANAGEMENT.html
File metadata and controls
209 lines (181 loc) · 5.26 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<style>
*{
padding: 0;
margin: 0;
user-select: none;
box-sizing: border-box;
}
#canvas{
border: 2px solid black;
max-width: 100%;
max-height: 100%;
}
div#buttons{
position: absolute;
bottom: 60%;
width: 50%;
border: 2px solid black;
display: flex;
align-items: center;
justify-content: center;
}
#charge, #swarm, #idle{
font-size: 2rem;
width: 33.33%;
}
button:hover, button:focus{
color: white;
background-color: black;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>State Management Advanced</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p id = 'info'>Idle</p>
<div id='buttons'>
<button id= 'idle'>I</button>
<button id= 'charge'>C</button>
<button id= 'swarm'>S</button>
</div>
<script>
//akaka(ajjsjsjwwjj#hhwh^bwbbsbsbbsbsbbsbb#bbsbssnbsbssbsbsbsbbsbsbbsbsbsbbsbsbsbbsbsbbssbbsbsznbshsbsbbsbsb)
class Game{
constructor(canvas){
this.canvas = canvas;
this.width = this.canvas.width;
this.height = this.canvas.height;
this.states = {
idle: new Idle(this),
charge: new Charge(this),
swarm: new Swarm(this)
}
this.alien = this.states['idle'];
this.alien.start();
//keys
this.keys = new Set();
this.keypressed = false;
const btns = document.querySelectorAll('button');
btns.forEach(btn=>{
btn.addEventListener('touchstart', (e)=>{
this.keypressed = true;
this.keys.add(e.target.id);
})
btn.addEventListener('touchend', (e)=>{
this.keypressed = false;
this.keys.clear();
})
})
//text
this.info = document.getElementById('info');
}
setAlienState(state){
this.alien = this.states[state];
this.alien.start();
}
render(ctx){
this.alien.draw(ctx);
}
update(ctx, deltaTime){
this.alien.update(ctx, deltaTime)
}
}
class Alien{
constructor(game){
this.game = game;
this.width = 200;
this.height = 80;
this.x = this.game.width * 0.5 - this.width * 0.5;
this.y = this.game.height * 0.5 - this.height * 0.5;
this.color = 'black';
this.counter = 10000;
}
draw(ctx){
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
class Idle extends Alien{
start(){
this.color = 'red';
this.counter = 10000;
}
update(ctx, deltaTime){
this.game.info.innerText = 'IDLE! Press C to charge or S to swarm';
if(this.game.keys.has('charge') && this.game.keypressed){
this.game.info.innerText = 'Charging'
this.game.setAlienState('charge');
this.game.keypressed = false;
}
else if(this.game.keys.has('swarm') && this.game.keypressed){
this.game.info.innerText = 'Swarming! Press I \n or wait for '+this.counter/1000 +
' seconds to automatically switch back to Idle';
this.game.setAlienState('swarm');
this.game.keypressed = false;
}
}
}
class Charge extends Alien{
start(){
this.color = 'green';
this.game.info.innerText = 'CHARGING! Press S to swarm';
}
update(ctx, deltaTime){
if(this.game.keys.has('swarm') && this.game.keypressed){
this.game.setAlienState('swarm');
this.game.keypressed = false;
}
else if(this.game.keys.has('idle')){
this.game.info.innerText = 'You cannot switch to Idle, press S to swarm';
}
}
}
class Swarm extends Alien{
start(){
this.color = 'blue';
this.counter = 10000;
this.game.info.innerText = 'Swarming! Press I \n or wait for '+this.counter/1000 +
' seconds to automatically switch back to Idle';
}
update(ctx, deltaTime){
ctx.font = '30px Impact';
ctx.fillText(Math.floor(this.counter/1000) , 10, 30)
this.counter-= deltaTime;
if(this.counter <= 0){
this.game.setAlienState('idle');
this.game.info.innerText = 'Idle';
}
else if(this.game.keys.has('idle')){
this.game.setAlienState('idle');
this.game.info.innerText = 'Idle';
}
else if(this.game.keys.has('charge')){
this.game.info.innerText = 'You cannot switch to Charge, \n Press I to idle'
}
}
}
addEventListener('load', ()=>{
canvas.width = 800;
canvas.height = 360;
const ctx = canvas.getContext('2d');
const game = new Game(canvas);
let timestart = 0, deltaTime = 0;
function loop(timestamp ){
deltaTime = timestamp - timestart;
timestart = timestamp;
ctx.clearRect(0,0, canvas.width, canvas.height);
game.render(ctx);
game.update(ctx, deltaTime);
requestAnimationFrame(loop);
}
loop()
})
</script>
<canvas id = 'canvas'> </canvas>
</body>
</html>