From 8e82614874bff96b0a96b8e503bd7e9ee3e15c4f Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 30 Nov 2014 14:11:21 +0100 Subject: [PATCH] Feature: Player now gains small immunity after an impact. --- .../spacescooter/entity/CollidableEntity.java | 20 +++++++ .../spacescooter/entity/Entity.java | 15 ++++- .../spacescooter/entity/Player.java | 59 +++++++++++++++---- .../spacescooter/entity/spi/Collidable.java | 11 ++++ .../spacescooter/screen/MainMenuScreen.java | 26 ++++---- .../utility/CollisionHandler.java | 13 ++-- 6 files changed, 113 insertions(+), 31 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java index 5984cc0..917f8f0 100644 --- a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java @@ -12,12 +12,18 @@ public abstract class CollidableEntity extends Entity implements Collidable { */ private int collisionDamage; + /** + * Whether or not this CollidableEntity can collide. + */ + private boolean canCollide; + /** * Default constructor. */ public CollidableEntity(int x, int y) { super(x, y); + this.setCollide(true); } @@ -28,6 +34,20 @@ public abstract class CollidableEntity extends Entity implements Collidable { */ public abstract void collideWith(Collidable entity); + /** + * Tell whether the Collidable is currently + * an active Collidable. + */ + public boolean canCollide() { + return this.canCollide; + } + + /** + * Set whether the Collidable is currently active. + */ + public void setCollide(boolean canCollide) { + this.canCollide = canCollide; + } /** * Set the collision damage of the Entity. diff --git a/src/de/teamteamteam/spacescooter/entity/Entity.java b/src/de/teamteamteam/spacescooter/entity/Entity.java index c362d39..b1cb35d 100644 --- a/src/de/teamteamteam/spacescooter/entity/Entity.java +++ b/src/de/teamteamteam/spacescooter/entity/Entity.java @@ -62,17 +62,24 @@ public abstract class Entity implements Updateable, Paintable { * Whether or not the Entity is able to move using transpose. */ private boolean canMove; + /** * Internal reference to the entities image. */ private BufferedImage img; + /** + * Until the Entity is disposed through remove(), this is false. + */ + private boolean disposed; + /** * Constructor. * All entities are within a static array list for our convenience. */ public Entity(int x, int y) { + this.disposed = false; this.setPosition(x, y); this.setCanMove(true); Screen.currentScreen.addEntity(this); @@ -195,6 +202,12 @@ public abstract class Entity implements Updateable, Paintable { * to remove it from its list. */ public void remove() { - Screen.currentScreen.removeEntity(this); + if(this.disposed) { + return; + } + else{ + Screen.currentScreen.removeEntity(this); + this.disposed = true; + } } } diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index 11da017..72d3305 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -1,12 +1,13 @@ package de.teamteamteam.spacescooter.entity; +import java.awt.Color; +import java.awt.Graphics2D; import java.awt.event.KeyEvent; import de.teamteamteam.spacescooter.brain.GameConfig; import de.teamteamteam.spacescooter.brain.PlayerSession; import de.teamteamteam.spacescooter.control.Keyboard; import de.teamteamteam.spacescooter.control.KeyboardListener; -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; @@ -17,33 +18,50 @@ import de.teamteamteam.spacescooter.sound.SoundSystem; public class Player extends ShootingEntity implements KeyboardListener { /** - * the Players Keyboard + * Keyboard instance used to register on for KeyboardEvents. */ - private Keyboard keyboard = null; + private Keyboard keyboard; /** - * the Players Rocket Ammunition + * Rocket Ammunition */ - private int rocketAmount = 10; + private int rocketAmount; /** - * the Players Beam Ammunition + * Beam Ammunition */ - private int beamAmount = 10; + private int beamAmount; + /** + * Cooldown countdown value to use in case + * the Player got hit. + */ + private int collisionCooldown; + + /** + * The actual countdown variable used to enforce the + * collision "timeout". + * (Player gets hit, blinks a while, gets solid again) + */ + private int currentCollisionCooldown; + /** * Constructor for initializing the Player on the GameScreen */ public Player(int x, int y) { super(x, y); + this.rocketAmount = 10; + this.beamAmount = 10; + this.collisionCooldown = 150; + this.currentCollisionCooldown = 0; this.setImage("images/ship.png"); this.setPrimaryShotImage("images/shots/laser_blue.png"); this.setShootDelay(20); this.setShootSpawn(50, 16); this.setShootDirection(Shot.RIGHT); this.setShootSpeed(10); - this.setCollisionDamage(10); + this.setCollisionDamage(5); this.setScore(0); this.setHealthPoints(PlayerSession.getShipHealthPoints()); this.setMaximumHealthPoints(PlayerSession.getShipHealthPoints()); @@ -67,6 +85,13 @@ public class Player extends ShootingEntity implements KeyboardListener { public void update() { if(this.canMove() == false) return; super.update(); + //Collision cooldown handling + if(this.currentCollisionCooldown > 0) { + this.currentCollisionCooldown--; + if(this.currentCollisionCooldown == 0) { + this.setCollide(true); + } + } int offset = 3; if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.getY() > 51) { this.transpose(0, -1 * offset); @@ -91,6 +116,18 @@ public class Player extends ShootingEntity implements KeyboardListener { this.shootBeam(); } } + + /** + * Override paint method for custom effects + */ + @Override + public void paint(Graphics2D g) { + if(this.currentCollisionCooldown > 0) { + g.setColor(Color.RED); + g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); + } + super.paint(g); + } /** * Determine what will happen if a Player collides with an Item. @@ -98,10 +135,8 @@ public class Player extends ShootingEntity implements KeyboardListener { @Override public void collideWith(Collidable entity) { super.collideWith(entity); - if(this instanceof Player && entity instanceof Item){ - //Item item = (Item) entity; - //Apply cool item effects here ... - } + this.setCollide(false); + this.currentCollisionCooldown = this.collisionCooldown; } /** diff --git a/src/de/teamteamteam/spacescooter/entity/spi/Collidable.java b/src/de/teamteamteam/spacescooter/entity/spi/Collidable.java index f3888f7..1b71ce7 100644 --- a/src/de/teamteamteam/spacescooter/entity/spi/Collidable.java +++ b/src/de/teamteamteam/spacescooter/entity/spi/Collidable.java @@ -29,4 +29,15 @@ public interface Collidable { * Notify the Collidable that a collision happened. */ public void collideWith(Collidable entity); + + /** + * Tell whether the Collidable is currently + * an active Collidable. + */ + public boolean canCollide(); + + /** + * Set whether the Collidable is currently active. + */ + public void setCollide(boolean canCollide); } diff --git a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java index 587d46c..f41a451 100644 --- a/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/MainMenuScreen.java @@ -98,19 +98,19 @@ public class MainMenuScreen extends Screen { } else animationStatus = 2; } else if(animationStatus == 2) { switch (menuPoint) { - case 0: - this.parent.setOverlay(new GameScreen(this.parent, "levels/test.level")); - break; - case 1: - this.parent.setOverlay(new ShopScreen(this.parent)); - break; - case 2: - break; - case 3: - break; - case 4: - System.exit(0); - break; + case 0: + this.parent.setOverlay(new GameScreen(this.parent, "levels/test.level")); + break; + case 1: + this.parent.setOverlay(new ShopScreen(this.parent)); + break; + case 2: + break; + case 3: + break; + case 4: + System.exit(0); + break; } } } diff --git a/src/de/teamteamteam/spacescooter/utility/CollisionHandler.java b/src/de/teamteamteam/spacescooter/utility/CollisionHandler.java index 40d07b8..eaa34be 100644 --- a/src/de/teamteamteam/spacescooter/utility/CollisionHandler.java +++ b/src/de/teamteamteam/spacescooter/utility/CollisionHandler.java @@ -4,6 +4,7 @@ import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator; import de.teamteamteam.spacescooter.entity.Entity; import de.teamteamteam.spacescooter.entity.Player; import de.teamteamteam.spacescooter.entity.enemy.Enemy; +import de.teamteamteam.spacescooter.entity.shot.Shot; import de.teamteamteam.spacescooter.entity.spi.Collidable; import de.teamteamteam.spacescooter.screen.Screen; @@ -28,20 +29,22 @@ public class CollisionHandler { iteratorOne.reset(); while(iteratorOne.hasNext()) { Entity entityOne = iteratorOne.next(); - //Only check Player and Enemy for the active side of a collision. - if(!((entityOne instanceof Player) || (entityOne instanceof Enemy))) continue; + if(!(entityOne instanceof Collidable)) continue; Collidable collidableOne = (Collidable) entityOne; + if(!collidableOne.canCollide()) continue; //Loop to check collisions against entityOne. iteratorTwo.reset(); while(iteratorTwo.hasNext()) { Entity entityTwo = iteratorTwo.next(); - //We want all Collidables on the passive side of the collision. if(!(entityTwo instanceof Collidable)) continue; + //Ignore certain combinations at all: + if(entityOne instanceof Enemy && entityTwo instanceof Enemy) continue; + if(entityOne instanceof Player && entityTwo instanceof Player) continue; + if(entityOne instanceof Shot && entityTwo instanceof Shot) continue; Collidable collidableTwo = (Collidable) entityTwo; + if(!collidableTwo.canCollide()) continue; //skip checks against itself if(entityTwo.equals(entityOne)) continue; - //Weed out Player and Enemy from the passive side - if(entityTwo instanceof Player || entityTwo instanceof Enemy) continue; //check for the actual collision if(CollisionHandler.doCollide(collidableOne, collidableTwo)) { CollisionHandler.handleCollision(collidableOne, collidableTwo);