Created
December 1, 2009 03:46
-
-
Save akisute/246029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Yet another DANMAKU #1 rev2 | |
* =========================== | |
* Bullets now reflect @ walls! | |
* | |
* This sample assigns bullet objects every time when new bullets are | |
* needed. Creating a new bullet costs much, we'd better find an another | |
* way to manage bullets. | |
*/ | |
ArrayList bullets; | |
float launchDegree = 90; | |
float launchDegreeDelta = 3.6; | |
float OUT_OF_BOUND_MERGIN = 50.0; | |
/////////////////////////////////////////////////////////// | |
void setup() { | |
size(400, 400); | |
smooth(); | |
frameRate(30); | |
bullets = new ArrayList(1024); | |
} | |
void draw() { | |
background(238); | |
{ | |
Bullet bullet = null; | |
{ | |
bullet = new Bullet(width/2, 20); | |
bullet.setSpeed(4.0, launchDegree); | |
bullets.add(bullet); | |
} | |
{ | |
bullet = new Bullet(width/2, 20); | |
bullet.setSpeed(3.6, launchDegree+3); | |
bullets.add(bullet); | |
} | |
{ | |
bullet = new Bullet(width/2, 20); | |
bullet.setSpeed(3.6, launchDegree-3); | |
bullets.add(bullet); | |
} | |
if (launchDegree > 175 || launchDegree < 5) { | |
launchDegreeDelta *= -1; | |
} | |
launchDegree += launchDegreeDelta; | |
} | |
for(Iterator it = bullets.iterator(); it.hasNext();) { | |
Bullet bullet = (Bullet) it.next(); | |
bullet.move(); | |
if (bullet.isOutOfBound()) { | |
it.remove(); | |
} | |
else { | |
bullet.draw(); | |
} | |
} | |
} | |
/** | |
* Bullet Class | |
*/ | |
class Bullet { | |
float x; | |
float y; | |
float vx; | |
float vy; | |
float size = 5; | |
int reflectionCount = 1; | |
public Bullet(float x, float y) { | |
this.x = x; | |
this.y = y; | |
} | |
void setSpeed(float v, float degree) { | |
vx = v * cos(radians(degree)); | |
vy = v * sin(radians(degree)); | |
} | |
void setSpeedWithRadian(float v, float radian) { | |
vx = v * cos(radian); | |
vy = v * sin(radian); | |
} | |
void draw() { | |
if (this.reflectionCount > 0) { | |
stroke(0); | |
} else { | |
stroke(0xFFb00000); | |
} | |
ellipseMode(CENTER); | |
ellipse(x, y, size, size); | |
} | |
void move() { | |
this.x += vx; | |
this.y += vy; | |
// Reflection at top, right, and left walls | |
if (this.x < size || | |
this.x > width - size || | |
this.y < size) { | |
if (this.reflectionCount > 0) { | |
float targetRadian = atan2(mouseY - this.y, mouseX - this.x); | |
this.setSpeedWithRadian(3.6, targetRadian); | |
this.reflectionCount--; | |
} | |
} | |
} | |
boolean isOutOfBound() { | |
return (this.x < - OUT_OF_BOUND_MERGIN || | |
this.x > width + OUT_OF_BOUND_MERGIN || | |
this.y < - OUT_OF_BOUND_MERGIN || | |
this.y > height + OUT_OF_BOUND_MERGIN | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment