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

Revision 106, 5.8 KB checked in by atzm, 13 years ago (diff)
  • fixed small bug
  • add bullet frame
  • parameter adjust
  • 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, hitSize, color, hitColor,
70                       x, y, w, h, life, speed, numBombs, bombColors, 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.hitSize    = hitSize;
78    this.color      = color;
79    this.hitColor   = hitColor;
80    this.x          = x;
81    this.y          = y;
82    this.w          = w;
83    this.h          = h;
84    this.life       = life;
85    this.speed      = speed;
86    this.numBombs   = numBombs;
87    this.bombColors = bombColors;
88    this.barrages   = barrages;
89
90    this.move = function(x, y, area) {
91        this.x += x;
92        this.y += y;
93        this.adjustPosition(area);
94    };
95
96    this.moveAbs = function(x, y, area) {
97        this.x = x;
98        this.y = y;
99        this.adjustPosition(area);
100    };
101
102    this.adjustPosition = function(area) {
103        if (area == null)
104            area = 1;
105
106        if (this.x > this.w)
107            this.x = this.w;
108
109        if (this.y > this.h / area)
110            this.y = this.h / area;
111
112        if (this.x < 0)
113            this.x = 0;
114
115        if (this.y < 0)
116            this.y = 0;
117    };
118
119    this.isDead = function() {
120        return this.life <= 0;
121    };
122
123    this.update = function(enemy) {
124        var delIdx = null;
125
126        // update my action
127        this.actList.update(this, enemy);
128
129        // update bomb
130        if (this.bomb) {
131            this.bomb.update(enemy);
132            if (this.bomb.vanished())
133                this.bomb = null;
134        }
135
136        // update/delete my bullets
137        delIdx = new Array();
138        for (var i = 0; i < this.bullets.length; i++) {
139            this.bullets[i].next();
140
141            if (this.bullets[i].vanished())
142                delIdx.push(i);
143        }
144        this.delBullet(delIdx);
145
146        // judge/delete hit bullets
147        delIdx = new Array();
148        for (var i = 0; i < enemy.bullets.length; i++) {
149            if (touch(this.x, this.y, this.hitSize,
150                      enemy.bullets[i].x, enemy.bullets[i].y, enemy.bullets[i].size))
151                delIdx.push(i);
152        }
153        enemy.delBullet(delIdx);
154
155        // update life
156        if (delIdx.length > 0 && this.life > 0)
157            this.life--;
158    };
159
160    this.draw = function(ctx) {
161        // draw bomb
162        if (this.bomb)
163            this.bomb.draw(ctx);
164
165        // draw bullets
166        for (var i = 0; i < this.bullets.length; i++)
167            this.bullets[i].draw(ctx);
168
169        // draw trooper
170        ctx.beginPath();
171        ctx.fillStyle = this.color;
172        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
173        ctx.fill();
174        ctx.closePath();
175
176        if (this.color != this.hitColor || this.size != this.hitSize) {
177            ctx.beginPath();
178            ctx.fillStyle = this.hitColor;
179            ctx.arc(this.x, this.y, this.hitSize, 0, Math.PI * 2.0, true);
180            ctx.fill();
181            ctx.closePath();
182        }
183    };
184
185    this.addBullet = function(bulletType, size, color, frame, x, y, dir, speed) {
186        this.bullets.push(
187            new bulletType(size, color, frame, this.x + x, this.y + y, dir, speed));
188    };
189
190    this.delBullet = function(idx) {
191        if (idx.length <= 0)
192            return;
193
194        var newBullets = new Array();
195
196        for (var i = 0; i < idx.length; i++)
197            this.bullets[idx[i]] = null;
198
199        for (var i = 0; i < this.bullets.length; i++)
200            if (this.bullets[i])
201                newBullets.push(this.bullets[i]);
202
203        this.bullets = newBullets;
204    };
205
206    this.clearBullet = function() {
207        this.bullets = new Array();
208    };
209
210    this.addBomb = function() {
211        if (!this.bomb && this.numBombs > 0) {
212            this.bomb = new Bomb(this.x, this.y, this.w, this.h,
213                                 this.size * 2, 10, this.bombColors);
214            this.numBombs--;
215        }
216    };
217}
Note: See TracBrowser for help on using the repository browser.