Reintroduce CollidableEntity, add Sounds for powerups, explosions and stuff.

This commit is contained in:
Jan Philipp Timme 2014-11-11 13:07:13 +01:00
parent d36e1901e6
commit 755fc2bc27
16 changed files with 123 additions and 86 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,46 @@
package de.teamteamteam.spacescooter.entity;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
/**
* The CollidableEntity provides pure methods to handle collisions.
*/
public abstract class CollidableEntity extends Entity implements Collidable {
/**
* Damage other LivingEntities take when colliding with this.
*/
private int collisionDamage;
/**
* Default constructor.
*/
public CollidableEntity(int x, int y) {
super(x, y);
}
/**
* Handle collisions based on what the LivingEntity collided with.
* Triggers damage calculations for itself only.
* Override to add specific behaviour, e.g. a Player picking up Items.
*/
public abstract void collideWith(Collidable entity);
/**
* Set the collision damage of the Entity.
*/
public void setCollisionDamage(int d) {
this.collisionDamage = d;
}
/**
* Get the current collision damage of the Entity.
*/
public int getCollisionDamage() {
return this.collisionDamage;
}
}

View File

@ -1,8 +1,7 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.Rectangle;
import de.teamteamteam.spacescooter.datastructure.Score;
import de.teamteamteam.spacescooter.entity.enemy.Enemy;
import de.teamteamteam.spacescooter.entity.shot.Shot;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
@ -11,18 +10,13 @@ import de.teamteamteam.spacescooter.gui.Credits;
import de.teamteamteam.spacescooter.utility.GameConfig;
/**
* A LivingEntity is an Entity that is able to take damage.
* It can collide with other Collidable Entities.
* It knows about its collision box, which is based on width and height.
* Also, it contains the generic logic about health points and shield points and
* takes care of damage calculations.
* A LivingEntity is an Entity that is able to take damage and to collide with
* stuff. (See CollidableEntity) It can collide with other Collidable Entities.
* It knows about its collision box, which is based on width and height. Also,
* it contains the generic logic about health points and shield points and takes
* care of damage calculations.
*/
public abstract class LivingEntity extends Entity implements Collidable, Hittable {
/**
* Damage other LivingEntities take when colliding with this.
*/
private int collisionDamage;
public abstract class LivingEntity extends CollidableEntity implements Hittable {
/**
* The LivingEntities health points.
@ -35,7 +29,7 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl
private int shieldPoints;
/**
* The LivingEntities shield points.
* The score points awarded if the LivingEntity dies.
*/
private int ScorePoints;
@ -47,52 +41,30 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl
super(x, y);
}
/**
* Create a Rectangle containing all information to check for
* intersection with other Rectangles.
*/
public Rectangle getCollisionBox() {
return new Rectangle(this.getX(), this.getY(), this.getWidth(),
this.getHeight());
}
/**
* Handle collisions based on what the LivingEntity collided with.
* Triggers damage calculations for itself only.
* Override to add specific behaviour, e.g. a Player picking up Items.
* Handle collisions based on what the LivingEntity collided with. Triggers
* damage calculations for itself only. Override to add specific behaviour,
* e.g. a Player picking up Items.
*/
public void collideWith(Collidable entity) {
if(entity instanceof Shot) {
if (entity instanceof Shot) {
Shot s = (Shot) entity;
if(this instanceof Enemy && s.getDirection() == Shot.LEFT) return;
if(this instanceof Player && s.getDirection() == Shot.RIGHT) return;
if (this instanceof Enemy && s.getDirection() == Shot.LEFT)
return;
if (this instanceof Player && s.getDirection() == Shot.RIGHT)
return;
this.takeDamage(s.getDamageValue());
}
if(entity instanceof Player && (!(this instanceof Player))) {
if (entity instanceof Player && (!(this instanceof Player))) {
Player player = (Player) entity;
this.takeDamage(player.getCollisionDamage());
}
if(entity instanceof Enemy && (!(this instanceof Enemy))) {
if (entity instanceof Enemy && (!(this instanceof Enemy))) {
Enemy enemy = (Enemy) entity;
this.takeDamage(enemy.getCollisionDamage());
}
}
/**
* Set the collision damage of the Entity.
*/
public void setCollisionDamage(int d) {
this.collisionDamage = d;
}
/**
* Get the current collision damage of the Entity.
*/
public int getCollisionDamage() {
return this.collisionDamage;
}
/**
* Determine whether the LivingEntity is still alive.
*/
@ -101,37 +73,39 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl
}
/**
* Process incoming damage by calculating remaining health points
* and shield points.
* Also check for the need of triggering an explosion if dead.
* Process incoming damage by calculating remaining health points and shield
* points. Also check for the need of triggering an explosion if dead.
*/
public void takeDamage(int damage) {
//Skip everything if already dead.
if(this.isAlive() == false) return;
// Skip everything if already dead.
if (this.isAlive() == false)
return;
// TODO: shield and health logic
this.healthPoints -= damage;
if (this.isAlive() == false) {
//Set the correct values for gui indicators
// Set the correct values for gui indicators
this.healthPoints = 0;
this.shieldPoints = 0;
Score.addScore(ScorePoints);
if(this instanceof Enemy){ // Add 1 credit for the shop
Credits.setCredits(Credits.getCredits() + 1);
}
if(GameConfig.DEBUG) System.out.println(this + " ist gestorben. RIP");
if (GameConfig.DEBUG)
System.out.println(this + " ist gestorben. RIP");
this.die();
}
}
/**
* The default way the LivingEntity explodes.
* Override this method for a different explosion behaviour.
* The default way the LivingEntity explodes. Override this method for a
* different explosion behaviour.
*/
public void explode() {}
public void explode() {
}
/**
* The default way the LivingEntity dies.
* Override this method for a different death behaviour.
* The default way the LivingEntity dies. Override this method for a
* different death behaviour.
*/
public void die() {
this.explode();

View File

@ -4,7 +4,7 @@ import java.awt.event.KeyEvent;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.control.KeyboardListener;
import de.teamteamteam.spacescooter.entity.item.Items;
import de.teamteamteam.spacescooter.entity.item.Item;
import de.teamteamteam.spacescooter.entity.shot.Shot;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.sound.SoundSystem;
@ -57,13 +57,15 @@ public class Player extends ShootingEntity implements KeyboardListener {
}
}
/**
* Determine what will happen if a Player collides with an Item.
*/
@Override
public void collideWith(Collidable entity) {
super.collideWith(entity);
if(this instanceof Player && entity instanceof Items){
Items item = (Items) entity;
item.setHealthPoints(0);
item.remove();
if(this instanceof Player && entity instanceof Item){
//Item item = (Item) entity;
//Apply cool item effects here ...
}
}

View File

@ -6,7 +6,7 @@ import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.explosion.MultiExplosion;
import de.teamteamteam.spacescooter.entity.item.Items;
import de.teamteamteam.spacescooter.entity.item.Item;
import de.teamteamteam.spacescooter.screen.Screen;
import de.teamteamteam.spacescooter.utility.GameConfig;
@ -39,7 +39,7 @@ public class EnemyThree extends Enemy{
*/
@Override
public void die() {
if(random.nextInt(10) < 5) Items.create(getX(), getY());
if(random.nextInt(10) < 5) Item.create(getX(), getY());
new EnemyThree(0, 0);
super.die();
}

View File

@ -1,6 +1,7 @@
package de.teamteamteam.spacescooter.entity.explosion;
import de.teamteamteam.spacescooter.entity.Animation;
import de.teamteamteam.spacescooter.sound.SoundSystem;
public class ExplosionOne extends Animation {
@ -17,5 +18,6 @@ public class ExplosionOne extends Animation {
};
this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2));
SoundSystem.playSound("sounds/bad_explosion1.wav");
}
}

View File

@ -1,6 +1,7 @@
package de.teamteamteam.spacescooter.entity.explosion;
import de.teamteamteam.spacescooter.entity.Animation;
import de.teamteamteam.spacescooter.sound.SoundSystem;
public class ExplosionTwo extends Animation {
@ -26,6 +27,7 @@ public class ExplosionTwo extends Animation {
};
this.configure(images, 10);
this.setPosition(x - (this.getWidth()/2), y - (this.getHeight()/2));
SoundSystem.playSound("sounds/bad_explosion2.wav");
}
}

View File

@ -1,33 +1,43 @@
package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
import de.teamteamteam.spacescooter.entity.CollidableEntity;
import de.teamteamteam.spacescooter.entity.Entity;
import de.teamteamteam.spacescooter.entity.LivingEntity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.screen.Screen;
import de.teamteamteam.spacescooter.sound.SoundSystem;
public abstract class Items extends LivingEntity {
public abstract class Item extends CollidableEntity {
private ConcurrentIterator<Entity> entityIterator;
public Items(int x, int y) {
public Item(int x, int y) {
super(x, y);
this.setHealthPoints(1);
this.entityIterator = Screen.currentScreen.createEntityIterator();
}
/**
* If an item collides with a player, play the powerup pickup sound and remove the item.
*/
public void collideWith(Collidable entity) {
if(entity instanceof Player) {
SoundSystem.playSound("sounds/powerup_pickup.wav");
this.remove();
}
}
public void update(){
this.transpose(-1, 0);
if(this.getX() < 0-getWidth()){
if(this.getX() < 0-this.getWidth()){
this.remove();
};
if(!this.isAlive()){
entityIterator.reset();
while(entityIterator.hasNext()) {
Entity e = entityIterator.next();
if(e instanceof Player){
itemCollected((Player) e);
}
entityIterator.reset();
while(entityIterator.hasNext()) {
Entity e = entityIterator.next();
if(e instanceof Player){
itemCollected((Player) e);
}
}
}

View File

@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.entity.Player;
public class TestItem1 extends Items{
public class TestItem1 extends Item {
public TestItem1(int x, int y) {
super(x, y);
@ -13,4 +13,5 @@ public class TestItem1 extends Items{
public void itemCollected(Player player) {
player.setShootDamage(player.getShootDamage()+5);
}
}

View File

@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.entity.Player;
public class TestItem2 extends Items{
public class TestItem2 extends Item {
public static int chance = 2;

View File

@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.entity.Player;
public class TestItem3 extends Items{
public class TestItem3 extends Item {
public static int chance = 3;

View File

@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity.item;
import de.teamteamteam.spacescooter.entity.Player;
public class TestItem4 extends Items{
public class TestItem4 extends Item {
public static int chance = 4;

View File

@ -1,9 +1,10 @@
package de.teamteamteam.spacescooter.entity.shot;
import de.teamteamteam.spacescooter.entity.LivingEntity;
import de.teamteamteam.spacescooter.entity.CollidableEntity;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.entity.enemy.Enemy;
import de.teamteamteam.spacescooter.entity.spi.Collidable;
import de.teamteamteam.spacescooter.sound.SoundSystem;
import de.teamteamteam.spacescooter.utility.GameConfig;
/**
@ -11,7 +12,7 @@ import de.teamteamteam.spacescooter.utility.GameConfig;
* It takes care of its movements, contains information about its
* look and damage, and ends its life once it is out of the visible screen.
*/
public class Shot extends LivingEntity {
public class Shot extends CollidableEntity {
/**
* Valid value for shootDirection parameter.
@ -97,8 +98,7 @@ public class Shot extends LivingEntity {
public void collideWith(Collidable entity) {
if(this.direction == LEFT && entity instanceof Enemy) return;
if(this.direction == RIGHT && entity instanceof Player) return;
super.collideWith(entity);
this.explode();
SoundSystem.playSound("sounds/shot_hit_something.wav");
this.remove();
}