Reorganized classes & namespaces. Made sprite a component.

This commit is contained in:
2016-12-02 22:20:04 +02:00
parent f255905c73
commit 33de4a8d1f
51 changed files with 648 additions and 580 deletions

View File

@@ -8,170 +8,20 @@
#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 {
namespace storage {
/****** Base namespace ******/
/****** Components ******/
template <>
base::Camera* parse<base::Camera> (boost::property_tree::ptree& root)
{
if (root.front().first == "Camera")
root = root.front().second;
base::Camera* camera = new base::Camera();
camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return camera;
}
template <>
base::Frame* parse<base::Frame> (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 == "Frame")
root = root.front().second;
base::Frame* frame = new base::Frame();
// Obtine tile set id
std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
frame->tileSetId = resources::ResourceManager::instance().getId(tileSetPath);
// Set properties
frame->tileSetCell = root.get<int>("<xmlattr>.cell");
frame->width = root.get<uint32_t>("<xmlattr>.w", 1u);
frame->height = root.get<uint32_t>("<xmlattr>.h", 1u);
frame->duration = root.get<uint32_t>("<xmlattr>.duration");
return frame;
}
template <>
base::GameObject* parse<base::GameObject> (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 == "GameObject")
root = root.front().second;
base::GameObject* gameObj = new base::GameObject();
gameObj->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
// Base objects
if (child.first == "Transform")
gameObj->addComponent(parse<base::Transform>(child.second));
else if (child.first == "Camera")
gameObj->addComponent(parse<base::Camera>(child.second));
// Model
else if (child.first == "Background")
{
std::string path = child.second.get<std::string>("<xmlattr>.src");
resources::ResourceId id = resources::ResourceManager::instance().getId(path);
gameObj->addComponent(parse<model::Background>(id));
}
else if (child.first == "Item")
gameObj->addComponent(parse<model::Item>(child.second));
// Graphics
else if (child.first == "BackgroundRenderer")
gameObj->addComponent(parse<graphics::BackgroundRenderer>(child.second));
else if (child.first == "SpriteRenderer")
gameObj->addComponent(parse<graphics::SpriteRenderer>(child.second));
// Controllers
else if (child.first == "DebugController")
gameObj->addComponent(parse<controller::DebugController>(child.second));
else if (child.first == "Giftable")
gameObj->addComponent(parse<controller::items::Giftable>(child.second));
else if (child.first == "Weapon")
gameObj->addComponent(parse<controller::items::Weapon>(child.second));
else if (child.first == "PlayerController")
gameObj->addComponent(parse<controller::player::PlayerController>(child.second));
// !!! Add additional types here !!!
}
return gameObj;
}
template <>
base::Sprite* parse<base::Sprite> (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 == "Sprite")
root = root.front().second;
base::Sprite* sprite = new base::Sprite();
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
for (auto child : root)
{
if (child.first == "State")
{
base::SpriteState* state = parse<base::SpriteState>(child.second);
sprite->addState(*state);
delete state;
}
}
return sprite;
}
template <>
base::SpriteState* parse<base::SpriteState> (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 == "SpriteState")
root = root.front().second;
base::SpriteState* spriteState = new base::SpriteState();
spriteState->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
if (child.first == "Frame")
{
base::Frame* frame = parse<base::Frame>(child.second);
spriteState->frames.push_back(*frame);
delete frame;
}
}
return spriteState;
}
template <>
base::Transform* parse<base::Transform> (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 == "Transform")
root = root.front().second;
base::Transform* transform = new base::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);
transform->h = root.get<float>("<xmlattr>.h", 0.0f);
return transform;
}
/****** Model namespace ******/
void parseBackgroundCells(resources::ResourceId cellsResource, model::Background* back, size_t layer)
void parseBackgroundCells(resources::ResourceId cellsResource, Background* back, size_t layer)
{
Assert(RInfo[cellsResource].type == ResourceType::BackgroundLayer, "Resource must be a level layer.");
@@ -195,7 +45,7 @@ void parseBackgroundCells(resources::ResourceId cellsResource, model::Background
char* nextNum = strtok(buffer, ",;");
for (size_t col = 0; col < back->columnCount() && nextNum != NULL; col++)
{
model::Cell cell = (model::Cell)strtol(nextNum, NULL, 10);
Cell cell = (Cell) strtol(nextNum, NULL, 10);
back->setCell(layer, row, col, cell);
nextNum = strtok(NULL, ",;");
@@ -206,19 +56,27 @@ void parseBackgroundCells(resources::ResourceId cellsResource, model::Background
}
template <>
model::Background* parse<model::Background> (boost::property_tree::ptree& root)
Background* parse<Background> (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 == "Background")
root = root.front().second;
// This object can be declared in another file
std::string src = root.get<std::string>("<xmlattr>.src", "");
if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Background>(id);
}
// Read sizes
uint32_t layers = root.count("Layer");
uint32_t rows = root.get<uint32_t>("<xmlattr>.rows");
uint32_t cols = root.get<uint32_t>("<xmlattr>.columns");
// Create background object
model::Background* back = new model::Background(layers, rows, cols);
Background* back = new Background(layers, rows, cols);
// Read layers
size_t layerNum = 0;
@@ -245,91 +103,144 @@ model::Background* parse<model::Background> (boost::property_tree::ptree& root)
}
template <>
model::Configuration* parse<model::Configuration> (boost::property_tree::ptree& root)
Camera* parse<Camera> (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 == "Configuration")
if (root.front().first == "Camera")
root = root.front().second;
model::Configuration* config = new model::Configuration();
config->animationFps = root.get<float>("animationFps");
return config;
Camera* camera = new Camera();
camera->scale = root.get<float>("<xmlattr>.scale", 1.0f);
camera->mainCamera = root.get<bool>("<xmlattr>.mainCamera", false);
return camera;
}
template <>
model::Item* parse<model::Item> (boost::property_tree::ptree& root)
Frame* parse<Frame> (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")
if (root.front().first == "Frame")
root = root.front().second;
// Parse
model::Item* item = new model::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;
Frame* frame = new Frame();
// Obtine tile set id
std::string tileSetPath = root.get<std::string>("<xmlattr>.tileSet");
frame->tileSetId = resources::ResourceManager::instance().getId(tileSetPath);
// Set properties
frame->tileSetCell = root.get<int>("<xmlattr>.cell");
frame->width = root.get<uint32_t>("<xmlattr>.w", 1u);
frame->height = root.get<uint32_t>("<xmlattr>.h", 1u);
frame->duration = root.get<float>("<xmlattr>.duration");
return frame;
}
template <>
model::Scene* parse<model::Scene> (boost::property_tree::ptree& root)
Sprite* parse<Sprite> (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 == "Scene")
if (root.front().first == "Sprite")
root = root.front().second;
model::Scene* scene = new model::Scene();
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight");
// This object can be declared in another file
std::string src = root.get<std::string>("<xmlattr>.src", "");
if (!src.empty())
{
ResourceId id = ResourceManager::instance().getId(src);
return parse<Sprite>(id);
}
// Parse sprite
Sprite* sprite = new Sprite();
sprite->anchorX = root.get<float>("<xmlattr>.anchorX");
sprite->anchorY = root.get<float>("<xmlattr>.anchorY");
sprite->animationVelocity = root.get<float>("<xmlattr>.animationVelocity", 1.0f);
for (auto child : root)
{
if (child.first == "GameObject")
if (child.first == "State")
{
base::GameObject* obj = parse<base::GameObject>(child.second);
scene->root.addChild(obj);
SpriteState* state = parse<SpriteState>(child.second);
sprite->addState(*state);
delete state;
}
}
return scene;
return sprite;
}
template <>
SpriteState* parse<SpriteState> (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 == "SpriteState")
root = root.front().second;
/****** Controller namespace ******/
SpriteState* spriteState = new SpriteState();
spriteState->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
if (child.first == "Frame")
{
Frame* frame = parse<Frame>(child.second);
spriteState->frames.push_back(*frame);
delete frame;
}
}
return spriteState;
}
template <>
controller::DebugController* parse<controller::DebugController> (boost::property_tree::ptree& root)
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)
if (root.front().first == "Transform")
root = root.front().second;
Transform* transform = new 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);
transform->h = root.get<float>("<xmlattr>.h", 0.0f);
return transform;
}
template <>
DebugController* parse<DebugController> (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 == "DebugController")
root = root.front().second;
controller::DebugController* controller = new controller::DebugController();
DebugController* controller = new DebugController();
return controller;
}
template <>
controller::items::Giftable* parse<controller::items::Giftable> (boost::property_tree::ptree& root)
Giftable* parse<items::Giftable> (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 == "Giftable")
root = root.front().second;
// Parse
return new controller::items::Giftable();
return new Giftable();
}
template <>
controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tree::ptree& root)
Weapon* parse<items::Weapon> (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 == "Weapon")
root = root.front().second;
// Parse
controller::items::Weapon* weapon = new controller::items::Weapon();
Weapon* weapon = new Weapon();
weapon->damage = root.get<float>("<xmlattr>.damage");
weapon->critProbability = root.get<float>("<xmlattr>.critProbability");
weapon->critDamage = root.get<float>("<xmlattr>.critDamage");
@@ -338,18 +249,18 @@ controller::items::Weapon* parse<controller::items::Weapon> (boost::property_tre
}
template <>
controller::player::PlayerController* parse<controller::player::PlayerController> (boost::property_tree::ptree& root)
PlayerController* parse<PlayerController> (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")
root = root.front().second;
controller::player::PlayerController* controller = new controller::player::PlayerController();
PlayerController* controller = new PlayerController();
return controller;
}
/****** Graphics namespace ******/
/****** Graphics ******/
template <>
graphics::BackgroundRenderer* parse<graphics::BackgroundRenderer> (boost::property_tree::ptree& root)
@@ -370,15 +281,127 @@ graphics::SpriteRenderer* parse<graphics::SpriteRenderer> (boost::property_tree:
root = root.front().second;
graphics::SpriteRenderer* renderer = new graphics::SpriteRenderer();
// Load sprite
std::string spritePath = root.get<std::string>("<xmlattr>.sprite");
resources::ResourceId spriteId = resources::ResourceManager::instance().getId(spritePath);
renderer->sprite = parse<base::Sprite>(spriteId);
return renderer;
}
/****** Model ******/
template <>
GameObject* parse<GameObject> (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 == "GameObject")
root = root.front().second;
GameObject* gameObj = new GameObject();
gameObj->name = root.get<std::string>("<xmlattr>.name");
for (auto child : root)
{
// Components::basic
if (child.first == "Camera")
gameObj->addComponent(parse<Camera>(child.second));
else if (child.first == "Sprite")
gameObj->addComponent(parse<Sprite>(child.second));
else if (child.first == "Transform")
gameObj->addComponent(parse<Transform>(child.second));
// Components::items
else if (child.first == "Giftable")
gameObj->addComponent(parse<Giftable>(child.second));
else if (child.first == "Item")
gameObj->addComponent(parse<Item>(child.second));
else if (child.first == "Weapon")
gameObj->addComponent(parse<Weapon>(child.second));
// Components::player
else if (child.first == "PlayerController")
gameObj->addComponent(parse<PlayerController>(child.second));
// Components
else if (child.first == "Background")
gameObj->addComponent(parse<Background>(child.second));
else if (child.first == "DebugController")
gameObj->addComponent(parse<DebugController>(child.second));
// Graphics
else if (child.first == "BackgroundRenderer")
gameObj->addComponent(parse<BackgroundRenderer>(child.second));
else if (child.first == "SpriteRenderer")
gameObj->addComponent(parse<SpriteRenderer>(child.second));
// !!! Add additional types here !!!
}
return gameObj;
}
template <>
Configuration* parse<Configuration> (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 == "Configuration")
root = root.front().second;
Configuration* config = new Configuration();
config->animationFps = root.get<float>("animationFps");
return config;
}
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)
if (root.front().first == "Scene")
root = root.front().second;
Scene* scene = new Scene();
scene->cellWidth = root.get<uint32_t>("<xmlattr>.cellWidth");
scene->cellHeight = root.get<uint32_t>("<xmlattr>.cellHeight");
for (auto child : root)
{
if (child.first == "GameObject")
{
GameObject* obj = parse<GameObject>(child.second);
scene->root.addChild(obj);
}
}
return scene;
}
/****** Controller namespace ******/
}
}