Entity-Api durchstrukturiert und an GameScreen gekoppelt.

This commit is contained in:
Jan Philipp Timme 2014-10-28 14:58:19 +01:00
parent a9bbc264eb
commit 4ebe78b848
17 changed files with 217 additions and 93 deletions

View File

@ -15,7 +15,8 @@ public abstract class Background extends Entity {
Background.backgrounds = new ArrayList<Background>();
}
public Background() {
public Background(int x, int y) {
super(x, y);
Background.backgrounds.add(this);
}

View File

@ -11,6 +11,10 @@ import de.teamteamteam.spacescooter.entity.Player;
public class StarBackground extends Background {
public StarBackground(int x, int y) {
super(x, y);
}
private static BufferedImage img;
static {
try {

View File

@ -0,0 +1,5 @@
package de.teamteamteam.spacescooter.entity;
public interface Collidable {
public void collideWith(Collidable entity);
}

View File

@ -1,39 +1,18 @@
package de.teamteamteam.spacescooter.entity;
public abstract class CollidableEntity extends Entity {
public abstract class CollidableEntity extends Entity implements Collidable{
public CollidableEntity(int x, int y) {
super(x, y);
}
/**
* Position of the Entity
*/
protected int x;
protected int y;
/**
* Dimensions of the Entity
*/
protected int width;
protected int height;
/**
* Health Points of the Object
*/
protected int heakthPoints;
/**
* Shieldpoints of the Object
*/
protected int shieldPoints;
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.width;
}

View File

@ -1,20 +1,27 @@
package de.teamteamteam.spacescooter.entity;
public abstract class Enemy extends CollidableEntity {
public abstract class Enemy extends ShootingEntity {
public Enemy(int x, int y) {
super(x, y);
}
protected String name;
protected boolean willShoot;
@Override
public void update() {
super.update();
if(willShoot)
this.shoot();
}
protected abstract void shoot();
public void collideWith(Collidable e) {
if(e instanceof Shot) {
Shot s = (Shot) e;
//TODO: Expand with shield logic and stuff.
this.healthPoints -= s.getDamageValue();
}
}
}

View File

@ -0,0 +1,21 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.Graphics;
public class EnemyOne extends Enemy {
public EnemyOne(int x, int y) {
super(x, y);
}
public void paint(Graphics g) {
// TODO Auto-generated method stub
}
@Override
public void update() {
super.update();
}
}

View File

@ -1,24 +1,29 @@
package de.teamteamteam.spacescooter.entity;
import java.util.ArrayList;
public abstract class Entity implements Updateable, Paintable {
public static ArrayList<Entity> entities = null;
/**
* We need to initialize the ArrayList, so the EntityUpdateThread won't beat us.
* Entity position
*/
static {
Entity.entities = new ArrayList<Entity>();
}
protected int x;
protected int y;
/**
* Constructor.
* All entities are within a static array list for our convenience.
*/
public Entity() {
Entity.entities.add(this);
public Entity(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}

View File

@ -0,0 +1,9 @@
package de.teamteamteam.spacescooter.entity;
public interface Hittable {
public int getHealthPoints();
public int getShieldPoints();
}

View File

@ -0,0 +1,15 @@
package de.teamteamteam.spacescooter.entity;
public abstract class LivingEntity extends CollidableEntity {
public LivingEntity(int x, int y) {
super(x, y);
}
protected int healthPoints;
protected int shieldPoints;
public boolean isAlive() {
return healthPoints > 0;
}
}

View File

@ -10,29 +10,30 @@ import javax.imageio.ImageIO;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.utility.GameConfig;
public class Player extends Entity {
public class Player extends ShootingEntity {
private int x;
private int y;
private static BufferedImage img;
static {
try {
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/nyancat.png"));
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/ship.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Player() {
super();
public Player(int x, int y) {
super(x, y);
this.x = 200;
this.y = 300;
this.shootDelay = 40;
this.shootSpawnX = 32;
this.shootSpawnY = 16;
}
public void update() {
super.update();
int off = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) {
this.y -= off;
@ -46,11 +47,18 @@ public class Player extends Entity {
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (GameConfig.windowWidth - Player.img.getWidth())) {
this.x += off;
}
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {
this.shoot();
}
}
public void paint(Graphics g) {
g.drawImage(img, this.x, this.y, null);
}
public void collideWith(Collidable entity) {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,28 @@
package de.teamteamteam.spacescooter.entity;
import de.teamteamteam.spacescooter.screen.Screen;
public abstract class ShootingEntity extends LivingEntity {
protected int shootDelay;
protected int currentShootDelay;
protected int shootSpawnX;
protected int shootSpawnY;
public ShootingEntity(int x, int y) {
super(x, y);
this.shootDelay = 10;
this.currentShootDelay = 0;
}
public void update() {
if(this.currentShootDelay > 0) this.currentShootDelay--;
}
protected void shoot() {
if(this.currentShootDelay == 0) {
Screen.currentScreen.addEntity(new SingleShot(this.x + this.shootSpawnX, this.y + this.shootSpawnY));
this.currentShootDelay = this.shootDelay;
}
}
}

View File

@ -0,0 +1,26 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.image.BufferedImage;
public abstract class Shot extends CollidableEntity {
protected int damageValue;
protected int collisionCount;
public Shot(int x, int y, BufferedImage img) {
super(x - img.getWidth() / 2, y - img.getHeight() / 2);
this.collisionCount = 1;
}
public void collideWith(Collidable entity) {
this.collisionCount--;
if(this.collisionCount == 0) {
//scoot is over
}
}
public int getDamageValue() {
return this.damageValue;
}
}

View File

@ -0,0 +1,36 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SingleShot extends Shot {
private static BufferedImage img;
static {
try {
img = ImageIO.read(Player.class.getClassLoader().getResourceAsStream("images/shot.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public SingleShot(int x, int y) {
super(x, y, SingleShot.img);
}
public void update() {
this.x += 2;
}
public void paint(Graphics g) {
g.drawImage(SingleShot.img, this.x, this.y, SingleShot.img.getWidth(), SingleShot.img.getHeight(), null);
}
}

View File

@ -1,27 +0,0 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.Color;
import java.awt.Graphics;
public class TestEntity extends Entity {
private Color color;
public int x;
public int y;
public TestEntity() {
super();
this.color = new Color(255, 0, 0);
}
public void paint(Graphics g) {
g.setColor(this.color);
g.fillRect(300, 300, 10, 10);
}
public void update() {
}
}

View File

@ -3,35 +3,39 @@ package de.teamteamteam.spacescooter.screen;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedList;
import de.teamteamteam.spacescooter.background.StarBackground;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.TestEntity;
public class GameScreen extends Screen {
public GameScreen(Screen parent) {
super(parent);
this.entities.add(new StarBackground());
this.entities.add(new Player());
this.entities.add(new TestEntity());
this.entities.add(new StarBackground(0, 0));
this.entities.add(new Player(200, 300));
}
@Override
protected void paint(Graphics g) {
for(Entity e : this.entities) {
e.paint(g);
LinkedList<Entity> list = new LinkedList<Entity>(this.entities);
Iterator<Entity> i = list.iterator();
while (i.hasNext()) {
i.next().paint(g);
}
}
@Override
protected void update() {
for(Entity e : this.entities) {
e.update();
LinkedList<Entity> list = new LinkedList<Entity>(this.entities);
Iterator<Entity> i = list.iterator();
while (i.hasNext()) {
i.next().update();
}
if(Keyboard.isKeyDown(KeyEvent.VK_ESCAPE)) {
if (Keyboard.isKeyDown(KeyEvent.VK_ESCAPE)) {
this.parent.setOverlay(new MainMenuScreen(this.parent));
}
}

View File

@ -1,24 +1,26 @@
package de.teamteamteam.spacescooter.screen;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.LinkedList;
import de.teamteamteam.spacescooter.entity.Entity;
public abstract class Screen {
public static Screen currentScreen;
protected Screen overlay;
protected Screen parent;
protected ArrayList<Entity> entities;
protected LinkedList<Entity> entities;
public Screen(Screen parent) {
this.overlay = null;
this.parent = parent;
this.entities = new ArrayList<Entity>();
this.entities = new LinkedList<Entity>();
}
protected void addEntity(Entity e) {
public void addEntity(Entity e) {
this.entities.add(e);
}
@ -44,5 +46,6 @@ public abstract class Screen {
public void setOverlay(Screen screen) {
this.overlay = screen;
Screen.currentScreen = screen;
}
}

View File

@ -34,7 +34,7 @@ public class GraphicsSettings {
this.height = dm.getHeight();
this.width = dm.getWidth();
//System.out.println("Display Mode " + i + ": " + this.width + "x" + this.height+ "@" + this.refreshRate + "Hz, " + this.bitDepth + " bit");
System.out.println("Display Mode " + i + ": " + this.width + "x" + this.height+ "@" + this.refreshRate + "Hz, " + this.bitDepth + " bit");
}
}