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 | [new LinerBarrage(YExtendBullet, |
---|
236 | playerData.shotsize, |
---|
237 | "#3FF", |
---|
238 | playerData.shotinterval, |
---|
239 | playerData.shotspeed, |
---|
240 | playerData.shotlevel, |
---|
241 | -0.5), |
---|
242 | new LinerBarrage(YExtendBullet, |
---|
243 | playerData.shotsize, |
---|
244 | "#3FF", |
---|
245 | playerData.shotinterval, |
---|
246 | playerData.shotspeed, |
---|
247 | playerData.shotlevel, |
---|
248 | 0.5), |
---|
249 | new CircularBarrage(LinerBullet, |
---|
250 | playerData.shotsize, |
---|
251 | "#3FF", |
---|
252 | playerData.shotinterval, |
---|
253 | playerData.shotspeed, |
---|
254 | playerData.shotlevel + 2, |
---|
255 | -0.5)] |
---|
256 | ); |
---|
257 | |
---|
258 | var actList = EnemyActionLists[bossData.mtime % EnemyActionLists.length]; |
---|
259 | var shot = EnemyShots[bossData.hitpoint % EnemyShots.length]; |
---|
260 | var numAct = bossData.agility % (EnemyActions.length - 1) + 1; |
---|
261 | var numBlt = bossData.skills.length % (EnemyBullets.length - 1) + 1; |
---|
262 | var numBrrg = bossData.skills.length % (EnemyBarrages.length - 1) + 1; |
---|
263 | var acts = new Array(); |
---|
264 | var brrgs = new Array(); |
---|
265 | |
---|
266 | setMessage(System.message, "ActionsIdx:"); |
---|
267 | |
---|
268 | for (i = 0; i < numAct; i++) { |
---|
269 | var idx = (bossData.agility + i) % EnemyActions.length; |
---|
270 | acts.push(new EnemyActions[idx](new shot())); |
---|
271 | |
---|
272 | addMessage(System.message, String(idx) + ","); |
---|
273 | } |
---|
274 | |
---|
275 | addMessage(System.message, "<br>"); |
---|
276 | |
---|
277 | for (i = 0; i < numBrrg; i++) { |
---|
278 | var idx = (bossData.skills.length + i) % EnemyBarrages.length; |
---|
279 | var brrgCls = EnemyBarrages[idx]; |
---|
280 | |
---|
281 | addMessage(System.message, |
---|
282 | "BarragesIdx:" + String(idx) + " / BulletsIdx:"); |
---|
283 | |
---|
284 | for (k = 0; k < numBlt; k++) { |
---|
285 | var iidx = (bossData.skills.length + i + k) % EnemyBullets.length; |
---|
286 | var ss = Math.ceil(bossData.luck / 3); |
---|
287 | brrgs.push( |
---|
288 | new brrgCls( |
---|
289 | EnemyBullets[iidx], |
---|
290 | ss < 3 ? 3 : ss, |
---|
291 | "#FF3", |
---|
292 | 200 * 1 / Math.log(bossData.skillpoint + 0.1), |
---|
293 | Math.log(bossData.strength / 15 + 0.1), |
---|
294 | Math.ceil(bossData.concentration / 5) |
---|
295 | ) |
---|
296 | ); |
---|
297 | |
---|
298 | addMessage(System.message, String(iidx) + ","); |
---|
299 | } |
---|
300 | |
---|
301 | addMessage(System.message, "<br>"); |
---|
302 | } |
---|
303 | |
---|
304 | System.boss = new Trooper( |
---|
305 | bossData.name, |
---|
306 | new actList(acts), |
---|
307 | Math.ceil(50 * (1 / bossData.defense)), |
---|
308 | "#F33", |
---|
309 | System.screen.width / 2, |
---|
310 | System.screen.height / 7, |
---|
311 | System.screen.width, |
---|
312 | System.screen.height, |
---|
313 | bossData.hitpoint, |
---|
314 | Math.log(bossData.agility + 0.1) * 3, |
---|
315 | brrgs |
---|
316 | ); |
---|
317 | |
---|
318 | System.backgroundObject = new Array(); |
---|
319 | System.mainIntervalId = setInterval(mainLoop, 20); |
---|
320 | } |
---|