source: pycodeshooter/trunk/shooter/bullet.js @ 81

Revision 81, 4.2 KB checked in by atzm, 13 years ago (diff)

add pycodeshooter

  • 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
16var Bullet = function(size, color, x, y, dir, speed) {
17    this.size  = size;
18    this.color = color;
19    this.x     = x;
20    this.y     = y;
21    this.dir   = dir;
22    this.speed = speed;
23
24    this.draw = function(ctx) {
25        ctx.beginPath();
26        ctx.fillStyle = this.color;
27        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2.0, true);
28        ctx.fill();
29        ctx.closePath();
30    };
31    this.vanished = function(width, height) {
32        return (width < this.x || height < this.y || this.x < 0 || this.y < 0);
33    };
34};
35
36var LinerBullet = function(size, color, x, y, dir, speed) {
37    var that = new Bullet(size, color, x, y, dir, speed);
38
39    that.deltaBaseX = Math.cos(that.dir);
40    that.deltaBaseY = Math.sin(that.dir);
41
42    that.next = function() {
43        this.x += this.getDeltaX();
44        this.y += this.getDeltaY();
45    };
46    that.getDeltaX = function() {
47        return this.speed * this.deltaBaseX;
48    };
49    that.getDeltaY = function() {
50        return this.speed * this.deltaBaseY;
51    };
52
53    return that;
54};
55
56var AxisExtendBullet = function(size, color, x, y, dir, speed) {
57    var that = new LinerBullet(size, color, x, y, dir, speed);
58
59    that.dx = that.speed * that.deltaBaseX / 2;
60    that.dy = that.speed * that.deltaBaseY / 2;
61
62    that.getDeltaX = function() {
63        return this.dx * this.getAxisDeltaX();
64    };
65    that.getDeltaY = function() {
66        return this.dy * this.getAxisDeltaY();
67    };
68    that.getCurrentDelta = function(count, thresh) {
69        var c = count / this.speed;
70        return c > thresh ? thresh : c;
71    }
72
73    return that;
74};
75
76var XYExtendBullet = function(size, color, x, y, dir, speed) {
77    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
78
79    that.i = 1;
80    that.getAxisDeltaX = function() {
81        return this.getCurrentDelta(this.i, 2);
82    };
83    that.getAxisDeltaY = function() {
84        return this.getCurrentDelta(this.i++, 2);
85    };
86
87    return that;
88};
89
90var XExtendBullet = function(size, color, x, y, dir, speed) {
91    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
92
93    that.i = 1;
94    that.getAxisDeltaX = function() {
95        return this.getCurrentDelta(this.i, 2);
96    };
97    that.getAxisDeltaY = function() {
98        return 1 / this.getCurrentDelta(this.i++, 2);
99    };
100
101    return that;
102};
103
104var YExtendBullet = function(size, color, x, y, dir, speed) {
105    var that = new AxisExtendBullet(size, color, x, y, dir, speed);
106
107    that.i = 1;
108    that.getAxisDeltaX = function() {
109        return 1 / this.getCurrentDelta(this.i, 2);
110    };
111    that.getAxisDeltaY = function() {
112        return this.getCurrentDelta(this.i++, 2);
113    };
114
115    return that;
116};
117
118var CurveBullet = function(size, color, x, y, dir, speed) {
119    var that = new Bullet(size, color, x, y, dir, speed);
120
121    that.delta = 1 / (that.speed * 100);
122    that.i     = 1;
123
124    that.next = function() {
125        this.x   += Math.cos(this.dir) * this.i * this.getSign();
126        this.y   += Math.sin(this.dir) * this.i;
127        this.dir += (this.delta / this.i);
128        this.i   += (this.delta / this.i);
129    };
130
131    return that;
132};
133
134var LeftCurveBullet = function(size, color, x, y, dir, speed) {
135    var that = new CurveBullet(size, color, x, y, dir, speed);
136
137    that.getSign = function() {
138        return 1;
139    };
140
141    return that;
142};
143
144var RightCurveBullet = function(size, color, x, y, dir, speed) {
145    var that = new CurveBullet(size, color, x, y, dir, speed);
146
147    that.getSign = function() {
148        return -1;
149    };
150
151    return that;
152};
153
154
155var EnemyBullets = [LinerBullet,
156                    XYExtendBullet,
157                    XExtendBullet,
158                    YExtendBullet,
159                    LeftCurveBullet,
160                    RightCurveBullet];
Note: See TracBrowser for help on using the repository browser.