Feature: Player now gains small immunity after an impact.
This commit is contained in:
parent
dba0f6990e
commit
8e82614874
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,19 +18,32 @@ 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;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -37,13 +51,17 @@ public class Player extends ShootingEntity implements KeyboardListener {
|
|||
*/
|
||||
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);
|
||||
|
@ -92,16 +117,26 @@ public class Player extends ShootingEntity implements KeyboardListener {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue