mirror of
https://github.com/schmaeddes/untitledTextAdventure.git
synced 2024-11-14 18:38:05 +01:00
Cleanup and simplification
This commit is contained in:
parent
75eb4771c3
commit
976acb8d41
13 changed files with 198 additions and 365 deletions
|
@ -1,6 +1,5 @@
|
|||
import java.io.IOException;
|
||||
|
||||
import javax.script.Invocable;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
|
@ -35,7 +34,6 @@ public class Main {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.exit(0);
|
||||
Parser parser = new Parser();
|
||||
try (GameLogic logic = new GameLogic(parser)) {
|
||||
logic.loadGameState("games/damnCoolTextAdventureFTW.json");
|
||||
|
|
|
@ -6,13 +6,8 @@ import java.util.HashMap;
|
|||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import game.logic.actionsystem.Action;
|
||||
import game.logic.actionsystem.PlayerAction;
|
||||
import game.logic.actionsystem.actions.GoDirection;
|
||||
import game.logic.actionsystem.actions.Open;
|
||||
import game.logic.actionsystem.actions.TakeFrom;
|
||||
import game.state.CircularLocationException;
|
||||
import game.state.Entity;
|
||||
import game.state.EntitySet;
|
||||
|
@ -35,135 +30,136 @@ public class GameLogic implements Closeable {
|
|||
// TODO setup code, load from json or so
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// final String NORTH = "north";
|
||||
// final String SOUTH = "south";
|
||||
final String EAST = "east";
|
||||
final String WEST = "west";
|
||||
final String INSIDE = "inside";
|
||||
final String OUTSIDE = "outside";
|
||||
Entity east = this.state.createEntity("east");
|
||||
Entity west = this.state.createEntity("west");
|
||||
Entity inside = this.state.createEntity("inside");
|
||||
Entity outside = this.state.createEntity("outside");
|
||||
EntitySet genericDirections = EntitySet.createPersistent("genericDirections");
|
||||
genericDirections.add(east, west);
|
||||
EntitySet houseDirections = EntitySet.createPersistent("houseDirections");
|
||||
houseDirections.add(inside, outside);
|
||||
|
||||
Entity forestPath01 = this.state.createEntity("forest_path_01");
|
||||
Entity clearing = this.state.createEntity("clearing");
|
||||
Entity houseOutside = this.state.createEntity("house_outside");
|
||||
Entity houseInside = this.state.createEntity("house_inside");
|
||||
Entity houseMainDoor = this.state.createEntity("house_main_door");
|
||||
houseMainDoor.setClosed(true);
|
||||
|
||||
forestPath01.connectBidirectional(EAST, null, WEST, clearing);
|
||||
forestPath01.connectBidirectional(WEST, null, EAST, houseOutside);
|
||||
houseOutside.connectBidirectional(INSIDE, houseMainDoor, OUTSIDE, houseInside);
|
||||
EntitySet locations = EntitySet.createPersistent("locations");
|
||||
locations.add(forestPath01, clearing, houseInside, houseOutside);
|
||||
|
||||
this.player = this.state.createEntity("player");
|
||||
Entity apple01 = this.state.createEntity("apple_01", "green");
|
||||
Entity apple02 = this.state.createEntity("apple_02", "red");
|
||||
forestPath01.connectBidirectional(east, west, clearing);
|
||||
forestPath01.connectBidirectional(west, east, houseOutside);
|
||||
houseOutside.connectBidirectional(inside, outside, houseInside);
|
||||
|
||||
this.player.setLocation(clearing);
|
||||
apple01.setLocation(forestPath01);
|
||||
apple02.setLocation(forestPath01);
|
||||
this.player = clearing.createContainedEntity(this, "player");
|
||||
Entity apple01 = forestPath01.createContainedEntity(this, "apple_01");
|
||||
Entity apple02 = forestPath01.createContainedEntity(this, "apple_02");
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// TODO game specific action parsers
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
EntitySet collectibles = EntitySet.createPersistent("collectibles");
|
||||
collectibles.add(apple01, apple02);
|
||||
|
||||
this.parser.pushActionParser(userInput -> userInput.get(0).equals("go")
|
||||
? new GoDirection(userInput.size() < 2 ? null : userInput.get(1))
|
||||
: null);
|
||||
this.parser.pushActionParser(userInput -> {
|
||||
if (userInput.get(0).equals("take")) {
|
||||
int fromIdx = userInput.indexOf("from");
|
||||
List<EntityDescription> whatToTake = this
|
||||
.parseDescriptionList(userInput.subList(1, fromIdx == -1 ? userInput.size() : fromIdx));
|
||||
List<EntityDescription> whereToTakeFrom = fromIdx == -1 ? null
|
||||
: this.parseDescriptionList(userInput.subList(fromIdx + 1, userInput.size()));
|
||||
return new TakeFrom(whatToTake, whereToTakeFrom);
|
||||
} else {
|
||||
return null;
|
||||
collectibles.pushPlayerAction("take", (logic, args) -> {
|
||||
Entity collectible = args[0];
|
||||
if (logic.getPlayer().getLocation() == collectible.getLocation()) {
|
||||
try {
|
||||
collectible.setLocation(logic.getPlayer());
|
||||
logic.printRaw("Du nimmst %s, du Schuft.\n", collectible);
|
||||
} catch (CircularLocationException ex) {
|
||||
// Should not happpen
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
this.parser.pushActionParser(userInput -> {
|
||||
if (userInput.get(0).equals("open")) {
|
||||
return new Open(this.parseDescriptionList(userInput.subList(1, userInput.size())));
|
||||
|
||||
// this.player becomes first argument by calling pushPlayerAction on it
|
||||
PlayerAction goAction = this.player.pushPlayerAction("go", (logic, args) -> {
|
||||
Entity character = args[0];
|
||||
Entity direction = args[1];
|
||||
Entity newLocation = character.getLocation().getConnectedEntity(direction);
|
||||
if (newLocation != null) {
|
||||
try {
|
||||
logic.getPlayer().setLocation(newLocation);
|
||||
logic.printRaw("Du gehst Richtung %s und landest hier: %s, du Lutscher!\n", direction, newLocation);
|
||||
} catch (CircularLocationException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
logic.printRaw("Hier geht es nicht nach %s, du Nichtsnutz.\n", direction);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
// second argument must be exactly 1 of the entites contained in genericDirections
|
||||
goAction.pushVaryingNeededEntites(genericDirections, 1);
|
||||
|
||||
// again first argument becomes this.player by calling pushPlayerAction on it
|
||||
PlayerAction goHouseAction = this.player.pushPlayerAction("go", (logic, args) -> {
|
||||
Entity character = args[0];
|
||||
Entity direction = args[1];
|
||||
Entity newLocation = character.getLocation().getConnectedEntity(direction);
|
||||
if (newLocation != null && character.getLocation() == houseInside
|
||||
|| character.getLocation() == houseOutside) {
|
||||
if (houseMainDoor.getBoolAttribute("open")) {
|
||||
try {
|
||||
character.setLocation(newLocation);
|
||||
} catch (CircularLocationException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
logic.printRaw("Du gehst durch die Tür, du Eumel.\n");
|
||||
} else {
|
||||
logic.printRaw("Die Tür ist zu, du Dödel.\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// second argument must be exactly 1 of the entites contained in houseDirections
|
||||
goHouseAction.pushVaryingNeededEntites(houseDirections, 1);
|
||||
|
||||
houseMainDoor.pushPlayerAction("open", (logic, args) -> {
|
||||
if (logic.getPlayer().getLocation() == houseInside || logic.getPlayer().getLocation() == houseOutside) {
|
||||
if (houseMainDoor.getBoolAttribute("open")) {
|
||||
logic.printRaw("Die Tür ist schon offen, du Hammel.\n");
|
||||
} else {
|
||||
houseMainDoor.setAttribute("open", true);
|
||||
logic.printRaw("Du öffnest die Tür, du Dummbatz.\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
houseMainDoor.pushPlayerAction("close", (logic, args) -> {
|
||||
if (logic.getPlayer().getLocation() == houseInside || logic.getPlayer().getLocation() == houseOutside) {
|
||||
if (!houseMainDoor.getBoolAttribute("open")) {
|
||||
logic.printRaw("Die Tür ist schon geschlossen, du Mummenschanz.\n");
|
||||
} else {
|
||||
houseMainDoor.setAttribute("open", false);
|
||||
logic.printRaw("Du schließt die Tür, du Angsthase.\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public GameState getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public Entity getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public EntitySet searchForEntity(EntityDescription description) {
|
||||
return this.state.searchForEntity(description);
|
||||
}
|
||||
|
||||
public EntitySet searchForEntity(EntityDescription description, Predicate<Entity> acceptFunction) {
|
||||
return this.state.searchForEntity(description).getFiltered(acceptFunction);
|
||||
}
|
||||
|
||||
public EntitySet searchForNearbyEntity(EntityDescription description) {
|
||||
return this.searchForEntity(description, e -> {
|
||||
if (this.player == null) {
|
||||
return false;
|
||||
} else if (this.player.contains(e, false)) {
|
||||
return true;
|
||||
} else if (this.player.getLocation() != null && this.player.getLocation().contains(e, false)) {
|
||||
return true;
|
||||
} else {
|
||||
for (Entity.EntityConnection c : this.player.getLocation().getConnections().stream()
|
||||
.map(this.player.getLocation()::getConnection).toList()) {
|
||||
if (c.associatedEntity() == e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public void mainLoop() {
|
||||
while (!this.discontinue) {
|
||||
Action action = this.parser.readAction();
|
||||
if (action != null) {
|
||||
action.execute(this);
|
||||
}
|
||||
this.parser.executeUserInput(this);
|
||||
}
|
||||
}
|
||||
|
||||
private List<EntityDescription> parseDescriptionList(List<String> words) {
|
||||
List<EntityDescription> descriptions = new LinkedList<>();
|
||||
List<String> desc = new LinkedList<>();
|
||||
for (String word : words) {
|
||||
if (word.equals("and")) {
|
||||
descriptions.add(new EntityDescription(desc));
|
||||
desc = new LinkedList<>();
|
||||
} else {
|
||||
desc.add(word);
|
||||
}
|
||||
}
|
||||
if (!desc.isEmpty()) {
|
||||
descriptions.add(new EntityDescription(desc));
|
||||
}
|
||||
return descriptions;
|
||||
}
|
||||
|
||||
public void printToUser(String messageId, Object... args) {
|
||||
System.out.print(messageId);
|
||||
for (int i = 0; i < args.length; ++i) {
|
||||
System.out.print(i == 0 ? " " : ", ");
|
||||
System.out.print(args[i]);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public boolean canPlayerOpen(Entity entity) {
|
||||
// TODO
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canPlayerTake(Entity entity) {
|
||||
// TODO
|
||||
return true;
|
||||
public void printRaw(String rawMessage, Object... args) {
|
||||
System.out.printf(rawMessage, args);
|
||||
}
|
||||
|
||||
public void registerPlayerAction(PlayerAction action) {
|
||||
|
|
|
@ -2,33 +2,53 @@ package game.logic;
|
|||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import game.logic.actionsystem.Action;
|
||||
import game.logic.actionsystem.ActionParser;
|
||||
import game.state.Entity;
|
||||
import game.state.EntitySet;
|
||||
import startup.Environment;
|
||||
import util.Commands;
|
||||
import util.TextColors;
|
||||
|
||||
public class Parser implements Closeable {
|
||||
private final Scanner scanner = new Scanner(System.in);
|
||||
private final List<ActionParser> actionParsers = new LinkedList<>();
|
||||
|
||||
public Action readAction() {
|
||||
public void executeUserInput(GameLogic logic) {
|
||||
// Big TODO
|
||||
// currently just reads an action id followed by entity ids
|
||||
|
||||
String greenPrompt = TextColors.BLUE.colorize(">");
|
||||
System.out.printf("%s ", greenPrompt);
|
||||
List<String> input = Arrays.stream(scanner.nextLine().split("\\s+")).map(String::toLowerCase).toList();
|
||||
|
||||
for (ActionParser actionParser : this.actionParsers) {
|
||||
Action action = actionParser.parseAction(input);
|
||||
if (action != null) {
|
||||
return action;
|
||||
if (!input.isEmpty()) {
|
||||
String actionId = input.get(0);
|
||||
List<Entity> args = new ArrayList<>(input.size() - 1);
|
||||
for (int i = 1; i < input.size(); ++i) {
|
||||
Entity e = logic.getState().getEntityById(input.get(i));
|
||||
if (e == null) {
|
||||
logic.printRaw("Keine Ahnung, was du mit %s meinst, du Tölpel.\n", input.get(i));
|
||||
return;
|
||||
}
|
||||
args.add(e);
|
||||
}
|
||||
Entity primaryEntity = this.getPrimaryEntity(logic, actionId, args);
|
||||
if (primaryEntity != null) {
|
||||
primaryEntity.tryExecutePlayerAction(actionId, EntitySet.createTemporary(args), logic);
|
||||
} else if(logic.tryExecutePlayerAction(actionId, EntitySet.createTemporary(args))) {
|
||||
logic.printRaw("Das geht doch so nicht.\n", args);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Entity getPrimaryEntity(GameLogic logic, String actionId, List<Entity> arguments) {
|
||||
return switch (actionId) {
|
||||
case "go" -> logic.getPlayer();
|
||||
case "take", "open", "close" -> arguments.remove(0);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
public void parse(List<String> parameter) {
|
||||
|
@ -40,10 +60,6 @@ public class Parser implements Closeable {
|
|||
}
|
||||
}
|
||||
|
||||
public void pushActionParser(ActionParser actionParser) {
|
||||
this.actionParsers.add(actionParser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.scanner.close();
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
package game.logic.actionsystem;
|
||||
|
||||
import game.logic.GameLogic;
|
||||
|
||||
public abstract class Action {
|
||||
public enum Type {
|
||||
TAKE, DROP,
|
||||
COMBINE_WITH, USE,
|
||||
GIVE_TO, TAKE_FROM,
|
||||
PUT_ON,
|
||||
PUSH, PULL, ROLL, ROLL_TO,
|
||||
TRACE_RAY_TO,
|
||||
KILL, KILL_WITH,
|
||||
SEARCH, SEARCH_FOR,
|
||||
TALK_TO, TELL_TO, ANNOY,
|
||||
LOOK_AT, EXAMINE, READ, WRITE_ON_WITH,
|
||||
HIT, HIT_WITH,
|
||||
GO_TO,
|
||||
}
|
||||
|
||||
public abstract void execute(GameLogic logic);
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package game.logic.actionsystem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ActionParser {
|
||||
public Action parseAction(List<String> userInput);
|
||||
}
|
|
@ -116,7 +116,6 @@ public class PlayerAction {
|
|||
if (Arrays.stream(entitiesToUse).anyMatch(Objects::isNull)) {
|
||||
return false;
|
||||
}
|
||||
this.executor.execute(logic, entitiesToUse);
|
||||
return true;
|
||||
return this.executor.execute(logic, entitiesToUse);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import game.state.Entity;
|
|||
* A player action executor provides specific instructions how to manipulate the
|
||||
* game logic.
|
||||
*/
|
||||
public class PlayerActionExecutor {
|
||||
public interface PlayerActionExecutor {
|
||||
|
||||
/**
|
||||
* Executes the game logic manupulation.
|
||||
|
@ -15,7 +15,5 @@ public class PlayerActionExecutor {
|
|||
* @param logic The game logic
|
||||
* @param args Arguments
|
||||
*/
|
||||
public void execute(GameLogic logic, Entity... args) {
|
||||
// TODO
|
||||
}
|
||||
public boolean execute(GameLogic logic, Entity... args);
|
||||
}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
package game.logic.actionsystem.actions;
|
||||
|
||||
import game.logic.GameLogic;
|
||||
import game.logic.actionsystem.Action;
|
||||
import game.state.CircularLocationException;
|
||||
import game.state.Entity;
|
||||
|
||||
public class GoDirection extends Action {
|
||||
private final String directionId;
|
||||
|
||||
public GoDirection(String directionId) {
|
||||
this.directionId = directionId;
|
||||
}
|
||||
|
||||
public String getDirectionId() {
|
||||
return this.directionId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(GameLogic logic) {
|
||||
if (logic.getPlayer() == null) {
|
||||
logic.printToUser("go.player.null");
|
||||
} else {
|
||||
Entity location = logic.getPlayer().getLocation();
|
||||
if (location == null) {
|
||||
logic.printToUser("go.player.location.null", logic.getPlayer());
|
||||
} else {
|
||||
Entity.EntityConnection connection = location.getConnection(this.directionId);
|
||||
if (connection == null || connection.to() == null) {
|
||||
logic.printToUser("go.direction.unknown", logic.getPlayer().getLocation(), this.directionId);
|
||||
} else if (connection.associatedEntity() != null && connection.associatedEntity().isClosed()) {
|
||||
logic.printToUser("go.location.closed", connection.associatedEntity());
|
||||
} else {
|
||||
try {
|
||||
logic.getPlayer().setLocation(connection.to());
|
||||
logic.printToUser("go.success", location, connection.to());
|
||||
} catch (CircularLocationException ex) {
|
||||
logic.printToUser("go.locationCircular", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package game.logic.actionsystem.actions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import game.logic.EntityDescription;
|
||||
import game.logic.GameLogic;
|
||||
import game.logic.actionsystem.Action;
|
||||
import game.state.Entity;
|
||||
import game.state.EntitySet;
|
||||
|
||||
public class Open extends Action {
|
||||
private final List<EntityDescription> descriptions;
|
||||
|
||||
public Open(List<EntityDescription> descriptions) {
|
||||
this.descriptions = descriptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(GameLogic logic) {
|
||||
for (EntitySet set : this.descriptions.stream().map(logic::searchForNearbyEntity).toList()) {
|
||||
Entity e = set.collapse(logic);
|
||||
if(e == null) {
|
||||
logic.printToUser("open.noEntity");
|
||||
} else if (logic.canPlayerOpen(e)) {
|
||||
e.setClosed(false);
|
||||
logic.printToUser("open.success", e);
|
||||
} else {
|
||||
logic.printToUser("open.failed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package game.logic.actionsystem.actions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import game.logic.EntityDescription;
|
||||
import game.logic.GameLogic;
|
||||
import game.logic.actionsystem.Action;
|
||||
import game.state.CircularLocationException;
|
||||
import game.state.Entity;
|
||||
import game.state.EntitySet;
|
||||
|
||||
public class TakeFrom extends Action {
|
||||
private final List<EntityDescription> what;
|
||||
private final List<EntityDescription> fromWhere;
|
||||
|
||||
public TakeFrom(List<EntityDescription> what, List<EntityDescription> fromWhere) {
|
||||
this.what = what;
|
||||
this.fromWhere = fromWhere;
|
||||
}
|
||||
|
||||
public List<EntityDescription> getWhat() {
|
||||
return this.what;
|
||||
}
|
||||
|
||||
public List<EntityDescription> getFromWhere() {
|
||||
return this.fromWhere;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(GameLogic logic) {
|
||||
// TODO incorporate fromWhere
|
||||
for (EntityDescription ed : this.what) {
|
||||
EntitySet es = logic.searchForNearbyEntity(ed);
|
||||
if(es.isEmpty()) {
|
||||
logic.printToUser("entity.notFound", ed);
|
||||
}
|
||||
Entity e = es.collapse(logic);
|
||||
if (e != null && logic.canPlayerTake(e)) {
|
||||
try {
|
||||
e.setLocation(logic.getPlayer());
|
||||
logic.printToUser("take.success", e);
|
||||
} catch (CircularLocationException ex) {
|
||||
logic.printToUser("take.error", ex);
|
||||
}
|
||||
} else {
|
||||
logic.printToUser("take.failed", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package game.state;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
@ -24,21 +23,16 @@ import game.logic.actionsystem.PlayerActionExecutor;
|
|||
* can store generic attributes.
|
||||
*/
|
||||
public class Entity {
|
||||
public record EntityConnection(Entity to, Entity associatedEntity) {
|
||||
}
|
||||
|
||||
private final String id;
|
||||
private final Set<String> attributes = new HashSet<>();
|
||||
private Entity location;
|
||||
private boolean closed = false;
|
||||
private final EntitySet contents;
|
||||
private final Map<String, EntityConnection> connections = new HashMap<>();
|
||||
private final Map<String, List<PlayerAction>> playerActions = new HashMap<>();
|
||||
final Set<EntitySet> containingPersistentSets = new HashSet<>();
|
||||
private final Map<String, String> attributes = new HashMap<>();
|
||||
private Entity location;
|
||||
private final EntitySet contents;
|
||||
private final Map<Entity, Entity> connections = new HashMap<>();
|
||||
private final Map<String, List<PlayerAction>> playerActions = new HashMap<>();
|
||||
|
||||
public Entity(String id, String... attributes) {
|
||||
public Entity(String id) {
|
||||
this.id = id;
|
||||
this.attributes.addAll(Arrays.asList(attributes));
|
||||
this.contents = EntitySet.createPersistent(this.id + "::contents");
|
||||
}
|
||||
|
||||
|
@ -46,30 +40,25 @@ public class Entity {
|
|||
return this.id;
|
||||
}
|
||||
|
||||
public Set<String> getAttributes() {
|
||||
return Collections.unmodifiableSet(this.attributes);
|
||||
public String getAttribute(String key) {
|
||||
String value = this.attributes.get(key);
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
public boolean addAttribute(String attribute) {
|
||||
return this.attributes.add(attribute);
|
||||
public boolean getBoolAttribute(String key) {
|
||||
return Boolean.parseBoolean(this.getAttribute(key));
|
||||
}
|
||||
|
||||
public boolean removeAttribute(String attribute) {
|
||||
return this.attributes.remove(attribute);
|
||||
}
|
||||
|
||||
public void toggleAttribute(String attribute) {
|
||||
if (!this.attributes.remove(attribute)) {
|
||||
this.attributes.add(attribute);
|
||||
public void setAttribute(String key, Object value) {
|
||||
if (value == null) {
|
||||
this.attributes.remove(key);
|
||||
} else {
|
||||
this.attributes.put(key, value.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void switchAttribute(String attr1, String attr2) {
|
||||
if (this.attributes.remove(attr1)) {
|
||||
this.attributes.add(attr2);
|
||||
} else if (this.attributes.remove(attr2)) {
|
||||
this.attributes.add(attr1);
|
||||
}
|
||||
public void removeAttribute(String key) {
|
||||
this.setAttribute(key, null);
|
||||
}
|
||||
|
||||
public void setLocation(Entity location) throws CircularLocationException {
|
||||
|
@ -88,18 +77,21 @@ public class Entity {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return this.closed;
|
||||
}
|
||||
|
||||
public void setClosed(boolean closed) {
|
||||
this.closed = closed;
|
||||
}
|
||||
|
||||
public boolean contains(Entity other, boolean recursive) {
|
||||
return this.contents.getAll().stream().anyMatch(e -> e == other || (recursive && e.contains(other, true)));
|
||||
}
|
||||
|
||||
public Entity createContainedEntity(GameLogic logic, String id) {
|
||||
Entity e = logic.getState().createEntity(id);
|
||||
try {
|
||||
e.setLocation(this);
|
||||
} catch (CircularLocationException ex) {
|
||||
// should not happen
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
public Entity getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
@ -108,32 +100,31 @@ public class Entity {
|
|||
return this.contents.getAll();
|
||||
}
|
||||
|
||||
public void connectUnidirectional(String directionId, Entity to, Entity associatedEntity) {
|
||||
this.connections.put(directionId, new EntityConnection(to, associatedEntity));
|
||||
public void connectUnidirectional(Entity direction, Entity to) {
|
||||
this.connections.put(direction, to);
|
||||
}
|
||||
|
||||
public void connectBidirectional(String dirIdFromThisToOther, Entity associatedEntity, String dirIdFromOtherToThis,
|
||||
Entity to) {
|
||||
this.connections.put(dirIdFromThisToOther, new EntityConnection(to, associatedEntity));
|
||||
to.connections.put(dirIdFromOtherToThis, new EntityConnection(this, associatedEntity));
|
||||
public void connectBidirectional(Entity dirFromThisToOther, Entity dirFromOtherToThis, Entity other) {
|
||||
this.connections.put(dirFromThisToOther, other);
|
||||
other.connections.put(dirFromOtherToThis, this);
|
||||
}
|
||||
|
||||
public void removeSingleConnection(String directionId) {
|
||||
this.connections.remove(directionId);
|
||||
public void removeSingleConnection(Entity direction) {
|
||||
this.connections.remove(direction);
|
||||
}
|
||||
|
||||
public void removeBidirectionalConnection(String dirIdFromThisToOther, String dirIdFromOtherToThis) {
|
||||
EntityConnection c = this.connections.remove(dirIdFromThisToOther);
|
||||
if (c != null) {
|
||||
c.to.connections.remove(dirIdFromOtherToThis);
|
||||
public void removeBidirectionalConnection(Entity dirFromThisToOther, Entity dirFromOtherToThis) {
|
||||
Entity other = this.connections.remove(dirFromThisToOther);
|
||||
if (other != null && other.connections.get(dirFromOtherToThis) == this) {
|
||||
other.connections.remove(dirFromOtherToThis);
|
||||
}
|
||||
}
|
||||
|
||||
public EntityConnection getConnection(String directionId) {
|
||||
return this.connections.get(directionId);
|
||||
public Entity getConnectedEntity(Entity direction) {
|
||||
return this.connections.get(direction);
|
||||
}
|
||||
|
||||
public Set<String> getConnections() {
|
||||
public Set<Entity> getConnectionDirections() {
|
||||
return Collections.unmodifiableSet(this.connections.keySet());
|
||||
}
|
||||
|
||||
|
|
|
@ -33,15 +33,12 @@ public class EntitySet {
|
|||
}
|
||||
|
||||
private final String name;
|
||||
private final Set<Entity> entities;
|
||||
private final Set<Entity> entities = new HashSet<>();
|
||||
private final Map<String, List<PlayerAction>> playerActions = new HashMap<>();
|
||||
|
||||
private EntitySet(String name, Collection<Entity> entities) {
|
||||
this.name = name;
|
||||
this.entities = new HashSet<>(entities);
|
||||
if (this.name != null) {
|
||||
this.entities.forEach(e -> e.containingPersistentSets.add(this));
|
||||
}
|
||||
this.add(entities.toArray(new Entity[0]));
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
|
@ -56,24 +53,20 @@ public class EntitySet {
|
|||
return this.entities.size();
|
||||
}
|
||||
|
||||
public boolean add(Entity entity) {
|
||||
if (this.entities.add(entity)) {
|
||||
if (this.name != null) {
|
||||
entity.containingPersistentSets.add(this);
|
||||
public void add(Entity... entities) {
|
||||
for (Entity e : entities) {
|
||||
if (this.entities.add(e) && this.name != null) {
|
||||
e.containingPersistentSets.add(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean remove(Entity entity) {
|
||||
if(this.entities.remove(entity)) {
|
||||
if(this.name != null) {
|
||||
entity.containingPersistentSets.remove(this);
|
||||
public void remove(Entity... entities) {
|
||||
for (Entity e : entities) {
|
||||
if (this.entities.remove(e) && this.name != null) {
|
||||
e.containingPersistentSets.remove(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Entity entity) {
|
||||
|
|
|
@ -1,25 +1,22 @@
|
|||
package game.state;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import game.logic.EntityDescription;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GameState {
|
||||
private final List<Entity> entities = new LinkedList<>();
|
||||
private final Map<String, Entity> entities = new HashMap<>();
|
||||
|
||||
public GameState() {
|
||||
|
||||
}
|
||||
|
||||
public Entity createEntity(String id, String... attributes) {
|
||||
Entity e = new Entity(id, attributes);
|
||||
this.entities.add(e);
|
||||
public Entity createEntity(String id) {
|
||||
Entity e = new Entity(id);
|
||||
this.entities.put(id, e);
|
||||
return e;
|
||||
}
|
||||
|
||||
public EntitySet searchForEntity(EntityDescription description) {
|
||||
return EntitySet.createTemporary(this.entities.stream().filter(e -> e.getId().equals(description.getMainWord())
|
||||
&& e.getAttributes().containsAll(description.getAttributes())).toList());
|
||||
public Entity getEntityById(String id) {
|
||||
return this.entities.get(id);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue