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

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

scoring support

  • Property svn:keywords set to Id
RevLine 
[81]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
[92]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
[108]34    this.update = function(enemies) {
[92]35        this.size += this.speed;
36
[108]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--;
[92]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
[109]73var Trooper = function(name, actList, image, size, hitSize, color, hitColor,
[98]74                       x, y, w, h, life, speed, numBombs, bombColors, barrages) {
[81]75
[98]76    this.bullets    = new Array();
77    this.bomb       = null;
78    this.name       = name;
79    this.actList    = actList;
[109]80    this.image      = image;
[98]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;
[112]90    this.maxLife    = life;
[98]91    this.speed      = speed;
92    this.numBombs   = numBombs;
93    this.bombColors = bombColors;
94    this.barrages   = barrages;
[81]95
[103]96    this.move = function(x, y, area) {
[81]97        this.x += x;
98        this.y += y;
[103]99        this.adjustPosition(area);
[81]100    };
101
[103]102    this.moveAbs = function(x, y, area) {
[81]103        this.x = x;
104        this.y = y;
[103]105        this.adjustPosition(area);
[81]106    };
107
[103]108    this.adjustPosition = function(area) {
109        if (area == null)
110            area = 1;
111
[81]112        if (this.x > this.w)
113            this.x = this.w;
114
[103]115        if (this.y > this.h / area)
116            this.y = this.h / area;
[81]117
118        if (this.x < 0)
119            this.x = 0;
120
121        if (this.y < 0)
122            this.y = 0;
123    };
124
125    this.isDead = function() {
126        return this.life <= 0;
127    };
128
[108]129    this.update = function(enemies) {
[81]130        // update my action
[108]131        this.actList.update(this, enemies);
[81]132
[92]133        // update bomb
134        if (this.bomb) {
[108]135            this.bomb.update(enemies);
[92]136            if (this.bomb.vanished())
137                this.bomb = null;
138        }
139
[81]140        // update/delete my bullets
[108]141        {
142            var delIdx = new Array();
143            for (var i = 0; i < this.bullets.length; i++) {
144                this.bullets[i].next();
[81]145
[108]146                if (this.bullets[i].vanished())
147                    delIdx.push(i);
148            }
149            this.delBullet(delIdx);
[81]150        }
151
[108]152        // update/delete enemies' bullets
153        {
154            var hit = false;
155
156            for (var ei = 0; ei < enemies.length; ei++) {
157                var enemy  = enemies[ei];
158                var delIdx = new Array();
159
160                // judge/delete hit bullets
161                for (var i = 0; i < enemy.bullets.length; i++) {
162                    if (touch(this.x, this.y, this.hitSize,
163                              enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
164                        delIdx.push(i);
165                }
166
167                enemy.delBullet(delIdx);
168
169                if (delIdx.length > 0)
170                    hit = true;
171            }
172
173            // update my life
174            if (hit && this.life > 0)
175                this.life--;
[81]176        }
177    };
178
179    this.draw = function(ctx) {
[92]180        // draw bomb
181        if (this.bomb)
182            this.bomb.draw(ctx);
183
[106]184        // draw bullets
185        for (var i = 0; i < this.bullets.length; i++)
186            this.bullets[i].draw(ctx);
187
[81]188        // draw trooper
[109]189        if (this.image) {
190            var w = this.image.width * (this.size / this.image.height);
191            var h = this.size;
192            var x = this.x - (w / 2);
193            var y = this.y - (h / 2);
194            ctx.drawImage(this.image, x, y, w, h);
195        }
196        else {
197            ctx.beginPath();
198            ctx.fillStyle = this.color;
199            ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
200            ctx.fill();
201            ctx.closePath();
202        }
[81]203
[109]204        // draw hit area
[97]205        if (this.color != this.hitColor || this.size != this.hitSize) {
206            ctx.beginPath();
207            ctx.fillStyle = this.hitColor;
208            ctx.arc(this.x, this.y, this.hitSize, 0, Math.PI * 2.0, true);
209            ctx.fill();
210            ctx.closePath();
211        }
[81]212    };
213
[106]214    this.addBullet = function(bulletType, size, color, frame, x, y, dir, speed) {
[81]215        this.bullets.push(
[106]216            new bulletType(size, color, frame, this.x + x, this.y + y, dir, speed));
[81]217    };
218
219    this.delBullet = function(idx) {
220        if (idx.length <= 0)
221            return;
222
223        var newBullets = new Array();
224
[96]225        for (var i = 0; i < idx.length; i++)
[81]226            this.bullets[idx[i]] = null;
227
[96]228        for (var i = 0; i < this.bullets.length; i++)
[81]229            if (this.bullets[i])
230                newBullets.push(this.bullets[i]);
231
232        this.bullets = newBullets;
233    };
[92]234
[95]235    this.clearBullet = function() {
236        this.bullets = new Array();
237    };
238
[92]239    this.addBomb = function() {
240        if (!this.bomb && this.numBombs > 0) {
[98]241            this.bomb = new Bomb(this.x, this.y, this.w, this.h,
242                                 this.size * 2, 10, this.bombColors);
[92]243            this.numBombs--;
244        }
245    };
[81]246}
Note: See TracBrowser for help on using the repository browser.