-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
226 lines (169 loc) · 6.49 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
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
import { Utils } from '/misc/Utils.js';
import { World } from './World/World.js';
World.Keyboard.enable('Space', 'KeyA', 'KeyW', 'KeyS', 'KeyD', 'KeyQ', 'KeyR', 'ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeft');
let width = 2050;
let height = 1550;
const suiciders = [];
const gameCanvas = new World.Canvase('game', width, height);
const viewPortCanvas = new World.Canvase('viewport', window.innerWidth, window.innerHeight);
// const miniMapCanvas = new World.Canvase(
// 'minimap',
// 250,
// 250,
// );
const player = new World.Sprites.Player({
canvas: gameCanvas,
x: width / 2,
y: height / 2,
width: 25,
height: 25,
health: 50,
color: 'yellow',
speed: { x: 10, y: 10 },
gun: new World.Sprites.Gun(gameCanvas, 'SMALLRAPIDS')
});
const enemy = new World.Sprites.Enemy('suicider', {
x: 0,
y: 0,
color: "skyblue",
follow: player,
velocity: { x: 9.5, y: 8 },
health: 2500,
canvas: gameCanvas
});
const enemy2 = new World.Sprites.Enemy('suicider', {
x: width,
y: height,
color: "orange",
follow: player,
velocity: { x: 7, y: 11 },
health: 2500,
canvas: gameCanvas
});
suiciders.push(enemy);
suiciders.push(enemy2);
gameCanvas.context.font = "20px Arial";
const camera = new World.Camera(0, 0, width, height, window.innerWidth, window.innerHeight);
camera.follow(player, window.innerWidth / 2, window.innerHeight / 2);
function game() {
gameCanvas.context.setTransform(1, 0, 0, 1, 0, 0);
gameCanvas.clear();
viewPortCanvas.clear();
drawWorld();
drawAndMove();
playerFire();
//miniMapCanvas.clear();
//miniMapCanvas.context.drawImage(gameCanvas.element, 0, 0, 500, 500, miniMapCanvas.element.style.left, miniMapCanvas.element.style.top, 500, 500);
window.requestAnimationFrame(game);
}
game();
window.onresize = function() {
gameCanvas.width = gameCanvas.width;
gameCanvas.height = gameCanvas.height;
viewPortCanvas.height = viewPortCanvas.height;
viewPortCanvas.width = viewPortCanvas.width;
}
function drawWorld() {
camera.update();
gameCanvas.context.translate(camera.viewX, camera.viewY);
viewPortCanvas.context.font = "30px Arial";
viewPortCanvas.context.fillText(`Player(${player.x}, ${player.y})`, 0, 30);
viewPortCanvas.context.fillText(`Enemy(${Math.round(enemy.x)}, ${Math.round(enemy.y)})`, 0, 60);
//grid
for (let r = 0, rCount = 0; r < gameCanvas.width; r += gameCanvas.width / 10, rCount += 1) {
for (let c = 0, cCount = 0; c < gameCanvas.height; c += gameCanvas.height / 10, cCount += 1) {
gameCanvas.context.fillStyle = 'lime';
gameCanvas.context.fillText(`(${rCount}, ${cCount})`, r, c + 20);
gameCanvas.context.fillRect(r, 0, 1, gameCanvas.height);
gameCanvas.context.fillRect(0, c, gameCanvas.width, 1);
}
}
//borders
gameCanvas.context.fillRect(0, 0, width, 1);
gameCanvas.context.fillRect(width, 0, 1, height);
gameCanvas.context.fillRect(0, 0, 1, height);
gameCanvas.context.fillRect(0, height, width, 1);
}
function playerFire() {
if (World.keys[World.Keybinds.find('shoot_right')].isPressed) {
player.fire({
x: player.x + (player.width * player.activeGun.inherited.width) * Math.cos(player.radian),
y: player.y + (player.height * player.activeGun.inherited.height) * Math.sin(player.radian),
direction: 'right'
});
} else if (World.keys[World.Keybinds.find('shoot_up')].isPressed) {
player.fire({
x: player.x + (player.width * player.activeGun.inherited.height) * Math.cos(player.radian),
y: player.y + (player.height * player.activeGun.inherited.width) * Math.sin(player.radian),
direction: 'up'
});
} else if (World.keys[World.Keybinds.find('shoot_left')].isPressed) {
player.fire({
x: player.x + (player.width * player.activeGun.inherited.width) * Math.cos(player.radian),
y: player.y + (player.height * player.activeGun.inherited.height) * Math.sin(player.radian),
direction: 'left'
});
} else if (World.keys[World.Keybinds.find('shoot_down')].isPressed) {
player.fire({
x: player.x + (player.width * player.activeGun.inherited.width) * Math.cos(player.radian),
y: player.y + (player.height * player.activeGun.inherited.width) * Math.sin(player.radian),
direction: 'down'
});
}
if (World.keys[World.Keybinds.find('reload')].isPressed) {
player.reload();
}
player.activeGun.bullets.forEach(bullet => {
suiciders.forEach(enemy => {
if (Utils.rectCollides(enemy, bullet)) {
enemy.hit(player.activeGun.inherited.bullet.damage);
enemy.explode("scatter", 2000, 10, 3);
player.activeGun.killBullet(bullet);
enemy.healthColor = 'red';
setTimeout(() => {
enemy.healthColor = 'blue';
}, 200);
enemy.isHit = true;
} else {
enemy.isHit = false;
}
});
});
if(enemy.alive === false && !enemy2.hyper) {
enemy2.velocity.x += 4.5;
enemy2.velocity.y += 4.5;
enemy2.health = enemy2.maxHealth;
enemy2.color = 'red';
enemy2.hyper = true;
}
if(enemy2.alive === false && !enemy.hyper) {
enemy.velocity.x += 4.5;
enemy.velocity.y += 4.5;
enemy.health = enemy.maxHealth;
enemy.color = 'red';
enemy.hyper = true;
}
suiciders.forEach(enemy => {
if(Utils.rectCollides(enemy, player)) {
// enemy.y -= enemy.height - enemy.height * 50 / 51;
// enemy.x -= enemy.width - enemy.width * 50 / 51;
// enemy.width *= 50 / 51;
// enemy.height *= 50 / 51;
enemy.explode("scatter", 2000, 7);
player.color = 'red';
setTimeout(() => {
if(enemy.alive) player.hit(enemy.damage / 50);
//enemy.die();
player.color = 'red';
}, 300);
}
});
}
function drawAndMove() {
player.draw();
player.move();
suiciders.forEach(enemy => {
enemy.draw();
enemy.move();
});
}