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

Revision 97, 5.6 KB checked in by atzm, 13 years ago (diff)

modified atari hantei

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