From 034d046e39177ac9ef7fac98759e5eef23c4da82 Mon Sep 17 00:00:00 2001 From: punchready Date: Thu, 29 Apr 2021 16:40:23 +0200 Subject: [PATCH] feat: add initial classes for entities package --- .../gamelibrary/entities/Character.java | 68 +++++++++++++++ .../gamelibrary/entities/Entity.java | 45 ++++++++++ .../gamelibrary/entities/EntityID.java | 37 ++++++++ .../gamelibrary/entities/EntityType.java | 21 +++++ .../gamelibrary/entities/InfinityStone.java | 21 +++++ .../gamelibrary/entities/Inventory.java | 84 +++++++++++++++++++ .../marvelous/gamelibrary/entities/NPC.java | 32 +++++++ .../gamelibrary/entities/NPCType.java | 15 ++++ .../marvelous/gamelibrary/entities/Rock.java | 34 ++++++++ .../marvelous/gamelibrary/entities/Stat.java | 43 ++++++++++ .../gamelibrary/entities/StatType.java | 15 ++++ .../gamelibrary/entities/StoneType.java | 12 +++ 12 files changed, 427 insertions(+) create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/Character.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/Entity.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/EntityID.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/EntityType.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/InfinityStone.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/Inventory.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/NPC.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/NPCType.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/Rock.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/Stat.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/StatType.java create mode 100644 src/com/uulm/marvelous/gamelibrary/entities/StoneType.java diff --git a/src/com/uulm/marvelous/gamelibrary/entities/Character.java b/src/com/uulm/marvelous/gamelibrary/entities/Character.java new file mode 100644 index 0000000..0c29711 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/Character.java @@ -0,0 +1,68 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import com.uulm.marvelous.gamelibrary.IntVector2; + +/** Represents a playable character inside a match. + */ +public class Character extends Entity { + /** The name of the character. + */ + public final String name; + + /** The hp stat of the character. + */ + public final Stat hp; + + /** The mp stat of the character. + */ + public final Stat mp; + + /** The ap stat of the character. + */ + public final Stat ap; + + /** The ranged attack range of the character. + */ + public final int attackRange; + + /** The ranged attack damage of the character. + */ + public final int rangedDamage; + + /** The melee attack damage of the character. + */ + public final int meleeDamage; + + /** The {@link Inventory} of the character. + */ + public final Inventory inventory = new Inventory(); + + /** Constructs a new {@link Character} with an empty inventory. + * @param id The {@link EntityID} of the character. + * @param position The position of the character. + * @param name The name of the character. + * @param hp The maximum hp of the character. + * @param mp The maximum mp of the character. + * @param ap The maximum ap of the character. + * @param attackRange The ranged attack range of the character. + * @param rangedDamage The ranged damage of the character. + * @param meleeDamage The melee damage of the character. + */ + public Character(EntityID id, IntVector2 position, String name, int hp, int mp, int ap, int attackRange, int rangedDamage, int meleeDamage) { + super(id, position); + this.name = name; + this.hp = new Stat(StatType.HP, hp); + this.mp = new Stat(StatType.MP, mp); + this.ap = new Stat(StatType.AP, ap); + this.attackRange = attackRange; + this.rangedDamage = rangedDamage; + this.meleeDamage = meleeDamage; + } + + /** Checks if the character is still alive. + * @return Whether or not the characters hp is greater than 0. + */ + public boolean isAlive() { + return hp.getValue() > 0; + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/Entity.java b/src/com/uulm/marvelous/gamelibrary/entities/Entity.java new file mode 100644 index 0000000..bdccf5a --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/Entity.java @@ -0,0 +1,45 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import com.uulm.marvelous.gamelibrary.IntVector2; + +/** Represents an abstract entity. + */ +public abstract class Entity { + /** Whether or not the entity is currently active in the game. + */ + private boolean active = true; + + /** The position of the entity. + */ + private IntVector2 position; + + /** The {@link EntityID} of the entity. + */ + public final EntityID id; + + /** Constructs a new {@link Entity}. + * @param id The {@link EntityID} of the entity. + * @param position The position of the entity. + */ + protected Entity(EntityID id, IntVector2 position) { + this.position = position; + this.id = id; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public IntVector2 getPosition() { + return position; + } + + public void setPosition(IntVector2 position) { + this.position = position; + } +} + diff --git a/src/com/uulm/marvelous/gamelibrary/entities/EntityID.java b/src/com/uulm/marvelous/gamelibrary/entities/EntityID.java new file mode 100644 index 0000000..98c5723 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/EntityID.java @@ -0,0 +1,37 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Represents a distinct identification for every {@link Entity} in a game. + */ +public class EntityID { + /** The index of the entity. + */ + public final int id; + + /** The type of the entity. + */ + public final EntityType type; + + /** Constructs a new {@link Entity}-{@link EntityID} based on the given index and {@link EntityType}. + * @param id The index of the entity. + * @param type The type of the entity. + */ + public EntityID(int id, EntityType type) { + this.id = id; + this.type = type; + } + + /** Checks if the id has the same {@link EntityType} as the given one. + * @param other The type to compare to. + * @return Whether or not the id has the same type. + */ + public boolean isSameType(EntityType other) { + return type == other; + } + + /** Serializes the id for debugging. + * @return A debug string containing all the necessary information about the id. + */ + public String toString() { + return "["+type.toString()+":"+id+"]"; + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/EntityType.java b/src/com/uulm/marvelous/gamelibrary/entities/EntityType.java new file mode 100644 index 0000000..a7df553 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/EntityType.java @@ -0,0 +1,21 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Specifies the type of an {@link Entity}. + */ +public enum EntityType { + /** Represents an NPC entity. + */ + NPC, + /** Represents the first Player. + */ + P1, + /** Represents the second Player. + */ + P2, + /** Represents a Rock entity. + */ + Rocks, + /** Represents an InfinityStone entity. + */ + InfinityStones +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/InfinityStone.java b/src/com/uulm/marvelous/gamelibrary/entities/InfinityStone.java new file mode 100644 index 0000000..34702e2 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/InfinityStone.java @@ -0,0 +1,21 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import com.uulm.marvelous.gamelibrary.IntVector2; + +/** Represents an infinity stone {@link Entity}. Can only exist on the map. + */ +public class InfinityStone extends Entity { + /** The {@link StoneType} of the infinity stone. + */ + public final StoneType type; + + /** Constructs a new {@link InfinityStone}. + * @param id The {@link EntityID} of the stone. + * @param position The position of the stone. + * @param type The {@link StoneType} of the stone. + */ + public InfinityStone(EntityID id, IntVector2 position, StoneType type) { + super(id, position); + this.type = type; + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/Inventory.java b/src/com/uulm/marvelous/gamelibrary/entities/Inventory.java new file mode 100644 index 0000000..a2cb00d --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/Inventory.java @@ -0,0 +1,84 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; + +/** Represents an inventory of 6 slots of {@link StoneType}s that can be manipulated. + */ +public class Inventory implements Iterable { + /** The size of the inventory. + */ + private final int size = 6; + + /** The content of the inventory. + */ + private final HashSet content = new HashSet<>(size); + + /** Constructs a new {@link Inventory}. + * @param content The starting content of the inventory. + */ + public Inventory(ArrayList content) { + if(content.size() > size) { + throw new IllegalArgumentException("Attempted to construct an inventory with more than "+size+" initial stones."); + } + + for(StoneType stone : content) { + if(content.contains(stone)) { + throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries."); + } + content.add(stone); + } + } + + /** Constructs a new empty {@link Inventory}. + */ + public Inventory() { + + } + + /** Returns the number of free slots the inventory has. + */ + public int getFreeSlots() { + return size - content.size(); + } + + /** Checks if the inventory contains the given stone. + * @param stone The {@link StoneType} to check for. + */ + public boolean hasStone(StoneType stone) { + return content.contains(stone); + } + + /** Adds a stone to the inventory. + * @param stone The {@link StoneType} to add. + */ + public void addStone(StoneType stone) { + if(content.contains(stone)) { + throw new IllegalArgumentException("Attempted to add a duplicate stone to an inventory."); + } + if(content.size() == size) { + throw new IllegalArgumentException("Attempted to add a stone to a full inventory."); + } + + content.add(stone); + } + + /** Removes a stone from the inventory. + * @param stone The {@link StoneType} to remove. + */ + public void removeStone(StoneType stone) { + if(!content.contains(stone)) { + throw new IllegalArgumentException("Attempted to remove a nonexistent stone from an inventory."); + } + + content.remove(stone); + } + + /** Iterates over the inventory. + */ + @Override + public Iterator iterator() { + return content.iterator(); + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/NPC.java b/src/com/uulm/marvelous/gamelibrary/entities/NPC.java new file mode 100644 index 0000000..541ac21 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/NPC.java @@ -0,0 +1,32 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import com.uulm.marvelous.gamelibrary.IntVector2; + +import java.util.ArrayList; + +/** Represents an NPC inside the game. + */ +public class NPC extends Entity { + /** The {@link Inventory} of the NPC. + */ + public final Inventory inventory; + + /** Constructs a new {@link NPC}. + * @param id The {@link EntityID} of the NPC. + * @param position The position of the NPC. + * @param inventory The starting inventory the NPC should have. + */ + public NPC(EntityID id, IntVector2 position, ArrayList inventory) { + super(id, position); + this.inventory = new Inventory(inventory); + } + + /** Constructs a new {@link NPC} with an empty inventory. + * @param id The {@link EntityID} of the NPC. + * @param position The position of the NPC. + */ + public NPC(EntityID id, IntVector2 position) { + super(id, position); + this.inventory = new Inventory(); + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/NPCType.java b/src/com/uulm/marvelous/gamelibrary/entities/NPCType.java new file mode 100644 index 0000000..0e91bae --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/NPCType.java @@ -0,0 +1,15 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Specifies the type of an {@link NPC}. + */ +public enum NPCType { + /** Represents the Goose. + */ + Goose, + /** Represents Stan Lee. + */ + Stan, + /** Represents Thanos. + */ + Thanos +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/Rock.java b/src/com/uulm/marvelous/gamelibrary/entities/Rock.java new file mode 100644 index 0000000..58f830c --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/Rock.java @@ -0,0 +1,34 @@ +package com.uulm.marvelous.gamelibrary.entities; + +import com.uulm.marvelous.gamelibrary.IntVector2; + +/** Represents a rock entity on the map. + */ +public class Rock extends Entity { + /** The maximum hp of the rock. + */ + public final int maxHP; + + /** The current hp of the rock. + */ + private int hp; + + /** Constructs a new {@link Rock}. + * @param id The {@link EntityID} of the rock. + * @param position The position of the rock. + * @param hp The hp of the rock. + */ + public Rock(EntityID id, IntVector2 position, int hp) { + super(id, position); + this.maxHP = hp; + this.hp = hp; + } + + public int getHp() { + return hp; + } + + public void decreaseHp(int damage) { + this.hp -= damage; + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/Stat.java b/src/com/uulm/marvelous/gamelibrary/entities/Stat.java new file mode 100644 index 0000000..1b8d907 --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/Stat.java @@ -0,0 +1,43 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Represents a stat property of a {@link Character}. + */ +public class Stat { + /** The {@link StatType} of the stat. + */ + public final StatType type; + + /** The maximum value of the stat. + */ + public final int max; + + /** The current value of the stat. + */ + private int value; + + /** Constructs a new {@link Stat} with the initial value set to the maximum value. + * @param type The {@link StatType} of the stat. + * @param max The maximum value of the stat. + */ + public Stat(StatType type, int max) { + this.type = type; + this.max = max; + this.value = max; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public void increaseValue(int value) { + this.value += value; + } + + public void decreaseValue(int value) { + this.value -= value; + } +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/StatType.java b/src/com/uulm/marvelous/gamelibrary/entities/StatType.java new file mode 100644 index 0000000..3ea103e --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/StatType.java @@ -0,0 +1,15 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Specifies the type of a {@link Stat}. + */ +public enum StatType { + /** Represents the life points of a character. + */ + HP, + /** Represents the mana points of a character. + */ + MP, + /** Represents thr action points of a character. + */ + AP +} diff --git a/src/com/uulm/marvelous/gamelibrary/entities/StoneType.java b/src/com/uulm/marvelous/gamelibrary/entities/StoneType.java new file mode 100644 index 0000000..157151e --- /dev/null +++ b/src/com/uulm/marvelous/gamelibrary/entities/StoneType.java @@ -0,0 +1,12 @@ +package com.uulm.marvelous.gamelibrary.entities; + +/** Specifies the type of an {@link InfinityStone}. + */ +public enum StoneType { + SpaceStone, + MindStone, + RealityStone, + PowerStone, + TimeStone, + SoulStone +}