Feature: Add centered hitboxes that can have seperate dimensions.

This commit is contained in:
Jan Philipp Timme 2014-12-09 11:58:31 +01:00
parent be8f7a231f
commit 73f948b4e1
17 changed files with 144 additions and 42 deletions

View File

@ -11,6 +11,7 @@ import de.teamteamteam.spacescooter.entity.enemy.EnemyOne;
import de.teamteamteam.spacescooter.entity.enemy.EnemyThree; import de.teamteamteam.spacescooter.entity.enemy.EnemyThree;
import de.teamteamteam.spacescooter.entity.enemy.EnemyTwo; import de.teamteamteam.spacescooter.entity.enemy.EnemyTwo;
import de.teamteamteam.spacescooter.entity.item.ItemNuke; import de.teamteamteam.spacescooter.entity.item.ItemNuke;
import de.teamteamteam.spacescooter.screen.GameScreen;
/** /**
* This is our main control input source. * This is our main control input source.
@ -126,6 +127,9 @@ public class Keyboard implements KeyListener {
if(e.getKeyCode() == KeyEvent.VK_0) { if(e.getKeyCode() == KeyEvent.VK_0) {
new EnemyBoss(400,400); new EnemyBoss(400,400);
} }
if(e.getKeyCode() == KeyEvent.VK_5) {
GameScreen.getPlayer().setCollide(!GameScreen.getPlayer().canCollide());
}
} }

View File

@ -1,5 +1,9 @@
package de.teamteamteam.spacescooter.entity; package de.teamteamteam.spacescooter.entity;
import java.awt.Color;
import java.awt.Graphics2D;
import de.teamteamteam.spacescooter.brain.GameConfig;
import de.teamteamteam.spacescooter.entity.spi.Collidable; import de.teamteamteam.spacescooter.entity.spi.Collidable;
/** /**
@ -22,6 +26,16 @@ public abstract class CollidableEntity extends Entity implements Collidable {
*/ */
private boolean damaging; private boolean damaging;
/**
* Height of the hitbox.
*/
private int hitboxHeight;
/**
* Width of the hitbox.
*/
private int hitboxWidth;
/** /**
* Default constructor. Initializes sane defaults. * Default constructor. Initializes sane defaults.
@ -32,6 +46,78 @@ public abstract class CollidableEntity extends Entity implements Collidable {
this.setDamaging(true); this.setDamaging(true);
} }
/**
* Get X-Position of the Collidable.
*/
public int getHitboxX() {
return this.getCenteredX() - (this.hitboxWidth / 2);
}
/**
* Get Y-Position of the Collidable.
*/
public int getHitboxY() {
return this.getCenteredY() - (this.hitboxHeight / 2);
}
/**
* Get the width of the Collidable.
*/
public int getHitboxWidth() {
return this.hitboxWidth;
}
/**
* Set the width of the Collidable.
*/
public void setHitboxWidth(int hitboxWidth) {
this.hitboxWidth = hitboxWidth;
}
/**
* Get the height of the Collidable.
*/
public int getHitboxHeight() {
return this.hitboxHeight;
}
/**
* Set the height of the Collidable.
*/
public void setHitboxHeight(int hitboxHeight) {
this.hitboxHeight = hitboxHeight;
}
/**
* Set the dimensions of the CollidableEntities hitbox.
*/
public void setHitboxDimenstions(int width, int height) {
this.hitboxWidth = width;
this.hitboxHeight = height;
}
/**
* Overriding the Entities setImageDimension to (ab)use their width/height
* for reuse in the hitbox.
*/
@Override
public void setImageDimensions(int width, int height) {
super.setImageDimensions(width, height);
this.hitboxWidth = this.getImageWidth();
this.hitboxHeight = this.getImageHeight();
}
/**
* Adding a debugging option to draw the hitbox in red.
*/
@Override
public void paint(Graphics2D g) {
super.paint(g);
if(GameConfig.DEBUG) {
g.setColor(new Color(255,0,0));
g.drawRect(this.getHitboxX(), this.getHitboxY(), this.hitboxWidth, this.hitboxHeight);
}
}
/** /**
* Handle collisions based on what the LivingEntity collided with. * Handle collisions based on what the LivingEntity collided with.

View File

@ -53,12 +53,12 @@ public abstract class Entity implements Updateable, Paintable {
/** /**
* Entity width. * Entity width.
*/ */
private int width; private int imageWidth;
/** /**
* Entity height. * Entity height.
*/ */
private int height; private int imageHeight;
/** /**
* Whether or not the Entity is able to move using transpose. * Whether or not the Entity is able to move using transpose.
@ -106,14 +106,14 @@ public abstract class Entity implements Updateable, Paintable {
* Get the centered X-Position of the Entity. * Get the centered X-Position of the Entity.
*/ */
public int getCenteredX() { public int getCenteredX() {
return this.x + (this.width / 2); return this.x + (this.imageWidth / 2);
} }
/** /**
* Get the centered Y-Position of the Entity. * Get the centered Y-Position of the Entity.
*/ */
public int getCenteredY() { public int getCenteredY() {
return this.y + (this.height / 2); return this.y + (this.imageHeight / 2);
} }
/** /**
@ -138,23 +138,23 @@ public abstract class Entity implements Updateable, Paintable {
/** /**
* Get the Entities width. * Get the Entities width.
*/ */
public int getWidth() { public int getImageWidth() {
return this.width; return this.imageWidth;
} }
/** /**
* Get the Entities height. * Get the Entities height.
*/ */
public int getHeight() { public int getImageHeight() {
return this.height; return this.imageHeight;
} }
/** /**
* Set the entities width and height. * Set the entities width and height.
*/ */
public void setDimensions(int width, int height) { public void setImageDimensions(int width, int height) {
this.width = width; this.imageWidth = width;
this.height = height; this.imageHeight = height;
} }
/** /**
@ -170,7 +170,7 @@ public abstract class Entity implements Updateable, Paintable {
public void setImage(String filename) { public void setImage(String filename) {
this.img = Loader.getBufferedImageByFilename(filename); this.img = Loader.getBufferedImageByFilename(filename);
//set the entities dimensions using the dimensions of the image. //set the entities dimensions using the dimensions of the image.
this.setDimensions(this.img.getWidth(), this.img.getHeight()); this.setImageDimensions(this.img.getWidth(), this.img.getHeight());
} }
/** /**
@ -190,11 +190,12 @@ public abstract class Entity implements Updateable, Paintable {
/** /**
* The default way to paint the Entity. * The default way to paint the Entity.
* Simply draw the Entities image on its current position. * Simply draw the Entities image on its current position.
* Debugging option: draw the image border in green.
*/ */
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
if(GameConfig.DEBUG) { if(GameConfig.DEBUG) {
g.setColor(new Color(255,0,0)); g.setColor(new Color(0,255,0));
g.drawRect(this.x, this.y, this.width, this.height); g.drawRect(this.x, this.y, this.imageWidth, this.imageHeight);
} }
g.drawImage(this.img, this.x, this.y, null); g.drawImage(this.img, this.x, this.y, null);
} }

View File

@ -128,7 +128,7 @@ public class Player extends ShootingEntity implements KeyboardListener {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
if(this.currentCollisionCooldown > 0) { if(this.currentCollisionCooldown > 0) {
g.setColor(Color.RED); g.setColor(Color.RED);
g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.drawRect(this.getX(), this.getY(), this.getImageWidth(), this.getImageHeight());
} }
super.paint(g); super.paint(g);
} }

View File

@ -42,7 +42,7 @@ public abstract class Enemy extends ShootingEntity {
super.update(); super.update();
// enemy has fully left the screen ..to the left! // enemy has fully left the screen ..to the left!
if(this.getX() + this.getWidth() < 0){ if(this.getX() + this.getImageWidth() < 0){
this.remove(); this.remove();
return; return;
} }

View File

@ -44,7 +44,7 @@ public class EnemyBoss extends Enemy{
public void update() { public void update() {
super.update(); super.update();
//Move into the Screen until it fits on X-Axis //Move into the Screen until it fits on X-Axis
if(this.getX() > GameConfig.gameScreenWidth+GameConfig.gameScreenXOffset-this.getWidth()) { if(this.getX() > GameConfig.gameScreenWidth+GameConfig.gameScreenXOffset-this.getImageWidth()) {
this.transpose(-1, 0); this.transpose(-1, 0);
} }
//Move up or down within the GameScreen. //Move up or down within the GameScreen.
@ -52,12 +52,12 @@ public class EnemyBoss extends Enemy{
if(this.getY() == GameConfig.gameScreenYOffset){ if(this.getY() == GameConfig.gameScreenYOffset){
move = 1; move = 1;
} }
if(this.getY() == GameConfig.gameScreenHeight + GameConfig.gameScreenYOffset - this.getHeight()){ if(this.getY() == GameConfig.gameScreenHeight + GameConfig.gameScreenYOffset - this.getImageHeight()){
move = -1; move = -1;
} }
//Randomly spawn minions. //Randomly spawn minions.
if(Random.nextInt(1000) < 5) { if(Random.nextInt(1000) < 5) {
new EnemyBossMinion(GameConfig.gameScreenWidth +GameConfig.gameScreenXOffset - this.getWidth(), this.getY()); new EnemyBossMinion(GameConfig.gameScreenWidth +GameConfig.gameScreenXOffset - this.getImageWidth(), this.getY());
} }
//Randomly fire doubleshots. //Randomly fire doubleshots.
if(Random.nextInt(1000) < 50) { if(Random.nextInt(1000) < 50) {

View File

@ -17,7 +17,7 @@ public class ExplosionOne extends Animation {
"images/explosions/01/explosion7.png" "images/explosions/01/explosion7.png"
}; };
this.configure(images, 10); this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2)); this.setPosition(x - (this.getImageWidth()/2), y - (this.getImageHeight()/2));
SoundSystem.playSound("sounds/bad_explosion1.wav"); SoundSystem.playSound("sounds/bad_explosion1.wav");
} }
} }

View File

@ -26,7 +26,7 @@ public class ExplosionTwo extends Animation {
"images/explosions/02/explosion2_16.png" "images/explosions/02/explosion2_16.png"
}; };
this.configure(images, 10); this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2)); this.setPosition(x - (this.getImageWidth()/2), y - (this.getImageHeight()/2));
SoundSystem.playSound("sounds/bad_explosion2.wav"); SoundSystem.playSound("sounds/bad_explosion2.wav");
} }

View File

@ -38,7 +38,7 @@ public abstract class Item extends CollidableEntity {
*/ */
public void update(){ public void update(){
this.transpose(-1, 0); this.transpose(-1, 0);
if(this.getX() < this.getWidth()) { if(this.getX() < this.getImageWidth()) {
this.remove(); this.remove();
} }
} }

View File

@ -9,6 +9,7 @@ public class StoneOne extends MovingObstacle {
super(x, y); super(x, y);
this.setImage("images/stones/stone01.png"); this.setImage("images/stones/stone01.png");
this.setCollisionDamage(9001); this.setCollisionDamage(9001);
this.setHitboxDimenstions(this.getImageWidth() - 5, this.getImageHeight() - 5);
} }
} }

View File

@ -9,6 +9,7 @@ public class StoneThree extends MovingObstacle {
super(x, y); super(x, y);
this.setImage("images/stones/stone03.png"); this.setImage("images/stones/stone03.png");
this.setCollisionDamage(9001); this.setCollisionDamage(9001);
this.setHitboxDimenstions(this.getImageWidth() - 5, this.getImageHeight() - 5);
} }
} }

View File

@ -9,6 +9,7 @@ public class StoneTwo extends MovingObstacle {
super(x, y); super(x, y);
this.setImage("images/stones/stone02.png"); this.setImage("images/stones/stone02.png");
this.setCollisionDamage(9001); this.setCollisionDamage(9001);
this.setHitboxDimenstions(this.getImageWidth() - 5, this.getImageHeight() - 5);
} }
} }

View File

@ -15,7 +15,6 @@ public class BeamImage extends Entity {
this.setImage("images/shots/beam.png"); this.setImage("images/shots/beam.png");
} }
@Override
public void update() { public void update() {
this.lifetime++; this.lifetime++;
if(this.lifetime>30){ if(this.lifetime>30){

View File

@ -90,8 +90,8 @@ public class Shot extends CollidableEntity {
public void update() { public void update() {
this.transpose(this.direction * this.speed, 0); this.transpose(this.direction * this.speed, 0);
//remove the shot in case it is out of the game window. //remove the shot in case it is out of the game window.
if ((this.getX() + this.getWidth()) < 0 || this.getX() > GameConfig.windowWidth if ((this.getX() + this.getImageWidth()) < 0 || this.getX() > GameConfig.windowWidth
|| (this.getY() + this.getHeight()) < 0 || (this.getY() + this.getImageHeight()) < 0
|| this.getY() > GameConfig.windowHeight) { || this.getY() > GameConfig.windowHeight) {
this.remove(); this.remove();
} }

View File

@ -8,22 +8,32 @@ public interface Collidable {
/** /**
* Get X-Position of the Collidable. * Get X-Position of the Collidable.
*/ */
public int getX(); public int getHitboxX();
/** /**
* Get Y-Position of the Collidable. * Get Y-Position of the Collidable.
*/ */
public int getY(); public int getHitboxY();
/** /**
* Get the width of the Collidable. * Get the width of the Collidable.
*/ */
public int getWidth(); public int getHitboxWidth();
/**
* Set the width of the Collidable.
*/
public void setHitboxWidth(int hitboxWidth);
/** /**
* Get the height of the Collidable. * Get the height of the Collidable.
*/ */
public int getHeight(); public int getHitboxHeight();
/**
* Set the height of the Collidable.
*/
public void setHitboxHeight(int hitboxHeight);
/** /**
* Notify the Collidable that a collision happened. * Notify the Collidable that a collision happened.

View File

@ -30,7 +30,6 @@ public class SecondaryWeaponAmount extends Entity{
} }
} }
@Override
public void update() {} public void update() {}
} }

View File

@ -70,18 +70,18 @@ public class CollisionHandler {
* Check if two Collidables actually collided. * Check if two Collidables actually collided.
*/ */
private static boolean doCollide(Collidable cOne, Collidable cTwo) { private static boolean doCollide(Collidable cOne, Collidable cTwo) {
int x1 = cOne.getX(); int x1 = cOne.getHitboxX();
int x2 = cOne.getX() + cOne.getWidth(); int x2 = cOne.getHitboxX() + cOne.getHitboxWidth();
int x3 = cTwo.getX(); int x3 = cTwo.getHitboxX();
int x4 = cTwo.getX() + cTwo.getWidth(); int x4 = cTwo.getHitboxX() + cTwo.getHitboxWidth();
int total_width = cOne.getWidth() + cTwo.getWidth(); int total_width = cOne.getHitboxWidth() + cTwo.getHitboxWidth();
boolean x_overlap = total_width > Math.abs(Math.max(x2, x4) - Math.min(x1, x3)); boolean x_overlap = total_width > Math.abs(Math.max(x2, x4) - Math.min(x1, x3));
int y1 = cOne.getY(); int y1 = cOne.getHitboxY();
int y2 = cOne.getY() + cOne.getHeight(); int y2 = cOne.getHitboxY() + cOne.getHitboxHeight();
int y3 = cTwo.getY(); int y3 = cTwo.getHitboxY();
int y4 = cTwo.getY() + cTwo.getHeight(); int y4 = cTwo.getHitboxY() + cTwo.getHitboxHeight();
int total_height = cOne.getHeight() + cTwo.getHeight(); int total_height = cOne.getHitboxHeight() + cTwo.getHitboxHeight();
boolean y_overlap = total_height > Math.abs(Math.max(y2, y4) - Math.min(y1, y3)); boolean y_overlap = total_height > Math.abs(Math.max(y2, y4) - Math.min(y1, y3));
return x_overlap && y_overlap; return x_overlap && y_overlap;