-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.class.js
179 lines (152 loc) · 4.76 KB
/
script.class.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
class Rect {
X;
Y;
width;
height;
halfHeight;
halfWidth;
color;
lineWeight;
constructor (px, py, width, height) {
this.X = px;
this.Y = py;
this.width = width;
this.height = height;
this.setProperties();
}
setProperties () {
this.halfWidth = this.width / 2;
this.halfHeight = this.height / 2;
}
centerX () {
return this.X + this.width / 2;
}
centerY () {
return this.Y + this.height / 2;
}
}
class Enemy extends Rect{
constructor (px, py, width, height, line, color = 'red') {
super(px, py, width, height);
this.color = color;
this.lineWeight = line;
}
}
class Player extends Rect {
constructor (px, py, width, height, line, color) {
super(px, py, width, height);
// this.color = 'green';
this.color = color;
this.lineWeight = line;
}
}
const output = document.getElementById('collisionType');
const canvas = document.getElementById('canvas');
const plrColor = document.getElementById('inpPlayer');
const enmColor = document.getElementById('inpEnemy');
const plrWidth = document.getElementById('inpPlayerWidth');
const plrHeight = document.getElementById('inpPlayerHeight');
const plrLine = document.getElementById('inpPlayerLineWidth');
const plrSpeed = document.getElementById('inpPlayerSpeed');
let enemy,
player,
playerColor = 'green',
enemyColor = 'red',
keyStates = [],
ctx;
// console.log('Player: ' , player )
function isColliding(plr, obj) {
return !(
plr.X > obj.X + obj.width ||
plr.X + plr.width < obj.X ||
plr.Y > obj.Y + obj.height ||
plr.Y + plr.height < obj.Y
)
}
function handleCollision() {
if (isColliding(player, enemy)) {
// Calculate the distance between centers
let diffX = player.centerX() - enemy.centerX(),
diffY = player.centerY() - enemy.centerY();
// Calculate the minimum distance to separate along X and Y
let minDistX = player.halfWidth + enemy.halfWidth,
minDistY = player.halfHeight + enemy.halfHeight;
// Calculate the depth of collision for both the X and Y axis
let depthX = diffX > 0 ? minDistX - diffX : -minDistX - diffX,
depthY = diffY > 0 ? minDistY - diffY : -minDistY - diffY;
// having the depth, pick the smaller depth and move along that axis
if (depthX != 0 && depthY != 0) {
// Collision along the X-axis...
if (Math.abs(depthX) < Math.abs(depthY)) {
if (depthX > 0) return 'left';
return 'right';
// Collision along the Y-axis...
} else {
if (depthY > 0) return 'top';
return 'bottom';
}
}
} else {
return null;
}
}
function move(step = 2) {
// move right
if (keyStates[39]) player.X += step;
if (player.X + player.width > canvas.width) player.X = canvas.width - player.width;
// move left
if (keyStates[37]) player.X -= step;
if (player.X < 0) player.X = 0;
// move up
if (keyStates[38]) player.Y -= step;
if (player.Y < 0) player.Y = 0;
// move down
if (keyStates[40]) player.Y += step;
if (player.Y + player.height > canvas.height) player.Y = canvas.height - player.height;
}
function draw(obj, clear = true) {
if (clear) ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = obj.lineWeight;
ctx.beginPath();
ctx.strokeStyle = obj.color;
ctx.rect(obj.X, obj.Y, obj.width, obj.height);
ctx.stroke();
}
function run () {
player = new Player(50,50, +plrWidth.value, +plrHeight.value, +plrLine.value, playerColor);
enemy = new Enemy(300,200,100,100,6,enemyColor),
main();
}
function main() {
move(+plrSpeed.value);
draw(player);
draw(enemy, false);
output.innerHTML = handleCollision() + ' collision';
window.requestAnimationFrame(main);
}
window.onkeydown = function(e) {
keyStates[e.keyCode] = true
}
window.onkeyup = function(e) {
keyStates[e.keyCode] = false
}
plrColor.oninput = function() {
playerColor = this.value;
};
enmColor.oninput = function() {
enemyColor = this.value;
};
plrWidth.oninput = function () {
document.getElementById('outPlayerWidth').innerText = ' ' + this.value;
};
plrHeight.oninput = function () {
document.getElementById('outPlayerHeight').innerText = ' ' + this.value;
};
plrLine.oninput = function () {
document.getElementById('outPlayerLineWidth').innerText = ' ' + this.value;
};
plrSpeed.oninput = function () {
document.getElementById('outPlayerSpeed').innerText = ' ' + this.value;
};
ctx = canvas.getContext('2d');
// main();