Implemented some tools. Also, implemented inventory.

This commit is contained in:
2016-12-03 19:43:28 +02:00
parent 91c0da855b
commit 0dc77aacb4
38 changed files with 1713 additions and 654 deletions

View File

@@ -8,12 +8,6 @@
#include <storage/Parsers.h>
#include <resources/Resources.h>
using namespace farmlands::components;
using namespace farmlands::components::basic;
using namespace farmlands::components::items;
using namespace farmlands::components::player;
using namespace farmlands::graphics;
using namespace farmlands::model;
using namespace farmlands::resources;
namespace farmlands {
@@ -21,7 +15,7 @@ namespace storage {
/****** Components ******/
void parseBackgroundCells(resources::ResourceId cellsResource, Background* back, size_t layer)
void parseBackgroundCells(resources::ResourceId cellsResource, components::Background* back, size_t layer)
{
Assert(RInfo[cellsResource].type == ResourceType::BackgroundLayer, "Resource must be a level layer.");
@@ -45,7 +39,7 @@ void parseBackgroundCells(resources::ResourceId cellsResource, Background* back,
char* nextNum = strtok(buffer, ",;");
for (size_t col = 0; col < back->columnCount() && nextNum != NULL; col++)
{
Cell cell = (Cell) strtol(nextNum, NULL, 10);
components::Cell cell = (components::Cell) strtol(nextNum, NULL, 10);
back->setCell(layer, row, col, cell);
nextNum = strtok(NULL, ",;");
@@ -56,9 +50,9 @@ void parseBackgroundCells(resources::ResourceId cellsResource, Background* back,
}
template <>
Background* parse<Background> (boost::property_tree::ptree& root)
components::Background* parse<components::Background> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Background")
root = root.front().second;
@@ -67,7 +61,7 @@ Background* parse<Background> (boost::property_tree::ptree& root)
if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Background>(id);
return parse<components::Background>(id);
}
// Read sizes
@@ -75,8 +69,8 @@ Background* parse<Background> (boost::property_tree::ptree& root)
uint32_t rows = root.get<uint32_t>("<xmlattr>.rows");
uint32_t cols = root.get<uint32_t>("<xmlattr>.columns");
// Create background object
Background* back = new Background(layers, rows, cols);
// Create components::Background object
components::Background* back = new components::Background(layers, rows, cols);
// Read layers
size_t layerNum = 0;
@@ -103,25 +97,25 @@ Background* parse<Background> (boost::property_tree::ptree& root)
}
template <>
Camera* parse<Camera> (boost::property_tree::ptree& root)
components::basic::Camera* parse<components::basic::Camera> (boost::property_tree::ptree& root)
{
if (root.front().first == "Camera")
root = root.front().second;
Camera* camera = new Camera();
components::basic::Camera* camera = new components::basic::Camera();
camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return camera;
}
template <>
Frame* parse<Frame> (boost::property_tree::ptree& root)
components::basic::Frame* parse<components::basic::Frame> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Frame")
root = root.front().second;
Frame* frame = new Frame();
components::basic::Frame* frame = new components::basic::Frame();
// Obtine tile set id
std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
@@ -137,9 +131,9 @@ Frame* parse<Frame> (boost::property_tree::ptree& root)
}
template <>
Sprite* parse<Sprite> (boost::property_tree::ptree& root)
components::basic::Sprite* parse<components::basic::Sprite> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Sprite")
root = root.front().second;
@@ -148,11 +142,11 @@ Sprite* parse<Sprite> (boost::property_tree::ptree& root)
if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Sprite>(id);
return parse<components::basic::Sprite>(id);
}
// Parse sprite
Sprite* sprite = new Sprite();
// Parse components::basic::Sprite
components::basic::Sprite* sprite = new components::basic::Sprite();
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
sprite->animationVelocity = root.get<float>("<xmlattr>.animationVelocity", 1.0f);
@@ -161,7 +155,7 @@ Sprite* parse<Sprite> (boost::property_tree::ptree& root)
{
if (child.first == "State")
{
SpriteState* state = parse<SpriteState>(child.second);
components::basic::SpriteState* state = parse<components::basic::SpriteState>(child.second);
sprite->addState(*state);
delete state;
}
@@ -171,20 +165,20 @@ Sprite* parse<Sprite> (boost::property_tree::ptree& root)
}
template <>
SpriteState* parse<SpriteState> (boost::property_tree::ptree& root)
components::basic::SpriteState* parse<components::basic::SpriteState> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "SpriteState")
root = root.front().second;
SpriteState* spriteState = new SpriteState();
components::basic::SpriteState* spriteState = new components::basic::SpriteState();
spriteState->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
if (child.first == "Frame")
{
Frame* frame = parse<Frame>(child.second);
components::basic::Frame* frame = parse<components::basic::Frame>(child.second);
spriteState->frames.push_back(*frame);
delete frame;
}
@@ -194,13 +188,13 @@ SpriteState* parse<SpriteState> (boost::property_tree::ptree& root)
}
template <>
Transform* parse<components::basic::Transform> (boost::property_tree::ptree& root)
components::basic::Transform* parse<components::basic::Transform> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Transform")
root = root.front().second;
Transform* transform = new Transform();
components::basic::Transform* transform = new components::basic::Transform();
transform->x = root.get<float>("<xmlattr>.x", 0.0f);
transform->y = root.get<float>("<xmlattr>.y", 0.0f);
transform->w = root.get<float>("<xmlattr>.w", 0.0f);
@@ -210,37 +204,111 @@ Transform* parse<components::basic::Transform> (boost::property_tree::ptree& roo
}
template <>
DebugController* parse<DebugController> (boost::property_tree::ptree& root)
components::DebugController* parse<components::DebugController> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "DebugController")
root = root.front().second;
DebugController* controller = new DebugController();
components::DebugController* controller = new components::DebugController();
return controller;
}
template <>
Giftable* parse<items::Giftable> (boost::property_tree::ptree& root)
components::items::Axe* parse<components::items::Axe> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Axe")
root = root.front().second;
// Parse
return new components::items::Axe();
}
template <>
components::items::Giftable* parse<components::items::Giftable> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Giftable")
root = root.front().second;
// Parse
return new Giftable();
return new components::items::Giftable();
}
template <>
components::items::Hoe* parse<components::items::Hoe> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Hoe")
root = root.front().second;
// Parse
return new components::items::Hoe();
}
template <>
components::items::Item* parse<components::items::Item> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Item")
root = root.front().second;
// Parse
components::items::Item* item = new components::items::Item();
item->name = root.get<std::string>("<xmlattr>.name");
item->description = root.get<std::string>("<xmlattr>.description");
item->level = root.get<uint8_t>("<xmlattr>.level");
return item;
}
template <>
Weapon* parse<items::Weapon> (boost::property_tree::ptree& root)
components::items::Pickaxe* parse<components::items::Pickaxe> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Pickaxe")
root = root.front().second;
// Parse
return new components::items::Pickaxe();
}
template <>
components::items::Scythe* parse<components::items::Scythe> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Pickaxe")
root = root.front().second;
// Parse
return new components::items::Scythe();
}
template <>
components::items::WateringCan* parse<components::items::WateringCan> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "WateringCan")
root = root.front().second;
// Parse
components::items::WateringCan* can = new components::items::WateringCan();
can->capacity = root.get<float>("<xmlattr>.capacity");
can->amountLeft = root.get<float>("<xmlattr>.amountLeft");
return can;
}
template <>
components::items::Weapon* parse<components::items::Weapon> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Weapon")
root = root.front().second;
// Parse
Weapon* weapon = new Weapon();
components::items::Weapon* weapon = new components::items::Weapon();
weapon->damage = root.get<float>("<xmlattr>.damage");
weapon->critProbability = root.get<float>("<xmlattr>.critProbability");
weapon->critDamage = root.get<float>("<xmlattr>.critDamage");
@@ -249,13 +317,29 @@ Weapon* parse<items::Weapon> (boost::property_tree::ptree& root)
}
template <>
PlayerController* parse<PlayerController> (boost::property_tree::ptree& root)
components::player::PlayerInventory* parse<components::player::PlayerInventory> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "PlayerController")
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "PlayerInventory")
root = root.front().second;
PlayerController* controller = new PlayerController();
components::player::PlayerInventory* controller = new components::player::PlayerInventory();
controller->capacity = root.get<uint32_t>("<xmlattr>.capacity");
controller->currentItemIndex = root.get<int>("<xmlattr>.currentItemIndex", -1);
return controller;
}
template <>
components::player::PlayerMovement* parse<components::player::PlayerMovement> (boost::property_tree::ptree& root)
{
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "PlayerMovement")
root = root.front().second;
components::player::PlayerMovement* controller = new components::player::PlayerMovement();
controller->facingDirection = (model::Direction)root.get<int>("<xmlattr>.facingDirection", model::Direction::South);
return controller;
}
@@ -265,7 +349,7 @@ PlayerController* parse<PlayerController> (boost::property_tree::ptree& root)
template <>
graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "BackgroundRenderer")
root = root.front().second;
@@ -276,7 +360,7 @@ graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::proper
template <>
graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "SpriteRenderer")
root = root.front().second;
@@ -288,57 +372,78 @@ graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree:
/****** Model ******/
template <>
GameObject* parse<GameObject> (boost::property_tree::ptree& root)
model::GameObject* parse<model::GameObject> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "GameObject")
root = root.front().second;
GameObject* gameObj = new GameObject();
model::GameObject* gameObj = new model::GameObject();
gameObj->name = root.get<std::string>("<xmlattr>.name");
gameObj->visible = root.get<bool>("<xmlattr>.visible", true);
gameObj->setEnabled(root.get<bool>("<xmlattr>.enabled", true));
for (auto child : root)
{
// Game object
if (child.first == "GameObject")
gameObj->addChild(parse<model::GameObject>(child.second));
// Components::basic
if (child.first == "Camera")
gameObj->addComponent(parse<Camera>(child.second));
else if (child.first == "Camera")
gameObj->addComponent(parse<components::basic::Camera>(child.second));
else if (child.first == "Sprite")
gameObj->addComponent(parse<Sprite>(child.second));
gameObj->addComponent(parse<components::basic::Sprite>(child.second));
else if (child.first == "Transform")
gameObj->addComponent(parse<Transform>(child.second));
gameObj->addComponent(parse<components::basic::Transform>(child.second));
// Components::items
else if (child.first == "Axe")
gameObj->addComponent(parse<components::items::Axe>(child.second));
else if (child.first == "Giftable")
gameObj->addComponent(parse<Giftable>(child.second));
gameObj->addComponent(parse<components::items::Giftable>(child.second));
else if (child.first == "Hoe")
gameObj->addComponent(parse<Hoe>(child.second));
gameObj->addComponent(parse<components::items::Hoe>(child.second));
else if (child.first == "Item")
gameObj->addComponent(parse<Item>(child.second));
gameObj->addComponent(parse<components::items::Item>(child.second));
else if (child.first == "Pickaxe")
gameObj->addComponent(parse<components::items::Pickaxe>(child.second));
else if (child.first == "Scythe")
gameObj->addComponent(parse<components::items::Scythe>(child.second));
else if (child.first == "WateringCan")
gameObj->addComponent(parse<components::items::WateringCan>(child.second));
else if (child.first == "Weapon")
gameObj->addComponent(parse<Weapon>(child.second));
gameObj->addComponent(parse<components::items::Weapon>(child.second));
// Components::player
else if (child.first == "PlayerController")
gameObj->addComponent(parse<PlayerController>(child.second));
else if (child.first == "PlayerInventory")
gameObj->addComponent(parse<components::player::PlayerInventory>(child.second));
else if (child.first == "PlayerMovement")
gameObj->addComponent(parse<components::player::PlayerMovement>(child.second));
// Components
else if (child.first == "Background")
gameObj->addComponent(parse<Background>(child.second));
gameObj->addComponent(parse<components::Background>(child.second));
else if (child.first == "DebugController")
gameObj->addComponent(parse<DebugController>(child.second));
gameObj->addComponent(parse<components::DebugController>(child.second));
// Graphics
else if (child.first == "BackgroundRenderer")
gameObj->addComponent(parse<BackgroundRenderer>(child.second));
gameObj->addComponent(parse<graphics::BackgroundRenderer>(child.second));
else if (child.first == "SpriteRenderer")
gameObj->addComponent(parse<SpriteRenderer>(child.second));
gameObj->addComponent(parse<graphics::SpriteRenderer>(child.second));
// !!! Add additional types here !!!
}
@@ -347,52 +452,26 @@ GameObject* parse<GameObject> (boost::property_tree::ptree& root)
}
template <>
Configuration* parse<Configuration> (boost::property_tree::ptree& root)
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Configuration")
root = root.front().second;
Configuration* config = new Configuration();
model::Configuration* config = new model::Configuration();
config->animationFps = root.get<float>("animationFps");
return config;
}
template <>
Hoe* parse<Hoe> (boost::property_tree::ptree& root)
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Hoe")
root = root.front().second;
Hoe* hoe = new Hoe();
return hoe;
}
template <>
Item* parse<Item> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
if (root.front().first == "Item")
root = root.front().second;
// Parse
Item* item = new Item();
item->name = root.get<std::string>("<xmlattr>.name");
item->description = root.get<std::string>("<xmlattr>.description");
item->level = root.get<uint8_t>("<xmlattr>.level");
return item;
}
template <>
Scene* parse<Scene> (boost::property_tree::ptree& root)
{
// Ensure we are on the scene node (property tree seems to add root of its own)
// Ensure we are on the model::Scene node (property tree seems to add root of its own)
if (root.front().first == "Scene")
root = root.front().second;
Scene* scene = new Scene();
model::Scene* scene = new model::Scene();
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight");
@@ -400,7 +479,7 @@ Scene* parse<Scene> (boost::property_tree::ptree& root)
{
if (child.first == "GameObject")
{
GameObject* obj = parse<GameObject>(child.second);
model::GameObject* obj = parse<model::GameObject>(child.second);
scene->root.addChild(obj);
}
}
@@ -409,13 +488,6 @@ Scene* parse<Scene> (boost::property_tree::ptree& root)
}
/****** Controller namespace ******/
}
}