feat: events

This commit is contained in:
2025-05-28 11:30:16 +02:00
parent 5eb6b8c2b5
commit 3bb9156ea8
5 changed files with 70 additions and 6 deletions

View File

@@ -10,9 +10,12 @@ import cz.jull.tui.Cli;
import cz.jull.tui.Menu; import cz.jull.tui.Menu;
import cz.jull.tui.Strings; import cz.jull.tui.Strings;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import org.w3c.dom.events.Event;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random;
@Getter @Getter
public class Game { public class Game {
@@ -29,7 +32,8 @@ public class Game {
private final Strings strings; private final Strings strings;
private final List<Item> buildings = new ArrayList<>(); @Setter
private List<Item> buildings = new ArrayList<>();
private Market market; private Market market;
@@ -37,10 +41,18 @@ public class Game {
private int milkedToday = 0; private int milkedToday = 0;
private Events currentEvent;
public Game(Strings strings) { public Game(Strings strings) {
this.strings = strings; this.strings = strings;
} }
private Events getRandomEvent() {
Random random = new Random();
return events.get(random.nextInt(events.size()));
}
public void play() { public void play() {
strings.print("welcome"); strings.print("welcome");
Cli.pressEnter(); Cli.pressEnter();
@@ -48,6 +60,10 @@ public class Game {
Menu menu = new Menu(this); Menu menu = new Menu(this);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
currentEvent = getRandomEvent();
strings.print("event", currentEvent);
currentEvent.getAction().accept(this);
menu.start(i); menu.start(i);
long wheatSeedsCount = buildings.stream().filter(item -> item == Item.WHEAT_SEEDS).count(); long wheatSeedsCount = buildings.stream().filter(item -> item == Item.WHEAT_SEEDS).count();
for (int j = 0; j < wheatSeedsCount; j++) { for (int j = 0; j < wheatSeedsCount; j++) {

View File

@@ -1,8 +1,45 @@
package cz.jull.stats; package cz.jull.stats;
import cz.jull.Game;
import cz.jull.Item;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public enum Events { public enum Events {
PEACEFUL_ROUND, PEACEFUL_ROUND("Peaceful round. enjoy!", game -> {
FLOOD, }),
ATTACK, FLOOD("Flood", game -> {
FIRE game.getStrings().print("eventFlood");
game.setBuildings(new ArrayList<>(
game.getBuildings().stream().filter(
building -> !List.of(Item.WHEAT_FARMLAND, Item.HOPS_FARMLAND, Item.VINEYARD).contains(building)
).toList())
);
}),
ATTACK("Attack", game -> {
game.getStrings().print("eventAttack");
game.getPlayer().setCoins(game.getPlayer().getCoins() - 100);
}),
FIRE("Fire", game -> {
game.getStrings().print("eventFire");
game.getForestType().setSize(game.getForestType().getSize() - 10);
});
private final String name;
@Getter
private final Consumer<Game> action;
Events(String name, Consumer<Game> action) {
this.name = name;
this.action = action;
}
@Override
public String toString() {
return name;
}
} }

View File

@@ -40,6 +40,7 @@ public class Cli {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
int choice; int choice;
System.out.println();
System.out.println("Chose an option: "); System.out.println("Chose an option: ");
for (int i = 0; i < options.size(); i++) { for (int i = 0; i < options.size(); i++) {
System.out.println((i + 1) + ". " + options.get(i)); System.out.println((i + 1) + ". " + options.get(i));

View File

@@ -41,6 +41,10 @@ public class Strings {
private String currentDay; private String currentDay;
private String cantMilkToMuch; private String cantMilkToMuch;
private String noTrees; private String noTrees;
private String event;
private String eventFire;
private String eventFlood;
private String eventAttack;
/** /**
* Loads strings from json file * Loads strings from json file
@@ -53,6 +57,7 @@ public class Strings {
} }
/** /**
* Generated AI
* Prints out a String based on the prop * Prints out a String based on the prop
* @param prop The name of the field * @param prop The name of the field
*/ */
@@ -68,6 +73,7 @@ public class Strings {
} }
/** /**
* Generated AI
* Prints out a template with string inserted based on the prop * Prints out a template with string inserted based on the prop
* @param prop The name of the field * @param prop The name of the field
* @param values Values for the template * @param values Values for the template

View File

@@ -26,5 +26,9 @@
"stats": "Your stats: ", "stats": "Your stats: ",
"currentDay": "Day: {0}", "currentDay": "Day: {0}",
"cantMilkToMuch": "You milked the cow already 3 times today!", "cantMilkToMuch": "You milked the cow already 3 times today!",
"noTrees": "You don't have any trees left!" "noTrees": "You don't have any trees left!",
"event": "Event for this day: {0}",
"eventFire": "Fire! You lost 10 trees...",
"eventFlood": "Flood! You lost All of your farmlands... If you had them.",
"eventAttack": "Attack! Your villagers got robbed and you lost 100 coins..."
} }