source: pycodeshooter/trunk/shooter/trooper.js @ 109

Revision 109, 6.7 KB checked in by atzm, 12 years ago (diff)

imaging support

  • 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
16function touch(x1, y1, s1, x2, y2, s2) {
17    var dx = Math.pow(x2 - x1, 2);
18    var dy = Math.pow(y2 - y1, 2);
19    if (Math.sqrt(dx + dy) < s1 + s2)
20        return true;
21    return false;
22}
23
24var Bomb = function(x, y, w, h, size, speed, colors) {
25    this.x      = x;
26    this.y      = y;
27    this.w      = w;
28    this.h      = h;
29    this.size   = size;
30    this.speed  = speed;
31    this.colors = colors;
32    this.colIdx = 0;
33
34    this.update = function(enemies) {
35        this.size += this.speed;
36
37        for (var ei = 0; ei < enemies.length; ei++) {
38            var enemy  = enemies[ei];
39            var delIdx = new Array();
40
41            // judge bullet
42            for (var i = 0; i < enemy.bullets.length; i++) {
43                if (touch(this.x, this.y, this.size,
44                          enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
45                    delIdx.push(i);
46            }
47            enemy.delBullet(delIdx);
48
49            // judge enemy
50            if (touch(this.x, this.y, this.size, enemy.x, enemy.y, enemy.size) && enemy.life > 0)
51                enemy.life--;
52        }
53    };
54
55    this.draw = function(ctx) {
56        ctx.beginPath();
57        ctx.fillStyle = this.colors[this.colIdx];
58        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
59        ctx.fill();
60        ctx.closePath();
61
62        if (++this.colIdx >= this.colors.length)
63            this.colIdx = 0;
64    };
65
66    this.vanished = function() {
67        if (this.size > this.w && this.size > this.h)
68            return true;
69        return false;
70    };
71};
72
73var Trooper = function(name, actList, image, size, hitSize, color, hitColor,
74                       x, y, w, h, life, speed, numBombs, bombColors, barrages) {
75
76    this.bullets    = new Array();
77    this.bomb       = null;
78    this.name       = name;
79    this.actList    = actList;
80    this.image      = image;
81    this.size       = size;
82    this.hitSize    = hitSize;
83    this.color      = color;
84    this.hitColor   = hitColor;
85    this.x          = x;
86    this.y          = y;
87    this.w          = w;
88    this.h          = h;
89    this.life       = life;
90    this.speed      = speed;
91    this.numBombs   = numBombs;
92    this.bombColors = bombColors;
93    this.barrages   = barrages;
94
95    this.move = function(x, y, area) {
96        this.x += x;
97        this.y += y;
98        this.adjustPosition(area);
99    };
100
101    this.moveAbs = function(x, y, area) {
102        this.x = x;
103        this.y = y;
104        this.adjustPosition(area);
105    };
106
107    this.adjustPosition = function(area) {
108        if (area == null)
109            area = 1;
110
111        if (this.x > this.w)
112            this.x = this.w;
113
114        if (this.y > this.h / area)
115            this.y = this.h / area;
116
117        if (this.x < 0)
118            this.x = 0;
119
120        if (this.y < 0)
121            this.y = 0;
122    };
123
124    this.isDead = function() {
125        return this.life <= 0;
126    };
127
128    this.update = function(enemies) {
129        // update my action
130        this.actList.update(this, enemies);
131
132        // update bomb
133        if (this.bomb) {
134            this.bomb.update(enemies);
135            if (this.bomb.vanished())
136                this.bomb = null;
137        }
138
139        // update/delete my bullets
140        {
141            var delIdx = new Array();
142            for (var i = 0; i < this.bullets.length; i++) {
143                this.bullets[i].next();
144
145                if (this.bullets[i].vanished())
146                    delIdx.push(i);
147            }
148            this.delBullet(delIdx);
149        }
150
151        // update/delete enemies' bullets
152        {
153            var hit = false;
154
155            for (var ei = 0; ei < enemies.length; ei++) {
156                var enemy  = enemies[ei];
157                var delIdx = new Array();
158
159                // judge/delete hit bullets
160                for (var i = 0; i < enemy.bullets.length; i++) {
161                    if (touch(this.x, this.y, this.hitSize,
162                              enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
163                        delIdx.push(i);
164                }
165
166                enemy.delBullet(delIdx);
167
168                if (delIdx.length > 0)
169                    hit = true;
170            }
171
172            // update my life
173            if (hit && this.life > 0)
174                this.life--;
175        }
176    };
177
178    this.draw = function(ctx) {
179        // draw bomb
180        if (this.bomb)
181            this.bomb.draw(ctx);
182
183        // draw bullets
184        for (var i = 0; i < this.bullets.length; i++)
185            this.bullets[i].draw(ctx);
186
187        // draw trooper
188        if (this.image) {
189            var w = this.image.width * (this.size / this.image.height);
190            var h = this.size;
191            var x = this.x - (w / 2);
192            var y = this.y - (h / 2);
193            ctx.drawImage(this.image, x, y, w, h);
194        }
195        else {
196            ctx.beginPath();
197            ctx.fillStyle = this.color;
198            ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
199            ctx.fill();
200            ctx.closePath();
201        }
202
203        // draw hit area
204        if (this.color != this.hitColor || this.size != this.hitSize) {
205            ctx.beginPath();
206            ctx.fillStyle = this.hitColor;
207            ctx.arc(this.x, this.y, this.hitSize, 0, Math.PI * 2.0, true);
208            ctx.fill();
209            ctx.closePath();
210        }
211    };
212
213    this.addBullet = function(bulletType, size, color, frame, x, y, dir, speed) {
214        this.bullets.push(
215            new bulletType(size, color, frame, this.x + x, this.y + y, dir, speed));
216    };
217
218    this.delBullet = function(idx) {
219        if (idx.length <= 0)
220            return;
221
222        var newBullets = new Array();
223
224        for (var i = 0; i < idx.length; i++)
225            this.bullets[idx[i]] = null;
226
227        for (var i = 0; i < this.bullets.length; i++)
228            if (this.bullets[i])
229                newBullets.push(this.bullets[i]);
230
231        this.bullets = newBullets;
232    };
233
234    this.clearBullet = function() {
235        this.bullets = new Array();
236    };
237
238    this.addBomb = function() {
239        if (!this.bomb && this.numBombs > 0) {
240            this.bomb = new Bomb(this.x, this.y, this.w, this.h,
241                                 this.size * 2, 10, this.bombColors);
242            this.numBombs--;
243        }
244    };
245}
Note: See TracBrowser for help on using the repository browser.