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 | "mainIntervalId": 0 |
---|
28 | }; |
---|
29 | |
---|
30 | |
---|
31 | /* |
---|
32 | * Utility Functions |
---|
33 | */ |
---|
34 | function updateBackground(ctx, width, height, size, color, max) { |
---|
35 | if (System.backgroundObject.length < max) { |
---|
36 | var x = Math.ceil(Math.random() * width); |
---|
37 | var s = Math.ceil(Math.random() * 5); |
---|
38 | System.backgroundObject.push( |
---|
39 | new LinerBullet(size, color, x, 0, 0.5 * Math.PI, s)); |
---|
40 | } |
---|
41 | |
---|
42 | var newObjs = new Array(); |
---|
43 | for (i = 0; i < System.backgroundObject.length; i++) { |
---|
44 | System.backgroundObject[i].next(); |
---|
45 | System.backgroundObject[i].draw(ctx); |
---|
46 | if (!System.backgroundObject[i].vanished(width, height)) |
---|
47 | newObjs.push(System.backgroundObject[i]); |
---|
48 | } |
---|
49 | System.backgroundObject = newObjs; |
---|
50 | } |
---|
51 | |
---|
52 | function drawScreen(ctx, op, style, width, height) { |
---|
53 | var c = ctx.globalCompositeOperation; |
---|
54 | ctx.globalCompositeOperation = op; |
---|
55 | ctx.beginPath(); |
---|
56 | ctx.fillStyle = style; |
---|
57 | ctx.fillRect(0, 0, width, height); |
---|
58 | ctx.fill(); |
---|
59 | ctx.closePath(); |
---|
60 | ctx.globalCompositeOperation = c; |
---|
61 | } |
---|
62 | |
---|
63 | function drawLifeGauge(ctx, op, trooper, x, y, width) { |
---|
64 | var length = trooper.life; |
---|
65 | |
---|
66 | if (length > width - 20) |
---|
67 | length = width - 20; |
---|
68 | |
---|
69 | var c = ctx.globalCompositeOperation; |
---|
70 | ctx.globalCompositeOperation = op; |
---|
71 | ctx.beginPath(); |
---|
72 | ctx.fillStyle = trooper.color; |
---|
73 | ctx.fillRect(x, y, length, 10); |
---|
74 | ctx.fill(); |
---|
75 | ctx.closePath(); |
---|
76 | ctx.globalCompositeOperation = c; |
---|
77 | |
---|
78 | drawString(ctx, op, trooper.life, x + 2, y + 8, trooper.color, |
---|
79 | "6pt monospace", "left"); |
---|
80 | } |
---|
81 | |
---|
82 | function drawString(ctx, op, string, x, y, color, font, align) { |
---|
83 | var a = ctx.textAlign; |
---|
84 | var f = ctx.font; |
---|
85 | var c = ctx.globalCompositeOperation; |
---|
86 | ctx.globalCompositeOperation = op; |
---|
87 | ctx.beginPath(); |
---|
88 | ctx.textAlign = align; |
---|
89 | ctx.fillStyle = color; |
---|
90 | ctx.font = font; |
---|
91 | ctx.fillText(string, x, y); |
---|
92 | ctx.fill(); |
---|
93 | ctx.textAlign = a; |
---|
94 | ctx.font = f; |
---|
95 | ctx.closePath(); |
---|
96 | ctx.globalCompositeOperation = c; |
---|
97 | } |
---|
98 | |
---|
99 | function setMessage(elem, msg) { |
---|
100 | if (elem) |
---|
101 | elem.innerHTML = msg; |
---|
102 | } |
---|
103 | |
---|
104 | function addMessage(elem, msg) { |
---|
105 | if (elem) |
---|
106 | elem.innerHTML += msg; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /* |
---|
111 | * Main loop |
---|
112 | */ |
---|
113 | function mainLoop() { |
---|
114 | // clear screen |
---|
115 | drawScreen( |
---|
116 | System.screen.ctx, |
---|
117 | "source-over", |
---|
118 | "rgba(8,8,8,0.5)", |
---|
119 | System.screen.width, |
---|
120 | System.screen.height |
---|
121 | ); |
---|
122 | |
---|
123 | // update background objects |
---|
124 | updateBackground( |
---|
125 | System.screen.ctx, |
---|
126 | System.screen.width, |
---|
127 | System.screen.height, |
---|
128 | 1, "#CAF", 10 |
---|
129 | ); |
---|
130 | |
---|
131 | // update troopers |
---|
132 | System.player.update(System.boss); |
---|
133 | System.boss.update(System.player); |
---|
134 | |
---|
135 | // draw troopers |
---|
136 | System.player.draw(System.screen.ctx); |
---|
137 | System.boss.draw(System.screen.ctx); |
---|
138 | |
---|
139 | // draw player name/life |
---|
140 | drawString( |
---|
141 | System.screen.ctx, |
---|
142 | "source-over", |
---|
143 | System.player.name, |
---|
144 | 10, System.screen.height - 25, |
---|
145 | "#ACF", "9pt monospace", "left" |
---|
146 | ); |
---|
147 | drawLifeGauge( |
---|
148 | System.screen.ctx, |
---|
149 | "lighter", |
---|
150 | System.player, |
---|
151 | 10, |
---|
152 | System.screen.height - 20, |
---|
153 | System.screen.width |
---|
154 | ); |
---|
155 | |
---|
156 | // draw boss name/life |
---|
157 | drawString( |
---|
158 | System.screen.ctx, |
---|
159 | "source-over", |
---|
160 | System.boss.name, 10, 35, |
---|
161 | "#FCA", "9pt monospace", "left" |
---|
162 | ); |
---|
163 | drawLifeGauge( |
---|
164 | System.screen.ctx, |
---|
165 | "lighter", |
---|
166 | System.boss, |
---|
167 | 10, |
---|
168 | 10, |
---|
169 | System.screen.width |
---|
170 | ); |
---|
171 | |
---|
172 | // is dead? |
---|
173 | if (System.player.isDead()) { |
---|
174 | drawString( |
---|
175 | System.screen.ctx, "source-over", |
---|
176 | "YOU LOST", |
---|
177 | System.screen.width / 2, |
---|
178 | System.screen.height / 2, |
---|
179 | "#FCA", "24pt monospace", "center" |
---|
180 | ); |
---|
181 | clearInterval(System.mainIntervalId); |
---|
182 | System.mainIntervalId = 0; |
---|
183 | } |
---|
184 | |
---|
185 | if (System.boss.isDead()) { |
---|
186 | drawString( |
---|
187 | System.screen.ctx, "source-over", |
---|
188 | "YOU WON", |
---|
189 | System.screen.width / 2, |
---|
190 | System.screen.height / 2, |
---|
191 | "#ACF", "24pt monospace", "center" |
---|
192 | ); |
---|
193 | clearInterval(System.mainIntervalId); |
---|
194 | System.mainIntervalId = 0; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | |
---|
199 | /* |
---|
200 | * Initializer |
---|
201 | */ |
---|
202 | function initGame(canvas, msg, bossId, bossData, playerData) { |
---|
203 | System.screen.canvas = canvas; |
---|
204 | System.message = msg; |
---|
205 | System.screen.ctx = System.screen.canvas.getContext("2d"); |
---|
206 | System.screen.width = System.screen.canvas.width; |
---|
207 | System.screen.height = System.screen.canvas.height; |
---|
208 | |
---|
209 | System.screen.ctx.globalCompositeOperation = "lighter"; |
---|
210 | |
---|
211 | if (System.mainIntervalId) { |
---|
212 | clearInterval(System.mainIntervalId); |
---|
213 | System.mainIntervalId = 0; |
---|
214 | } |
---|
215 | |
---|
216 | drawScreen( |
---|
217 | System.screen.ctx, |
---|
218 | "source-over", |
---|
219 | "rgba(0,0,0,1)", |
---|
220 | System.screen.width, |
---|
221 | System.screen.height |
---|
222 | ); |
---|
223 | |
---|
224 | System.player = new Trooper( |
---|
225 | playerData.name, |
---|
226 | new ActionList([new ManualAction(new ManualShot())]), |
---|
227 | playerData.size, |
---|
228 | "#33F", |
---|
229 | System.screen.width / 2, |
---|
230 | System.screen.height - System.screen.height / 7, |
---|
231 | System.screen.width, |
---|
232 | System.screen.height, |
---|
233 | playerData.hitpoint, |
---|
234 | playerData.speed, |
---|
235 | playerData.numbombs, |
---|
236 | [new LinerBarrage(YExtendBullet, |
---|
237 | playerData.shotsize, |
---|
238 | "#3FF", |
---|
239 | playerData.shotinterval, |
---|
240 | playerData.shotspeed, |
---|
241 | playerData.shotlevel, |
---|
242 | -0.5), |
---|
243 | new LinerBarrage(YExtendBullet, |
---|
244 | playerData.shotsize, |
---|
245 | "#3FF", |
---|
246 | playerData.shotinterval, |
---|
247 | playerData.shotspeed, |
---|
248 | playerData.shotlevel, |
---|
249 | 0.5), |
---|
250 | new CircularBarrage(LinerBullet, |
---|
251 | playerData.shotsize, |
---|
252 | "#3FF", |
---|
253 | playerData.shotinterval, |
---|
254 | playerData.shotspeed, |
---|
255 | playerData.shotlevel + 2, |
---|
256 | -0.5)] |
---|
257 | ); |
---|
258 | |
---|
259 | var actList = EnemyActionLists[bossData.mtime % EnemyActionLists.length]; |
---|
260 | var shot = EnemyShots[bossData.hitpoint % EnemyShots.length]; |
---|
261 | var numAct = bossData.agility % (EnemyActions.length - 1) + 1; |
---|
262 | var numBlt = bossData.skills.length % (EnemyBullets.length - 1) + 1; |
---|
263 | var numBrrg = bossData.skills.length % (EnemyBarrages.length - 1) + 1; |
---|
264 | var acts = new Array(); |
---|
265 | var brrgs = new Array(); |
---|
266 | |
---|
267 | setMessage(System.message, "ActionsIdx:"); |
---|
268 | |
---|
269 | for (i = 0; i < numAct; i++) { |
---|
270 | var idx = (bossData.agility + i) % EnemyActions.length; |
---|
271 | acts.push(new EnemyActions[idx](new shot())); |
---|
272 | |
---|
273 | addMessage(System.message, String(idx) + ","); |
---|
274 | } |
---|
275 | |
---|
276 | addMessage(System.message, "<br>"); |
---|
277 | |
---|
278 | for (i = 0; i < numBrrg; i++) { |
---|
279 | var idx = (bossData.skills.length + i) % EnemyBarrages.length; |
---|
280 | var brrgCls = EnemyBarrages[idx]; |
---|
281 | |
---|
282 | addMessage(System.message, |
---|
283 | "BarragesIdx:" + String(idx) + " / BulletsIdx:"); |
---|
284 | |
---|
285 | for (k = 0; k < numBlt; k++) { |
---|
286 | var iidx = (bossData.skills.length + i + k) % EnemyBullets.length; |
---|
287 | var ss = Math.ceil(bossData.luck / 3); |
---|
288 | brrgs.push( |
---|
289 | new brrgCls( |
---|
290 | EnemyBullets[iidx], |
---|
291 | ss < 3 ? 3 : ss, |
---|
292 | "#FF3", |
---|
293 | 200 * 1 / Math.log(bossData.skillpoint + 0.1), |
---|
294 | Math.log(bossData.strength / 15 + 0.1), |
---|
295 | Math.ceil(bossData.concentration / 5) |
---|
296 | ) |
---|
297 | ); |
---|
298 | |
---|
299 | addMessage(System.message, String(iidx) + ","); |
---|
300 | } |
---|
301 | |
---|
302 | addMessage(System.message, "<br>"); |
---|
303 | } |
---|
304 | |
---|
305 | System.boss = new Trooper( |
---|
306 | bossData.name, |
---|
307 | new actList(acts), |
---|
308 | Math.ceil(50 * (1 / bossData.defense)), |
---|
309 | "#F33", |
---|
310 | System.screen.width / 2, |
---|
311 | System.screen.height / 7, |
---|
312 | System.screen.width, |
---|
313 | System.screen.height, |
---|
314 | bossData.hitpoint, |
---|
315 | Math.log(bossData.agility + 0.1) * 3, |
---|
316 | 0, |
---|
317 | brrgs |
---|
318 | ); |
---|
319 | |
---|
320 | System.backgroundObject = new Array(); |
---|
321 | System.mainIntervalId = setInterval(mainLoop, 20); |
---|
322 | } |
---|