-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
249 lines (214 loc) · 6.17 KB
/
index.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const canvas = document.querySelector('canvas')
const c = canvas.getContext('2d')
//Set canvas size to fit the screen... could this be relative to fit all screens? this is a 16:9 aspect ratio
canvas.width = 1024
canvas.height = 576
const scaledCanvas = {
width: canvas.width / 4,
height: canvas.height / 4
}
const floorCollisions2d = []
for(let i = 0; i < floorCollisions.length; i += 36) {
floorCollisions2d.push(floorCollisions.slice(i, i+36))
}
const collisionBlocks = []
floorCollisions2d.forEach((row, y) => {
row.forEach((symbol, x) => {
if (symbol === 202) {
collisionBlocks.push(new CollisionBlock({
position: {
x: x * 16,
y: y * 16
}}))
}
})
})
const PlatformCollisions2d = []
for(let i = 0; i < PlatformCollisions.length; i += 36) {
PlatformCollisions2d.push(PlatformCollisions.slice(i, i+36))
}
const platformCollisionBlocks = []
PlatformCollisions2d.forEach((row, y) => {
row.forEach((symbol, x) => {
if (symbol === 202) {
platformCollisionBlocks.push(new CollisionBlock({
position: {
x: x * 16,
y: y * 16
},
height: 4}))
}
})
})
const gravity = 0.1
const player = new Player({
position: {
x: 100,
y: 300,
},
collisionBlocks,
platformCollisionBlocks,
imageSrc: "./img/warrior/Idle.png",
frameRate: 8,
animations: {
Idle: {
imageSrc: "./img/warrior/Idle.png",
frameRate: 8,
frameBuffer: 6,
},
IdleLeft: {
imageSrc: "./img/warrior/IdleLeft.png",
frameRate: 8,
frameBuffer: 6,
},
Run: {
imageSrc: "./img/warrior/Run.png",
frameRate: 8,
frameBuffer: 5,
},
RunLeft: {
imageSrc: "./img/warrior/RunLeft.png",
frameRate: 8,
frameBuffer: 5,
},
Jump: {
imageSrc: "./img/warrior/Jump.png",
frameRate: 2,
frameBuffer: 3,
},
JumpLeft: {
imageSrc: "./img/warrior/JumpLeft.png",
frameRate: 2,
frameBuffer: 3,
},
Fall: {
imageSrc: "./img/warrior/Fall.png",
frameRate: 2,
frameBuffer: 3,
},
FallLeft: {
imageSrc: "./img/warrior/FallLeft.png",
frameRate: 2,
frameBuffer: 3,
}
}
})
const keys = {
d: {
pressed: false
},
a: {
pressed: false
},
}
const background = new Sprite({
position: {
x: 0,
y:0,
},
imageSrc: './img/background.png'
})
const backgroundImgHeight = 432
const camera = {
position: {
x: 0,
y: -backgroundImgHeight + scaledCanvas.height,
}
}
const ctx = canvas.getContext('2d');
const controller = new GameController({
context: ctx,
buttons: [
{ x: 50, y: 500, width: 80, height: 40, label: 'Left', color: '#f00', onClick: () => player.moveLeft() },
{ x: 150, y: 500, width: 80, height: 40, label: 'Jump', color: '#0f0', onClick: () => player.jump() },
{ x: 250, y: 500, width: 80, height: 40, label: 'Right', color: '#00f', onClick: () => player.moveRight() }
],
});
// ^^^ Initialising the game controller
function animate() {
window.requestAnimationFrame(animate)
//^^ Calling recursive function to allow animation to happen
c.fillStyle = "white"
c.fillRect(0, 0, canvas.width, canvas.height)
//^^ Creates white canvas background, within function to clear background each time to stop character "dripping"
controller.draw()
c.save()
c.scale(4,4)
c.translate(camera.position.x, camera.position.y)
background.update()
// renders the collision blocks !!!!!!
// collisionBlocks.forEach((collisionBlock) => {
// collisionBlock.update()
// })
// // ^^ calling the collision blocks and rendering them on the screen
// platformCollisionBlocks.forEach((block) => {
// block.update()
// })
player.checkHorizontalCanvasCollision()
player.update()
// Handling sprite switching:
player.velocity.x = 0
if (keys.d.pressed) {
player.switchSprite(`Run`)
player.lastDirection = `right`
player.velocity.x = 2
player.shouldPanCamToRight({canvas, camera})
}else if (keys.a.pressed) {
player.switchSprite(`RunLeft`)
player.lastDirection = `left`
player.velocity.x = -2
player.shouldPanCamToLeft({canvas, camera})
} else if (player.velocity.y === 0) {
player.jumping = false
if(player.lastDirection === "right") {
player.switchSprite("Idle")
} else if (player.lastDirection === "left") {
player.switchSprite("IdleLeft")
}
}
if (player.velocity.y < 0) {
player.shouldPanCamUp({camera, canvas})
if(player.lastDirection === "right") {
player.switchSprite(`Jump`)
} else if (player.lastDirection === "left") {
player.switchSprite(`JumpLeft`)
}
} else if (player.velocity.y > 0 ) {
player.shouldPanCamDown({camera, canvas})
if(player.lastDirection === "right") {
player.switchSprite(`Fall`)
} else if (player.lastDirection === "left") {
player.switchSprite(`FallLeft`)
}
}
c.restore()
// ^^ c.save and c.restore wrap whatever is inside to stop it impacting globally
}
animate()
window.addEventListener('keydown', (event) => {
switch (event.key) {
case 'd':
keys.d.pressed = true
console.log("d")
break
case 'a':
keys.a.pressed = true
break
case 'w':
if(player.jumping) return
player.velocity.y = -4
player.jumping = true
break
}
})
window.addEventListener('keyup', (event) => {
switch (event.key) {
case 'd':
keys.d.pressed = false
break
case 'a':
keys.a.pressed = false
break
}
})
// takes an argument of what it is looking for (i.e. a keystroke)