forked from att14/OSGCC---TD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.dart
251 lines (223 loc) · 6.34 KB
/
game.dart
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
class Game{
HTMLCanvasElement canvas, shop;
HTMLImageElement anim1, anim2, anim3;
HTMLAudioElement bloop, laser, baconarrow, goldfish;
CanvasRenderingContext2D ctx, ctx2;
int castle;
ToolBar tool;
////////////////////////////////////////
List<Tower> towers;
List<Enemy> enemies;
List<int> enemyLevelBarrier;
List<double> probabilities;
//////////////////////////////////////
int kills;
Player p;
Grid grid;
bool gameover;
int en;
Game(){
//Canvas setting//////////////////////
shop = window.document.getElementById('shop');
ctx2 = shop.getContext('2d');
canvas = window.document.getElementById('canvas');
ctx = canvas.getContext('2d');
///////////////////////////////////////////////
kills = 0;
//Array init//////////////////////////////
anim1 = window.document.createElement('img');
anim1.src = "images/towers/blast.png";
anim2 = window.document.createElement('img');
anim2.src = "images/towers/bacon.png";
anim3 = window.document.createElement('img');
anim3.src = "images/towers/poured-lava.png";
grid = new Grid();
towers = new List<Tower>();
enemies = new List<Enemy>();
enemyLevelBarrier = new List<int>();
probabilities = new List<double>();
probabilities.add(1.0);
probabilities.add(.3);
probabilities.add(.1);
probabilities.add(.05);
probabilities.add(.01);
probabilities.add(.08);
enemyLevelBarrier.add(0);
enemyLevelBarrier.add(100);
enemyLevelBarrier.add(250);
enemyLevelBarrier.add(200);
enemyLevelBarrier.add(200);
enemyLevelBarrier.add(200);
p = new Player(10000,100);
tool = new ToolBar(p);
window.setTimeout((){ generateEnemy(kills,((Math.random()*700).floor())); }, 10000);
////////////////////////////////////////
en = 0;
gameover = false;
castle = (750 *.69).floor();
window.webkitRequestAnimationFrame(drawFrame, canvas);
canvas.addEventListener("mousedown", (e){
mouseClick(e);
});
shop.addEventListener("mousedown", (e){
mouseToolbar(e);
});
}
void mouseClick(MouseEvent e){
if(e.offsetX >= castle && checkTower(e.offsetX, e.offsetY)){
addTower(e.offsetX, e.offsetY);
}
if(gameover){
gameover = false;
towers.clear();
enemies.clear();
new Title();
}
}
void mouseToolbar(MouseEvent e){
if(tool.mouseClick(e)){
kills += enemies.length;
tool.copernicium = false;
enemies.removeRange(0, enemies.length-1);
}
else{
for(final til in tool.tools){
if(til.withinBox(e.offsetX, e.offsetY)){
til.selected = true;
en = til.type;
print(en);
}
else{
til.selected = false;
}
}
}
}
void drawFrame(int time){
grid.draw();//Draw background
//Ally delegation
for(final tower in towers){
tower.drawTower();
}
//////////////////
//Enemy delegation
for(int i = 0; i < enemies.length; i++){
if(!enemies[i].move()){
gameover = p.gameOver();
}
p.life -= enemies[i].attack;
enemies[i].draw();
delegateEnemy(enemies[i], i);
}
////////////////////
//Info update//////////////////
ctx.font = "20pt Arial";
ctx.setFillColor("red");
tool.draw();
ctx2.fillText("${p.displayLife()}", 10, 30);
ctx2.fillText("Kills: $kills", 10, 50);
ctx2.fillText("${p.displayMoney()}", 10, 70);
ctx2.fillText("30", 20, 120);
ctx2.fillText("10", 20, 180);
ctx2.fillText("100", 20, 240);
ctx2.fillText("1000", 20, 300);
///////////////////////////////
if(!gameover){
window.webkitRequestAnimationFrame(drawFrame, canvas);
}
else{
grid.draw();
ctx.font = "20pt Arial";
ctx.setFillColor("red");
ctx.fillText("GAME OVER SKULL FUCKER", 200, 50);
}
}
//Check if tower can attack enemy
void delegateEnemy(Enemy a, int i){
for(final tower in towers){
if(tower.intersect(a.x, a.y)){
if(tower.fireRate){
tower.fireRate = false;
window.setTimeout(tower.fireRefresh,100);
lazer(tower.x, tower.y, a.x, a.y, tower.swa);
a.life-= tower.hit;
if(a.life <= 0){
enemies.removeRange(i, 1);
kills++;
p._money+=5;
break;
}
}
}
}
}
void addTower(int x, int y){
Tower temp = new Tower(20, 105, 80, en);
bool canBuy = p.buyTower(temp.cost);
if (canBuy){
temp.placeTower(x,y);
towers.add(temp);
}
}
bool checkTower(int x, int y){
for(final tower in towers){
if(tower.towerIntersect(x,y)){
return false;
}
}
return true;
}
void addEnemy(int i, num y){
Enemy temp = new Enemy(-30, y, i, kills);
enemies.add(temp);
}
void generateEnemy(int killz, num y){
int i = 0;
for(i = enemyLevelBarrier.length-1; i > 0; i--){
if(killz >= enemyLevelBarrier[i]){
double percent = (probabilities[i]*100).floor();
if((Math.random()*100).floor() <= percent){
break;
}
}
}
addEnemy(i,y);
if(!gameover){
window.setTimeout((){ generateEnemy(kills,((Math.random()*700).floor())); }, 300);
}
}
void lazer(int ax, int ay, int bx, int by, int curr){
switch(curr){
case 0:
ctx.drawImage(anim1, ax-anim1.width,ay-anim1.height/2);
ctx.drawImage(anim1, bx-anim1.width/2,by-anim1.height/2);
baconarrow = window.document.createElement('audio');
baconarrow.src = 'sounds/pew.ogg';
baconarrow.autoplay = true;
break;
case 1:
ctx.drawImage(anim3, bx,by);
bloop = window.document.createElement('audio');
bloop.src = 'sounds/pew.ogg';
bloop.autoplay = true;
break;
case 3:
ctx.drawImage(anim2, bx-anim2.width/2,by-anim2.height/2);
laser = window.document.createElement('audio');
laser.src = 'sounds/pew.ogg';
laser.autoplay = true;
break;
default:
ctx.setFillColor('orange');
ctx.beginPath();
ctx.moveTo(ax, ay);
ctx.lineTo(bx, by);
ctx.closePath();
ctx.stroke();
goldfish = window.document.createElement('audio');
goldfish.src = 'sounds/pew.ogg';
goldfish.autoplay = true;
break;
};
}
}