Player changes:

- reimplemented & improved player inventory
- now player has "Action2" which means he can interact with the
environment
- added weapon energy cost
- updated player action interface
This commit is contained in:
2016-12-12 23:28:41 +02:00
parent 6fe33a99a8
commit 6a35b11ec2
34 changed files with 885 additions and 573 deletions

View File

@ -0,0 +1,148 @@
/*
* Inventory.cpp
*
* Created on: Dec 11, 2016
* Author: tibi
*/
#include <components/basic/Inventory.h>
#include <components/basic/InventoryItem.h>
#include <iostream>
namespace farmlands {
namespace components {
namespace basic {
Inventory::Inventory()
: m_capacity(10),
m_items(nullptr)
{
}
Inventory::~Inventory()
{
delete[] m_items;
}
model::Component* Inventory::clone()
{
Inventory* clone = new Inventory();
clone->m_capacity = m_capacity;
return clone;
}
void Inventory::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Inventory capacity=" << m_capacity << "\n";
}
void Inventory::onCreate()
{
// Allocate items array
m_items = new model::GameObject*[m_capacity];
memset(m_items, 0, sizeof(m_items[0]) * m_capacity);
// Find all children
for (auto it = gameObject->childrenBegin(); it != gameObject->childrenEnd(); it++)
{
InventoryItem* item = (*it)->component<InventoryItem>();
if (item)
{
Assert(item->slot >= 0 && item->slot < m_capacity, "Inventory item slot out of range.");
Assert(m_items[item->slot] == nullptr, "Multiple items on one slot.");
m_items[item->slot] = *it;
}
}
}
bool Inventory::add(model::GameObject* item)
{
Assert(m_items != nullptr, "Inventory not initialized");
Assert(item != nullptr, "Cannot add null object.");
// Find an empty slot
size_t slot = (size_t)-1;
for (size_t i = 0; i < m_capacity; i++)
if (m_items[i] == nullptr)
{
slot = i;
break;
}
if (slot != (size_t)-1)
{
set(slot, item);
return true;
}
return false;
}
void Inventory::remove(size_t slot)
{
Assert(m_items != nullptr, "Inventory not initialized.");
Assert(slot < m_capacity, "Slot out of range.");
if (m_items[slot] != nullptr)
{
m_items[slot]->removeComponent<InventoryItem>();
m_items[slot] = nullptr;
}
}
void Inventory::set(size_t slot, model::GameObject* item)
{
Assert(m_items != nullptr, "Inventory not initialized.");
Assert(slot < m_capacity, "Slot out of range.");
Assert(item != nullptr, "Cannot set null element.");
remove(slot);
m_items[slot] = item;
// Update InventoryItem
if (!item->haveComponent<InventoryItem>())
item->addComponent(new InventoryItem);
InventoryItem* invItem = item->component<InventoryItem>();
invItem->slot = slot;
}
model::GameObject* Inventory::get(size_t slot)
{
Assert(m_items != nullptr, "Inventory not initialized.");
Assert(slot < m_capacity, "Slot out of range.");
return m_items[slot];
}
void Inventory::setCapacity(size_t capacity)
{
if (m_items != nullptr)
{
// Remove extra items
for (size_t i = m_capacity; i < capacity; i++)
remove(i);
// Reallocate
model::GameObject** old = m_items;
m_items = new model::GameObject*[capacity];
memset(m_items, 0, sizeof(m_items[0]) * capacity);
// Copy old objects
for (size_t i = 0; i < std::min(m_capacity, capacity); i++)
m_items[i] = old[i];
// Finish
delete[] old;
}
m_capacity = capacity;
}
} /* namespace basic */
} /* namespace components */
} /* namespace farmlands */

View File

@ -0,0 +1,77 @@
/*
* Inventory.h
*
* Created on: Dec 11, 2016
* Author: tibi
*/
#ifndef COMPONENTS_INVENTORY_INVENTORY_H_
#define COMPONENTS_INVENTORY_INVENTORY_H_
#include <model/Component.h>
#include <model/GameObject.h>
namespace farmlands {
namespace components {
namespace basic {
class Inventory: public model::Component
{
public:
Inventory();
virtual ~Inventory();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onCreate() override;
// Inventory management
/**
* Adds a game object to first available slot.
* Game object should be already added as a child of the parent game object.
* If object doesn't have an "InventoryItem" component, it will be added.
* @returns true if added successfully, false if no empty slot was found.
*/
bool add(model::GameObject* item);
/**
* Removes the game object from the given slot.
* Doesn't delete/destroy the game object from the parent game object.
* Destroys the "InventoryItem" component of the object.
*/
void remove(size_t slot);
/**
* Sets the given game object (which should be a child of the parent game object) to have the given slot.
* If there is an item in that slot, it is removed.
*/
void set(size_t slot, model::GameObject* item);
/**
* Gets the game object in the given slot.
* Returns null if slot is empty.
*/
model::GameObject* get(size_t slot);
/**
* Modifies the capacity of the inventory.
* If the inventory is already initialized, objects might be removed.
*/
void setCapacity(size_t capacity);
/**
* Gets the capacity
*/
size_t capacity() const { return m_capacity; }
private:
size_t m_capacity;
model::GameObject** m_items;
};
} /* namespace basic */
} /* namespace components */
} /* namespace farmlands */
#endif /* COMPONENTS_INVENTORY_INVENTORY_H_ */

View File

@ -0,0 +1,36 @@
/*
* InventoryItem.cpp
*
* Created on: Dec 11, 2016
* Author: tibi
*/
#include <components/basic/InventoryItem.h>
#include <iostream>
namespace farmlands {
namespace components {
namespace basic {
InventoryItem::~InventoryItem()
{
}
model::Component* InventoryItem::clone()
{
InventoryItem* item = new InventoryItem();
item->slot = slot;
return item;
}
void InventoryItem::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: InventoryItem slot=" << slot << "\n";
}
} /* namespace basic */
} /* namespace components */
} /* namespace farmlands */

View File

@ -0,0 +1,32 @@
/*
* InventoryItem.h
*
* Created on: Dec 11, 2016
* Author: tibi
*/
#ifndef COMPONENTS_INVENTORY_INVENTORYITEM_H_
#define COMPONENTS_INVENTORY_INVENTORYITEM_H_
#include <model/Component.h>
namespace farmlands {
namespace components {
namespace basic {
class InventoryItem: public model::Component
{
public:
virtual ~InventoryItem();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
size_t slot;
};
} /* namespace basic */
} /* namespace components */
} /* namespace farmlands */
#endif /* COMPONENTS_INVENTORY_INVENTORYITEM_H_ */