feat: add initial classes for entities package

This commit is contained in:
punchready 2021-04-29 16:40:23 +02:00
parent dd2b653b3a
commit 034d046e39
12 changed files with 427 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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+"]";
}
}

View File

@ -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
}

View File

@ -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;
}
}

View File

@ -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<StoneType> {
/** The size of the inventory.
*/
private final int size = 6;
/** The content of the inventory.
*/
private final HashSet<StoneType> content = new HashSet<>(size);
/** Constructs a new {@link Inventory}.
* @param content The starting content of the inventory.
*/
public Inventory(ArrayList<StoneType> 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<StoneType> iterator() {
return content.iterator();
}
}

View File

@ -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<StoneType> 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();
}
}

View File

@ -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
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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
}

View File

@ -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
}