diff --git a/SpaceEvaders/bin/.DS_Store b/SpaceEvaders/bin/.DS_Store new file mode 100644 index 0000000..7f109e8 Binary files /dev/null and b/SpaceEvaders/bin/.DS_Store differ diff --git a/SpaceEvaders/bin/game/Polygon.class b/SpaceEvaders/bin/game/Polygon.class index 99b12eb..de48a1b 100644 Binary files a/SpaceEvaders/bin/game/Polygon.class and b/SpaceEvaders/bin/game/Polygon.class differ diff --git a/SpaceEvaders/bin/game/SpaceEvaders.class b/SpaceEvaders/bin/game/SpaceEvaders.class index 38d64f3..0b8bebf 100644 Binary files a/SpaceEvaders/bin/game/SpaceEvaders.class and b/SpaceEvaders/bin/game/SpaceEvaders.class differ diff --git a/SpaceEvaders/src/game/Asteroid.java b/SpaceEvaders/src/game/Asteroid.java index 777abd5..92615f5 100644 --- a/SpaceEvaders/src/game/Asteroid.java +++ b/SpaceEvaders/src/game/Asteroid.java @@ -1,5 +1,24 @@ package game; public class Asteroid extends Polygon implements Projectile { + private int XVelocity; + private int YVelocity; + Polygon hitbox; + public Asteroid() { + super(instantiateShape(), new Point(300, 0), 180); + this.scalePolygon(37); + XVelocity = 0; + YVelocity = 20; + hitbox = new Polygon(instantiateShape(), this.position, this.rotation); + } + private static Point[] instantiateShape(){ + Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(2,7), new Point(0,3), new Point(0,1), new Point(1,0)}; + return shape; + } + @Override + public boolean checkCollision(Polygon other) { + // TODO Auto-generated method stub + return false; + } } diff --git a/SpaceEvaders/src/game/BackgroundGenerator.java b/SpaceEvaders/src/game/BackgroundGenerator.java deleted file mode 100644 index b9a8863..0000000 --- a/SpaceEvaders/src/game/BackgroundGenerator.java +++ /dev/null @@ -1,5 +0,0 @@ -package game; - -public interface BackgroundGenerator { - -} diff --git a/SpaceEvaders/src/game/BlackHole.java b/SpaceEvaders/src/game/BlackHole.java index 6968108..da8b4c2 100644 --- a/SpaceEvaders/src/game/BlackHole.java +++ b/SpaceEvaders/src/game/BlackHole.java @@ -1,10 +1,55 @@ package game; -public class BlackHole extends Polygon { +import java.awt.Color; +import java.awt.Graphics; - public BlackHole(Point[] inShape, Point inPosition, double inRotation) { - super(inShape, inPosition, inRotation); - // TODO Auto-generated constructor stub +public class BlackHole extends Polygon { + private Polygon hitbox; + int counter = 0; + + public BlackHole() { + super(instantiateShape(), new Point(400,300), 0); + this.scalePolygon(10); + Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(3,4), new Point(1,4), new Point(0,3), new Point(0,1), new Point(1,0)}; + hitbox = new Polygon(shape, this.findCenter(), this.rotation); + hitbox.scalePolygon(10); + } + private static Point[] instantiateShape() { + Point[] shape = {new Point(2,2), new Point(5,3), new Point(6,0), new Point(7,3), new Point(10,2), new Point(9,5), new Point(12,6), new Point(9,7), new Point(10,10), new Point(7,9), new Point(6,12), new Point(5,9), new Point(2,10), new Point(3,7), new Point(0,6), new Point(3,5)}; + return shape; + } + + public void paint(Graphics brush) { + counter++; //erase later + this.rotate(counter);//use get counter from space evaders class + int[] x2 = new int[this.getPoints().length]; + int[] y2 = new int[this.getPoints().length]; + for(int i = 0; i < x2.length; i++) { + x2[i] = (int) this.getPoints()[i].x; + y2[i] = (int) this.getPoints()[i].y; + } + + brush.setColor(Color.DARK_GRAY); + brush.fillPolygon(x2, y2, x2.length); + + hitbox.rotation = this.rotation; + hitbox.position = this.position; + int[] x3 = new int[hitbox.getPoints().length]; + int[] y3 = new int[hitbox.getPoints().length]; + for(int i = 0; i < x3.length; i++) { + x3[i] = (int) hitbox.getPoints()[i].x; + y3[i] = (int) hitbox.getPoints()[i].y; + } + + brush.setColor(Color.BLACK); + brush.drawPolygon(x3, y3, x3.length); + + counter++; + } + + public getHitbox() { + //implement... + } } diff --git a/SpaceEvaders/src/game/BorderGenerator.java b/SpaceEvaders/src/game/BorderGenerator.java new file mode 100644 index 0000000..86168dc --- /dev/null +++ b/SpaceEvaders/src/game/BorderGenerator.java @@ -0,0 +1,5 @@ +package game; +import java.awt.Graphics; +public interface BorderGenerator { + void generateBorder(Graphics brush); +} diff --git a/SpaceEvaders/src/game/Polygon.java b/SpaceEvaders/src/game/Polygon.java index 1412e7f..6b1b75e 100644 --- a/SpaceEvaders/src/game/Polygon.java +++ b/SpaceEvaders/src/game/Polygon.java @@ -57,6 +57,15 @@ public Point[] getPoints() { return points; } + public void scalePolygon(int scalar) {//makes stuff off center... figure out how to fix that? + for(int i = 0; i < shape.length; i++) { + shape[i] = new Point(shape[i].getX() * scalar, shape[i].getY() * scalar); + } + //shape = getPoints(); <- figure out how to re-center the dilated polygon... + + } + + // "contains" implements some magical math (i.e. the ray-casting algorithm). public boolean contains(Point point) { Point[] points = getPoints(); diff --git a/SpaceEvaders/src/game/Projectile.java b/SpaceEvaders/src/game/Projectile.java index a003fab..806b114 100644 --- a/SpaceEvaders/src/game/Projectile.java +++ b/SpaceEvaders/src/game/Projectile.java @@ -1,7 +1,7 @@ package game; public interface Projectile { - Point getVelocity(); - boolean checkBlackHoleCollision(BlackHole blackHole); - boolean checkShipCollision(Spaceship shipHitbox); + double getVelocity(); + boolean checkCollision(Polygon other); + } diff --git a/SpaceEvaders/src/game/SpaceEvaders.java b/SpaceEvaders/src/game/SpaceEvaders.java index 15f0c10..838c97e 100644 --- a/SpaceEvaders/src/game/SpaceEvaders.java +++ b/SpaceEvaders/src/game/SpaceEvaders.java @@ -9,12 +9,14 @@ */ import java.awt.*; import java.awt.event.*; +import java.util.Iterator; class SpaceEvaders extends Game { static int counter = 0; public static final int WIDTH = 800; public static final int LENGTH = 600; - + private BorderGenerator borderGen; + private Polygon borderHitbox; static Spaceship thing1 = new Spaceship(new Point(300, 300), 0); static Spaceship thing2 = new Spaceship(new Point(600, 300), 0); @@ -22,9 +24,22 @@ public SpaceEvaders() { super("Space Evaders", WIDTH, LENGTH); this.setFocusable(true); this.requestFocus(); + borderGen = (Graphics brush) -> { + int[] XCoords = {50, 750, 750, 50}; + int[] YCoords = {50, 50, 550, 550}; + + brush.setColor(Color.BLUE); + brush.drawPolygon(XCoords, YCoords, 4); + }; + Point[] shape = {new Point(50,50), new Point(750,50), new Point(750, 550), new Point(50,550)}; + borderHitbox = new Polygon(shape, new Point(400,300), 0); this.addKeyListener(thing1); this.addKeyListener(thing2); } + + public Polygon getBorderHitbox() { + return borderHitbox; + } public void paint(Graphics brush) { brush.setColor(Color.black); @@ -33,20 +48,41 @@ public void paint(Graphics brush) { // sample code for printing message for debugging // counter is incremented and this message printed // each time the canvas is repainted - + borderGen.generateBorder(brush); brush.setColor(Color.white); - thing1.paint(brush); thing2.paint(brush); + Asteroid ast = new Asteroid();//please turn this process into a method in polygon!!!!!! + int[] x = new int[ast.getPoints().length]; + int[] y = new int[ast.getPoints().length]; + for(int i = 0; i < x.length; i++) { + x[i] = (int) ast.getPoints()[i].x; + y[i] = (int) ast.getPoints()[i].y + counter; + } + + brush.drawPolygon(x, y, x.length); + + BlackHole bh = new BlackHole();//please turn this process into a method in polygon!!!!!! + bh.paint(brush); + + + //for(int i = 0; ) {} + //int[] yMissileCords = + //int[] xMissileCords = + //brush.setColor(Color.ORANGE); + //brush.fillPolygon(xMissileCords, yMissileCords, 8); + + /* * the comment below is just me messing with the code before we meet * officially on friday * feel free to delete it or something it doesn't do anything important */ - brush.drawString("Time: " + Integer.toString(counter / 60) + ":" + Integer.toString(counter % 60 * 5 / 3), 10, 10); + //brush.drawString("Time: " + Integer.toString(counter / 60) + ":" + Integer.toString(counter % 60 * 5 / 3), 10, 10); + counter++; } @@ -58,4 +94,6 @@ public static void main (String[] args) { SpaceEvaders a = new SpaceEvaders(); a.repaint(); } + + } \ No newline at end of file diff --git a/SpaceEvaders/src/game/Spaceship.java b/SpaceEvaders/src/game/Spaceship.java index 16e245f..d98f9c2 100644 --- a/SpaceEvaders/src/game/Spaceship.java +++ b/SpaceEvaders/src/game/Spaceship.java @@ -3,15 +3,16 @@ import java.awt.Graphics; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.util.Iterator; -public class Spaceship extends Polygon implements KeyListener{ +public class Spaceship extends Polygon implements KeyListener, Iterable{ private static int id = 1; private static final int SIZE = 30; // Will be deprecated once Hans is done private static final double ACCELRATE = 0.1; private static final int ROTATERATE = 3; - private static final int MAXMISSILES = 400; + private static final int MAXPROJECTILES = 400; private int player; private double xVel; @@ -22,7 +23,7 @@ public class Spaceship extends Polygon implements KeyListener{ private boolean isTurningLeft; private boolean isTurningRight; private boolean missileFired; - private Missile[] missiles; + private Projectile[] firedProjectiles; public Spaceship(Point position, double rotation) { super(instantiateShape(), position, rotation); @@ -30,13 +31,12 @@ public Spaceship(Point position, double rotation) { yVel = 0.0; xAccel = 0.0; yAccel = 0.0; - missiles = new Missile[MAXMISSILES]; + firedProjectiles = new Projectile[MAXPROJECTILES]; player = id++; if (player == 2) { this.rotate(180); } - } /* @@ -45,13 +45,8 @@ public Spaceship(Point position, double rotation) { * here. */ private static Point[] instantiateShape() { - Point[] square = new Point[4]; - square[0] = new Point(0, 0); - square[1] = new Point(SIZE, 0); - square[2] = new Point(SIZE, SIZE); - square[3] = new Point(0, SIZE); - - return square; + Point[] shape = {new Point(0,0), new Point(2,0), new Point(1,1), new Point(3,1), new Point(4,2), new Point(3,3), new Point(1,3), new Point(2,4), new Point(0,4), new Point(0,0)}; + return shape; } @@ -112,13 +107,13 @@ private void handleMovement() { private void fireMissile() { int openMissile = canFireMissile(); if (openMissile != -1) { - missiles[openMissile] = new Spaceship.Missile(this, this.rotation); + firedProjectiles[openMissile] = new Spaceship.Missile(this, this.rotation); } } private int canFireMissile() { - for (int i = 0; i < MAXMISSILES; i++) { - if (missiles[i] == null) { + for (int i = 0; i < MAXPROJECTILES; i++) { + if (firedProjectiles[i] == null) { return i; } } @@ -126,9 +121,9 @@ private int canFireMissile() { } private void drawMissiles(Graphics brush) { - for (int i = 0; i < MAXMISSILES; i++) { - if (missiles[i] != null) { - missiles[i].paint(brush); + for (int i = 0; i < MAXPROJECTILES; i++) { + if (firedProjectiles[i] != null) { + ((Missile)firedProjectiles[i]).paint(brush); } } } @@ -244,14 +239,8 @@ public Missile(Spaceship spaceship, double rotation) { * missile polygon construction code should go here. */ private static Point[] instantiateShape() { - Point[] square = new Point[4]; - - square[0] = new Point(0, 0); - square[1] = new Point(0, WIDTH); - square[2] = new Point(LENGTH, WIDTH); - square[3] = new Point(LENGTH, 0); - - return square; + Point[] shape = {new Point(0,0), new Point(4,0), new Point(3,1), new Point(3,4), new Point(2,5), new Point(1,4), new Point(1,1), new Point(0,0)}; + return shape; } @@ -331,7 +320,61 @@ public boolean checkShipCollision(Spaceship spaceship) { } -// private class Mine extends Polygon implements Projectile { -// -// } + //comments for this iterator must explicitly state that it mutates as it traverses... + @Override + public Iterator iterator() { + Iterator it = new Iterator() { + int pos = 0; //so basically u call the next method manually and never use a for in enhaced for loop and it iterates to the next null after rmemoving any out of bounds missiles and then u just use the pos instance var to set that null value to a new missile; essentially the next method only serves to iterate the pos instance to the next null elemnt rather than returning the next null elemnt... + public boolean hasNext() { + eraseOutOfBoundsProjectiles(); + if(pos < MAXPROJECTILES && hasNullsAfter(pos)) { + return true; + } + return false; + } + public Projectile next() { + eraseOutOfBoundsProjectiles(); + pos++; + while(firedProjectiles[pos] != null) { + pos++; + } + return firedProjectiles[pos]; + } + + private boolean hasNullsAfter(int pos) { + for(int i = pos; i < firedProjectiles.length; i++) { + if(firedProjectiles[i] == null) { + return true; + } + } + return false; + } + + public void eraseOutOfBoundsProjectiles() { + for(int i = 0; i < firedProjectiles.length; i++) { + if(!firedProjectiles[i].checkCollision(getBorderHitbox())) { //how can I access this method from the space evaders class? + firedProjectiles[i] = null; + } + } + } + }; + return it; + } + + + private class Mine extends Polygon implements Projectile { + public Mine() { + super(instantiateShape(), Spaceship.position, Spaceship.roatation); + this.scalePolygon(7); + } + + private static Point[] instantiateShape() { + Point[] shape = {new Point(1,0), new Point(3,0), new Point(4,1), new Point(4,3), new Point(3,4), new Point(1,4), new Point(0,3), new Point(0,1), new Point(1,0)}; + return shape; + } + + + } + + }