-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
153 lines (130 loc) · 4.63 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
import {mouseParticles} from './mouseParticles.js';
import { bulletParticles } from './bulletParticles.js';
import { objectiveParticles } from './objectiveParticles.js';
import { enemyParticles } from './enemyParticles.js';
window.addEventListener('load',function(){
// * Get elements for start screen
const startScreen = this.document.getElementById('startScreen');
const buttonBox = document.getElementById('buttonBox');
const howToButton = document.getElementById('howToButton');
const playGameButton = document.getElementById('startButton');
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
const mouseParticleArray = [];
const bulletParticleArray = [];
const objectiveParticleArray= [];
const enemyParticleArray = [];
let mouse = {
x:null,
y:null,
}
let gameToggle = 0;
let hue = 0;
// * Implements buttons
playGameButton.addEventListener('click', function(){
console.log('Start game!')
startScreen.style.display = 'none';
handleStart();
})
canvas.addEventListener('mousemove',function(e){
mouse.x = e.x;
mouse.y = e.y;
hue++;
// Add a particle to array
for (let i = 0; i < 5; i++){
mouseParticleArray.push(new mouseParticles(mouse,hue));
}
})
window.addEventListener('keypress', function(e){
for (let i = 0; i < 50; i++){
bulletParticleArray.push(new bulletParticles(mouse.x,mouse.y))
}
})
// ! Implement a "How to play" page
howToButton.addEventListener('click',function(){
buttonBox.style.display = 'none';
})
// ! Implement a "High Score" page
// ! Implement bullets
// ! Implement friendly
// ! Implement enemies
// * Start handling
function handleStart(){
canvas.style.display = 'block';
// set canvas height and width
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
animate();
}
// * Particle handling
function handleMouseParticles(){
for (let i = 0; i < mouseParticleArray.length;i++){
mouseParticleArray[i].update(ctx);
mouseParticleArray[i].draw(ctx);
if (mouseParticleArray[i].size < 0.2){
mouseParticleArray.splice(i,1);
i--;
}
}
}
function handleBulletParticles(){
for (let i =0; i < bulletParticleArray.length;i++){
bulletParticleArray[i].update()
bulletParticleArray[i].draw(ctx);
if (bulletParticleArray[i].size < 0.2){
bulletParticleArray.splice(i,1);
i--;
}
}
}
function handleObjectiveParticles(){
for (let i = 0; i < objectiveParticleArray.length; i++){
let objective = objectiveParticleArray[i]
objective.update();
objective.draw(ctx);
// * check if there is a mouse collision
if (mouse.x <= objective.x || mouse.x >= objective.x+objective.size || mouse.y <= objective.y || mouse.y >= objective.y+objective.size){
continue;
}
objectiveParticleArray.splice(i,1)
i--;
for (let i = 0; i < 50; i++){
bulletParticleArray.push(new bulletParticles(objective.x,objective.y));
}
}
}
function handleEnemyParticles(){
for (let i = 0; i < enemyParticleArray.length;i++){
const enemy = enemyParticleArray[i];
enemy.update(mouse);
ctx.save();
enemy.draw(ctx);
for (let j = 0; j < bulletParticleArray.length;j++){
const isPointInPath = ctx.isPointInPath(enemy.sprite,bulletParticleArray[j].x,bulletParticleArray[j].y);
if (isPointInPath == true){
console.log('yes')
enemyParticleArray.splice(i,1);
i--;
break;
}
}
ctx.restore();
}
}
// * Animations
let frame = 0;
function animate(){
// Clear the screen at each iteration
if (frame%250 == 0){
objectiveParticleArray.push(new objectiveParticles(canvas));
enemyParticleArray.push(new enemyParticles(canvas));
}
ctx.clearRect(0,0,canvas.width,canvas.height);
handleMouseParticles();
handleBulletParticles();
handleObjectiveParticles();
handleEnemyParticles();
frame++;
requestAnimationFrame(animate)
}
})