Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed the errors and tweaked the spawning system, hopefully it should
work better now
  • Loading branch information
jdm13003 committed Apr 20, 2016
1 parent 4ad74b6 commit 3e1e64e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/main/Controller.java
Expand Up @@ -34,6 +34,11 @@ public class Controller {


} }
} }
public void createSD(int SDcount){
for(int n = 0; n < SDcount; n++){
addEntity(new StarDestroyer(810, r.nextInt(400), skin, this, game));
}
}
public void createStarDestroyer(int SDcount){ public void createStarDestroyer(int SDcount){
for(int i = 0; i < SDcount; i++){ for(int i = 0; i < SDcount; i++){
addEntity(new StarDestroyer(810, r.nextInt(400), skin, this, game)); addEntity(new StarDestroyer(810, r.nextInt(400), skin, this, game));
Expand Down
15 changes: 15 additions & 0 deletions src/main/EnemyFighter.java
Expand Up @@ -14,6 +14,7 @@ public class EnemyFighter extends GameObject implements EntityTypeB{
private Game game; private Game game;
private Controller controller; private Controller controller;
private double speed = 0.3 + 3 * r.nextDouble() ; private double speed = 0.3 + 3 * r.nextDouble() ;
public double is_shooting = r.nextInt(60);


public EnemyFighter(double x, double y, Skins skin, Controller c, Game game){ public EnemyFighter(double x, double y, Skins skin, Controller c, Game game){
super(x,y); super(x,y);
Expand All @@ -24,9 +25,23 @@ public class EnemyFighter extends GameObject implements EntityTypeB{
public double getX(){ public double getX(){
return x; return x;
} }
public void shoot(){
if(is_shooting == 0){
controller.addEntity(new EnemyLaser(x, y, skin, game));
is_shooting++;
}
else if(is_shooting == 190){
is_shooting = 0;
}
else{
is_shooting++;
}
}



public void tick(){ public void tick(){
x -= speed; x -= speed;
shoot();
for(int n = 0; n < game.ea.size(); n++){ for(int n = 0; n < game.ea.size(); n++){
EntityTypeA tempa = game.ea.get(n); EntityTypeA tempa = game.ea.get(n);
if(GamePhysics.Collision(this, tempa)){ if(GamePhysics.Collision(this, tempa)){
Expand Down
34 changes: 21 additions & 13 deletions src/main/Game.java
Expand Up @@ -71,16 +71,17 @@ public class Game extends Canvas implements Runnable{
} }
addKeyListener(new KeyboardInput(this)); addKeyListener(new KeyboardInput(this));
tp = new Skins(this); tp = new Skins(this);
p = new Player(200, 200, tp, this); c = new Controller(tp, this);

if(numberOfPlayers == 2){ if(numberOfPlayers == 2){
p2 = new Player2(300, 300, tp, this); p2 = new Player2(300, 300, tp, this, c);
} }
c = new Controller(tp, this); p = new Player(200, 200, tp, this, c);

ea = c.getEntityA(); ea = c.getEntityA();
eb = c.getEntityB(); eb = c.getEntityB();
ec = c.getEntityC(); ec = c.getEntityC();
ed = c.getEntityD(); ed = c.getEntityD();
c.createStarDestroyer(1);
c.createEnemy(enemy_count); c.createEnemy(enemy_count);
} }


Expand Down Expand Up @@ -164,10 +165,17 @@ public class Game extends Canvas implements Runnable{
p2.tick(); p2.tick();
} }
if(kills >= enemy_count){ if(kills >= enemy_count){
if (enemy_count == 10){
c.createSD(SDcount);
enemy_count = 2;
SDcount++;
c.createEnemy(enemy_count);
}
enemy_count +=1; enemy_count +=1;
kills = 0; kills = 0;
c.createEnemy(enemy_count); c.createEnemy(enemy_count);
} }

} }
private void render(){ private void render(){
BufferStrategy bufferstrat = this.getBufferStrategy(); BufferStrategy bufferstrat = this.getBufferStrategy();
Expand All @@ -193,14 +201,14 @@ public class Game extends Canvas implements Runnable{
public void keyPressed(KeyEvent e){ public void keyPressed(KeyEvent e){
int key = e.getKeyCode(); int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT){ if(key == KeyEvent.VK_RIGHT){
p.setVelX(5); p.setVelX(6);


} else if(key == KeyEvent.VK_LEFT){ } else if(key == KeyEvent.VK_LEFT){
p.setVelX(-5); p.setVelX(-6);
} else if(key == KeyEvent.VK_DOWN){ } else if(key == KeyEvent.VK_DOWN){
p.setVelY(5); p.setVelY(6);
} else if(key == KeyEvent.VK_UP){ } else if(key == KeyEvent.VK_UP){
p.setVelY(-5); p.setVelY(-6);
} else if(key == KeyEvent.VK_L ){ } else if(key == KeyEvent.VK_L ){
if(is_shooting == 3){ if(is_shooting == 3){
is_shooting = 0; is_shooting = 0;
Expand All @@ -209,15 +217,15 @@ public class Game extends Canvas implements Runnable{
c.addEntity(new Laser(p.getX(),p.getY(), tp, this));} c.addEntity(new Laser(p.getX(),p.getY(), tp, this));}
is_shooting++; is_shooting++;
} else if(key == KeyEvent.VK_A){ } else if(key == KeyEvent.VK_A){
p2.setVelX(-5); p2.setVelX(-4);
} else if(key == KeyEvent.VK_D){ } else if(key == KeyEvent.VK_D){
p2.setVelX(5); p2.setVelX(4);
}else if(key == KeyEvent.VK_S){ }else if(key == KeyEvent.VK_S){
p2.setVelY(5); p2.setVelY(4);
}else if(key == KeyEvent.VK_W){ }else if(key == KeyEvent.VK_W){
p2.setVelY(-5); p2.setVelY(-4);
}else if(key == KeyEvent.VK_SPACE ){ }else if(key == KeyEvent.VK_SPACE ){
if(is_shooting2 == 3){ if(is_shooting2 == 4){
is_shooting2 = 0; is_shooting2 = 0;
} }
if(is_shooting2 == 0){ if(is_shooting2 == 0){
Expand Down
10 changes: 9 additions & 1 deletion src/main/GamePhysics.java
@@ -1,10 +1,10 @@
package main; package main;


import java.util.LinkedList;


import entity.EntityTypeA; import entity.EntityTypeA;
import entity.EntityTypeB; import entity.EntityTypeB;
import entity.EntityTypeC; import entity.EntityTypeC;
import entity.EntityTypeD;


public class GamePhysics { public class GamePhysics {
public static boolean Collision(EntityTypeA enta, EntityTypeB bb){ public static boolean Collision(EntityTypeA enta, EntityTypeB bb){
Expand All @@ -31,4 +31,12 @@ public class GamePhysics {


return false; return false;
} }
public static boolean Collision(EntityTypeA entA, EntityTypeD dd){

if(entA.getBounds().intersects(dd.getBounds())){
return true;
}

return false;
}
} }
35 changes: 25 additions & 10 deletions src/main/Player.java
Expand Up @@ -3,26 +3,30 @@ import java.awt.Graphics;
import java.awt.Rectangle; import java.awt.Rectangle;


import entity.EntityTypeA; import entity.EntityTypeA;
import entity.EntityTypeD;


public class Player extends GameObject implements EntityTypeA{ public class Player extends GameObject implements EntityTypeA{

private double velX = 0; private double velX = 0;
private double velY = 0; private double velY = 0;

private int health = 3;



private Skins skin; private Skins skin;
private Game game; private Game game;

private Controller controller;
public Player(double x, double y, Skins skin, Game game){
public Player(double x, double y, Skins skin, Game game, Controller controller){
super(x,y); super(x,y);
this.skin = skin; this.skin = skin;
this.game = game; this.game = game;
this.controller = controller;
} }

public void tick(){ public void tick(){
x += velX; x += velX;
y += velY; y += velY;

if (x <= 0 ){ if (x <= 0 ){
x = 0; x = 0;
} }
Expand All @@ -35,9 +39,20 @@ public class Player extends GameObject implements EntityTypeA{
if (y >= 536){ if (y >= 536){
y = 536; y = 536;
} }

for(int n = 0; n < game.ed.size(); n++){
EntityTypeD tempa = game.ed.get(n);
if(GamePhysics.Collision(this, tempa)){
if(health == 1){
controller.removeEntity(this);}
else{
health--;
controller.removeEntity(tempa);
}
}
}

} }

public void render(Graphics g){ public void render(Graphics g){
g.drawImage(skin.player, (int)x, (int)y, null); g.drawImage(skin.player, (int)x, (int)y, null);
} }
Expand All @@ -52,7 +67,7 @@ public class Player extends GameObject implements EntityTypeA{
} }
public void setY(double y){ public void setY(double y){
this.y = y; this.y = y;

} }
public void setVelX(double velX){ public void setVelX(double velX){
this.velX = velX; this.velX = velX;
Expand Down
17 changes: 16 additions & 1 deletion src/main/Player2.java
Expand Up @@ -3,6 +3,7 @@ import java.awt.Graphics;
import java.awt.Rectangle; import java.awt.Rectangle;


import entity.EntityTypeA; import entity.EntityTypeA;
import entity.EntityTypeD;


public class Player2 extends GameObject implements EntityTypeA{ public class Player2 extends GameObject implements EntityTypeA{


Expand All @@ -12,11 +13,14 @@ public class Player2 extends GameObject implements EntityTypeA{


private Skins skin; private Skins skin;
private Game game; private Game game;
private Controller controller;
private int health = 4;


public Player2(double x, double y, Skins skin, Game game){ public Player2(double x, double y, Skins skin, Game game, Controller controller){
super(x,y); super(x,y);
this.skin = skin; this.skin = skin;
this.game = game; this.game = game;
this.controller = controller;
} }


public void tick(){ public void tick(){
Expand All @@ -35,6 +39,17 @@ public class Player2 extends GameObject implements EntityTypeA{
if (y >= 536){ if (y >= 536){
y = 536; y = 536;
} }
for(int n = 0; n < game.ed.size(); n++){
EntityTypeD tempa = game.ed.get(n);
if(GamePhysics.Collision(this, tempa)){
if(health == 1){
controller.removeEntity(this);}
else{
health--;
controller.removeEntity(tempa);
}
}
}


} }


Expand Down
6 changes: 3 additions & 3 deletions src/main/StarDestroyer.java
Expand Up @@ -36,8 +36,8 @@ public class StarDestroyer extends GameObject implements EntityTypeC{
if(GamePhysics.Collision(this, tempa)){ if(GamePhysics.Collision(this, tempa)){
if(health ==1) if(health ==1)
{controller.removeEntity(this); {controller.removeEntity(this);
game.setKills(game.getKills()+1);} }
else{health--;} else{health--;}
controller.removeEntity(tempa); controller.removeEntity(tempa);
} }
} }
Expand All @@ -48,7 +48,7 @@ public class StarDestroyer extends GameObject implements EntityTypeC{
controller.addEntity(new EnemyLaser(x, y, skin, game)); controller.addEntity(new EnemyLaser(x, y, skin, game));
is_shooting++; is_shooting++;
} }
else if(is_shooting == r.nextDouble()* 60 + 60){ else if(is_shooting == 100){
is_shooting = 0; is_shooting = 0;
} }
else{ else{
Expand Down

0 comments on commit 3e1e64e

Please sign in to comment.