1 | /* -*- coding: utf-8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2010 by Atzm WATANABE <atzm@atzm.org> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify it |
---|
6 | * under the terms of the GNU General Public License (version 2) as |
---|
7 | * published by the Free Software Foundation. It is distributed in the |
---|
8 | * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
---|
9 | * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
10 | * PURPOSE. See the GNU General Public License for more details. |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | System = { |
---|
17 | "screen": { |
---|
18 | "canvas": null, |
---|
19 | "ctx": null, |
---|
20 | "width": 0, |
---|
21 | "height": 0 |
---|
22 | }, |
---|
23 | "message": null, |
---|
24 | "boss": null, |
---|
25 | "player": null, |
---|
26 | "backgroundObject": new Array(), |
---|
27 | "deathPieces": new Array(), |
---|
28 | "gameOver": false, |
---|
29 | "mainIntervalId": 0 |
---|
30 | }; |
---|
31 | |
---|
32 | |
---|
33 | /* |
---|
34 | * Tiun Tiun |
---|
35 | */ |
---|
36 | var DeathPiece = function(sizes, colors, x, y, dir, speed) { |
---|
37 | var that = new LinerBullet(sizes[0], colors[0], x, y, dir, speed); |
---|
38 | |
---|
39 | var sizeIdx = -1; |
---|
40 | var colorIdx = -1; |
---|
41 | |
---|
42 | that.getSize = function() { |
---|
43 | if (++sizeIdx >= sizes.length) |
---|
44 | sizeIdx = 0; |
---|
45 | return sizes[sizeIdx]; |
---|
46 | }; |
---|
47 | |
---|
48 | that.getColor = function() { |
---|
49 | if (++colorIdx >= colors.length) |
---|
50 | colorIdx = 0; |
---|
51 | return colors[colorIdx]; |
---|
52 | }; |
---|
53 | |
---|
54 | return that; |
---|
55 | }; |
---|
56 | |
---|
57 | |
---|
58 | /* |
---|
59 | * Utility Functions |
---|
60 | */ |
---|
61 | function updateBackground(ctx, width, height, size, color, max) { |
---|
62 | if (System.backgroundObject.length < max) { |
---|
63 | var x = Math.ceil(Math.random() * width); |
---|
64 | var s = Math.ceil(Math.random() * 5); |
---|
65 | System.backgroundObject.push( |
---|
66 | new LinerBullet(size, color, x, 0, 0.5 * Math.PI, s)); |
---|
67 | } |
---|
68 | |
---|
69 | var newObjs = new Array(); |
---|
70 | for (var i = 0; i < System.backgroundObject.length; i++) { |
---|
71 | System.backgroundObject[i].next(); |
---|
72 | System.backgroundObject[i].draw(ctx); |
---|
73 | if (!System.backgroundObject[i].vanished(width, height)) |
---|
74 | newObjs.push(System.backgroundObject[i]); |
---|
75 | } |
---|
76 | System.backgroundObject = newObjs; |
---|
77 | } |
---|
78 | |
---|
79 | function updateDeathPieces(ctx, width, height, x, y, sizes, colors, speed, way) { |
---|
80 | if (System.deathPieces.length <= 0) { |
---|
81 | if (way % 2) |
---|
82 | way++; |
---|
83 | |
---|
84 | var angle = 0; |
---|
85 | var delta = 2 / way; |
---|
86 | |
---|
87 | for(i = 0; i < way; i++) { |
---|
88 | System.deathPieces.push( |
---|
89 | new DeathPiece(sizes, colors, x, y, angle * Math.PI, speed)); |
---|
90 | angle += delta; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | var newObjs = new Array(); |
---|
95 | for (var i = 0; i < System.deathPieces.length; i++) { |
---|
96 | System.deathPieces[i].next(); |
---|
97 | System.deathPieces[i].draw(ctx); |
---|
98 | if (!System.deathPieces[i].vanished(width, height)) |
---|
99 | newObjs.push(System.deathPieces[i]); |
---|
100 | } |
---|
101 | System.deathPieces = newObjs; |
---|
102 | |
---|
103 | return !System.deathPieces.length; |
---|
104 | } |
---|
105 | |
---|
106 | function drawScreen(ctx, op, style, width, height) { |
---|
107 | var c = ctx.globalCompositeOperation; |
---|
108 | ctx.globalCompositeOperation = op; |
---|
109 | ctx.beginPath(); |
---|
110 | ctx.fillStyle = style; |
---|
111 | ctx.fillRect(0, 0, width, height); |
---|
112 | ctx.fill(); |
---|
113 | ctx.closePath(); |
---|
114 | ctx.globalCompositeOperation = c; |
---|
115 | } |
---|
116 | |
---|
117 | function drawLifeGauge(ctx, op, trooper, x, y, width) { |
---|
118 | var length = trooper.life; |
---|
119 | |
---|
120 | if (length > width - 20) |
---|
121 | length = width - 20; |
---|
122 | |
---|
123 | var c = ctx.globalCompositeOperation; |
---|
124 | ctx.globalCompositeOperation = op; |
---|
125 | ctx.beginPath(); |
---|
126 | ctx.fillStyle = trooper.color; |
---|
127 | ctx.fillRect(x, y, length, 10); |
---|
128 | ctx.fill(); |
---|
129 | ctx.closePath(); |
---|
130 | ctx.globalCompositeOperation = c; |
---|
131 | |
---|
132 | drawString(ctx, op, trooper.life, x + 2, y + 8, trooper.color, |
---|
133 | "6pt monospace", "left"); |
---|
134 | } |
---|
135 | |
---|
136 | function drawString(ctx, op, string, x, y, color, font, align) { |
---|
137 | var a = ctx.textAlign; |
---|
138 | var f = ctx.font; |
---|
139 | var c = ctx.globalCompositeOperation; |
---|
140 | ctx.globalCompositeOperation = op; |
---|
141 | ctx.beginPath(); |
---|
142 | ctx.textAlign = align; |
---|
143 | ctx.fillStyle = color; |
---|
144 | ctx.font = font; |
---|
145 | ctx.fillText(string, x, y); |
---|
146 | ctx.fill(); |
---|
147 | ctx.textAlign = a; |
---|
148 | ctx.font = f; |
---|
149 | ctx.closePath(); |
---|
150 | ctx.globalCompositeOperation = c; |
---|
151 | } |
---|
152 | |
---|
153 | function updateTrooper(trooper, enemy) { |
---|
154 | if (System.gameOver) { |
---|
155 | trooper.clearBullet(); |
---|
156 | enemy.clearBullet(); |
---|
157 | |
---|
158 | drawString( |
---|
159 | System.screen.ctx, "source-over", |
---|
160 | "GAME OVER", |
---|
161 | System.screen.width / 2, |
---|
162 | System.screen.height / 2, |
---|
163 | "#ACF", "24pt monospace", "center" |
---|
164 | ); |
---|
165 | } |
---|
166 | else if (trooper.isDead()) { |
---|
167 | trooper.clearBullet(); |
---|
168 | enemy.clearBullet(); |
---|
169 | |
---|
170 | System.gameOver = updateDeathPieces( |
---|
171 | System.screen.ctx, |
---|
172 | System.screen.width, |
---|
173 | System.screen.height, |
---|
174 | trooper.x, |
---|
175 | trooper.y, |
---|
176 | [6, 8, 10], ["#55F", "#AAF"], 3, 8 |
---|
177 | ); |
---|
178 | } |
---|
179 | else if (enemy.isDead()) { |
---|
180 | trooper.draw(System.screen.ctx); |
---|
181 | } |
---|
182 | else { |
---|
183 | trooper.update(enemy); |
---|
184 | trooper.draw(System.screen.ctx); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | function setMessage(elem, msg) { |
---|
189 | if (elem) |
---|
190 | elem.innerHTML = msg; |
---|
191 | } |
---|
192 | |
---|
193 | function addMessage(elem, msg) { |
---|
194 | if (elem) |
---|
195 | elem.innerHTML += msg; |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /* |
---|
200 | * Main loop |
---|
201 | */ |
---|
202 | function mainLoop() { |
---|
203 | // clear screen |
---|
204 | drawScreen( |
---|
205 | System.screen.ctx, |
---|
206 | "source-over", |
---|
207 | "rgba(8,8,8,0.5)", |
---|
208 | System.screen.width, |
---|
209 | System.screen.height |
---|
210 | ); |
---|
211 | |
---|
212 | // update background objects |
---|
213 | updateBackground( |
---|
214 | System.screen.ctx, |
---|
215 | System.screen.width, |
---|
216 | System.screen.height, |
---|
217 | 1, "#CAF", 10 |
---|
218 | ); |
---|
219 | |
---|
220 | // update/draw troopers |
---|
221 | updateTrooper(System.player, System.boss); |
---|
222 | updateTrooper(System.boss, System.player); |
---|
223 | |
---|
224 | // draw player name/life |
---|
225 | drawString( |
---|
226 | System.screen.ctx, |
---|
227 | "source-over", |
---|
228 | System.player.name, |
---|
229 | 10, System.screen.height - 25, |
---|
230 | "#ACF", "9pt monospace", "left" |
---|
231 | ); |
---|
232 | drawLifeGauge( |
---|
233 | System.screen.ctx, |
---|
234 | "lighter", |
---|
235 | System.player, |
---|
236 | 10, |
---|
237 | System.screen.height - 20, |
---|
238 | System.screen.width |
---|
239 | ); |
---|
240 | |
---|
241 | // draw boss name/life |
---|
242 | drawString( |
---|
243 | System.screen.ctx, |
---|
244 | "source-over", |
---|
245 | System.boss.name, 10, 35, |
---|
246 | "#FCA", "9pt monospace", "left" |
---|
247 | ); |
---|
248 | drawLifeGauge( |
---|
249 | System.screen.ctx, |
---|
250 | "lighter", |
---|
251 | System.boss, |
---|
252 | 10, |
---|
253 | 10, |
---|
254 | System.screen.width |
---|
255 | ); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /* |
---|
260 | * Initializer |
---|
261 | */ |
---|
262 | function initGame(canvas, msg, bossId, bossData, playerData) { |
---|
263 | System.screen.canvas = canvas; |
---|
264 | System.message = msg; |
---|
265 | System.screen.ctx = System.screen.canvas.getContext("2d"); |
---|
266 | System.screen.width = System.screen.canvas.width; |
---|
267 | System.screen.height = System.screen.canvas.height; |
---|
268 | System.gameOver = false; |
---|
269 | |
---|
270 | System.screen.ctx.globalCompositeOperation = "lighter"; |
---|
271 | |
---|
272 | if (System.mainIntervalId) { |
---|
273 | clearInterval(System.mainIntervalId); |
---|
274 | System.mainIntervalId = 0; |
---|
275 | } |
---|
276 | |
---|
277 | drawScreen( |
---|
278 | System.screen.ctx, |
---|
279 | "source-over", |
---|
280 | "rgba(0,0,0,1)", |
---|
281 | System.screen.width, |
---|
282 | System.screen.height |
---|
283 | ); |
---|
284 | |
---|
285 | System.player = new Trooper( |
---|
286 | playerData.name, |
---|
287 | new ActionList([new ManualAction(new ManualShot())]), |
---|
288 | playerData.size, |
---|
289 | playerData.hitsize, |
---|
290 | "#33F", |
---|
291 | "#F33", |
---|
292 | System.screen.width / 2, |
---|
293 | System.screen.height - System.screen.height / 7, |
---|
294 | System.screen.width, |
---|
295 | System.screen.height, |
---|
296 | playerData.hitpoint, |
---|
297 | playerData.speed, |
---|
298 | playerData.numbombs, |
---|
299 | ["rgba(255,0,0,0.3)", "rgba(0,0,255,0.3)"], |
---|
300 | [new LinerBarrage(YExtendBullet, |
---|
301 | playerData.shotsize, |
---|
302 | "#3FF", |
---|
303 | playerData.shotinterval, |
---|
304 | playerData.shotspeed, |
---|
305 | playerData.shotlevel, |
---|
306 | -0.5), |
---|
307 | new LinerBarrage(YExtendBullet, |
---|
308 | playerData.shotsize, |
---|
309 | "#3FF", |
---|
310 | playerData.shotinterval, |
---|
311 | playerData.shotspeed, |
---|
312 | playerData.shotlevel, |
---|
313 | 0.5), |
---|
314 | new CircularBarrage(LinerBullet, |
---|
315 | playerData.shotsize, |
---|
316 | "#3FF", |
---|
317 | playerData.shotinterval, |
---|
318 | playerData.shotspeed, |
---|
319 | playerData.shotlevel + 2, |
---|
320 | -0.5)] |
---|
321 | ); |
---|
322 | |
---|
323 | var actList = EnemyActionLists[bossData.mtime % EnemyActionLists.length]; |
---|
324 | var shot = EnemyShots[bossData.hitpoint % EnemyShots.length]; |
---|
325 | var numAct = bossData.agility % (EnemyActions.length - 1) + 1; |
---|
326 | var numBlt = bossData.skills.length % (EnemyBullets.length - 1) + 1; |
---|
327 | var numBrrg = bossData.skills.length % (EnemyBarrages.length - 1) + 1; |
---|
328 | var acts = new Array(); |
---|
329 | var brrgs = new Array(); |
---|
330 | |
---|
331 | setMessage(System.message, "ActionsIdx:"); |
---|
332 | |
---|
333 | for (var i = 0; i < numAct; i++) { |
---|
334 | var idx = (bossData.agility + i) % EnemyActions.length; |
---|
335 | acts.push(new EnemyActions[idx](new shot())); |
---|
336 | |
---|
337 | addMessage(System.message, String(idx) + "(" + |
---|
338 | String(bossData.hitpoint % EnemyShots.length) + "),"); |
---|
339 | } |
---|
340 | |
---|
341 | addMessage(System.message, "<br>"); |
---|
342 | |
---|
343 | for (var i = 0; i < numBrrg; i++) { |
---|
344 | var idx = (bossData.skillpoint + i) % EnemyBarrages.length; |
---|
345 | var brrgCls = EnemyBarrages[idx]; |
---|
346 | |
---|
347 | addMessage(System.message, |
---|
348 | "BarragesIdx:" + String(idx) + " / BulletsIdx:"); |
---|
349 | |
---|
350 | for (var k = 0; k < numBlt; k++) { |
---|
351 | var iidx = (bossData.skills.length + i + k) % EnemyBullets.length; |
---|
352 | var ss = Math.ceil(bossData.luck / 5); |
---|
353 | brrgs.push( |
---|
354 | new brrgCls( |
---|
355 | EnemyBullets[iidx], |
---|
356 | ss < 3 ? 3 : ss, |
---|
357 | "#FF3", |
---|
358 | 200 * 1 / Math.log(bossData.skillpoint + 0.1), |
---|
359 | Math.log(bossData.strength / 15 + 0.1), |
---|
360 | Math.ceil(bossData.concentration / 5) |
---|
361 | ) |
---|
362 | ); |
---|
363 | |
---|
364 | addMessage(System.message, String(iidx) + ","); |
---|
365 | } |
---|
366 | |
---|
367 | addMessage(System.message, "<br>"); |
---|
368 | } |
---|
369 | |
---|
370 | System.boss = new Trooper( |
---|
371 | bossData.name, |
---|
372 | new actList(acts), |
---|
373 | Math.ceil(50 * (1 / bossData.defense)), |
---|
374 | Math.ceil(50 * (1 / bossData.defense)), |
---|
375 | "#F33", |
---|
376 | "#F33", |
---|
377 | System.screen.width / 2, |
---|
378 | System.screen.height / 7, |
---|
379 | System.screen.width, |
---|
380 | System.screen.height, |
---|
381 | bossData.hitpoint, |
---|
382 | Math.log(bossData.agility + 0.1) * 3, |
---|
383 | 0, |
---|
384 | ["rgba(255,0,0,0.3)", "rgba(0,0,255,0.3)"], |
---|
385 | brrgs |
---|
386 | ); |
---|
387 | |
---|
388 | System.backgroundObject = new Array(); |
---|
389 | System.mainIntervalId = setInterval(mainLoop, 20); |
---|
390 | } |
---|