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

Revision 96, 5.3 KB checked in by atzm, 13 years ago (diff)
  • add barrage patterns
  • fixed a trivial bug
  • 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(enemy) {
35        this.size += this.speed;
36
37        // judge bullet
38        var delIdx = new Array();
39        for (var i = 0; i < enemy.bullets.length; i++) {
40            if (touch(this.x, this.y, this.size,
41                      enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
42                delIdx.push(i);
43        }
44        enemy.delBullet(delIdx);
45
46        // judge enemy
47        if (touch(this.x, this.y, this.size, enemy.x, enemy.y, enemy.size) && enemy.life > 0)
48            enemy.life--;
49    };
50
51    this.draw = function(ctx) {
52        ctx.beginPath();
53        ctx.fillStyle = this.colors[this.colIdx];
54        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
55        ctx.fill();
56        ctx.closePath();
57
58        if (++this.colIdx >= this.colors.length)
59            this.colIdx = 0;
60    };
61
62    this.vanished = function() {
63        if (this.size > this.w && this.size > this.h)
64            return true;
65        return false;
66    };
67};
68
69var Trooper = function(name, actList, size, color, x, y, w, h,
70                       life, speed, numBombs, barrages) {
71
72    this.bullets  = new Array();
73    this.bomb     = null;
74    this.name     = name;
75    this.actList  = actList;
76    this.size     = size;
77    this.color    = color;
78    this.x        = x;
79    this.y        = y;
80    this.w        = w;
81    this.h        = h;
82    this.life     = life;
83    this.speed    = speed;
84    this.numBombs = numBombs;
85    this.barrages = barrages;
86
87    this.move = function(x, y) {
88        this.x += x;
89        this.y += y;
90        this.adjustPosition();
91    };
92
93    this.moveAbs = function(x, y) {
94        this.x = x;
95        this.y = y;
96        this.adjustPosition();
97    };
98
99    this.adjustPosition = function() {
100        if (this.x > this.w)
101            this.x = this.w;
102
103        if (this.y > this.h)
104            this.y = this.h;
105
106        if (this.x < 0)
107            this.x = 0;
108
109        if (this.y < 0)
110            this.y = 0;
111    };
112
113    this.isDead = function() {
114        return this.life <= 0;
115    };
116
117    this.update = function(enemy) {
118        var delIdx = null;
119
120        // update my action
121        this.actList.update(this, enemy);
122
123        // update bomb
124        if (this.bomb) {
125            this.bomb.update(enemy);
126            if (this.bomb.vanished())
127                this.bomb = null;
128        }
129
130        // update/delete my bullets
131        delIdx = new Array();
132        for (var i = 0; i < this.bullets.length; i++) {
133            this.bullets[i].next();
134
135            if (this.bullets[i].vanished())
136                delIdx.push(i);
137        }
138        this.delBullet(delIdx);
139
140        // judge/delete hit bullets
141        delIdx = new Array();
142        for (var i = 0; i < enemy.bullets.length; i++) {
143            if (touch(this.x, this.y, this.size,
144                      enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
145                delIdx.push(i);
146        }
147        enemy.delBullet(delIdx);
148
149        // update life
150        if (delIdx.length > 0 && this.life > 0)
151            this.life--;
152    };
153
154    this.draw = function(ctx) {
155        // draw bomb
156        if (this.bomb)
157            this.bomb.draw(ctx);
158
159        // draw trooper
160        ctx.beginPath();
161        ctx.fillStyle = this.color;
162        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
163        ctx.fill();
164        ctx.closePath();
165
166        // draw bullets
167        for (var i = 0; i < this.bullets.length; i++)
168            this.bullets[i].draw(ctx);
169    };
170
171    this.addBullet = function(bulletType, size, color, x, y, dir, speed) {
172        this.bullets.push(
173            new bulletType(size, color, this.x + x, this.y + y, dir, speed));
174    };
175
176    this.delBullet = function(idx) {
177        if (idx.length <= 0)
178            return;
179
180        var newBullets = new Array();
181
182        for (var i = 0; i < idx.length; i++)
183            this.bullets[idx[i]] = null;
184
185        for (var i = 0; i < this.bullets.length; i++)
186            if (this.bullets[i])
187                newBullets.push(this.bullets[i]);
188
189        this.bullets = newBullets;
190    };
191
192    this.clearBullet = function() {
193        this.bullets = new Array();
194    };
195
196    this.addBomb = function() {
197        if (!this.bomb && this.numBombs > 0) {
198            this.bomb = new Bomb(this.x, this.y, this.w, this.h, this.size * 2, 10,
199                                 ["rgba(255,0,0,0.3)", "rgba(0,0,255,0.3)"]);
200            this.numBombs--;
201        }
202    };
203}
Note: See TracBrowser for help on using the repository browser.