Add ship attributes to the PlayerSession, reset PlayerSession on GameOver.

This commit is contained in:
Jan Philipp Timme 2014-11-28 21:14:24 +01:00
parent 308ee0550f
commit 970ddb348f
10 changed files with 270 additions and 121 deletions

View File

@ -3,6 +3,7 @@ package de.teamteamteam.spacescooter;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import de.teamteamteam.spacescooter.brain.PlayerSession;
import de.teamteamteam.spacescooter.screen.LoadingScreen;
import de.teamteamteam.spacescooter.screen.SuperScreen;
import de.teamteamteam.spacescooter.thread.PaintThread;
@ -69,5 +70,8 @@ public class Main {
// Finally start loading and everything will follow up.
// This has to happen after the AWT-Eventqueue is done initializing everything.
Loader.load(loadingScreen);
//Initialize the player session the first time.
PlayerSession.reset();
}
}

View File

@ -40,6 +40,21 @@ public class GameConfig {
*/
public static int maximumPlayerScore = 99999999;
/**
* Initial health points the player will have.
*/
public static int initialPlayerHealthPoints = 100;
/**
* Initial shield points the player ship will have.
*/
public static int initialPlayerShieldPoints = 0;
/**
* Damage the player ships shots will cause initially.
*/
public static int initialPlayerShotDamage = 10;
/**
* Private constructor, this class will never be instantiated.
*/

View File

@ -16,6 +16,39 @@ public class PlayerSession {
* The players current amount of credits.
*/
private static int credits;
/**
* Damage value of the normal Shots the ship fires.
* This can be changed by upgrades in the shop.
*/
private static int shipShotDamage;
/**
* The ships default maximum shield points.
* This can be changed by upgrades in the shop.
*/
private static int shipShieldPoints;
/**
* The ships default maximum health points.
* This can be changed by upgrades in the shop.
*/
private static int shipHealthPoints;
/**
* The number of health upgrades the player bought for the ship.
*/
private static int shipHealthUpgadesBought;
/**
* The number of shield upgrades the player bought for the ship.
*/
private static int shipShieldUpgadesBought;
/**
* The number of shot damage upgrades the player bought for the ship.
*/
private static int shipShotUpgadesBought;
/**
@ -78,14 +111,126 @@ public class PlayerSession {
}
}
/**
* Get the ships maximum health points.
*/
public static int getShipHealthPoints() {
return PlayerSession.shipHealthPoints;
}
/**
* Set the ships maximum health points.
*/
public static void setShipHealthPoints(int shipHealthPoints) {
PlayerSession.shipHealthPoints = shipHealthPoints;
}
/**
* Add to the ships maximum health points.
*/
public static void addShipHealthPoints(int shipHealthPoints) {
PlayerSession.shipHealthPoints += shipHealthPoints;
}
/**
* Get the ships maximum shield points.
*/
public static int getShipShieldPoints() {
return PlayerSession.shipShieldPoints;
}
/**
* Set the ships maximum shield points.
*/
public static void setShipShieldPoints(int shipShieldPoints) {
PlayerSession.shipShieldPoints = shipShieldPoints;
}
/**
* Add to the ships maximum shield points.
*/
public static void addShipShieldPoints(int shipShieldPoints) {
PlayerSession.shipShieldPoints += shipShieldPoints;
}
/**
* Get the ships shot damage value.
*/
public static int getShipShotDamage() {
return PlayerSession.shipShotDamage;
}
/**
* Set the ships shot damage value.
*/
public static void setShipShotDamage(int shipShotDamage) {
PlayerSession.shipShotDamage = shipShotDamage;
}
/**
* Add to the ships shot damage value.
*/
public static void addShipShotDamage(int shipShotDamage) {
PlayerSession.shipShotDamage += shipShotDamage;
}
/**
* Get the number of ship health upgrades the player bought in the shop.
*/
public static int getShipHealthUpgradesBought() {
return PlayerSession.shipHealthUpgadesBought;
}
/**
* Increment the number of ship health upgrades the player bought.
*/
public static void incrementShipHealthUpgradesBought() {
PlayerSession.shipHealthUpgadesBought++;
}
/**
* Get the number of ship shield upgrades the player bought in the shop.
*/
public static int getShipShieldUpgradesBought() {
return PlayerSession.shipShieldUpgadesBought;
}
/**
* Increment the number of ship shield upgrades the player bought.
*/
public static void incrementShipShieldUpgradesBought() {
PlayerSession.shipShieldUpgadesBought++;
}
/**
* Get the number of ship shot upgrades the player bought in the shop.
*/
public static int getShipShotUpgradesBought() {
return PlayerSession.shipShotUpgadesBought;
}
/**
* Increment the number of ship shot upgrades the player bought.
*/
public static void incrementShipShotUpgradesBought() {
PlayerSession.shipShotUpgadesBought++;
}
/**
* This will reset all data from the players session.
* Used to initialize the session at the beginning of the game or
* after the player went game over and entered his name for the highscore.
* (So the next player can start a fresh session.)
*/
public static void reset() {
PlayerSession.score = 0;
PlayerSession.credits = 0;
PlayerSession.shipHealthPoints = GameConfig.initialPlayerHealthPoints;
PlayerSession.shipShieldPoints = GameConfig.initialPlayerShieldPoints;
PlayerSession.shipShotDamage = GameConfig.initialPlayerShotDamage;
PlayerSession.shipHealthUpgadesBought = 0;
PlayerSession.shipShieldUpgadesBought = 0;
PlayerSession.shipShotUpgadesBought = 0;
}
}

View File

@ -1,40 +0,0 @@
package de.teamteamteam.spacescooter.brain;
/**
* TODO: Rename and merge with other classes containing single static information.
* Static Values for the Player and the Shop
*
*/
public class StaticValue {
//Player
/**
* the ShootDamage of the Player
*/
public static int shotDamage = 5;
/**
* the HealthPoints of the Player may be changed by the 1Up-Item
*/
public static int healthPoints = 100;
/**
* the ShieldPoints of the Player
*/
public static int shieldPoints = 100;
//Shop
/**
* The Damage Value of the Shop
*/
public static int damage = 0;
/**
* the Shield value of the Shop
*/
public static int shield = 0;
/**
* the Health value of the Shop
*/
public static int life = 0;
}

View File

@ -17,15 +17,25 @@ import de.teamteamteam.spacescooter.entity.spi.Hittable;
public abstract class LivingEntity extends CollidableEntity implements Hittable {
/**
* The LivingEntities health points.
* The LivingEntities current health points.
*/
private int healthPoints;
/**
* The LivingEntities shield points.
* The LivingEntities current shield points.
*/
private int shieldPoints;
/**
* The LivingEntities maximum health points.
*/
private int maximumHealthPoints;
/**
* The LivingEntities maximum shield points.
*/
private int maximumShieldPoints;
/**
* The score points awarded if the LivingEntity dies.
*/
@ -131,6 +141,14 @@ public abstract class LivingEntity extends CollidableEntity implements Hittable
public int getHealthPoints() {
return this.healthPoints;
}
/**
* Get the current health percentage.
* Returns an int in [0,100], divide by 100 for double percentage.
*/
public int getHealthPercentage() {
return (this.healthPoints * 100) / this.maximumHealthPoints;
}
/**
* Set the current shield points.
@ -146,6 +164,42 @@ public abstract class LivingEntity extends CollidableEntity implements Hittable
return this.shieldPoints;
}
/**
* Get the current shield percentage.
* Returns an int in [0,100], divide by 100 for double percentage.
*/
public int getShieldPercentage() {
return (this.shieldPoints * 100) / this.maximumShieldPoints;
}
/**
* Set the maximum health points.
*/
public void setMaximumHealthPoints(int maxhp) {
this.maximumHealthPoints = maxhp;
}
/**
* Get the maximum health points.
*/
public int getMaximumHealthPoints() {
return this.maximumHealthPoints;
}
/**
* Set the maximum shield points.
*/
public void setMaximumShieldPoints(int maxsp) {
this.maximumShieldPoints = maxsp;
}
/**
* Get the maximum shield points.
*/
public int getMaximumShieldPoints() {
return this.maximumShieldPoints;
}
/**
* Set the current score points.
*/

View File

@ -3,7 +3,7 @@ package de.teamteamteam.spacescooter.entity;
import java.awt.event.KeyEvent;
import de.teamteamteam.spacescooter.brain.GameConfig;
import de.teamteamteam.spacescooter.brain.StaticValue;
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;
@ -21,16 +21,6 @@ public class Player extends ShootingEntity implements KeyboardListener {
*/
private Keyboard keyboard = null;
/**
* the Players Health Points in percent
*/
private double healthPercent = 0;
/**
* the Players ShieldPoints in percent
*/
private double shieldPercent = 0;
/**
* the Players Rocket Ammunition
*/
@ -49,14 +39,16 @@ public class Player extends ShootingEntity implements KeyboardListener {
super(x, y);
this.setImage("images/ship.png");
this.setPrimaryShotImage("images/shots/laser_blue.png");
this.setShootDamage(StaticValue.shotDamage);
this.setShootDelay(20);
this.setShootSpawn(50, 16);
this.setShootDirection(Shot.RIGHT);
this.setShootSpeed(10);
this.setCollisionDamage(10);
this.setShieldPoints(StaticValue.shieldPoints);
this.setHealthPoints(StaticValue.healthPoints);
this.setHealthPoints(PlayerSession.getShipHealthPoints());
this.setMaximumHealthPoints(PlayerSession.getShipHealthPoints());
this.setShieldPoints(PlayerSession.getShipShieldPoints());
this.setMaximumShieldPoints(PlayerSession.getShipShieldPoints());
this.setShootDamage(PlayerSession.getShipShotDamage());
this.registerOnKeyboard(Keyboard.getInstance());
}
@ -72,41 +64,30 @@ public class Player extends ShootingEntity implements KeyboardListener {
* Standard update method
*/
public void update() {
if (StaticValue.healthPoints != 0) {
this.healthPercent = ((double) this.getHealthPoints() / (double) StaticValue.healthPoints) * 100;
if(this.canMove() == false) return;
super.update();
int offset = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.getY() > 51) {
this.transpose(0, -1 * offset);
}
if (StaticValue.shieldPoints != 0) {
this.shieldPercent = ((double) this.getShieldPoints() / (double) StaticValue.shieldPoints) * 100;
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.getY() < (GameConfig.windowHeight - this.getImage().getHeight())) {
this.transpose(0, offset);
}
if(this.canMove()) {
super.update();
int offset = 3;
if(Keyboard.isKeyDown(KeyEvent.VK_UP) && this.getY() > 51) {
this.transpose(0, -1 * offset);
}
if(Keyboard.isKeyDown(KeyEvent.VK_DOWN) && this.getY() < (GameConfig.windowHeight - this.getImage().getHeight())) {
this.transpose(0, offset);
}
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.getX() > 0) {
this.transpose(-1 * offset, 0);
}
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)) {
this.shoot();
}
if(Keyboard.isKeyDown(KeyEvent.VK_Y)) {
if(rocketAmount > 0){
this.shootRocket();
}
}
if(Keyboard.isKeyDown(KeyEvent.VK_X)) {
if(this.beamAmount > 0){
this.shootBeam();
}
}
if(Keyboard.isKeyDown(KeyEvent.VK_LEFT) && this.getX() > 0) {
this.transpose(-1 * offset, 0);
}
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)) {
this.shoot();
}
if(Keyboard.isKeyDown(KeyEvent.VK_Y) && this.rocketAmount > 0) {
this.shootRocket();
}
if(Keyboard.isKeyDown(KeyEvent.VK_X) && this.beamAmount > 0) {
this.shootBeam();
}
}
@ -182,29 +163,15 @@ public class Player extends ShootingEntity implements KeyboardListener {
* empty keyTyped method, maybe useful for cheatcodes later
*/
public void keyTyped(KeyEvent e) {}
/**
* return the Healthpercentage of the Player
*/
public int getHealthPercent() {
return (int) this.healthPercent;
}
/**
* return the Shieldpercentage of the Player
*/
public int getShieldPercent() {
return (int) this.shieldPercent;
}
/**
* method for increasing the HealthPoints with the Heal-Item
*/
public void increaseHealthPoints(int inc) {
if (this.getHealthPoints() <= (StaticValue.healthPoints - 15)) {
if (this.getHealthPoints() <= (PlayerSession.getShipHealthPoints() - 15)) {
this.setHealthPoints(getHealthPoints() + inc);
} else {
this.setHealthPoints(StaticValue.healthPoints);
this.setHealthPoints(PlayerSession.getShipHealthPoints());
}
}
@ -212,10 +179,10 @@ public class Player extends ShootingEntity implements KeyboardListener {
* method for increasing the ShieldPoints with the Shield-Item
*/
public void increaseShieldPoints(int inc) {
if (this.getShieldPoints() <= (StaticValue.shieldPoints - 5)) {
if (this.getShieldPoints() <= (PlayerSession.getShipShieldPoints() - 5)) {
this.setShieldPoints(getShieldPoints() + inc);
} else {
this.setShieldPoints(StaticValue.shieldPoints);
this.setShieldPoints(PlayerSession.getShipShieldPoints());
}
}

View File

@ -22,7 +22,7 @@ public class HealthBar extends Entity {
public void paint(Graphics2D g) {
Player player = GameScreen.getPlayer();
try {
this.health = player.getHealthPercent();
this.health = player.getHealthPercentage();
this.healthwidth = ((this.width) * this.health) / 100;
} catch(Exception e) {
this.healthwidth = 0;

View File

@ -23,7 +23,7 @@ public class ShieldBar extends Entity {
public void paint(Graphics2D g) {
Player player = GameScreen.getPlayer();
try {
this.shield = player.getShieldPercent();
this.shield = player.getShieldPercentage();
this.shieldwidth = ((this.width) * this.shield) / 100;
} catch(Exception e) {
this.shieldwidth = 0;

View File

@ -7,6 +7,7 @@ import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import de.teamteamteam.spacescooter.brain.GameConfig;
import de.teamteamteam.spacescooter.brain.PlayerSession;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.gui.Button;
@ -35,6 +36,10 @@ public class GameOverScreen extends Screen {
player = new Player(GameConfig.windowWidth/2-170, 309);
player.setCanMove(false);
player.setCanShoot(false);
//Reset the player session for the next player.
//TODO: Make sure highscore "enter name" stuff happened before!
PlayerSession.reset();
}
@Override

View File

@ -8,7 +8,6 @@ import java.awt.image.BufferedImage;
import de.teamteamteam.spacescooter.brain.GameConfig;
import de.teamteamteam.spacescooter.brain.PlayerSession;
import de.teamteamteam.spacescooter.brain.StaticValue;
import de.teamteamteam.spacescooter.control.Keyboard;
import de.teamteamteam.spacescooter.entity.Player;
import de.teamteamteam.spacescooter.gui.Button;
@ -31,9 +30,9 @@ public class ShopScreen extends Screen {
super(parent);
this.img = Loader.getBufferedImageByFilename("images/testbackground.png");
new Button(GameConfig.windowWidth/2-125, 450);
damage = new ShopOffer(100, 160, 15, StaticValue.damage, "Schaden");
shield = new ShopOffer(100, 260, 15, StaticValue.shield, "Schild");
life = new ShopOffer(100, 360, 15, StaticValue.life, "Leben");
damage = new ShopOffer(100, 160, 15, PlayerSession.getShipShotUpgradesBought(), "Schaden");
shield = new ShopOffer(100, 260, 15, PlayerSession.getShipShieldUpgradesBought(), "Schild");
life = new ShopOffer(100, 360, 15, PlayerSession.getShipHealthUpgradesBought(), "Leben");
player = new Player(50, 159);
player.setCanMove(false);
player.setCanShoot(false);
@ -78,24 +77,24 @@ public class ShopScreen extends Screen {
case 0:
if(PlayerSession.getCredits() >= 5 && damage.getBought() < damage.getMax()){
damage.buy();
StaticValue.shotDamage += 5;
StaticValue.damage++;
PlayerSession.addShipShotDamage(5);
PlayerSession.incrementShipShotUpgradesBought();
PlayerSession.removeCredits(5);
}
break;
case 1:
if(PlayerSession.getCredits() >= 10 && shield.getBought() < shield.getMax()){
shield.buy();
StaticValue.shieldPoints += 10;
StaticValue.shield++;
PlayerSession.addShipShieldPoints(10);
PlayerSession.incrementShipShieldUpgradesBought();
PlayerSession.removeCredits(10);
}
break;
case 2:
if(PlayerSession.getCredits() >= 10 && life.getBought() < life.getMax()){
life.buy();
StaticValue.healthPoints += 10;
StaticValue.life++;
PlayerSession.addShipHealthPoints(10);
PlayerSession.incrementShipHealthUpgradesBought();
PlayerSession.removeCredits(10);
}
break;