81 lines
1.9 KiB
Java
81 lines
1.9 KiB
Java
package shootergame.items;
|
|
|
|
import shootergame.entity.Entity;
|
|
import shootergame.entity.EntityInventory;
|
|
import shootergame.inventory.Inventory;
|
|
import shootergame.util.gl.GlHelpers;
|
|
import shootergame.util.gl.texture.TextureReference;
|
|
import shootergame.util.math.vec.Vec2d;
|
|
import shootergame.world.chunk.Chunk;
|
|
import shootergame.world.layer.Layer;
|
|
|
|
public class Item
|
|
{
|
|
public TextureReference texture;
|
|
public String id;
|
|
|
|
public Item(String id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public void render(Vec2d pos, Vec2d size)
|
|
{
|
|
GlHelpers.begin();
|
|
|
|
texture.texCoord(0, 1); GlHelpers.vertex3(pos.x+size.x , pos.y , 0);
|
|
texture.texCoord(0, 0); GlHelpers.vertex3(pos.x+size.x , pos.y+size.y , 0);
|
|
texture.texCoord(1, 0); GlHelpers.vertex3(pos.x , pos.y+size.y , 0);
|
|
texture.texCoord(1, 1); GlHelpers.vertex3(pos.x , pos.y , 0);
|
|
|
|
GlHelpers.end();
|
|
}
|
|
|
|
public void onAction(ItemStack stack, Layer layer, Chunk chunk, Entity entity) {
|
|
stack.count -= 1;
|
|
}
|
|
|
|
public String getName(short meta) {
|
|
return "";
|
|
}
|
|
|
|
public String getLore(short meta) {
|
|
return "";
|
|
}
|
|
|
|
public void onPickedUp(ItemStack stack, Layer layer, Chunk chunk, Entity entity)
|
|
{
|
|
// Update the stacks count
|
|
if(stack.isEmpty()) return;
|
|
|
|
// Does the entity have an inventory
|
|
if(entity instanceof EntityInventory)
|
|
{
|
|
// Get the entities inventory
|
|
Inventory entity_i = ((EntityInventory) entity).getInventory();
|
|
|
|
// Loop over the inventory
|
|
for(int i=0;i<entity_i.getSlotCount();i++)
|
|
{
|
|
// Is this item the same as the picked up item
|
|
ItemStack i_stack = entity_i.getItem(i);
|
|
if(stack.stackEquals(i_stack))
|
|
{
|
|
// Move the stack count to the inventory slot
|
|
i_stack.count += stack.count;
|
|
stack.count = 0;
|
|
return;
|
|
}
|
|
|
|
// Is this item stack empty
|
|
if(i_stack.isEmpty())
|
|
{
|
|
// Send the entity stack to the inventory stack
|
|
entity_i.setItem(stack.copy(), i);
|
|
stack.count = 0;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|