-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.js
224 lines (210 loc) · 6.18 KB
/
components.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
//components
var mainCharacter;
var borderTop;
var borderBottom;
var borderLeft;
var myScore;
var topScore;
var borderRight;
let bullet_array = [];
let interval = 70;
let time_before_next_shot = 0;
let health = 3;
let chosen_border = "";
let mainCharx = 340;
let mainChary = 195;
//array of border components
let border_arr = ["top", "bottom", "left", "right"];
//function causes one of the borders to shoot a bullet from a random spot on the side facing inwards and at a random degree. They start of slowly shooting, but the interval shrinks to a certain number, until it shoots 4-5 per minuite.
function choose_shooting_border() {
if (interval > 20) {
interval -= 1;
}
chosen_border = border_arr[Math.floor(Math.random() * border_arr.length)];
}
//bullet component. will have a hit
function bullet_comp(x, y, name, angle, speedX, speedY) {
this.speedX = speedX;
this.speedY = speedY;
this.angle = angle;
this.name = name;
this.x = x;
this.y = y;
this.width = 10;
this.height = 10;
this.color = "red";
this.update = function () {
ctx = myGameArea.context;
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.fillStyle = "red";
ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
ctx.restore();
};
//changes the position of the component
this.newPos = function () {
this.x += this.speedX;
this.y += this.speedY;
};
//collision detection
this.crashWith = function (otherobj) {
var myleft = this.x;
var myright = this.x + this.width;
var mytop = this.y;
var mybottom = this.y + this.height;
var otherleft = otherobj.x;
var otherright = otherobj.x + otherobj.width;
var othertop = otherobj.y;
var otherbottom = otherobj.y + otherobj.height;
var crash = true;
if (
mybottom < othertop ||
mytop > otherbottom ||
myright < otherleft ||
myleft > otherright
) {
crash = false;
}
return crash;
};
}
//border component, which will be shooting out the flames
function border_comp(width, height, color, x, y, name) {
this.name = name;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function () {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
this.shoot = function () {
let nom = makeid(5);
if (this.name == "top") {
let random_int = getRandomInt(30, 670);
let ang = Math.atan(
Math.abs(mainCharx - random_int) / Math.abs(mainChary - 20)
);
let slope = (mainChary - 20) / (mainCharx - random_int);
if (mainCharx < random_int) slope = -slope;
bullet_array.push(new bullet_comp(random_int, 20, nom, ang, slope, 1));
} else if (this.name == "bottom") {
let random_int = getRandomInt(20, 680);
let ang = Math.atan(
Math.abs(mainCharx - random_int) / Math.abs(mainChary - 360)
);
let slope = Math.abs(mainChary - 360) / Math.abs(mainCharx - random_int);
if (mainCharx < random_int) slope = -slope;
bullet_array.push(new bullet_comp(random_int, 360, nom, ang, slope, -1));
} else if (this.name == "left") {
let random_int = getRandomInt(20, 370);
let ang = Math.atan(
Math.abs(mainCharx - 20) / Math.abs(mainChary - random_int)
);
let slope = Math.abs(mainChary - random_int) / Math.abs(mainCharx - 20);
if (mainChary < random_int) slope = -slope;
bullet_array.push(new bullet_comp(20, random_int, nom, ang, 1, slope));
} else if (this.name == "right") {
let random_int = getRandomInt(20, 370);
let ang = Math.atan(
Math.abs(mainCharx - 660) / Math.abs(mainChary - random_int)
);
let slope = Math.abs(mainChary - random_int) / Math.abs(mainCharx - 660);
if (mainChary < random_int) slope = -slope;
bullet_array.push(new bullet_comp(660, random_int, nom, ang, -1, slope));
}
};
}
//Text component for the score and initial screen
function text_comp(size, font, color, x, y) {
this.x = x;
this.y = y;
this.update = function () {
ctx.font = size + " " + font;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
};
}
//default component for main characters and borders
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function () {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
};
//changes the position of the component
this.newPos = function () {
this.x += this.speedX;
this.y += this.speedY;
mainCharx = this.x;
mainChary = this.y;
};
//collision detection
this.crashWith = function (otherobj) {
var myleft = this.x;
var myright = this.x + this.width;
var mytop = this.y;
var mybottom = this.y + this.height;
var otherleft = otherobj.x;
var otherright = otherobj.x + otherobj.width;
var othertop = otherobj.y;
var otherbottom = otherobj.y + otherobj.height;
var crash = true;
if (
mybottom < othertop ||
mytop > otherbottom ||
myright < otherleft ||
myleft > otherright
) {
crash = false;
}
return crash;
};
}
//movements for the mainCharacter
function moveup() {
mainCharacter.speedY -= 1;
}
function movedown() {
mainCharacter.speedY += 1;
}
function moveleft() {
mainCharacter.speedX -= 1;
}
function moveright() {
mainCharacter.speedX += 1;
}
function stopMove() {
mainCharacter.speedX = 0;
mainCharacter.speedY = 0;
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function makeid(length) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
/**
* @todo remove all depreciated libraries like e.keyCode and use requestAnimaitonFrame()
* @todo ask gpt how to improve code
*/