diff --git a/src/main/java/cactusServer/entities/Character.java b/src/main/java/cactusServer/entities/Character.java index 220ab11..2661bc2 100644 --- a/src/main/java/cactusServer/entities/Character.java +++ b/src/main/java/cactusServer/entities/Character.java @@ -1,63 +1,115 @@ package cactusServer.entities; import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.UUID; import framework.model3D.Position3D; import framework.physics.AngularVelocity3D; import framework.physics.Velocity3D; +import net.arnx.jsonic.JSONHint; public class Character { - private URI account_uri; + private URI accountURI; private String name; private Position3D position; // private Angle angle; -// private Camera camera_state; -// private Emote emote_state; +// private Camera cameraState; +// private Emote emoteState; // private Model model; - private String area_id; - private boolean is_active; - + private String areaID; + private boolean isActive; + @JSONHint(ignore = true) HashMap itemMap = new HashMap<>(); + @JSONHint(ignore = true) private static final int UNIQUE_ID_LENGTH = 12; + public Character(URI accountURI, String name, Position3D position, String areaId, boolean isActive) { - this.account_uri = accountURI; + this.accountURI = accountURI; this.name = name; this.position = position; - this.area_id = areaId; - this.is_active = isActive; + this.areaID = areaId; + this.isActive = isActive; } public URI getAccountURI() { - return account_uri; + return accountURI; } - - public void setAccountURI(URI accountURI) { - this.account_uri = accountURI; - } - + public String getName() { return name; } + public Position3D getPosition() { + return position; + } + + public String getAreaId() { + return areaID; + } + + public boolean isAactive() { + return isActive; + } + + public HashMap getItemMap() { + return itemMap; + } + + public Item getItemMap(String itemId) { + return itemMap.get(itemId); + } + + public void setAccountURI(URI accountURI) { + this.accountURI = accountURI; + } + public void setName(String name) { this.name = name; } - public Position3D getPosition() { - return position; - } - public void setPosition(Position3D position) { this.position = position; } - public boolean isAactive() { - return is_active; - } - public void setActive(boolean isActive) { - this.is_active = isActive; + this.isActive = isActive; } - public String getAreaId() { - return area_id; - } + public Map createItem(String name, int amount) { + String id = createUniqueId(UNIQUE_ID_LENGTH, itemMap.keySet()); + Item item = new Item(name, amount); + itemMap.put(id, item); + return new HashMap() { + {put(id, item);} + }; + } + + public Item addItem(String itemId, int num) { + return itemMap.get(itemId).addAmount(num); + } + + public Item destroyItem(String itemId) { + return itemMap.remove(itemId); + } + + private String createUniqueId(int length, Set idSet) { + String id; + do { + id = createRandomId(length); + } while (checkId(id, idSet)); + return id; + } + + private boolean checkId(String id, Set idSet) { + return idSet.contains(id); + } + + private String createRandomId(int length) { + StringBuilder id = new StringBuilder(""); + do { + id.append(UUID.randomUUID().toString()); + } while (id.length() < length); + return id.substring(0, length); + } }