Skip to content

Commit

Permalink
Add some basic types for entities.
Browse files Browse the repository at this point in the history
Probably much revision still to come.
  • Loading branch information
kingjon3377 committed Dec 11, 2023
1 parent cc014f3 commit 45acd48
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
21 changes: 21 additions & 0 deletions model/src/main/java/common/entity/ComplexIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package common.entity;

import org.jetbrains.annotations.NotNull;

import java.util.UUID;

/**
* A way to identify entities that should be much more resistant to collisions than a simple increasing integer.
*
* @param originWorld The game-world where this entity originated, in case it travels to anotheer world.
* @param creatingPlayer The ID numbeer of the player whose action caused the entity to be created, or a negative ID
* if it was not created due to player action.
* @param id A unique identifying number for the entity.
*/
public record ComplexIdentifier(@NotNull String originWorld, int creatingPlayer, @NotNull UUID id) {
public ComplexIdentifier {
if (creatingPlayer < -1) {
throw new IllegalArgumentException("Player ID must be -1 if unknown, or nonnegative");
}
}
}
25 changes: 25 additions & 0 deletions model/src/main/java/common/entity/CoordinateLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package common.entity;

import org.jetbrains.annotations.NotNull;

/**
* An entity can be located in a particular region of a map at particular coordinates. Note that this class cannot
* check region or coordinate upper bounds.
*
* @param world The world in which the region is located
* @param region The ID of the region. TODO: Is this how we want to identify regions?
* @param row The row within the region where the entity is located
* TODO: Is row/column how we want to represent position within a region?
* @param column The column within the region where the entity is located
*/
public record CoordinateLocation(@NotNull String world, int region, int row, int column) implements Location {
public CoordinateLocation {
if (region < -1) {
throw new IllegalArgumentException("Region must be -1 if unknown, or nonnegative");
} else if (region < 0 && (row >= 0 || column >= 0)) {
throw new IllegalArgumentException("Coordinates cannot be known if region is unknown");
} else if ((row < 0 && column >= 0) || (row >= 0 && column < 0)) {
throw new IllegalArgumentException("Either both or neither row and column must be valid");
}
}
}
67 changes: 67 additions & 0 deletions model/src/main/java/common/entity/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package common.entity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class Entity implements IEntity {
public Entity(final @NotNull EntityIdentifier id, final @NotNull Location location) {
this.location = location;
this.id = id;
}
private @NotNull Location location;
private final @NotNull EntityIdentifier id;
private final Map<String, Object> properties = new HashMap<>();
@Override
public final @NotNull Location getLocation() {
return location;
}

public final void setLocation(final @NotNull Location location) {
this.location = location;
}
@Override
public final @NotNull EntityIdentifier getId() {
return id;
}

@Override
public final boolean hasProperty(final @NotNull String propertyName) {
return properties.containsKey(propertyName);
}

@Override
public final @Nullable Class<?> getPropertyType(final @NotNull String propertyName) {
final Object property = properties.get(propertyName);
if (null == property) {
return null;
} else {
return property.getClass();
}
}

@Override
public final @Nullable Object getProperty(final @NotNull String propertyName) {
return properties.get(propertyName);
}

@Override
public final <T> @Nullable T getTypedProperty(final @NotNull String propertyName, final @NotNull Class<T> type) {
final Object retval = properties.get(propertyName);
if (type.isInstance(retval)) {
return type.cast(retval);
} else {
return null;
}
}

public void setProperty(final @NotNull String propertyName, final @Nullable Object value) {
if (value == null) {
properties.remove(propertyName);
} else {
properties.put(propertyName, value);
}
}
}
10 changes: 10 additions & 0 deletions model/src/main/java/common/entity/EntityIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common.entity;

/**
* An interface for ways of identifying entities in the game-world. This is an interface to possibly allow us to import
* maps from the 2009-2022 campaign, and to allow us to extend the format over time without breaking changes.
*
* TODO: What should the interface contain?
*/
public interface EntityIdentifier {
}
16 changes: 16 additions & 0 deletions model/src/main/java/common/entity/IEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package common.entity;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* An interface for game-world entities.
*/
public interface IEntity {
@NotNull Location getLocation();
@NotNull EntityIdentifier getId();
boolean hasProperty(@NotNull String propertyName);
@Nullable Class<?> getPropertyType(@NotNull String propertyName);
@Nullable Object getProperty(@NotNull String propertyName);
@Nullable <T> T getTypedProperty(@NotNull String propertyName, @NotNull Class<T> type);
}
11 changes: 11 additions & 0 deletions model/src/main/java/common/entity/InParentLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common.entity;

import org.jetbrains.annotations.NotNull;

/**
* An entity can be located within, or "at the same location as", another entity. Care will need to be taken to ensure
* there are no location loops.
* @param parent The entity that is the parent of the one this location object is embedded in.
*/
public record InParentLocation(@NotNull EntityIdentifier parent) implements Location {
}
17 changes: 17 additions & 0 deletions model/src/main/java/common/entity/LegacyIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package common.entity;

/**
* In the 2009-2022 campaing, "fixtures" in the map were identified using a single integer, starting from 0 and
* counting up.
*
* TODO: Maybe add an "origin world" parameter, defaulting to "prime"?
*
* @param idNumber the ID number
*/
public record LegacyIdentifier(int idNumber) implements EntityIdentifier {
public LegacyIdentifier {
if (idNumber < -1) {
throw new IllegalArgumentException("ID number must be -1 if unspecified, or else nonnegative");
}
}
}
11 changes: 11 additions & 0 deletions model/src/main/java/common/entity/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common.entity;

/**
* An inteerface for where something in the game-world could be located. A marker interface to start with, since
* we will want to support both "inside such-and-such parent," "somewhere in such-and-such region," and "at
* such-and-such coordiantes in such-and-such region".
*
* TODO: What should the interface contain?
*/
public interface Location {
}
14 changes: 14 additions & 0 deletions model/src/main/java/common/entity/RegionLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common.entity;

import org.jetbrains.annotations.NotNull;

/**
* An entity can be located at an unspecified location in a region.
*/
public record RegionLocation(@NotNull String world, int region) implements Location {
public RegionLocation {
if (region < -1) {
throw new IllegalArgumentException("Region must be -1 if unspecified, or else non-negative");
}
}
}

0 comments on commit 45acd48

Please sign in to comment.