Comment Entity, make x and y private, add transpose method for easy movement.

This commit is contained in:
Jan Philipp Timme 2014-11-07 20:00:07 +01:00
parent 496e6792c3
commit f0a9e8a6ad
7 changed files with 97 additions and 39 deletions

View File

@ -8,68 +8,126 @@ import de.teamteamteam.spacescooter.screen.Screen;
import de.teamteamteam.spacescooter.utility.GameConfig;
import de.teamteamteam.spacescooter.utility.Loader;
/**
* The basic Entity class where all the logic of objects begins.
* Every Entity is Updateable and Paintable.
* It knows about its X/Y Position and its default image.
* Also, it is able to paint itself in the default way.
*/
public abstract class Entity implements Updateable, Paintable {
/**
* Entity position
* Entity position.
*/
private int x;
private int y;
/**
* Entity width and height.
*/
private int width;
private int height;
/**
* Internal reference to the entities image.
*/
protected int x;
protected int y;
private BufferedImage img;
/**
* Constructor.
* All entities are within a static array list for our convenience.
*/
public Entity(int x, int y) {
this.x = x;
this.y = y;
this.setPosition(x, y);
Screen.currentScreen.addEntity(this);
}
/**
* Get the X-Position of the Entity.
*/
public int getX() {
return this.x;
}
/**
* Get the Y-Position of the Entity.
*/
public int getY() {
return this.y;
}
public int getWidth() {
if(this.img == null) return 0; //no image -> nothing visible -> no width
return this.img.getWidth();
}
public int getHeight() {
if(this.img == null) return 0; //no image -> nothing visible -> no height
return this.img.getHeight();
}
/**
* Set the Entities new absolute position.
*/
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Moves the entity by the given X/Y deltas.
* Example: X = X + x_delta, Y = Y + y_delta.
*/
public void transpose(int x_delta, int y_delta) {
this.x += x_delta;
this.y += y_delta;
}
/**
* Get the Entities width.
*/
public int getWidth() {
return this.width;
}
/**
* Get the Entities height.
*/
public int getHeight() {
return this.height;
}
/**
* Set the entities width and height.
*/
public void setDimensions(int width, int height) {
this.width = width;
this.height = height;
}
/**
* Gets the Entities BufferedImage.
*/
public BufferedImage getImage() {
return this.img;
}
/**
* Sets the Entities BufferedImage by fetching it through the Loader.
*/
public void setImage(String filename) {
this.img = Loader.getBufferedImageByFilename(filename);
//set the entities dimensions using the dimensions of the image.
this.setDimensions(this.img.getWidth(), this.img.getHeight());
}
/**
* The default way to paint the Entity.
* Simply draw the Entities image on its current position.
*/
public void paint(Graphics2D g) {
//DEBUG ONLY
if(GameConfig.DEBUG) {
g.setColor(new Color(255,0,0));
g.drawRect(this.x, this.y, this.getWidth(), this.getHeight());
g.drawRect(this.x, this.y, this.width, this.height);
}
g.drawImage(this.img, this.x, this.y, null);
}
/**
* Removes entity from the game
* Removes entity from the game by telling the current Screen
* to remove it from its list.
*/
public void remove() {
Screen.currentScreen.removeEntity(this);

View File

@ -27,7 +27,7 @@ public class HealthBar extends Entity {
Graphics2D grpahic = (Graphics2D) g;
this.g = grpahic;
this.g.setColor(new Color(0,255,0));
this.g.fillRect(this.x, this.y, this.width, this.height);
this.g.fillRect(this.getX(), this.getY(), this.width, this.height);
}
public void update() {

View File

@ -94,7 +94,7 @@ public abstract class LivingEntity extends Entity implements Collidable {
}
public void explode() {
new Explosion(this.x, this.y);
new Explosion(this.getX(), this.getY());
}
public void setHealthPoints(int hp) {

View File

@ -34,18 +34,18 @@ public class Player extends ShootingEntity implements KeyboardListener {
public void update() {
if(this.canMove) {
super.update();
int off = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.y > 0) {
this.y -= off;
int offset = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.getY() > 0) {
this.transpose(0, -1 * offset);
}
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.y < (GameConfig.windowHeight - this.getImage().getHeight())) {
this.y += off;
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.getY() < (GameConfig.windowHeight - this.getImage().getHeight())) {
this.transpose(0, offset);
}
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.x > 0) {
this.x -= off;
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.getX() > 0) {
this.transpose(-1 * offset, 0);
}
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.x < (GameConfig.windowWidth - this.getImage().getWidth())) {
this.x += off;
if(Keyboard.isKeyDown(KeyEvent.VK_RIGHT) && this.getX() < (GameConfig.windowWidth - this.getImage().getWidth())) {
this.transpose(offset, 0);
}
//continuous fire takes place here
if(Keyboard.isKeyDown(KeyEvent.VK_SPACE)) {

View File

@ -36,8 +36,8 @@ public abstract class ShootingEntity extends LivingEntity {
public void createShot() {
new Shot(
this.x + this.shootSpawnX,
this.y + this.shootSpawnY,
this.getX() + this.shootSpawnX,
this.getY() + this.shootSpawnY,
this.shootDirection,
this.shootSpeed,
this.damageValue,

View File

@ -48,10 +48,10 @@ public class EnemyThree extends Enemy{
Entity entity = i.next();
if(entity instanceof Player){
Player player = (Player) entity;
if(this.y < player.getY()){
if(this.getY() < player.getY()){
this.newY += ySpeed;
this.setPosition(this.getX(), (int) newY);
}else if(this.y > player.getY()){
}else if(this.getY() > player.getY()){
this.newY -= ySpeed;
this.setPosition(this.getX(), (int) newY);
}

View File

@ -46,7 +46,7 @@ public class Shot extends LivingEntity {
this.speed = shootSpeed;
this.damageValue = damageValue;
this.setImage(filename);
this.setPosition(this.x - this.getImage().getWidth() / 2, this.y - this.getImage().getHeight() / 2);
this.setPosition(this.getX() - this.getImage().getWidth() / 2, this.getY() - this.getImage().getHeight() / 2);
}
/**
@ -68,11 +68,11 @@ public class Shot extends LivingEntity {
* Remove the shot once out of the visible area.
*/
public void update() {
this.x += this.direction * this.speed;
this.transpose(this.direction * this.speed, 0);
//remove the shot in case it is out of the game window.
if ((this.x + this.getWidth()) < 0 || this.x > GameConfig.windowWidth
|| (this.y + this.getHeight()) < 0
|| this.y > GameConfig.windowHeight) {
if ((this.getX() + this.getWidth()) < 0 || this.getX() > GameConfig.windowWidth
|| (this.getY() + this.getHeight()) < 0
|| this.getY() > GameConfig.windowHeight) {
this.remove();
}
}