Skip to content

Commit edcd26e

Browse files
authored
Merge pull request #197 from ameliakratzer-code/add-coin-functionalities
[Feature] Add coin functionalities so that coins affect game play
2 parents 852ad94 + 0870961 commit edcd26e

File tree

15 files changed

+182
-26
lines changed

15 files changed

+182
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,5 @@ target/classes
258258
.idea/*
259259

260260
highScore.ser
261+
totalCoins.ser
261262
settings.properties

src/main/java/com/dinosaur/dinosaurexploder/components/CollectedCoinsComponent.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
import com.dinosaur.dinosaurexploder.constants.GameConstants;
55
import com.dinosaur.dinosaurexploder.interfaces.CollectedCoins;
66
import com.dinosaur.dinosaurexploder.utils.LanguageManager;
7+
import com.dinosaur.dinosaurexploder.model.TotalCoins;
78
import javafx.scene.Node;
89
import javafx.scene.image.Image;
910
import javafx.scene.image.ImageView;
1011
import javafx.scene.layout.HBox;
1112
import javafx.scene.paint.Color;
1213
import javafx.scene.text.Font;
1314
import javafx.scene.text.Text;
15+
import java.io.*;
1416

1517
public class CollectedCoinsComponent extends Component implements CollectedCoins {
1618
private int coin = 0;
1719
private final int COIN_VALUE = 1;
1820

21+
private static TotalCoins totalCoins = new TotalCoins();
1922

2023
private final LanguageManager languageManager = LanguageManager.getInstance();
2124

@@ -24,6 +27,8 @@ public class CollectedCoinsComponent extends Component implements CollectedCoins
2427

2528
@Override
2629
public void onAdded() {
30+
loadTotalCoins(); // Deserialize once when the component is added
31+
2732
// Create UI elements
2833
coinText = new Text();
2934
coinText.setFill(Color.PURPLE);
@@ -35,25 +40,43 @@ public void onAdded() {
3540
entity.getViewComponent().addChild(coinUI);
3641
}
3742

38-
private void updateText(){
43+
private void updateText() {
3944
coinText.setText(languageManager.getTranslation("coin") + ": " + coin);
4045
}
4146

42-
private Node createCoinUI(){
47+
private Node createCoinUI() {
4348
var container = new HBox(5);
4449
Image image = new Image(GameConstants.COIN_IMAGE_PATH, 25, 20, false, false);
4550
ImageView imageView = new ImageView(image);
4651
container.getChildren().addAll(coinText, imageView);
4752
return container;
4853
}
4954

55+
private void loadTotalCoins() {
56+
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(GameConstants.TOTAL_COINS_FILE))) {
57+
totalCoins = (TotalCoins) in.readObject();
58+
} catch (IOException | ClassNotFoundException e) {
59+
totalCoins = new TotalCoins(); // Defaults to 0 if file is missing or corrupted
60+
}
61+
}
62+
63+
private void saveTotalCoins() {
64+
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(GameConstants.TOTAL_COINS_FILE))) {
65+
out.writeObject(totalCoins);
66+
} catch (IOException e) {
67+
System.err.println("Error saving coins: " + e.getMessage());
68+
}
69+
}
70+
5071
@Override
5172
public void onUpdate(double tpf) {
5273
updateText();
5374
}
5475

55-
public void incrementCoin(){
76+
public void incrementCoin() {
5677
coin += COIN_VALUE;
78+
totalCoins.setTotal(totalCoins.getTotal() + COIN_VALUE);
5779
updateText();
80+
saveTotalCoins();
5881
}
5982
}

src/main/java/com/dinosaur/dinosaurexploder/constants/GameConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ public class GameConstants {
5353
* CONSTANTS FOR SAVED FILES
5454
*/
5555
public static final String HIGH_SCORE_FILE = "highScore.ser";
56+
public static final String TOTAL_COINS_FILE = "totalCoins.ser";
5657
}

src/main/java/com/dinosaur/dinosaurexploder/controller/DinosaurController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ public void initPhysics() {
334334
System.out.println("You touched a coin!");
335335
collectedCoinsComponent.incrementCoin();
336336

337+
score.getComponent(ScoreComponent.class).incrementScore(2);
338+
337339
// Check for bomb regeneration when coin is collected
338340
if (bomb.hasComponent(BombComponent.class)) {
339341
bomb.getComponent(BombComponent.class).trackCoinForBombRegeneration();

src/main/java/com/dinosaur/dinosaurexploder/model/GameData.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public class GameData {
1212
private static final ShipUnlockChecker shipUnlockChecker = new ShipUnlockChecker();
1313
private static final WeaponUnlockChecker weaponUnlockChecker = new WeaponUnlockChecker();
1414

15+
// Static variable that stores the high score
16+
private static int highScore;
17+
18+
// Static variable that stores total coins
19+
private static int totalCoins;
20+
1521
// Getter and setter for the selected ship
1622
public static int getSelectedShip() {
1723
return selectedShip;
@@ -47,4 +53,17 @@ public static boolean checkUnlockedWeapon(int weaponNumber) {
4753
return false;
4854
}
4955
}
56+
57+
// Getter for the high score
58+
public static int getHighScore() {
59+
highScore = shipUnlockChecker.getHighScore().getHigh();
60+
return highScore;
61+
}
62+
63+
// Getter for total coins
64+
public static int getTotalCoins() {
65+
totalCoins = shipUnlockChecker.getTotalCoins().getTotal();
66+
System.out.println("Total: " + totalCoins);
67+
return totalCoins;
68+
}
5069
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.dinosaur.dinosaurexploder.model;
2+
import java.io.Serializable;
3+
public class TotalCoins implements Serializable {
4+
private Integer total;
5+
public TotalCoins() {
6+
this.total = 0;
7+
}
8+
public TotalCoins(Integer total) {
9+
this.total = total;
10+
}
11+
public Integer getTotal() {
12+
return total;
13+
}
14+
public void setTotal(Integer total) {
15+
this.total = total;
16+
}
17+
}

src/main/java/com/dinosaur/dinosaurexploder/utils/ShipUnlockChecker.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.dinosaur.dinosaurexploder.exception.LockedShipException;
44
import com.dinosaur.dinosaurexploder.model.HighScore;
5+
import com.dinosaur.dinosaurexploder.model.TotalCoins;
56

67
import java.io.FileInputStream;
78
import java.io.IOException;
@@ -11,37 +12,71 @@
1112
public class ShipUnlockChecker {
1213
public LanguageManager languageManager = LanguageManager.getInstance();
1314

14-
private static final Map<Integer, Integer> scoreMap = Map.of( //key: shipNumber, value: lower limit score
15+
private static final Map<Integer, Integer> scoreMap = Map.of( // key: shipNumber, value: lower limit score
1516
1, 0,
1617
2, 0,
1718
3, 100,
1819
4, 200,
1920
5, 300,
2021
6, 400,
2122
7, 600,
22-
8, 700
23-
);
23+
8, 700);
24+
25+
private static final Map<Integer, Integer> coinMap = Map.of( // key: shipNumber, value: lower limit total coins
26+
1, 0,
27+
2, 0,
28+
3, 10,
29+
4, 50,
30+
5, 100,
31+
6, 150,
32+
7, 200,
33+
8, 250);
34+
2435
private HighScore highScore = new HighScore();
36+
private TotalCoins totalCoins = new TotalCoins();
2537

2638
public int check(int shipNumber) {
2739
highScore = getHighScore();
28-
checkScore(shipNumber);
40+
checkScoreAndCoins(shipNumber);
2941
return shipNumber;
3042
}
3143

3244
public HighScore getHighScore() {
3345
try (FileInputStream file = new FileInputStream("highScore.ser");
34-
ObjectInputStream in = new ObjectInputStream(file)) {
46+
ObjectInputStream in = new ObjectInputStream(file)) {
3547
return (HighScore) in.readObject();
3648
} catch (IOException | ClassNotFoundException e) {
3749
return new HighScore();
3850
}
3951
}
4052

41-
private void checkScore(int shipNumber) {
42-
int lowerLimit = scoreMap.getOrDefault(shipNumber, 0);
43-
if (lowerLimit <= highScore.getHigh()) return;
44-
throw new LockedShipException(languageManager.getTranslation("ship_locked") + "\n" +
45-
languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerLimit)));
53+
public TotalCoins getTotalCoins() {
54+
try (FileInputStream file = new FileInputStream("totalCoins.ser");
55+
ObjectInputStream in = new ObjectInputStream(file)) {
56+
return (TotalCoins) in.readObject();
57+
} catch (IOException | ClassNotFoundException e) {
58+
return new TotalCoins();
59+
}
60+
}
61+
62+
private void checkScoreAndCoins(int shipNumber) {
63+
int lowerScoreLimit = scoreMap.getOrDefault(shipNumber, 0);
64+
int lowerCoinLimit = coinMap.getOrDefault(shipNumber, 0);
65+
66+
if (lowerScoreLimit <= highScore.getHigh() && lowerCoinLimit <= totalCoins.getTotal())
67+
return;
68+
else if (lowerScoreLimit > highScore.getHigh() && lowerCoinLimit <= totalCoins.getTotal()) {
69+
throw new LockedShipException(languageManager.getTranslation("ship_locked") + "\n" +
70+
languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerScoreLimit)));
71+
} else if (lowerScoreLimit <= highScore.getHigh() && lowerCoinLimit > totalCoins.getTotal()) {
72+
throw new LockedShipException(languageManager.getTranslation("ship_locked") + "\n" +
73+
languageManager.getTranslation("unlock_totalCoins").replace("##", String.valueOf(lowerCoinLimit)));
74+
} else {
75+
throw new LockedShipException(languageManager.getTranslation("ship_locked") + "\n"
76+
+ languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerScoreLimit))
77+
+ "\n" +
78+
languageManager.getTranslation("unlock_totalCoins").replace("##", String.valueOf(lowerCoinLimit)));
79+
}
80+
4681
}
4782
}

src/main/java/com/dinosaur/dinosaurexploder/utils/WeaponUnlockChecker.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.dinosaur.dinosaurexploder.utils;
22

3+
import com.dinosaur.dinosaurexploder.exception.LockedShipException;
34
import com.dinosaur.dinosaurexploder.exception.LockedWeaponException;
45
import com.dinosaur.dinosaurexploder.model.HighScore;
6+
import com.dinosaur.dinosaurexploder.model.TotalCoins;
7+
58
import java.io.FileInputStream;
69
import java.io.IOException;
710
import java.io.ObjectInputStream;
@@ -14,11 +17,18 @@ public class WeaponUnlockChecker {
1417
1, 0,
1518
2, 50,
1619
3, 100);
20+
21+
private static final Map<Integer, Integer> coinMap = Map.of( // key: weaponNumber, value: lower limit total coins
22+
1, 0,
23+
2, 5,
24+
3, 10);
25+
1726
private HighScore highScore = new HighScore();
27+
private TotalCoins totalCoins = new TotalCoins();
1828

1929
public int check(int weaponNumber) {
2030
highScore = getHighScore();
21-
checkScore(weaponNumber);
31+
checkScoreAndCoins(weaponNumber);
2232
return weaponNumber;
2333
}
2434

@@ -31,11 +41,33 @@ public HighScore getHighScore() {
3141
}
3242
}
3343

34-
private void checkScore(int weaponNumber) {
35-
int lowerLimit = scoreMap.getOrDefault(weaponNumber, 0);
36-
if (lowerLimit <= highScore.getHigh())
44+
public TotalCoins getTotalCoins() {
45+
try (FileInputStream file = new FileInputStream("totalCoins.ser");
46+
ObjectInputStream in = new ObjectInputStream(file)) {
47+
return (TotalCoins) in.readObject();
48+
} catch (IOException | ClassNotFoundException e) {
49+
return new TotalCoins();
50+
}
51+
}
52+
53+
private void checkScoreAndCoins(int weaponNumber) {
54+
int lowerScoreLimit = scoreMap.getOrDefault(weaponNumber, 0);
55+
int lowerCoinLimit = coinMap.getOrDefault(weaponNumber, 0);
56+
57+
if (lowerScoreLimit <= highScore.getHigh() && lowerCoinLimit <= totalCoins.getTotal())
3758
return;
38-
throw new LockedWeaponException(languageManager.getTranslation("weapon_locked") + "\n" +
39-
languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerLimit)));
59+
else if (lowerScoreLimit > highScore.getHigh() && lowerCoinLimit <= totalCoins.getTotal()) {
60+
throw new LockedWeaponException(languageManager.getTranslation("weapon_locked") + "\n" +
61+
languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerScoreLimit)));
62+
} else if (lowerScoreLimit <= highScore.getHigh() && lowerCoinLimit > totalCoins.getTotal()) {
63+
throw new LockedWeaponException(languageManager.getTranslation("weapon_locked") + "\n" +
64+
languageManager.getTranslation("unlock_totalCoins").replace("##", String.valueOf(lowerCoinLimit)));
65+
} else {
66+
throw new LockedWeaponException(languageManager.getTranslation("weapon_locked") + "\n"
67+
+ languageManager.getTranslation("unlock_highScore").replace("##", String.valueOf(lowerScoreLimit))
68+
+ "\n" +
69+
languageManager.getTranslation("unlock_totalCoins").replace("##", String.valueOf(lowerCoinLimit)));
70+
}
71+
4072
}
4173
}

src/main/java/com/dinosaur/dinosaurexploder/view/ShipSelectionMenu.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ShipSelectionMenu() {
5959
imageViewB.setY(0);
6060
imageViewB.setPreserveRatio(true);
6161

62-
//Background animation
62+
// Background animation
6363
TranslateTransition translateTransition = new TranslateTransition();
6464
translateTransition.setNode(imageViewB);
6565
translateTransition.setDuration(Duration.seconds(50));
@@ -71,7 +71,20 @@ public ShipSelectionMenu() {
7171
translateTransition.play();
7272

7373
// Title
74-
var title = FXGL.getUIFactoryService().newText(languageManager.getTranslation("select_ship"), Color.LIME, FontType.MONO, 35);
74+
var title = FXGL.getUIFactoryService().newText(languageManager.getTranslation("select_ship"), Color.LIME,
75+
FontType.MONO, 35);
76+
77+
// High Score display
78+
var highScore = FXGL.getUIFactoryService().newText(
79+
languageManager.getTranslation("high_score") + ": " + GameData.getHighScore(),
80+
Color.LIME,
81+
FontType.MONO, 25);
82+
83+
// Total Coin display
84+
var totalCoins = FXGL.getUIFactoryService().newText(
85+
languageManager.getTranslation("total_coins") + ": " + GameData.getTotalCoins(),
86+
Color.LIME,
87+
FontType.MONO, 25);
7588

7689
// GridPane for ships
7790
GridPane shipGrid = new GridPane();
@@ -104,7 +117,7 @@ public ShipSelectionMenu() {
104117
spacer.setOpacity(0);
105118

106119
// Vbox layout
107-
VBox layout = new VBox(20, spacer, title, shipGrid, backButton);
120+
VBox layout = new VBox(20, spacer, title, highScore, totalCoins, shipGrid, backButton);
108121
layout.setAlignment(Pos.CENTER);
109122
layout.setAlignment(Pos.CENTER);
110123
layout.setSpacing(50); // 50px spacing between nodes
@@ -132,7 +145,8 @@ private void showSelectionButton(double imageSize, int columns, GridPane shipGri
132145
shipView.setFitWidth(imageSize);
133146
applyDarkFilterIfLocked(isLocked, shipView);
134147

135-
ImageView lockIcon = new ImageView(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/lock.png"))));
148+
ImageView lockIcon = new ImageView(
149+
new Image(Objects.requireNonNull(getClass().getResourceAsStream("/assets/textures/lock.png"))));
136150
setLockProperties(lockIcon, isLocked);
137151

138152
Button shipButton = new Button();

src/main/resources/assets/translation/english.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
"yes": "Yes",
2525
"no": "No",
2626
"coin": "Coin",
27+
"total_coins": "Total Coins",
2728
"level": "Level",
2829
"ship_locked": "The spaceship is locked.",
2930
"weapon_locked": "The weapon is locked.",
30-
"unlock_highScore": "reach a high score of ## to unlock it.",
31+
"unlock_highScore": "Reach a high score of ## to unlock it.",
32+
"unlock_totalCoins": "Reach a total of ## coins to unlock it.",
3133
"ok": "ok"
3234
}

0 commit comments

Comments
 (0)