Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions RoguelikeConsole/src/base/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package base;

import java.util.Hashtable;

public class Entity extends Base {

protected double baseHealth;
protected double baseArmor;
public boolean damageable;
protected int inventorySize;
protected Hashtable<Integer, InventoryEntry> inventory = new Hashtable<>();

public boolean tryAddToInventory(Item item, double count, double remainder)
{
//Try to add to the existing entries
for (Integer key = 0; key <= this.inventorySize -1; key ++)
{
if (inventory.containsKey(key)) //Makes sure that the inventory slot has items in it
{
if (inventory.get(key).tryAddItem(item, count, remainder))
{
if (remainder == 0)
{
//If the remainder comes back 0 than everything has been added to the inventory slot
return true;
}
else
{
count = remainder;
}
}
}
}
//Try to add to the empty inventory slots because there was remainder
for (int i = 0; i <= this.inventorySize -1; i ++)
{
if (!inventory.containsKey(i))
{
if (item.maxStackSize > count)
{
//Check if the amount being added is larger than the maxStackSize and if so add a max stack to the current inventory slot
inventory.put(i, new InventoryEntry(item.maxStackSize, item));
count = count - item.maxStackSize;
}
else
{
//Add the rest of the remaining item to the current inventory slot and return true
inventory.put(i, new InventoryEntry(count, item));
remainder = 0;
return true;
}
}
}
if (count > 0)
{
remainder = count;
return false;
}
else
{
remainder = 0;
return true;
}
}

public boolean removeFromInventory(int location, double count, double removed)
{
//Make sure there are items in that inventory slot
if (inventory.containsKey(location))
{
removed = inventory.get(location).removeItem(count);

if (inventory.get(location).count == 0)
{
//Remove the inventory slot if it is now empty
inventory.remove(location);
}
return true;
}
else
{
log.warn("Failed to remove items from inventory slot because it was already empty.");
return false;
}
}

}
76 changes: 76 additions & 0 deletions RoguelikeConsole/src/base/InventoryEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package base;

public class InventoryEntry extends Base {

public double count;
protected Item item;

public InventoryEntry(double count, Item item)
{
this.count = count;
this.item = item;
}

public double removeItem(double count)
{
double removed = 0;

if (this.count > count)
{
//If you are removing less than the total
removed = count;
this.count -= count;
}
else if (this.count == count)
{
//If you are removing the same as the total
removed = count;
this.count = 0;
}
else
{
//If you are attempting to remove more than the total
removed = this.count;
this.count = 0;
}

return removed;
}

public boolean isType(Item i)
{
//Check to see if the types are the same as what is currently stored
return this.item.getClass().equals(i.getClass());
}

public boolean tryAddItem(Item item, double count, double remaining)
{
if (!item.stackable)
{
log.warn("Attempted to add item to stack that is not stackable.");
return false;
}
else if (!this.isType(item))
{
log.warn("Attempted to combine different item types.{" + this.item.displayName + "," + item.displayName + "}");
return false;
}
else
{
if (this.count + count > item.maxStackSize)
{
//Adding more than the maxStackSize so there will be leftover
remaining = this.count + count - item.maxStackSize;
this.count = item.maxStackSize;
}
else
{
//Adding less or equal to the maxStackSize so there will be no leftover
remaining = 0;
this.count += count;
}
return true;
}
}

}
12 changes: 12 additions & 0 deletions RoguelikeConsole/src/component/Damage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package component;

import base.Base;
import base.Entity;

public class Damage extends Base {

public void damage(Entity e)
{

}
}