Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from fsxfreak/master

Finally finished converting to the Vector2f system
  • Loading branch information...
commit 12c3433644b43f3f0158f330de448230ea98f6d8 2 parents 018b0a5 + 5490903
@SubSage authored
View
82 GDCalaga/src/org/gdc/gdcalaga/Bullet.java
@@ -3,71 +3,72 @@
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
-
+import org.newdawn.slick.geom.Vector2f;
public class Bullet extends Entity
{
- private float damage, xVel, yVel;
- private int width, height, alliance;
+ private static final int SIZE_WIDTH = 9;
+ private static final int SIZE_HEIGHT = 5;
+
+ private float damage;
+ private Vector2f velocity;
+ private int alliance;
private Image bullet;
- public Bullet(EntityManager manager, int xpos, int ypos, int dmg, int alnc)
+ public Bullet(EntityManager manager, Vector2f position, int dmg, int alnc)
{
super(manager);
- x=xpos;
- y=ypos;
- width=9;
- height=5;
- damage=dmg;
- yVel=0;
- xVel=0;
- alliance=alnc;
+ pos.set(position);
+ size = new Vector2f(SIZE_WIDTH, SIZE_HEIGHT);
+ damage = dmg;
+ velocity = new Vector2f(0, 0);
+ alliance = alnc;
- shape = new RectShape(x, y, width, height);
+ shape = new RectShape(pos, size);
-
try {
- bullet = new Image("Pics/Bullet.png");
- } catch (SlickException e) {
- e.printStackTrace();
- }
+ bullet = new Image("Pics/Bullet.png");
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
}
public void update(float delta)
{
-
-
- x+= xVel * delta / 1000;
- y+= yVel * delta / 1000;
-
+ pos.x += velocity.x * delta / 1000;
+ pos.y += velocity.y * delta / 1000;
RectShape rect = (RectShape)shape;
- rect.xpos = x;
- rect.ypos = y;
+ rect.pos.set(this.pos);
- if( x<0 && xVel<=0 ||
- x>1280 && xVel>=0 ||
- y<0 && yVel<=0 ||
- y>780 && yVel>=0){
- Destroy();
- }
+ if (pos.x < 0 && velocity.x <= 0 ||
+ pos.x > 1280 && velocity.x >= 0 ||
+ pos.y < 0 && velocity.y <= 0 ||
+ pos.y > 780 && velocity.y >= 0)
+ {
+ Destroy();
+ }
}
public void draw(Graphics g)
{
- int drawX = (int) (x - width/2);
- int drawY = (int) (y - height/2);
+ int drawX = (int)(pos.x - size.x / 2);
+ int drawY = (int)(pos.y - size.y / 2);
float bw = bullet.getWidth();
- float scale = width / bw;
+ float scale = size.x / bw;
bullet.draw(drawX, drawY, scale, Color.white);
}
- public void setSpeed(float a, float b)
+ public void setSpeed(float xSpeed, float ySpeed)
+ {
+ velocity.set(xSpeed, ySpeed);
+ }
+
+ public void setSpeed(Vector2f speed)
{
- xVel=a;
- yVel=b;
+ velocity = speed;
}
public void Collide(Entity other)
@@ -87,7 +88,6 @@ else if(other instanceof Player)
{
Destroy();
}
-
}
}
@@ -96,7 +96,7 @@ public float getDamage(){
}
- public int getAlliance() {
- return alliance;
- }
+ public int getAlliance() {
+ return alliance;
+ }
}
View
19 GDCalaga/src/org/gdc/gdcalaga/Collision.java
@@ -6,41 +6,26 @@
public static void checkCollisions(ArrayList<Entity> ents)
{
-
//Very inefficient, but should work fine for now
-
for(int entA = 0; entA < ents.size(); entA++)
{
-
Entity A = ents.get(entA);
-
if(A.shape != null && A.shape.type != Shape.ShapeType.Null)
{
-
for(int entB = entA + 1; entB < ents.size(); entB++)
{
-
Entity B = ents.get(entB);
if(B.shape != null && B.shape.type != Shape.ShapeType.Null)
{
-
if(A.shape.Intersects(B.shape) && !A.IsDying() && !B.IsDying())
{
-
A.Collide(B);
B.Collide(A);
-
}
-
- }
-
- }
-
+ }
+ }
}
-
}
-
}
-
}
View
36 GDCalaga/src/org/gdc/gdcalaga/DroppableUpgrade.java
@@ -0,0 +1,36 @@
+package org.gdc.gdcalaga;
+
+import org.newdawn.slick.Image;
+import org.newdawn.slick.geom.Vector2f;
+
+/*
+ * DroppableUpgrade class. DroppableUpgrades are dropped randomly when enemies
+ * die, and the player must pickup the upgrade to receive the upgrade.
+ * Change this class to just an Upgrade class, that only collides with the player
+ * to activate an upgrade() function
+ */
+
+
+public abstract class DroppableUpgrade extends Entity
+{
+ private static final float SPEED = 50.f;
+ private static final float SIZE = 25.f;
+
+ protected Vector2f pos, vel, size;
+ protected Shape shape;
+ protected Image image;
+
+ protected Path path;
+ protected PathNode node;
+
+ public DroppableUpgrade(EntityManager manager, Vector2f initPos)
+ {
+ super(manager);
+
+ pos = initPos;
+ vel.set(-SPEED, 0);
+ size.set(SIZE, SIZE);
+
+ shape = new RectShape(pos, size);
+ }
+}
View
220 GDCalaga/src/org/gdc/gdcalaga/Enemy.java
@@ -7,94 +7,97 @@
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
public class Enemy extends Entity
{
- private static final int MAX_FIRE = 1500;
-
- protected float x, y, xVel, yVel, health;
- protected int height, width, alliance, pointValue;
+ private static final int MAX_FIRE = 1500;
+ private static final int SIZE_WIDTH = 41;
+ private static final int SIZE_HEIGHT = 32;
+
+ protected Vector2f velocity;
+ protected float health;
+ protected int alliance, pointValue;
Image ship;
private Explosion exp;
private boolean exploding, pathing;
private Path path;
private PathNode node;
- private float startX, startY, pathXVel, pathYVel;
+ private Vector2f startPos, pathVelocity;
private float fireRate;
private float fireRateDT;
private Random rand;
private EnemyGroup group;
private boolean grouped;
- public float relX, relY;
+ public Vector2f relPos;
- protected AudioManager audioManager;
+ protected AudioManager audioManager;
- public Enemy(EntityManager manager, float xpos, float ypos)
- {
+ public Enemy(EntityManager manager, Vector2f position)
+ {
super(manager);
- x=xpos;
- y=ypos;
- width=41;
- height=32;
- xVel=0;
- yVel=100;
- health=3;
- alliance=0;
+ pos.set(position);
+ size = new Vector2f(SIZE_WIDTH, SIZE_HEIGHT);
+ velocity = new Vector2f(0, 100);
+ health = 3;
+ alliance = 0;
pointValue = 10;
- shape = new RectShape(x, y, width, height);
+ shape = new RectShape(pos, size);
pathing = false;
grouped = false;
- relX = 0;
- relY = 0;
+ relPos = new Vector2f(0, 0);
+
+ startPos = new Vector2f(0, 0);
+ pathVelocity = new Vector2f(0, 0);
rand = new Random(System.currentTimeMillis());
fireRate = rand.nextInt(MAX_FIRE);
fireRateDT = 0;
try {
- ship = new Image("Pics/Enemy.png");
- } catch (SlickException e) {
- e.printStackTrace();
- }
+ ship = new Image("Pics/Enemy.png");
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
audioManager = AudioManager.getAudioManager();
}
- public void setPath(Path newPath){
- path = newPath;
+ public void setPath(Path newPath)
+ {
+ path = newPath;
//Spawn at the first node in the path
PathNode spawnPos = path.next();
- x = spawnPos.goalX;
- y = spawnPos.goalY;
+ pos.set(spawnPos.goalPos);
//Then start moving toward the next node
- if(path.hasNext()){
- node = path.next();
- startX = x;
- startY = y;
- pathing = true;
+ if(path.hasNext())
+ {
+ node = path.next();
+ startPos.set(pos);
+ pathing = true;
}
}
- public void setGroup(EnemyGroup g, float x, float y){
- group = g;
+ public void setGroup(EnemyGroup g, Vector2f relativePosition)
+ {
+ group = g;
grouped = true;
- relX = x;
- relY = y;
- x = relX + group.xPos;
- y = relY + group.yPos;
+ relPos.set(relativePosition);
+ pos.x = relPos.x + group.pos.x;
+ pos.y = relPos.y + group.pos.y;
+
g.addEnemy(this);
}
public void update(float delta)
{
-
if(exploding)
{
exp.update(delta);
@@ -104,53 +107,49 @@ public void update(float delta)
else
{
- fireRateDT += delta;
+ fireRateDT += delta;
- if (fireRateDT > fireRate)
- {
+ if (fireRateDT > fireRate)
+ {
fire();
fireRateDT = 0;
fireRate = rand.nextInt(MAX_FIRE);
}
-
- move(delta);
-
+ move(delta);
}
RectShape rect = (RectShape)shape;
- rect.xpos = x;
- rect.ypos = y;
+ rect.pos.set(this.pos);
}
public void draw(Graphics g)
{
-
- int drawX = (int) (x - width/2);
- int drawY = (int) (y - height/2);
- float scale = width / ship.getWidth();
+ int drawX = (int)(pos.x - SIZE_WIDTH / 2);
+ int drawY = (int)(pos.y - SIZE_HEIGHT / 2);
+ float scale = SIZE_WIDTH / ship.getWidth();
if(!exploding){
- ship.draw(drawX, drawY, scale, Color.white);
+ ship.draw(drawX, drawY, scale, Color.white);
} else {
- exp.render(g);
+ exp.render(g);
}
-
}
public void Collide(Entity other)
{
- if(other instanceof Bullet && ((Bullet)other).getAlliance()!=alliance)
+ if(other instanceof Bullet && ((Bullet)other).getAlliance()!=alliance)
{
- Hurt(((Bullet)other).getDamage());
+ Hurt(((Bullet)other).getDamage());
}
}
public void fire()
{
- Bullet newBullet = new Bullet(entities, (int)x - width/2 + 3, (int)y ,1 , alliance);
+ Vector2f bulletPosition = new Vector2f((pos.x - size.x / 2) + 3, pos.y);
+ Bullet newBullet = new Bullet(entities, bulletPosition, 1, alliance);
newBullet.setSpeed(-250, 0);
audioManager.playSFX(AudioAsset.SFX_FIRE2);
}
@@ -158,69 +157,66 @@ public void fire()
public void move(float delta)
{
-
- if(!pathing)
- {
- if(grouped)
- {
- y = group.yPos + relY;
- x = group.xPos + relX;
+ if(!pathing)
+ {
+ if(grouped)
+ {
+ pos.x = group.pos.x + relPos.x;
+ pos.y = group.pos.y + relPos.y;
}
- else
+ else
{
- x+= xVel * delta / 1000;
- y+= yVel * delta / 1000;
-
+ pos.x += velocity.x * delta / 1000;
+ pos.y += velocity.y * delta / 1000;
- if(y<0)
- {
- y = 0;
- yVel*=-1;
- }
+ if(pos.y < 0)
+ {
+ pos.y = 0;
+ velocity.y *= -1;
+ }
- else if(y>720)
- {
- y = 720;
- yVel*=-1;
- }
+ else if(pos.y > 720)
+ {
+ pos.y = 720;
+ velocity.y *= -1;
+ }
}
- }
- else
- {
-
- pathXVel = (node.goalX - startX) / node.speed;
- pathYVel = (node.goalY - startY) / node.speed;
- x += pathXVel * delta / 1000;
- y += pathYVel * delta / 1000;
- if(pathXVel * (node.goalX - x) < 0 || pathYVel * (node.goalY - y) < 0)
- {
- x = node.goalX;
- y = node.goalY;
- startX = x;
- startY = y;
- if(path.hasNext())
- {
- node = path.next();
- if(grouped && !path.hasNext())
- {
- node.goalX = group.xPos + relX;
- node.goalY = group.getYAfterTime(node.speed) + relY;
- }
- }
- else
- {
- pathing = false;
- }
- }
-
+ }
+ else
+ {
+ pathVelocity.x = (node.goalPos.x - startPos.x) / node.speed;
+ pathVelocity.y = (node.goalPos.y - startPos.y) / node.speed;
+ pos.x += pathVelocity.x * delta / 1000;
+ pos.y += pathVelocity.y * delta / 1000;
- }
+ if (pathVelocity.x * (node.goalPos.x - pos.x) < 0
+ || pathVelocity.y * (node.goalPos.y - pos.y) < 0)
+ {
+ pos.set(node.goalPos);
+ startPos.set(pos);
+
+ if (path.hasNext())
+ {
+ node = path.next();
+ if (grouped && !path.hasNext())
+ {
+ node.goalPos.x = group.pos.x + relPos.x;
+ node.goalPos.y = group.getYAfterTime(node.speed) + relPos.y;
+ }
+ }
+ else
+ {
+ pathing = false;
+ }
+ }
+ }
}
- private void Explode(){
- shape.type = Shape.ShapeType.Null;
+ private void Explode()
+ {
+ shape.type = Shape.ShapeType.Null;
- exp = new Explosion(x, y, 41, 8, width, height);
+ exp = new Explosion(pos, 41, 8, size);
exp.SetImage(ship);
exploding = true;
View
42 GDCalaga/src/org/gdc/gdcalaga/Enemy2.java
@@ -3,30 +3,36 @@
import org.gdc.gdcalaga.audio.AudioAsset;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
//Testing class. Making sure we can implement new enemies. Works fine
//Just add this guy instead of Enemy and you can see it work.
-public class Enemy2 extends Enemy {
+public class Enemy2 extends Enemy
+{
+ private static final int SIZE_WIDTH = 73;
+ private static final int SIZE_HEIGHT = 43;
+ private static final int BULLET_SPEED = 250;
- public Enemy2(EntityManager manager, float xpos, float ypos) {
- super(manager, xpos, ypos);
- width=73;
- height=43;
+ public Enemy2(EntityManager manager, Vector2f position) {
+ super(manager, position);
+ size = new Vector2f(SIZE_WIDTH, SIZE_HEIGHT);
- try {
- ship = new Image("Pics/EnemyShip2.png");
- } catch (SlickException e) {
- e.printStackTrace();
- }
-
- }
+ try {
+ ship = new Image("Pics/EnemyShip2.png");
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
public void fire()
{
- Bullet newBullet = new Bullet(entities, (int)x - width/2 + 3, (int)y +height/2 ,1 , alliance);
- Bullet newBullet2 = new Bullet(entities, (int)x - width/2 + 3, (int)y -height/2,1 , alliance);
- newBullet.setSpeed(-250, 0);
- newBullet2.setSpeed(-250, 0);
+ Vector2f bulletPos = new Vector2f((int)(pos.x - size.x / 2) + 3, (int)(pos.y + size.y / 2));
+ Bullet newBullet = new Bullet(entities, bulletPos, 1, alliance);
+
+ bulletPos.set((int)(pos.x - size.x / 2) + 3, (int)(pos.y - size.y / 2));
+ Bullet newBullet2 = new Bullet(entities, bulletPos, 1, alliance);
+
+ newBullet.setSpeed(-BULLET_SPEED, 0);
+ newBullet2.setSpeed(-BULLET_SPEED, 0);
audioManager.playSFX(AudioAsset.SFX_FIRE2);
- }
-
+ }
}
View
35 GDCalaga/src/org/gdc/gdcalaga/EnemyGroup.java
@@ -1,24 +1,22 @@
package org.gdc.gdcalaga;
import java.util.*;
-
import org.newdawn.slick.Graphics;
+import org.newdawn.slick.geom.Vector2f;
public class EnemyGroup extends Entity{
- public float xPos, yPos, yVel;
+ public Vector2f velocity;
private float top, bottom;
private ArrayList<Enemy> enemies;
public EnemyGroup(EntityManager mgr, float vely, float x, float y){
super(mgr);
- yVel = vely;
- xPos = x;
- yPos = y;
+ velocity = new Vector2f(0, vely);
+ pos = new Vector2f(x, y);
enemies = new ArrayList<Enemy>();
-
}
public void changeDir(){
- yVel *= -1;
+ velocity.y *= -1;
}
public void addEnemy(Enemy enemy){
@@ -32,35 +30,38 @@ public void removeEnemy(Enemy enemy){
}
public void setY(float y){
- yPos = y;
+ pos.y = y;
}
private void getBounds(){
top = 720;
bottom = 0;
- for(Enemy e: enemies){
- if(e.relY < top) top = e.relY;
- if(e.relY > bottom) bottom = e.relY;
+ for(Enemy e: enemies)
+ {
+ if (e.relPos.y < top)
+ top = e.relPos.y;
+ if (e.relPos.y > bottom)
+ bottom = e.relPos.y;
}
}
public float getYAfterTime(float time){
- float dy = yPos + yVel / time;
+ float dy = pos.y + velocity.y / time;
if(dy + bottom > 720) dy = 720 - bottom + 720 - bottom - dy;
if(dy + top < 0) dy = 0 - top - dy;
return dy;
}
public void update(float delta) {
- yPos += yVel * delta / 1000;
+ pos.y += velocity.y * delta / 1000;
- if(yPos + top < 0){
- yPos = 0 - top;
+ if(pos.y + top < 0){
+ pos.y = 0 - top;
changeDir();
}
- if(yPos + bottom > 720){
- yPos = 720 - bottom;
+ if(pos.y + bottom > 720){
+ pos.y = 720 - bottom;
changeDir();
}
View
27 GDCalaga/src/org/gdc/gdcalaga/Entity.java
@@ -1,6 +1,6 @@
package org.gdc.gdcalaga;
import org.newdawn.slick.Graphics;
-
+import org.newdawn.slick.geom.Vector2f;
//Entity class. An entity is anything that moves and shoots. Main "thing" the player plays against.
//The player is also an entity. interp is the time inteveral, so that we can update the entity according to time
@@ -8,10 +8,10 @@
public abstract class Entity
{
-
public int id;
protected EntityManager entities;
- protected float x, y, width, height, alliance, interp;
+ protected float alliance, interp;
+ public Vector2f pos, size;
private boolean dying;
protected Shape shape;
@@ -23,22 +23,27 @@ public Entity(EntityManager manager)
entities.AddEntity(this);
dying = false;
- x=y=alliance=0;
+ alliance = 0;
+ pos = new Vector2f(0, 0);
}
public Entity(int xpos, int ypos)
{
- x=xpos;
- y=ypos;//Thinking of things, x and xpos should be switched, but the convenience....
+ pos = new Vector2f(xpos, ypos);
}
public Entity(int xpos, int ypos, float w, float h)
{
- x=xpos;
- y=ypos;//Thinking of things, x and xpos should be switched, but the convenience....
- width = w;
- height = h;
- alliance=0;
+ alliance = 0;
+
+ pos = new Vector2f(xpos, ypos);
+ size = new Vector2f(w, h);
+ }
+
+ public Entity(Vector2f position, Vector2f size)
+ {
+ this.pos.set(position);
+ this.size.set(position);
}
public boolean IsDying()
View
128 GDCalaga/src/org/gdc/gdcalaga/Explosion.java
@@ -1,82 +1,78 @@
package org.gdc.gdcalaga;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
+import org.newdawn.slick.geom.Vector2f;
public class Explosion {
-
- private int xtiles, ytiles;
- public float xpos, ypos;
- private float width, height, life, maxlife;
- private ExplosionParticle[] particles;
- private boolean dead;
+ private int xtiles, ytiles;
+ public Vector2f pos, size;
+ private float life, maxlife;
+ private ExplosionParticle[] particles;
+ private boolean dead;
- Explosion(float x, float y, int xnum, int ynum, float w, float h)
- {
- xpos = x;
- ypos = y;
- width = w;
- height = h;
- xtiles = xnum;
- ytiles = ynum;
- particles = new ExplosionParticle[xtiles * ytiles];
+ Explosion(Vector2f position, int xnum, int ynum, Vector2f size)
+ {
+ pos = new Vector2f(position);
+ this.size = new Vector2f(size);
+ xtiles = xnum;
+ ytiles = ynum;
+ particles = new ExplosionParticle[xtiles * ytiles];
- maxlife = 500f;
- life = maxlife;
- dead = false;
- }
+ maxlife = 500f;
+ life = maxlife;
+ dead = false;
+ }
- public void SetImage(Image img){
- int imgHeight = img.getHeight();
- int imgWidth = img.getWidth();
- int srcw = (int)(imgWidth / xtiles);
- int srch = (int)(imgHeight / ytiles);
- for(int tile = 0; tile < xtiles * ytiles; tile++){
- int yTile = (int) Math.floor(tile / xtiles);
- int xTile = tile - yTile * xtiles;
+ public void SetImage(Image img){
+ int imgHeight = img.getHeight();
+ int imgWidth = img.getWidth();
+ int srcw = (int)(imgWidth / xtiles);
+ int srch = (int)(imgHeight / ytiles);
+ for(int tile = 0; tile < xtiles * ytiles; tile++){
+ int yTile = (int) Math.floor(tile / xtiles);
+ int xTile = tile - yTile * xtiles;
- int srcYPos = yTile * srch;
- int srcXPos = xTile * srcw;
+ int srcYPos = yTile * srch;
+ int srcXPos = xTile * srcw;
- float w = (width / xtiles);
- float h = (height / ytiles);
- float x = xTile * w + xpos - width/2;
- float y = yTile * h + ypos - height/2;
- particles[tile] = new ExplosionParticle(this, x, y, w, h);
- particles[tile].SetImage(img, srcXPos, srcYPos, srcw, srch);
- }
- SetVelocity();
- }
+ float w = (size.x / xtiles);
+ float h = (size.y / ytiles);
+ float x = xTile * w + pos.x - size.x / 2;
+ float y = yTile * h + pos.y - size.y / 2;
+ particles[tile] = new ExplosionParticle(this, x, y, w, h);
+ particles[tile].SetImage(img, srcXPos, srcYPos, srcw, srch);
+ }
+ SetVelocity();
+ }
- public void SetVelocity(){
- for(int tile = 0; tile < xtiles * ytiles; tile++){
- particles[tile].SetVelocity();
- }
- }
+ public void SetVelocity(){
+ for(int tile = 0; tile < xtiles * ytiles; tile++){
+ particles[tile].SetVelocity();
+ }
+ }
- public void update(float delta){
- life -= delta;
- if(life <= 0) dead = true;
+ public void update(float delta){
+ life -= delta;
+ if(life <= 0) dead = true;
- if(!dead){
- for(int tile = 0; tile < xtiles * ytiles; tile++){
- float alpha = (life/maxlife);
- particles[tile].SetColor(1f, 1f, 1f, alpha);
- particles[tile].update(delta);
- }
- }
- }
-
- public void render(Graphics g){
- if(!dead){
- for(int tile = 0; tile < xtiles * ytiles; tile++){
- particles[tile].render(g);
- }
- }
- }
-
- public boolean IsDead(){
- return dead;
- }
+ if(!dead){
+ for(int tile = 0; tile < xtiles * ytiles; tile++){
+ float alpha = (life/maxlife);
+ particles[tile].SetColor(1f, 1f, 1f, alpha);
+ particles[tile].update(delta);
+ }
+ }
+ }
+ public void render(Graphics g){
+ if(!dead){
+ for(int tile = 0; tile < xtiles * ytiles; tile++){
+ particles[tile].render(g);
+ }
+ }
+ }
+ public boolean IsDead(){
+ return dead;
+ }
}
View
27 GDCalaga/src/org/gdc/gdcalaga/ExplosionParticle.java
@@ -1,5 +1,6 @@
package org.gdc.gdcalaga;
import org.newdawn.slick.Color;
+import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
@@ -7,21 +8,17 @@
public class ExplosionParticle{
private Image img;
- private float xpos, ypos, width, height, xvel, yvel;
+ private Vector2f pos, size, velocity;
private Color color;
private int srcx, srcy, srcwidth, srcheight;
private Explosion exp;
public ExplosionParticle(Explosion explosion, float x, float y, float w, float h) {
- xpos = x;
- ypos = y;
- width = w;
- height = h;
+ pos = new Vector2f(x, y);
+ size = new Vector2f(w, h);
+ velocity = new Vector2f(5, 5);
color = new Color(1,1,1,1);
exp = explosion;
-
- xvel = 5;
- yvel = 5;
}
public void SetImage(Image image, int xoffset, int yoffset, int srcw, int srch){
@@ -33,8 +30,8 @@ public void SetImage(Image image, int xoffset, int yoffset, int srcw, int srch){
}
public void SetVelocity(){
- xvel *= (xpos - exp.xpos) * (float)Math.random();
- yvel *= (ypos - exp.ypos) * (float)Math.random();
+ velocity.x *= (pos.x - exp.pos.x) * (float)Math.random();
+ velocity.y *= (pos.y - exp.pos.y) * (float)Math.random();
}
public void SetColor(float r, float g, float b, float a){
@@ -44,13 +41,15 @@ public void SetColor(float r, float g, float b, float a){
color.a = a;
}
- public void update(float delta){
- xpos += xvel * delta / 1000;
- ypos += yvel * delta / 1000;
+ public void update(float delta)
+ {
+ pos.x += velocity.x * delta / 1000;
+ pos.y += velocity.y * delta / 1000;
}
public void render(Graphics g){
- img.draw(xpos, ypos, xpos + width, ypos + height, srcx, srcy, srcx + srcwidth, srcy + srcheight, color);
+ img.draw(pos.x, pos.y, pos.x + size.x, pos.y + size.y,
+ srcx, srcy, srcx + srcwidth, srcy + srcheight, color);
}
}
View
53 GDCalaga/src/org/gdc/gdcalaga/Path.java
@@ -1,41 +1,46 @@
package org.gdc.gdcalaga;
import java.util.*;
+import org.newdawn.slick.geom.Vector2f;
public class Path extends Object {
- private float goalX, goalY;
+ private Vector2f goalPos;
private ArrayList<PathNode> nodes;
private int currentNode;
- public Path(float x, float y){
- goalX = x;
- goalY = y;
- currentNode = -1;
- nodes = new ArrayList<PathNode>();
+ public Path(Vector2f pos)
+ {
+ goalPos = new Vector2f(pos);
+ currentNode = -1;
+ nodes = new ArrayList<PathNode>();
}
- public Path copy(float x, float y){
- Path newPath = new Path(x, y);
- for(PathNode node: nodes){
- newPath.addNode(node.relative, node.goalX, node.goalY, node.speed);
+ public Path copy(Vector2f pos)
+ {
+ Path newPath = new Path(pos);
+ for (PathNode node : nodes)
+ {
+ newPath.addNode(node.relative, node.goalPos, node.speed);
}
return newPath;
}
-
- public void addNode(boolean relative, float x, float y, float speed){
- if(relative){
- x += goalX;
- y += goalY;
- }
-
- nodes.add(new PathNode(relative, x, y, speed));
+
+ public void addNode(boolean relative, Vector2f otherPos, float speed)
+ {
+ if (relative)
+ {
+ otherPos.add(this.goalPos);
+ }
+ nodes.add(new PathNode(relative, otherPos, speed));
}
- public void setPos(float newX, float newY){
- for(int i = 0; i < nodes.size(); i++){
- nodes.get(i).translatePos(newX - goalX, newY - goalY);
- }
- goalX = newX;
- goalY = newY;
+ public void setPos(Vector2f newPos)
+ {
+ for (int i = 0; i < nodes.size(); i++)
+ {
+ //I wish operator overloading was in java
+ nodes.get(i).translatePos(newPos.copy().sub(goalPos));
+ }
+ goalPos.set(newPos);
}
public boolean hasNext(){
View
50 GDCalaga/src/org/gdc/gdcalaga/PathNode.java
@@ -1,23 +1,41 @@
package org.gdc.gdcalaga;
+import org.newdawn.slick.geom.Vector2f;
+
public class PathNode {
- public float goalX, goalY, speed;
- public boolean relative;
+ public float speed;
+ public Vector2f goalPos;
+ public float goalX; //goalX and goalY to capture values from data/paths.json
+ public float goalY;
+ public boolean relative;
- PathNode(boolean rel, float x, float y, float moveSpeed){
- relative = rel;
- goalX = x;
- goalY = y;
- speed = moveSpeed;
- }
+ PathNode(boolean rel, float x, float y, float moveSpeed){
+ relative = rel;
+ goalPos = new Vector2f(x, y);
+ speed = moveSpeed;
+ }
+
+ PathNode(boolean rel, Vector2f position, float moveSpeed){
+ relative = rel;
+ goalPos = new Vector2f(position);
+ speed = moveSpeed;
+ }
- public void setPos(float newX, float newY){
- goalX = newX;
- goalY = newY;
- }
+ public void setPos(Vector2f newPosition){
+ goalPos = newPosition;
+ }
- public void translatePos(float addX, float addY){
- goalX += addX;
- goalY += addY;
- }
+ public void translatePos(Vector2f translateVector){
+ goalPos.add(translateVector);
+ }
+
+ /**
+ * Cleanup method to convert goalX and goalY values into goalPos,
+ * since our method of creating paths in json relies on initializing
+ * the goalX and goalY values
+ */
+ public void initialize()
+ {
+ goalPos = new Vector2f(goalX, goalY);
+ }
}
View
22 GDCalaga/src/org/gdc/gdcalaga/PathRegistry.java
@@ -4,6 +4,7 @@
import java.lang.reflect.*;
import com.google.gson.Gson;
import com.google.gson.reflect.*;
+import org.newdawn.slick.geom.Vector2f;
public class PathRegistry {
private ArrayList<Path> paths;
@@ -12,9 +13,11 @@ public PathRegistry(){
paths = new ArrayList<Path>();
}
- public Path getPath(int id, float x, float y){
- if(paths.size() > id){
- Path retPath = paths.get(id).copy(x, y);
+ public Path getPath(int id, Vector2f pos)
+ {
+ if (paths.size() > id)
+ {
+ Path retPath = paths.get(id).copy(pos);
return retPath;
} else {
return null;
@@ -50,10 +53,15 @@ public void loadFromJson(String path){
ArrayList<PathNode[]> nodes = new Gson().fromJson(buffer, collectionType);
- for(PathNode[] nodeArray: nodes){
- Path newPath = new Path(0, 0);
- for(PathNode node : nodeArray){
- newPath.addNode(node.relative, node.goalX, node.goalY, node.speed);
+ for(PathNode[] nodeArray: nodes)
+ {
+ Vector2f pathPos = new Vector2f(0, 0);
+ Path newPath = new Path(pathPos);
+
+ for(PathNode node : nodeArray)
+ {
+ node.initialize(); //to solve issues with json
+ newPath.addNode(node.relative, node.goalPos, node.speed);
}
registerPath(newPath);
}
View
5 GDCalaga/src/org/gdc/gdcalaga/PlayState.java
@@ -1,7 +1,6 @@
package org.gdc.gdcalaga;
import java.util.LinkedList;
import java.util.List;
-
import org.gdc.gdcalaga.audio.AudioAsset;
import org.gdc.gdcalaga.audio.AudioManager;
import org.newdawn.slick.GameContainer;
@@ -10,6 +9,7 @@
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
+import org.newdawn.slick.geom.Vector2f;
public class PlayState extends BasicGameState {
public static final int ID = 0;
@@ -29,7 +29,8 @@
@Override
public void init(GameContainer container, StateBasedGame game) throws SlickException {
input = container.getInput();
- player= new Player(entities,50,300);
+ Vector2f startPosition = new Vector2f(50, 300);
+ player= new Player(entities, startPosition);
audioManager.loadAudioAssets();
audioManager.playMusic(AudioAsset.MUSIC_LEVEL_1);
View
84 GDCalaga/src/org/gdc/gdcalaga/Player.java
@@ -3,82 +3,90 @@
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
-
+/*
+ * Upgrades planned for the player:
+ * Droppable: Health, shields, rapid fire mode, more bullets
+ * Buyable: Speed
+ */
public class Player extends Entity
{
- private float xVel, yVel, health;
- private int height, width, alliance;
+ private static final int SIZE_WIDTH = 49;
+ private static final int SIZE_HEIGHT = 29;
+ private static final int SPEED = 220;
+
+ private float health;
+ protected Vector2f velocity;
+ private int alliance;
private static int totalPoints = 0; //the score is static in case we give players multiple lives in the future
Image ship;
- public Player(EntityManager manager, int xpos, int ypos)
+ public Player(EntityManager manager, Vector2f position)
{
super(manager);
- x=xpos;
- y=ypos;
- width=49;
- height=29;
- xVel=220;
- yVel=220;
- health=10;
- alliance=1;
+
+ pos.set(position);
+ size = new Vector2f(SIZE_WIDTH, SIZE_HEIGHT);
+ velocity = new Vector2f(SPEED, SPEED);
- shape = new RectShape(x, y, width, height);
+ health = 10;
+ alliance = 1;
+
+ shape = new RectShape(pos, size);
-
try {
- ship= new Image("Pics/Player.png");
- } catch (SlickException e) {
- e.printStackTrace();
- }
+ ship= new Image("Pics/Player.png");
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
}
public void update(float delta)
{
RectShape rect = (RectShape)shape;
- rect.xpos = x;
- rect.ypos = y;
+ rect.pos.set(this.pos);
}
public void draw(Graphics g)
{
- int drawX = (int) (x - width/2);
- int drawY = (int) (y - height/2);
- float scale = width / ship.getWidth();
+ int drawX = (int)(pos.x - size.x / 2);
+ int drawY = (int)(pos.y - size.y / 2);
+ float scale = size.x / ship.getWidth();
ship.draw(drawX, drawY, scale, Color.white);
}
public void moveUp(float delta)
{
- y-=yVel*delta/1000;
- y = Math.max(height/2, y);
+ pos.y -= velocity.y * delta / 1000;
+ pos.y = Math.max(size.y / 2, pos.y);
}
public void moveDown(float delta)
- {
- y+=yVel*delta/1000;
- y = Math.min(720 - height/2, y);
+ {
+ pos.y += velocity.y * delta / 1000;
+ pos.y = Math.min(720 - size.y / 2, pos.y);
}
public void moveLeft(float delta)
{
- x-=xVel*delta/1000;
- x = Math.max(0 + width/2, x);
+ pos.x -= velocity.x * delta / 1000;
+ pos.x = Math.max(0 + size.x / 2, pos.x);
}
public void moveRight(float delta)
{
- x+=xVel*delta/1000;
- x = Math.min(1280 - width/2, x);
+ pos.x += velocity.x * delta / 1000;
+ pos.x = Math.min(1280 - size.x / 2, pos.x);
}
public void fire()
{
- Bullet newBullet = new Bullet(entities, (int)x + width/2, (int)y ,1 , alliance);
+ Vector2f position = new Vector2f(pos.x + size.x / 2, pos.y);
+ Bullet newBullet = new Bullet(entities, position, 1, alliance);
newBullet.setSpeed(500, 0);
}
@@ -86,7 +94,7 @@ public void Collide(Entity other)
{
if(other instanceof Bullet && ((Bullet)other).getAlliance()!=alliance)
{
- Hurt(((Bullet)other).getDamage());
+ Hurt(((Bullet)other).getDamage());
}
}
@@ -94,9 +102,9 @@ public void Hurt(float dmg){
health-=dmg;
}
- public int getHealth()
+ public float getHealth()
{
- return (int)health;
+ return health;
}
public int getAlliance()
@@ -118,10 +126,10 @@ public static void decreaseTotalPoints(int pointValue)
{
if( (totalPoints - pointValue) >= 0)
{
- totalPoints -= pointValue;
+ totalPoints -= pointValue;
}
else
- System.out.println("Can't spend any more points!"); //TODO Fix error message
+ System.out.println("Can't spend any more points!"); //TODO Fix error message
}
}
View
34 GDCalaga/src/org/gdc/gdcalaga/RectShape.java
@@ -1,12 +1,21 @@
package org.gdc.gdcalaga;
+import org.newdawn.slick.geom.Vector2f;
+
public class RectShape extends Shape {
- public float xpos, ypos, width, height;
+ public Vector2f pos, size;
- RectShape(float x, float y, float w, float h){
+ /**
+ * @param position Takes a Vector2f that represents the position in x and y
+ * coordinates.
+ * @param size Takes a Vector2f that represents the size in x and y floats.
+ */
+ RectShape(Vector2f position, Vector2f size){
type = ShapeType.Rectangle;
- xpos=x;ypos=y;width=w;height=h;
+
+ this.pos = new Vector2f(position);
+ this.size = new Vector2f(size);
}
public boolean Intersects(Shape other){
@@ -20,16 +29,17 @@ public boolean Intersects(Shape other){
}
private boolean Intersects(RectShape other){
- if(other.xpos + other.width/2 < xpos - width/2 ||
- other.xpos - other.width/2 > xpos + width/2 ||
- other.ypos + other.height/2 < ypos - height/2 ||
- other.ypos - other.height/2 > ypos + height/2){
-
+
+ if (other.pos.x + other.size.x / 2 < this.pos.x - this.size.x / 2
+ || other.pos.x - other.size.x / 2 > this.pos.x + this.size.x / 2
+ || other.pos.y + other.size.y / 2 < this.pos.y - this.size.y / 2
+ || other.pos.y - other.size.y / 2 > this.pos.y + this.size.y / 2)
+ {
return false;
- } else {
+ }
+ else
+ {
return true;
- }
+ }
}
-
-
}
View
9 GDCalaga/src/org/gdc/gdcalaga/Spawn.java
@@ -1,6 +1,6 @@
package org.gdc.gdcalaga;
import java.util.ArrayList;
-
+import org.newdawn.slick.geom.Vector2f;
/*
* I want this class to learn about the player constantly. It needs to observe the player and from there, decide how to suit the level
* for the player. We don't have many enemies yet, but we should later have enemies with unique characteristics and features. The class
@@ -40,6 +40,8 @@ public static void spawnWave(PathRegistry paths, EntityManager mng) {
x = (float)(Math.random()*400);
y = (float)(Math.random()*400);
+ Vector2f offsetPos = new Vector2f(x, y);
+
/*
Enemy newEnemy = new Enemy(mng,x,y);
@@ -48,7 +50,10 @@ public static void spawnWave(PathRegistry paths, EntityManager mng) {
enemyPath.addNode(false, 700, 300, 1);
enemyPath.addNode(true, 0, 0, 1f);
*/
- Path enemyPath = paths.getPath(pathNum, group.xPos + x, group.yPos + y);
+
+ Path enemyPath = paths.getPath(pathNum
+ , new Vector2f(group.pos.x + offsetPos.x,
+ group.pos.y + offsetPos.y));
int rows = (int)Math.ceil(Math.random() * 3 + 4);
int cols = (int)Math.ceil(Math.random() * 3 + 2);
View
19 GDCalaga/src/org/gdc/gdcalaga/Wave.java
@@ -1,6 +1,7 @@
package org.gdc.gdcalaga;
import java.util.*;
import org.newdawn.slick.Graphics;
+import org.newdawn.slick.geom.Vector2f;
public class Wave extends Entity {
private EnemyGroup group;
@@ -12,18 +13,17 @@
private float lastSpawn;
private ArrayList<Enemy> enemies;
-
public Wave(EntityManager manager, String waveType, int rows, int cols, int numEnemies, float spawnDelay, Path wavePath)
{
super(manager);
- x = 800;
- y = 50;
+ pos = new Vector2f(800, 50);
+
type = waveType;
yRows = rows;
xCols = cols;
delay = spawnDelay;
- group = new EnemyGroup(manager, 100, x, y);
+ group = new EnemyGroup(manager, 100, pos.x, pos.y);
path = wavePath;
lastSpawn = 0;
@@ -70,9 +70,10 @@ private void spawnEnemy(){
if(type == "block"){
float newX = curX * 400 / xCols;
float newY = curY * 620 / yRows;
- Enemy newEnemy = new Enemy(entities, newX, newY);
- newEnemy.setGroup(group, newX, newY);
- newEnemy.setPath(path.copy(x + newX, y + newY));
+ Vector2f newPosition = new Vector2f(newX, newY);
+ Enemy newEnemy = new Enemy(entities, newPosition);
+ newEnemy.setGroup(group, newPosition);
+ newEnemy.setPath(path.copy(new Vector2f(pos.x + newPosition.x, pos.y + newPosition.y)));
curY++;
if(curY >= yRows){
curY = 0;
@@ -80,8 +81,8 @@ private void spawnEnemy(){
}
enemies.add(newEnemy);
} else if(type == "stream"){
- Enemy newEnemy = new Enemy(entities, x, y);
- newEnemy.setPath(path.copy(x, y));
+ Enemy newEnemy = new Enemy(entities, pos.copy());
+ newEnemy.setPath(path.copy(pos.copy()));
enemies.add(newEnemy);
}
spawnedEnemies++;
Please sign in to comment.
Something went wrong with that request. Please try again.