From f76a59215c3f5e6c08cf339cf6f471fd4af1d2a9 Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 5 Jan 2025 13:53:53 -0500 Subject: [PATCH] world generation --- .../sim/character/Character.java | 45 +++++++++++++++ .../sim/character/gen/CharacterGenerator.java | 1 + .../sim/character/gen/PlayerCharSourcer.java | 7 ++- .../org/studiorailgun/sim/space/Location.java | 55 +++++++++++++++++++ .../org/studiorailgun/sim/space/Region.java | 49 ++++++++++------- .../org/studiorailgun/sim/space/World.java | 24 ++++++++ .../sim/space/gen/BuildingGenerator.java | 2 + .../sim/space/gen/LocationGenerator.java | 20 +++++++ .../sim/space/gen/TownGenerator.java | 2 +- .../sim/space/gen/WorldGenerator.java | 30 ++++++++++ .../sim/space/util/LocationResolver.java | 24 ++++++++ 11 files changed, 238 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/studiorailgun/sim/space/gen/LocationGenerator.java create mode 100644 src/main/java/org/studiorailgun/sim/space/util/LocationResolver.java diff --git a/src/main/java/org/studiorailgun/sim/character/Character.java b/src/main/java/org/studiorailgun/sim/character/Character.java index 0143e82..5fe580f 100644 --- a/src/main/java/org/studiorailgun/sim/character/Character.java +++ b/src/main/java/org/studiorailgun/sim/character/Character.java @@ -1,13 +1,20 @@ package org.studiorailgun.sim.character; +import org.studiorailgun.Globals; import org.studiorailgun.sim.character.emotion.EmotionData; import org.studiorailgun.sim.character.vis.CharacterAppearance; +import org.studiorailgun.sim.space.Location; /** * A reasoning entity being simulated in the game world */ public class Character { + /** + * An unassigned location + */ + public static final int LOCATION_UNASSIGNED = -1; + /** * The id of the character */ @@ -23,12 +30,32 @@ public class Character { */ EmotionData emotions; + /** + * The id of the location of this character is in + */ + int locationId; + /** * Simulates this character */ public void simulate(){ } + /** + * Moves this character to the destination + * @param destination The destination location + */ + public void move(Location destination){ + //remove from old pos + Location oldLoc = Globals.world.getLocation(locationId); + if(oldLoc != null){ + oldLoc.removeCharacter(this); + } + //add to new pos + destination.addCharacter(this); + locationId = destination.getId(); + } + /** * Gets the appearance of the character * @return The appearance of the character @@ -61,4 +88,22 @@ public class Character { this.emotions = emotions; } + /** + * Gets the id of the location this character is at + * @return The id of the location this character is at + */ + public int getLocationId() { + return locationId; + } + + /** + * Sets the id of the location this character is at + * @param locationId The id of the location this character is at + */ + public void setLocationId(int locationId) { + this.locationId = locationId; + } + + + } diff --git a/src/main/java/org/studiorailgun/sim/character/gen/CharacterGenerator.java b/src/main/java/org/studiorailgun/sim/character/gen/CharacterGenerator.java index bc1c829..209beb5 100644 --- a/src/main/java/org/studiorailgun/sim/character/gen/CharacterGenerator.java +++ b/src/main/java/org/studiorailgun/sim/character/gen/CharacterGenerator.java @@ -13,6 +13,7 @@ public class CharacterGenerator { */ public static Character generateCharacter(){ Character rVal = new Character(); + rVal.setLocationId(Character.LOCATION_UNASSIGNED); return rVal; } diff --git a/src/main/java/org/studiorailgun/sim/character/gen/PlayerCharSourcer.java b/src/main/java/org/studiorailgun/sim/character/gen/PlayerCharSourcer.java index 8da5070..ee1c6b5 100644 --- a/src/main/java/org/studiorailgun/sim/character/gen/PlayerCharSourcer.java +++ b/src/main/java/org/studiorailgun/sim/character/gen/PlayerCharSourcer.java @@ -1,6 +1,9 @@ package org.studiorailgun.sim.character.gen; +import org.studiorailgun.Globals; import org.studiorailgun.sim.character.Character; +import org.studiorailgun.sim.space.Location; +import org.studiorailgun.sim.space.util.LocationResolver; /** * Sources the player character (ie either by prompting the player, loading from file, generating, etc) @@ -12,7 +15,9 @@ public class PlayerCharSourcer { * @return The player's character */ public static Character getPlayerCharacter(){ - Character rVal = new Character(); + Character rVal = CharacterGenerator.generateCharacter(); + Location first = LocationResolver.first(Globals.world); + rVal.move(first); return rVal; } diff --git a/src/main/java/org/studiorailgun/sim/space/Location.java b/src/main/java/org/studiorailgun/sim/space/Location.java index f696065..2be6f9f 100644 --- a/src/main/java/org/studiorailgun/sim/space/Location.java +++ b/src/main/java/org/studiorailgun/sim/space/Location.java @@ -16,6 +16,11 @@ public class Location { * The id of the location */ int id; + + /** + * The type of the location + */ + String type; /** * The characters currently in this region @@ -27,6 +32,30 @@ public class Location { */ transient List items = new LinkedList(); + /** + * Constructor + * @param type The type of the location + */ + public Location(String type){ + this.type = type; + } + + /** + * Removes a character from this location + * @param character The character + */ + public void removeCharacter(Character character){ + this.chars.remove(character); + } + + /** + * Adds a character to this location + * @param character The character + */ + public void addCharacter(Character character){ + this.chars.add(character); + } + /** * Gets the characters in this location * @return The characters in this location @@ -58,6 +87,32 @@ public class Location { public void setItems(List items) { this.items = items; } + + /** + * Gets the type of the location + * @return The type of the location + */ + public String getType() { + return type; + } + + /** + * Sets the type of the location + * @param type The type of the location + */ + public void setType(String type) { + this.type = type; + } + + /** + * Gets the id of this location + * @return The id of this location + */ + public int getId() { + return id; + } + + diff --git a/src/main/java/org/studiorailgun/sim/space/Region.java b/src/main/java/org/studiorailgun/sim/space/Region.java index a2020bb..40d2c18 100644 --- a/src/main/java/org/studiorailgun/sim/space/Region.java +++ b/src/main/java/org/studiorailgun/sim/space/Region.java @@ -7,6 +7,11 @@ import java.util.List; * A recursive tree of regions that contain locations at their leaves */ public class Region { + + /** + * The id of the region + */ + int id; /** * The child reglions of this region @@ -14,9 +19,9 @@ public class Region { transient List children = new LinkedList(); /** - * The location data for this leaf node + * The discrete locations that are within this region */ - Location leaf; + transient List locations = new LinkedList(); /** * Gets the child regions of this region @@ -34,22 +39,6 @@ public class Region { this.children = children; } - /** - * Gets the leaf location of this region - * @return The leaf location of this region - */ - public Location getLeaf() { - return leaf; - } - - /** - * Sets the leaf location of this region - * @param leaf The leaf location of this region - */ - public void setLeaf(Location leaf) { - this.leaf = leaf; - } - /** * Adds a child region * @param child The child region @@ -58,6 +47,28 @@ public class Region { children.add(child); } - + /** + * Gets the locations of this region + * @return The locations of this region + */ + public List getLocations() { + return locations; + } + + /** + * Sets the locations of this region + * @param locations The locations of this region + */ + public void setLocations(List locations) { + this.locations = locations; + } + + /** + * Adds a location to the region + * @param location The location + */ + public void addLocation(Location location){ + this.locations.add(location); + } } diff --git a/src/main/java/org/studiorailgun/sim/space/World.java b/src/main/java/org/studiorailgun/sim/space/World.java index 28817c9..384ce4d 100644 --- a/src/main/java/org/studiorailgun/sim/space/World.java +++ b/src/main/java/org/studiorailgun/sim/space/World.java @@ -1,7 +1,9 @@ package org.studiorailgun.sim.space; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.studiorailgun.sim.character.Character; @@ -20,6 +22,11 @@ public class World { */ List characters = new LinkedList(); + /** + * Map of location id -> location + */ + transient Map idLocationMap = new HashMap(); + /** * Gets the top level region of this world * @return The top level region of this world @@ -52,6 +59,23 @@ public class World { this.characters = characters; } + /** + * Registers a location + * @param location The location + */ + public void registerLocation(Location location){ + this.idLocationMap.put(location.getId(), location); + } + + /** + * Gets a location based on its id + * @param id The id of the location + * @return The location if it exists, null otherwise + */ + public Location getLocation(int id){ + return this.idLocationMap.get(id); + } + } diff --git a/src/main/java/org/studiorailgun/sim/space/gen/BuildingGenerator.java b/src/main/java/org/studiorailgun/sim/space/gen/BuildingGenerator.java index 799905e..f7c7e55 100644 --- a/src/main/java/org/studiorailgun/sim/space/gen/BuildingGenerator.java +++ b/src/main/java/org/studiorailgun/sim/space/gen/BuildingGenerator.java @@ -14,6 +14,8 @@ public class BuildingGenerator { public static Region generateBuilding(){ Region rVal = new Region(); + rVal.addLocation(LocationGenerator.generate("Entrance")); + return rVal; } diff --git a/src/main/java/org/studiorailgun/sim/space/gen/LocationGenerator.java b/src/main/java/org/studiorailgun/sim/space/gen/LocationGenerator.java new file mode 100644 index 0000000..d5b0fec --- /dev/null +++ b/src/main/java/org/studiorailgun/sim/space/gen/LocationGenerator.java @@ -0,0 +1,20 @@ +package org.studiorailgun.sim.space.gen; + +import org.studiorailgun.sim.space.Location; + +/** + * Generates a location + */ +public class LocationGenerator { + + /** + * Generates a location + * @param type The type of the location + * @return The location + */ + public static Location generate(String type){ + Location rVal = new Location(type); + return rVal; + } + +} diff --git a/src/main/java/org/studiorailgun/sim/space/gen/TownGenerator.java b/src/main/java/org/studiorailgun/sim/space/gen/TownGenerator.java index f0efe82..4bc41ea 100644 --- a/src/main/java/org/studiorailgun/sim/space/gen/TownGenerator.java +++ b/src/main/java/org/studiorailgun/sim/space/gen/TownGenerator.java @@ -14,7 +14,7 @@ public class TownGenerator { public static Region generateTown(){ Region rVal = new Region(); - Region building = new Region(); + Region building = BuildingGenerator.generateBuilding(); rVal.addChild(building); return rVal; diff --git a/src/main/java/org/studiorailgun/sim/space/gen/WorldGenerator.java b/src/main/java/org/studiorailgun/sim/space/gen/WorldGenerator.java index 935138d..7d2dda0 100644 --- a/src/main/java/org/studiorailgun/sim/space/gen/WorldGenerator.java +++ b/src/main/java/org/studiorailgun/sim/space/gen/WorldGenerator.java @@ -1,6 +1,13 @@ package org.studiorailgun.sim.space.gen; +import java.util.List; + +import org.studiorailgun.sim.space.Location; import org.studiorailgun.sim.space.World; +import org.studiorailgun.sim.space.util.LocationResolver; +import org.studiorailgun.Globals; +import org.studiorailgun.sim.character.Character; +import org.studiorailgun.sim.character.gen.CharacterGenerator; /** * Generates a world @@ -13,8 +20,31 @@ public class WorldGenerator { */ public static World generateWorld(){ World rVal = new World(); + Globals.world = rVal; + //generate the regions + rVal.setRegion(TownGenerator.generateTown()); + + //generate the characters + WorldGenerator.generateCharacters(rVal); + return rVal; } + /** + * Populates the world with characters + * @param world The world + */ + private static void generateCharacters(World world){ + List characters = world.getCharacters(); + + //create the character + Character newChar = CharacterGenerator.generateCharacter(); + characters.add(newChar); + + //place the character in the world + Location placementDest = LocationResolver.first(world); + newChar.move(placementDest); + } + } diff --git a/src/main/java/org/studiorailgun/sim/space/util/LocationResolver.java b/src/main/java/org/studiorailgun/sim/space/util/LocationResolver.java new file mode 100644 index 0000000..3548c05 --- /dev/null +++ b/src/main/java/org/studiorailgun/sim/space/util/LocationResolver.java @@ -0,0 +1,24 @@ +package org.studiorailgun.sim.space.util; + +import org.studiorailgun.sim.space.Location; +import org.studiorailgun.sim.space.Region; +import org.studiorailgun.sim.space.World; + +/** + * Utilities for resolving locations + */ +public class LocationResolver { + + /** + * Gets the first location available + * @return The location + */ + public static Location first(World world){ + Region root = world.getRegion(); + while(root.getLocations().size() < 1){ + root = root.getChildren().get(0); + } + return root.getLocations().get(0); + } + +}