From 308ee0550f0820fee1f3f7f4a9b14ce1f2c8cd31 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Fri, 28 Nov 2014 20:27:55 +0100 Subject: [PATCH] Merge Score and Credits into PlayerSession, avoid directly setting the values. --- .../teamteamteam/spacescooter/GameFrame.java | 4 +- .../spacescooter/brain/Credits.java | 51 ----------- .../spacescooter/brain/GameConfig.java | 8 +- .../spacescooter/brain/PlayerSession.java | 91 +++++++++++++++++++ .../spacescooter/brain/Score.java | 56 ------------ .../spacescooter/entity/LivingEntity.java | 11 +-- .../spacescooter/entity/item/ItemCredit.java | 4 +- .../spacescooter/gui/ScoreBar.java | 4 +- .../spacescooter/screen/ShopScreen.java | 16 ++-- 9 files changed, 116 insertions(+), 129 deletions(-) delete mode 100644 src/de/teamteamteam/spacescooter/brain/Credits.java create mode 100644 src/de/teamteamteam/spacescooter/brain/PlayerSession.java delete mode 100644 src/de/teamteamteam/spacescooter/brain/Score.java diff --git a/src/de/teamteamteam/spacescooter/GameFrame.java b/src/de/teamteamteam/spacescooter/GameFrame.java index 9823aa9..2a15eb7 100644 --- a/src/de/teamteamteam/spacescooter/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/GameFrame.java @@ -159,10 +159,10 @@ public class GameFrame extends JFrame { * KEY_ANTIALIASING is very expensive and doesn't do much more over KEY_TEXT_ANTIALIASING */ private void applyRenderingHints(Graphics2D bufferedGraphics) { - if(GameConfig.key_antialiasing == true) { + if(GameConfig.keyAntialiasing == true) { bufferedGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } - if(GameConfig.text_antialiasing == true) { + if(GameConfig.textAntialiasing == true) { bufferedGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } } diff --git a/src/de/teamteamteam/spacescooter/brain/Credits.java b/src/de/teamteamteam/spacescooter/brain/Credits.java deleted file mode 100644 index 07375a9..0000000 --- a/src/de/teamteamteam/spacescooter/brain/Credits.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.teamteamteam.spacescooter.brain; - -/** - * TODO: Intelligently merge this with Score class. - * Static class accounting the credits a player earned during the game. - */ -public class Credits { - - /** - * Credit points for the shop - */ - private static int credits = 0; - - - /** - * Private constructor. - */ - private Credits() { } - - - /** - * Get the current amount of credits. - */ - public static int getCredits() { - return credits; - } - - /** - * Set the current amount of credits. - */ - public static void setCredits(int credits) { - Credits.credits = credits; - } - - /** - * Add a number of credits to the credits account. - */ - public static void add(int credits) { - Credits.credits += credits; - } - - /** - * Remove a number of credits to the credits account. - */ - public static void remove(int credits) { - Credits.credits -= credits; - if(Credits.credits < 0) { - Credits.credits = 0; - } - } -} diff --git a/src/de/teamteamteam/spacescooter/brain/GameConfig.java b/src/de/teamteamteam/spacescooter/brain/GameConfig.java index bd9f528..19850da 100644 --- a/src/de/teamteamteam/spacescooter/brain/GameConfig.java +++ b/src/de/teamteamteam/spacescooter/brain/GameConfig.java @@ -28,13 +28,17 @@ public class GameConfig { /** * Whether or not anti aliasing will be used for shapes. */ - public static boolean key_antialiasing = false; + public static boolean keyAntialiasing = false; /** * Whether or not to apply anti aliasing on text. */ - public static boolean text_antialiasing = false; + public static boolean textAntialiasing = false; + /** + * The maximum number of points a player can reach. + */ + public static int maximumPlayerScore = 99999999; /** * Private constructor, this class will never be instantiated. diff --git a/src/de/teamteamteam/spacescooter/brain/PlayerSession.java b/src/de/teamteamteam/spacescooter/brain/PlayerSession.java new file mode 100644 index 0000000..04f97c1 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/brain/PlayerSession.java @@ -0,0 +1,91 @@ +package de.teamteamteam.spacescooter.brain; + +/** + * This class is responsible for keeping track of all the data a player session + * usually contains, such as earned score points, earned credits, activated ship upgrades + * and more. + */ +public class PlayerSession { + + /** + * The players current score. + */ + private static int score; + + /** + * The players current amount of credits. + */ + private static int credits; + + + /** + * Private constructor, this class will never be instantiated. + */ + private PlayerSession() {} + + + /** + * Get the current amount of credits the player has. + */ + public static int getCredits() { + return PlayerSession.credits; + } + + /** + * Add a number of credits to the players credits. + */ + public static void addCredits(int credits) { + PlayerSession.credits += credits; + } + + /** + * Remove a number of credits from the players credits. + * This is capped at zero. + */ + public static void removeCredits(int credits) { + PlayerSession.credits -= credits; + if(PlayerSession.credits < 0) { + PlayerSession.credits = 0; + } + } + + /** + * Get the current player score. + */ + public static int getScore() { + return PlayerSession.score; + } + + /** + * Add a number of points to the players score. + * There is a maximum of X points to reach, score is capped then. + */ + public static void addScore(int points) { + PlayerSession.score += points; + if(PlayerSession.score > GameConfig.maximumPlayerScore) { + PlayerSession.score = GameConfig.maximumPlayerScore; + } + } + + /** + * Remove a number of points from the players score. + * This is capped at zero. + */ + public static void removeScore(int points) { + PlayerSession.score -= points; + if(PlayerSession.score < 0) { + PlayerSession.score = 0; + } + } + + /** + * 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. + */ + public static void reset() { + PlayerSession.score = 0; + PlayerSession.credits = 0; + } + +} diff --git a/src/de/teamteamteam/spacescooter/brain/Score.java b/src/de/teamteamteam/spacescooter/brain/Score.java deleted file mode 100644 index d9babab..0000000 --- a/src/de/teamteamteam/spacescooter/brain/Score.java +++ /dev/null @@ -1,56 +0,0 @@ -package de.teamteamteam.spacescooter.brain; - -/** - * Score Class to represent the Players Score - */ -public class Score { - - /** - * Score upper and lower boundaries. - */ - private static int score = 0; - private static int maxScore = 99999999; - - - /** - * Private constructor, this class will never be instantiated. - */ - private Score() {} - - - /** - * Getter for the Score. - */ - public static int getScore() { - return score; - } - - /** - * Setter for the Score. - */ - public static void setScore(int score) { - Score.score = score; - } - - /** - * Method for adding Score, capping it at the maximum value. - */ - public static void addScore(int score) { - Score.score += score; - if (Score.score >= Score.maxScore) { - Score.setScore(Score.maxScore); - } - } - - /** - * Method for removing Score, capping it at zero. - */ - public static void removeScore(int score) { - if (Score.score - score <= 0) { - Score.setScore(0); - } else if (Score.score != 0) { - Score.score -= score; - } - } - -} diff --git a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java index 301f69c..34cacaa 100644 --- a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java @@ -1,8 +1,7 @@ package de.teamteamteam.spacescooter.entity; -import de.teamteamteam.spacescooter.brain.Credits; import de.teamteamteam.spacescooter.brain.GameConfig; -import de.teamteamteam.spacescooter.brain.Score; +import de.teamteamteam.spacescooter.brain.PlayerSession; import de.teamteamteam.spacescooter.entity.enemy.Enemy; import de.teamteamteam.spacescooter.entity.shot.Shot; import de.teamteamteam.spacescooter.entity.spi.Collidable; @@ -30,7 +29,7 @@ public abstract class LivingEntity extends CollidableEntity implements Hittable /** * The score points awarded if the LivingEntity dies. */ - private int ScorePoints; + private int scorePoints; /** @@ -94,9 +93,9 @@ public abstract class LivingEntity extends CollidableEntity implements Hittable // Set the correct values for gui indicators this.healthPoints = 0; this.shieldPoints = 0; - Score.addScore(ScorePoints); + PlayerSession.addScore(scorePoints); if(this instanceof Enemy){ // Add 1 credit for the shop - Credits.setCredits(Credits.getCredits() + 1); + PlayerSession.addCredits(1); } if (GameConfig.DEBUG) System.out.println(this + " ist gestorben. RIP"); @@ -151,7 +150,7 @@ public abstract class LivingEntity extends CollidableEntity implements Hittable * Set the current score points. */ public void setScore(int s) { - this.ScorePoints = s; + this.scorePoints = s; } } diff --git a/src/de/teamteamteam/spacescooter/entity/item/ItemCredit.java b/src/de/teamteamteam/spacescooter/entity/item/ItemCredit.java index 08848a5..dda0e85 100644 --- a/src/de/teamteamteam/spacescooter/entity/item/ItemCredit.java +++ b/src/de/teamteamteam/spacescooter/entity/item/ItemCredit.java @@ -1,6 +1,6 @@ package de.teamteamteam.spacescooter.entity.item; -import de.teamteamteam.spacescooter.brain.Credits; +import de.teamteamteam.spacescooter.brain.PlayerSession; import de.teamteamteam.spacescooter.entity.Player; public class ItemCredit extends Item { @@ -12,6 +12,6 @@ public class ItemCredit extends Item { @Override public void itemCollected(Player player) { - Credits.add(10); + PlayerSession.addCredits(10); } } diff --git a/src/de/teamteamteam/spacescooter/gui/ScoreBar.java b/src/de/teamteamteam/spacescooter/gui/ScoreBar.java index 463a8d5..2ad1924 100644 --- a/src/de/teamteamteam/spacescooter/gui/ScoreBar.java +++ b/src/de/teamteamteam/spacescooter/gui/ScoreBar.java @@ -4,8 +4,8 @@ import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; +import de.teamteamteam.spacescooter.brain.PlayerSession; import de.teamteamteam.spacescooter.entity.Entity; -import de.teamteamteam.spacescooter.brain.Score; public class ScoreBar extends Entity { @@ -17,7 +17,7 @@ public class ScoreBar extends Entity { g.setColor(Color.WHITE); g.setFont(new Font("Monospace", 0, 24)); g.drawString("Score:", this.getX(), this.getY()); - g.drawString(String.format("%08d", Integer.parseInt("" + Score.getScore())), this.getX()+77, this.getY()); + g.drawString(String.format("%08d", Integer.parseInt("" + PlayerSession.getScore())), this.getX()+77, this.getY()); } public void update() {} diff --git a/src/de/teamteamteam/spacescooter/screen/ShopScreen.java b/src/de/teamteamteam/spacescooter/screen/ShopScreen.java index fe7a26c..f2c13a1 100644 --- a/src/de/teamteamteam/spacescooter/screen/ShopScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/ShopScreen.java @@ -6,8 +6,8 @@ import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; -import de.teamteamteam.spacescooter.brain.Credits; 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; @@ -48,7 +48,7 @@ public class ShopScreen extends Screen { } g.setFont(new Font("Monospace", 0, 20)); g.setColor(new Color(255, 255, 255)); - g.drawString("Credits: " + String.valueOf(Credits.getCredits()), GameConfig.windowWidth/2-30, 100); + g.drawString("Credits: " + String.valueOf(PlayerSession.getCredits()), GameConfig.windowWidth/2-30, 100); g.setColor(new Color(0, 0, 0)); g.drawString("Hauptmen\u00fc", GameConfig.windowWidth/2-55, 482); } @@ -76,27 +76,27 @@ public class ShopScreen extends Screen { /////////////////////////////////////////////////////////////// switch (this.menuPoint) { case 0: - if(Credits.getCredits() >= 5 && damage.getBought() < damage.getMax()){ + if(PlayerSession.getCredits() >= 5 && damage.getBought() < damage.getMax()){ damage.buy(); StaticValue.shotDamage += 5; StaticValue.damage++; - Credits.setCredits(Credits.getCredits() - 5); + PlayerSession.removeCredits(5); } break; case 1: - if(Credits.getCredits() >= 10 && shield.getBought() < shield.getMax()){ + if(PlayerSession.getCredits() >= 10 && shield.getBought() < shield.getMax()){ shield.buy(); StaticValue.shieldPoints += 10; StaticValue.shield++; - Credits.setCredits(Credits.getCredits() - 10); + PlayerSession.removeCredits(10); } break; case 2: - if(Credits.getCredits() >= 10 && life.getBought() < life.getMax()){ + if(PlayerSession.getCredits() >= 10 && life.getBought() < life.getMax()){ life.buy(); StaticValue.healthPoints += 10; StaticValue.life++; - Credits.setCredits(Credits.getCredits() - 10); + PlayerSession.removeCredits(10); } break; case 3: