[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 | |
---|
| 16 | var Trooper = function(name, actList, size, color, x, y, w, h, |
---|
| 17 | life, speed, barrages) { |
---|
| 18 | |
---|
| 19 | this.bullets = new Array(); |
---|
| 20 | this.name = name; |
---|
| 21 | this.actList = actList; |
---|
| 22 | this.size = size; |
---|
| 23 | this.color = color; |
---|
| 24 | this.x = x; |
---|
| 25 | this.y = y; |
---|
| 26 | this.w = w; |
---|
| 27 | this.h = h; |
---|
| 28 | this.life = life; |
---|
| 29 | this.speed = speed; |
---|
| 30 | this.barrages = barrages; |
---|
| 31 | |
---|
| 32 | this.move = function(x, y) { |
---|
| 33 | this.x += x; |
---|
| 34 | this.y += y; |
---|
| 35 | this.adjustPosition(); |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | this.moveAbs = function(x, y) { |
---|
| 39 | this.x = x; |
---|
| 40 | this.y = y; |
---|
| 41 | this.adjustPosition(); |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | this.adjustPosition = function() { |
---|
| 45 | if (this.x > this.w) |
---|
| 46 | this.x = this.w; |
---|
| 47 | |
---|
| 48 | if (this.y > this.h) |
---|
| 49 | this.y = this.h; |
---|
| 50 | |
---|
| 51 | if (this.x < 0) |
---|
| 52 | this.x = 0; |
---|
| 53 | |
---|
| 54 | if (this.y < 0) |
---|
| 55 | this.y = 0; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | this.isDead = function() { |
---|
| 59 | return this.life <= 0; |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | this.update = function(enemy) { |
---|
| 63 | var delIdx = null; |
---|
| 64 | |
---|
| 65 | // update my action |
---|
| 66 | this.actList.update(this, enemy); |
---|
| 67 | |
---|
| 68 | // update/delete my bullets |
---|
| 69 | delIdx = new Array(); |
---|
| 70 | for (i = 0; i < this.bullets.length; i++) { |
---|
| 71 | this.bullets[i].next(); |
---|
| 72 | |
---|
| 73 | if (this.bullets[i].vanished()) |
---|
| 74 | delIdx.push(i); |
---|
| 75 | } |
---|
| 76 | this.delBullet(delIdx); |
---|
| 77 | |
---|
| 78 | // judge/delete hit bullets |
---|
| 79 | delIdx = new Array(); |
---|
| 80 | for (i = 0; i < enemy.bullets.length; i++) { |
---|
| 81 | var dx = Math.pow(enemy.bullets[i].x - this.x, 2); |
---|
| 82 | var dy = Math.pow(enemy.bullets[i].y - this.y, 2); |
---|
| 83 | |
---|
| 84 | if (Math.sqrt(dx + dy) < this.size + enemy.bullets[i].size) |
---|
| 85 | delIdx.push(i); |
---|
| 86 | } |
---|
| 87 | enemy.delBullet(delIdx) |
---|
| 88 | |
---|
| 89 | // update life |
---|
| 90 | if (delIdx.length > 0 && this.life > 0) |
---|
| 91 | this.life--; |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | this.draw = function(ctx) { |
---|
| 95 | // draw trooper |
---|
| 96 | ctx.beginPath(); |
---|
| 97 | ctx.fillStyle = this.color; |
---|
| 98 | ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true); |
---|
| 99 | ctx.fill(); |
---|
| 100 | ctx.closePath(); |
---|
| 101 | |
---|
| 102 | // draw bullets |
---|
| 103 | for (i = 0; i < this.bullets.length; i++) |
---|
| 104 | this.bullets[i].draw(ctx); |
---|
| 105 | }; |
---|
| 106 | |
---|
| 107 | this.addBullet = function(bulletType, size, color, x, y, dir, speed) { |
---|
| 108 | this.bullets.push( |
---|
| 109 | new bulletType(size, color, this.x + x, this.y + y, dir, speed)); |
---|
| 110 | }; |
---|
| 111 | |
---|
| 112 | this.delBullet = function(idx) { |
---|
| 113 | if (idx.length <= 0) |
---|
| 114 | return; |
---|
| 115 | |
---|
| 116 | var newBullets = new Array(); |
---|
| 117 | |
---|
| 118 | for (i = 0; i < idx.length; i++) |
---|
| 119 | this.bullets[idx[i]] = null; |
---|
| 120 | |
---|
| 121 | for (i = 0; i < this.bullets.length; i++) |
---|
| 122 | if (this.bullets[i]) |
---|
| 123 | newBullets.push(this.bullets[i]); |
---|
| 124 | |
---|
| 125 | this.bullets = newBullets; |
---|
| 126 | }; |
---|
| 127 | } |
---|