-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
99 lines (78 loc) · 2.15 KB
/
main.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
import { Platform } from './Platform';
import { Player } from './Player';
import './style.css';
// TODO: create better controller. Get from three js cube
const randomIntFromInterval = (min, max) => {
if (min === max) return min;
return Math.floor(Math.random() * (max - min + 1) + min);
};
const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');
const mouse = {
x: 0,
y: 0,
pressed: false,
};
const setSize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
setSize();
const player = new Player(ctx, canvas);
const platforms = [];
for (let i = 0; i < 12; i++) {
platforms.push(
new Platform(
randomIntFromInterval(0, canvas.width),
randomIntFromInterval(0, canvas.height),
randomIntFromInterval(40, 200),
randomIntFromInterval(20, 100),
ctx,
player
)
);
}
// ----------------------------------------------
const handleRectOnMouse = () => {
player.pos.set(mouse.x - player.width * 0.5, mouse.y - player.height * 0.5);
player.vel.set(0, 0);
};
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
player.update();
platforms.forEach((platform) => {
platform.update();
platform.draw();
});
player.draw();
if (mouse.pressed) {
handleRectOnMouse();
}
requestAnimationFrame(animate);
}
animate();
function moveCallback({ code }) {
if (code === 'ArrowUp') player.jump();
if (code === 'ArrowLeft') {
player.accel.setX(player.accelXSpeed * -1);
player.what_sprite = 'left';
}
if (code === 'ArrowRight') {
player.accel.setX(player.accelXSpeed);
player.what_sprite = 'right';
}
}
document.addEventListener('keydown', moveCallback);
document.addEventListener('keyup', ({ code }) => {
if (code === 'ArrowLeft' || code === 'ArrowRight') player.accel.setX(0);
});
const toggleMouse = () => {
mouse.pressed ? (mouse.pressed = false) : (mouse.pressed = true);
};
document.addEventListener('mousedown', toggleMouse);
document.addEventListener('mouseup', toggleMouse);
document.addEventListener('mousemove', (e) => {
mouse.x = e.x;
mouse.y = e.y;
});
window.addEventListener('resize', setSize);