source: pycodeshooter/trunk/shooter/system.js @ 81

Revision 81, 8.5 KB checked in by atzm, 13 years ago (diff)

add pycodeshooter

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