diff --git a/assets/levels/Farm.back b/assets/levels/Farm.back deleted file mode 100644 index 0afbdf5..0000000 --- a/assets/levels/Farm.back +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/assets/levels/.directory b/assets/maps/.directory similarity index 100% rename from assets/levels/.directory rename to assets/maps/.directory diff --git a/assets/maps/Farm.map b/assets/maps/Farm.map new file mode 100644 index 0000000..5e7d694 --- /dev/null +++ b/assets/maps/Farm.map @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/levels/Farm_Background.csv b/assets/maps/Farm_Background.csv similarity index 100% rename from assets/levels/Farm_Background.csv rename to assets/maps/Farm_Background.csv diff --git a/assets/levels/Farm_Soil.csv b/assets/maps/Farm_Soil.csv similarity index 100% rename from assets/levels/Farm_Soil.csv rename to assets/maps/Farm_Soil.csv diff --git a/assets/plants/Plantable.xml b/assets/plants/Plantable.xml new file mode 100644 index 0000000..ff4db76 --- /dev/null +++ b/assets/plants/Plantable.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/plants/Seeds.xml b/assets/plants/Seeds.xml new file mode 100644 index 0000000..23effae --- /dev/null +++ b/assets/plants/Seeds.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/assets/plants/graphics/Ugliceea.png b/assets/plants/graphics/Ugliceea.png new file mode 100644 index 0000000..10107c0 Binary files /dev/null and b/assets/plants/graphics/Ugliceea.png differ diff --git a/assets/scenes/Game.scene b/assets/scenes/Game.scene index 6aa92f4..908e1c2 100644 --- a/assets/scenes/Game.scene +++ b/assets/scenes/Game.scene @@ -6,15 +6,17 @@ + - - - + + + + diff --git a/assets_original/plant_uglyceea.xcf b/assets_original/plant_uglyceea.xcf new file mode 100644 index 0000000..2f2241d Binary files /dev/null and b/assets_original/plant_uglyceea.xcf differ diff --git a/src/GameState.cpp b/src/GameState.cpp index 02112b2..3971375 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -11,12 +11,17 @@ namespace farmlands { GameState GameState::s_current; GameState::GameState() - : config(nullptr), + : gameInitialized(false), + config(nullptr), renderContext(), scene(nullptr), itemPrefabs(), - elapsedTime(0), - gameInitialized(false) + seedsPrefabs(), + plantsPrefabs(), + time(0), + date(0), + isMidnight(false), + deltaTime(0) { } @@ -24,6 +29,15 @@ GameState::~GameState() { if (scene) delete scene; if (config) delete config; + + for (auto obj : itemPrefabs) + delete obj; + + for (auto obj : seedsPrefabs) + delete obj; + + for (auto obj : plantsPrefabs) + delete obj; } GameState& farmlands::GameState::current() @@ -31,9 +45,4 @@ GameState& farmlands::GameState::current() return s_current; } -void farmlands::GameState::setCurrent(GameState& state) -{ - s_current = state; -} - } diff --git a/src/GameState.h b/src/GameState.h index d689fba..82d8e29 100644 --- a/src/GameState.h +++ b/src/GameState.h @@ -24,7 +24,9 @@ namespace farmlands { GameState(); ~GameState(); static GameState& current(); - static void setCurrent(GameState& state); + + // Initialized? + bool gameInitialized; // Game settings model::Configuration* config; @@ -35,10 +37,15 @@ namespace farmlands { // Scene model::Scene* scene; std::vector itemPrefabs; + std::vector seedsPrefabs; + std::vector plantsPrefabs; - // Misc - float elapsedTime; - bool gameInitialized; + // Timing + float time; + uint32_t date; + bool isMidnight; + + float deltaTime; private: static GameState s_current; diff --git a/src/assets/Ground.h b/src/assets/Ground.h index 2efb831..da74084 100644 --- a/src/assets/Ground.h +++ b/src/assets/Ground.h @@ -38,7 +38,7 @@ namespace assets { inline bool groundIsDirt(int cell) { return cell >= Ground::Dirt && cell <= Ground::DirtVariation8; } inline bool groundIsDrySoil(int cell) { return cell == Ground::SoilDry; } inline bool groundIsWetSoil(int cell) { return cell == Ground::SoilWet; } - + inline bool groundIsSoil(int cell) { return cell == Ground::SoilDry || cell == Ground::SoilWet; } } } diff --git a/src/components/DebugController.cpp b/src/components/DebugController.cpp index 9c30cd1..5313459 100644 --- a/src/components/DebugController.cpp +++ b/src/components/DebugController.cpp @@ -48,7 +48,7 @@ void DebugController::onUpdateLogic() vel = ScaleShiftVelocity; // Time independent - vel *= GameState::current().elapsedTime; + vel *= GameState::current().deltaTime; if (Input::instance().pressed(GameKey::Debug_ZoomIn)) m_camera->scale *= 1 + vel; diff --git a/src/components/GameTime.cpp b/src/components/GameTime.cpp new file mode 100644 index 0000000..9076071 --- /dev/null +++ b/src/components/GameTime.cpp @@ -0,0 +1,55 @@ +/* + * GameTime.cpp + * + * Created on: Dec 9, 2016 + * Author: tibi + */ + +#include +#include + +#include + +// Day length (in seconds) +//#define DAYLENGTH 24 * 60 +#define DAYLENGTH 10 + +namespace farmlands { +namespace components { + +GameTime::~GameTime() +{ +} + +model::Component* GameTime::clone() +{ + return new GameTime(); +} + +void GameTime::dump(unsigned level) +{ + for (unsigned i = 0; i < level; i++) + std::cout<<" "; + + std::cout << " .Component: GameTime \n"; +} + +void GameTime::onUpdateLogic() +{ + float delta = GameState::current().deltaTime; + GameState::current().time += delta; + GameState::current().isMidnight = false; + + if (GameState::current().time > DAYLENGTH) + { + GameState::current().time -= DAYLENGTH; + GameState::current().date++; + GameState::current().isMidnight = true; + + std::cout << "It's midnight! Date: " << GameState::current().date << "\n"; + } + +} + +} /* namespace components */ +} /* namespace farmlands */ diff --git a/src/components/GameTime.h b/src/components/GameTime.h new file mode 100644 index 0000000..25bd4f8 --- /dev/null +++ b/src/components/GameTime.h @@ -0,0 +1,33 @@ +/* + * GameTime.h + * + * Created on: Dec 9, 2016 + * Author: tibi + */ + +#ifndef COMPONENTS_GAMETIME_H_ +#define COMPONENTS_GAMETIME_H_ + +#include + +namespace farmlands { +namespace components { + + /** + * Maintains game time. + */ + class GameTime: public model::Component + { + public: + virtual ~GameTime(); + + virtual model::Component* clone() override; + virtual void dump(unsigned level) override; + + virtual void onUpdateLogic() override; + }; + +} /* namespace components */ +} /* namespace farmlands */ + +#endif /* COMPONENTS_GAMETIME_H_ */ diff --git a/src/components/basic/Grid.cpp b/src/components/basic/Grid.cpp index 157403c..78292b4 100644 --- a/src/components/basic/Grid.cpp +++ b/src/components/basic/Grid.cpp @@ -139,6 +139,11 @@ void Grid::set(model::GameObject* obj, int x, int y, bool throwOnOverwrite) } m_grid[index] = obj; + + // Set transform as well + Transform* transf = obj->component(); + transf->x = x; + transf->y = y; } } /* namespace basic */ diff --git a/src/components/basic/Grid.h b/src/components/basic/Grid.h index 6e97965..2f59f81 100644 --- a/src/components/basic/Grid.h +++ b/src/components/basic/Grid.h @@ -67,9 +67,9 @@ namespace basic { // Operations model::GameObject* get(int x, int y); + void set(model::GameObject* obj, int x, int y, bool throwOnOverwrite = true); private: - void set(model::GameObject* obj, int x, int y, bool throwOnOverwrite); utils::Rect m_bounds; model::GameObject** m_grid; diff --git a/src/components/basic/Sprite.cpp b/src/components/basic/Sprite.cpp index a79029f..5f5588a 100644 --- a/src/components/basic/Sprite.cpp +++ b/src/components/basic/Sprite.cpp @@ -64,7 +64,7 @@ void Sprite::dump(unsigned level) void Sprite::onPreRender() { - advanceTime(GameState::current().elapsedTime); + advanceTime(GameState::current().deltaTime); } void Sprite::addState(const SpriteState& state) diff --git a/src/components/items/Weapon.cpp b/src/components/items/Weapon.cpp index de4f76a..680b1d2 100644 --- a/src/components/items/Weapon.cpp +++ b/src/components/items/Weapon.cpp @@ -53,7 +53,7 @@ void Weapon::onUpdateLogic() { if (attackTimeLeft > 0) { - attackTimeLeft -= GameState::current().elapsedTime; + attackTimeLeft -= GameState::current().deltaTime; } } diff --git a/src/components/plants/Plant.cpp b/src/components/plants/Plant.cpp new file mode 100644 index 0000000..119a300 --- /dev/null +++ b/src/components/plants/Plant.cpp @@ -0,0 +1,101 @@ +/* + * Plant.cpp + * + * Created on: Dec 9, 2016 + * Author: tibi + */ + +#include +#include + +#include + +namespace farmlands { +namespace components { +namespace plants { + +Plant::Plant() + : needsWater(false), + daysWithoutWater(0), + maxDaysWithoutWater(2), + state(0), + stateDays(0), + stateLengths(), + m_sprite(nullptr) +{ + // The age is set to 0xffffffff so that it is updated at the next 'update logic' call +} + +Plant::~Plant() +{ +} + +model::Component* Plant::clone() +{ + Plant* clone = new Plant(); + clone->needsWater = needsWater; + clone->daysWithoutWater = daysWithoutWater; + clone->maxDaysWithoutWater = maxDaysWithoutWater; + clone->state = state; + clone->stateDays = stateDays; + clone->state = state; + std::copy(stateLengths.begin(), stateLengths.end(), std::back_inserter(clone->stateLengths)); + + return clone; +} + +void Plant::dump(unsigned level) +{ + for (unsigned i = 0; i < level; i++) + std::cout<<" "; + + std::cout << " .Component: Plant\n"; +} + +void Plant::onInitialize() +{ + m_sprite = gameObject->component(); + + if (stateLengths.empty()) + stateLengths.push_back(-1); +} + +void Plant::onUpdateLogic() +{ + // At midnight, the plant grows + if (GameState::current().isMidnight) + { + stateDays++; + daysWithoutWater++; + + // Exceeded plant's survival time without water + if (needsWater && daysWithoutWater > maxDaysWithoutWater) + die(); + + // Increment state + bool haveState = state < stateLengths.size(); + bool isFinite = (stateLengths[state] != -1); + bool finishedState = stateDays >= (size_t)stateLengths[state]; + + if (haveState && isFinite && finishedState) + { + ++state; + stateDays = 0; + m_sprite->setState(state); + } + } +} + +void Plant::onWatered() +{ + daysWithoutWater = 0; +} + +void Plant::die() +{ + // TODO: implement plant death +} + +} /* namespace plants */ +} /* namespace components */ +} /* namespace farmlands */ diff --git a/src/components/plants/Plant.h b/src/components/plants/Plant.h new file mode 100644 index 0000000..81a2c73 --- /dev/null +++ b/src/components/plants/Plant.h @@ -0,0 +1,55 @@ +/* + * Plant.h + * + * Created on: Dec 9, 2016 + * Author: tibi + */ + +#ifndef COMPONENTS_PLANTS_PLANT_H_ +#define COMPONENTS_PLANTS_PLANT_H_ + +#include +#include + +#include +#include + +namespace farmlands { +namespace components { +namespace plants { + + class Plant: public model::Component + { + public: + Plant(); + virtual ~Plant(); + + virtual model::Component* clone() override; + virtual void dump(unsigned level) override; + + virtual void onInitialize() override; + virtual void onUpdateLogic() override; + + void onWatered(); + void die(); + + bool needsWater; // Watering - if the plant needs water (trees don't need water). + uint32_t daysWithoutWater; // How many days passed since last watered + uint32_t maxDaysWithoutWater; // How many days the plant can survive without water + + size_t state; // Current state + uint32_t stateDays; // How many days have elapsed since the last state change + + // The length of each state, measured in days. + // After all the states are passed through, the plant dies; -1 means infinity + std::vector stateLengths; + + private: + basic::Sprite* m_sprite; + }; + +} /* namespace plants */ +} /* namespace components */ +} /* namespace farmlands */ + +#endif /* COMPONENTS_PLANTS_PLANT_H_ */ diff --git a/src/components/plants/Seed.cpp b/src/components/plants/Seed.cpp new file mode 100644 index 0000000..7fbe00f --- /dev/null +++ b/src/components/plants/Seed.cpp @@ -0,0 +1,101 @@ +/* + * Seed.cpp + * + * Created on: Dec 6, 2016 + * Author: tibi + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace farmlands::model; + +namespace farmlands { +namespace components { +namespace plants { + +Seed::Seed() + : plantName(), + m_plant(nullptr), + m_grid(nullptr), + m_map(nullptr) +{ +} + +Seed::~Seed() +{ +} + +model::Component* Seed::clone() +{ + Seed* clone = new Seed(); + clone->plantName = plantName; + + return clone; +} + +void Seed::dump(unsigned level) +{ + for (unsigned i = 0; i < level; i++) + std::cout<<" "; + + std::cout << " .Component: Seed plant=" << plantName <<"\n"; +} + +void Seed::onInitialize() +{ + GameObject* root = &GameState::current().scene->root; + + // Obtain pointer to plant + for (auto plant : GameState::current().plantsPrefabs) + if (plant->name == plantName) + m_plant = plant; + + Assert (m_plant != nullptr, "Plant " + plantName + " is missing."); + + // Obtain pointer to object layer & map + auto objIt = root->findByName("Object Layer"); + Assert (objIt != root->childrenEnd(), "Can't find object layer."); + m_grid = (*objIt)->component(); + + auto mapIt = root->findByComponent(); + Assert (mapIt != root->childrenEnd(), "Can't find map layer."); + m_map = (*mapIt)->component(); +} + +void Seed::performAction(float x, float y, model::Direction d) +{ + // Compute planting position + float digX, digY; + translate(x, y, d, 0.5f, &digX, &digY); + + size_t col = floorf(digX); + size_t row = floorf(digY); + + // Soil is located on layer 1 + Cell cell = m_map->layer(1).get(row, col); + bool isSoil = assets::groundIsSoil(cell); + + bool isFree = (m_grid->get(col, row) == nullptr); + + // Place plant + if (isSoil && isFree) + { + // Plant + auto inst = GameObject::instantiate(m_plant, plantName, m_grid->gameObject); + m_grid->set(inst, col, row); + + std::cout << "Planted " << plantName << " at " << col <<", " << row <<"\n"; + } +} + +} /* namespace plants */ +} /* namespace components */ +} /* namespace farmlands */ diff --git a/src/components/plants/Seed.h b/src/components/plants/Seed.h new file mode 100644 index 0000000..c54e031 --- /dev/null +++ b/src/components/plants/Seed.h @@ -0,0 +1,45 @@ +/* + * Seed.h + * + * Created on: Dec 6, 2016 + * Author: tibi + */ + +#ifndef COMPONENTS_ITEMS_SEED_H_ +#define COMPONENTS_ITEMS_SEED_H_ + +#include +#include +#include +#include + +namespace farmlands { +namespace components { +namespace plants { + + class Seed: public model::Component, public items::IPlayerAction + { + public: + Seed(); + virtual ~Seed(); + + virtual model::Component* clone() override; + virtual void dump(unsigned level) override; + + virtual void onInitialize() override; + + virtual void performAction(float x, float y, model::Direction d) override; + + std::string plantName; + + private: + model::GameObject* m_plant; + basic::Grid* m_grid; + Map* m_map; + }; + +} /* namespace plants */ +} /* namespace components */ +} /* namespace farmlands */ + +#endif /* COMPONENTS_ITEMS_SEED_H_ */ diff --git a/src/components/player/PlayerMovement.cpp b/src/components/player/PlayerMovement.cpp index 0184f76..ce02e06 100644 --- a/src/components/player/PlayerMovement.cpp +++ b/src/components/player/PlayerMovement.cpp @@ -81,7 +81,7 @@ void PlayerMovement::onUpdateLogic() velMultiplier = PlayerRunVelocity; // Make movement time independent - velMultiplier *= GameState::current().elapsedTime; + velMultiplier *= GameState::current().deltaTime; // Get velocity of axes float vx = Input::instance().getX() * velMultiplier; diff --git a/src/controller/FarmlandsGame.cpp b/src/controller/FarmlandsGame.cpp index 9a20483..d815ce7 100644 --- a/src/controller/FarmlandsGame.cpp +++ b/src/controller/FarmlandsGame.cpp @@ -113,7 +113,7 @@ int FarmlandsGame::run() { // Update elapsed time Uint32 now = SDL_GetTicks(); - GameState::current().elapsedTime = (now - m_time) * 0.001f; + GameState::current().deltaTime = (now - m_time) * 0.001f; m_time = now; SDL_Event event; diff --git a/src/resources/ResourceManager.cpp b/src/resources/ResourceManager.cpp index c472ef1..61109eb 100644 --- a/src/resources/ResourceManager.cpp +++ b/src/resources/ResourceManager.cpp @@ -87,6 +87,9 @@ void ResourceManager::loadGame() storage::parseCollection(R::Items::Tools, GameState::current().itemPrefabs); storage::parseCollection(R::Items::Weapons, GameState::current().itemPrefabs); + storage::parseCollection(R::Plants::Plantable, GameState::current().plantsPrefabs); + storage::parseCollection(R::Plants::Seeds, GameState::current().seedsPrefabs); + // Get to "Inventory" object model::GameObject* root = &GameState::current().scene->root; model::GameObject* player = *root->findByName("Player"); @@ -95,6 +98,9 @@ void ResourceManager::loadGame() // Give player all items for (auto prefab : GameState::current().itemPrefabs) model::GameObject::instantiate(prefab, "inv item", inventory); + + for (auto prefab : GameState::current().seedsPrefabs) + model::GameObject::instantiate(prefab, "inv seed", inventory); } std::string ResourceManager::getPath(ResourceId resourceId) diff --git a/src/resources/Resources.g.h b/src/resources/Resources.g.h index cccdd1f..90f45a3 100644 --- a/src/resources/Resources.g.h +++ b/src/resources/Resources.g.h @@ -28,45 +28,52 @@ namespace resources { { Game = 9, }; + enum Plants + { + Seeds = 10, + Plantable = 11, + graphics_Ugliceea = 12, + }; + enum Maps + { + Farm_Background = 13, + Farm = 14, + Farm_Soil = 15, + }; enum Fonts { - DejaVuSans = 10, + DejaVuSans = 16, }; enum Tilesets { - PlayerTiles = 11, - Ground = 12, + PlayerTiles = 17, + Ground = 18, }; enum Ui { - Mini_inventory = 13, - Cursor = 14, - }; - enum Levels - { - Farm_Background = 15, - Farm = 16, - Farm_Soil = 17, + Mini_inventory = 19, + Cursor = 20, }; enum Config { - Default = 18, + Default = 21, }; enum Items { - Tools = 19, - Weapons = 20, + Tools = 22, + Weapons = 23, }; } const int RInfo_Sprites_Begin = 0; const int RInfo_Scenes_Begin = 9; - const int RInfo_Fonts_Begin = 10; - const int RInfo_Tilesets_Begin = 11; - const int RInfo_Ui_Begin = 13; - const int RInfo_Levels_Begin = 15; - const int RInfo_Config_Begin = 18; - const int RInfo_Items_Begin = 19; + const int RInfo_Plants_Begin = 10; + const int RInfo_Maps_Begin = 13; + const int RInfo_Fonts_Begin = 16; + const int RInfo_Tilesets_Begin = 17; + const int RInfo_Ui_Begin = 19; + const int RInfo_Config_Begin = 21; + const int RInfo_Items_Begin = 22; /** * This array contains the names of all the files, and the corresponding file type. @@ -82,14 +89,17 @@ namespace resources { { "sprites/items/sword.png", ResourceType::Texture }, { "sprites/items/hoe.png", ResourceType::Texture }, { "scenes/Game.scene", ResourceType::Scene }, + { "plants/Seeds.xml", ResourceType::None }, + { "plants/Plantable.xml", ResourceType::None }, + { "plants/graphics/Ugliceea.png", ResourceType::Texture }, + { "maps/Farm_Background.csv", ResourceType::MapLayer }, + { "maps/Farm.map", ResourceType::None }, + { "maps/Farm_Soil.csv", ResourceType::MapLayer }, { "fonts/DejaVuSans.ttf", ResourceType::Font }, { "tilesets/PlayerTiles.png", ResourceType::Texture }, { "tilesets/Ground.png", ResourceType::Texture }, { "ui/mini_inventory.png", ResourceType::Texture }, { "ui/cursor.png", ResourceType::Texture }, - { "levels/Farm_Background.csv", ResourceType::MapLayer }, - { "levels/Farm.back", ResourceType::Map }, - { "levels/Farm_Soil.csv", ResourceType::MapLayer }, { "config/Default.config", ResourceType::Configuration }, { "items/Tools.items", ResourceType::ItemCollection }, { "items/Weapons.items", ResourceType::ItemCollection }, diff --git a/src/storage/Parsers.cpp b/src/storage/Parsers.cpp index a61bc73..372c15b 100644 --- a/src/storage/Parsers.cpp +++ b/src/storage/Parsers.cpp @@ -53,7 +53,7 @@ void parseMapCells(resources::ResourceId cellsResource, components::Map* map, co template <> components::Map* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Map") root = root.front().second; @@ -90,9 +90,12 @@ components::Map* parse (boost::property_tree::ptree& root) delete tileSet; // Read cells - std::string cellsPath = layerNode.second.get(".cells"); - resources::ResourceId cellsId = resources::ResourceManager::instance().getId(cellsPath); - parseMapCells(cellsId, map, layer); + std::string cellsPath = layerNode.second.get(".cells", ""); + if (!cellsPath.empty()) + { + resources::ResourceId cellsId = resources::ResourceManager::instance().getId(cellsPath); + parseMapCells(cellsId, map, layer); + } } } @@ -115,7 +118,7 @@ components::basic::Camera* parse (boost::property_tre template <> components::basic::Frame* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Frame") root = root.front().second; @@ -138,7 +141,7 @@ components::basic::Frame* parse (boost::property_tree: template <> components::basic::Grid* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Grid") root = root.front().second; @@ -158,7 +161,7 @@ components::basic::Grid* parse (boost::property_tree::p template <> components::basic::Sprite* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Sprite") root = root.front().second; @@ -192,7 +195,7 @@ components::basic::Sprite* parse (boost::property_tre template <> components::basic::SpriteState* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "SpriteState") root = root.front().second; @@ -215,7 +218,7 @@ components::basic::SpriteState* parse (boost::pr template <> components::basic::Transform* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Transform") root = root.front().second; @@ -231,7 +234,7 @@ components::basic::Transform* parse (boost::proper template <> components::DebugController* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "DebugController") root = root.front().second; @@ -239,10 +242,21 @@ components::DebugController* parse (boost::property return controller; } +template <> +components::GameTime* parse (boost::property_tree::ptree& root) +{ + // Ensure we are on the correct node (property tree seems to add root of its own) + if (root.front().first == "GameTime") + root = root.front().second; + + components::GameTime* gameTime = new components::GameTime(); + return gameTime; +} + template <> components::items::Axe* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Axe") root = root.front().second; @@ -253,7 +267,7 @@ components::items::Axe* parse (boost::property_tree::ptr template <> components::items::Giftable* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Giftable") root = root.front().second; @@ -264,7 +278,7 @@ components::items::Giftable* parse (boost::property template <> components::items::Hoe* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Hoe") root = root.front().second; @@ -275,7 +289,7 @@ components::items::Hoe* parse (boost::property_tree::ptr template <> components::items::Item* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Item") root = root.front().second; @@ -283,7 +297,7 @@ components::items::Item* parse (boost::property_tree::p components::items::Item* item = new components::items::Item(); item->name = root.get(".name"); item->description = root.get(".description"); - item->level = root.get(".level"); + item->level = root.get(".level", 1); return item; } @@ -291,7 +305,7 @@ components::items::Item* parse (boost::property_tree::p template <> components::items::Pickaxe* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Pickaxe") root = root.front().second; @@ -302,7 +316,7 @@ components::items::Pickaxe* parse (boost::property_t template <> components::items::Scythe* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Pickaxe") root = root.front().second; @@ -313,7 +327,7 @@ components::items::Scythe* parse (boost::property_tre template <> components::items::WateringCan* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "WateringCan") root = root.front().second; @@ -328,7 +342,7 @@ components::items::WateringCan* parse (boost::pr template <> components::items::Weapon* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Weapon") root = root.front().second; @@ -341,10 +355,48 @@ components::items::Weapon* parse (boost::property_tre return weapon; } +template <> +components::plants::Plant* parse (boost::property_tree::ptree& root) +{ + // Ensure we are on the correct node (property tree seems to add root of its own) + if (root.front().first == "Plant") + root = root.front().second; + + // Parse + components::plants::Plant* plant = new components::plants::Plant(); + plant->needsWater = root.get(".needsWater"); + plant->daysWithoutWater = root.get(".daysWithoutWater", 0); + plant->maxDaysWithoutWater = root.get(".maxDaysWithoutWater", 2); + plant->state = root.get(".state", 0); + plant->stateDays = root.get(".stateDays", 0); + + for (auto child : root) + if (child.first == "State") + { + int32_t length = child.second.get(".len", -1); + plant->stateLengths.push_back(length); + } + + return plant; +} + +template <> +components::plants::Seed* parse (boost::property_tree::ptree& root) +{ + // Ensure we are on the correct node (property tree seems to add root of its own) + if (root.front().first == "Seed") + root = root.front().second; + + // Parse + components::plants::Seed* seed = new components::plants::Seed(); + seed->plantName = root.get(".plantName"); + return seed; +} + template <> components::player::PlayerInventory* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "PlayerInventory") root = root.front().second; @@ -358,7 +410,7 @@ components::player::PlayerInventory* parse template <> components::player::PlayerMovement* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "PlayerMovement") root = root.front().second; @@ -374,7 +426,7 @@ components::player::PlayerMovement* parse (b template <> graphics::MapRenderer* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "MapRenderer") root = root.front().second; @@ -385,7 +437,7 @@ graphics::MapRenderer* parse (boost::property_tree::ptree template <> graphics::SpriteRenderer* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "SpriteRenderer") root = root.front().second; @@ -399,7 +451,7 @@ graphics::SpriteRenderer* parse (boost::property_tree: template <> model::GameObject* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "GameObject") root = root.front().second; @@ -418,6 +470,9 @@ model::GameObject* parse (boost::property_tree::ptree& root) else if (child.first == "Camera") gameObj->addComponent(parse(child.second)); + else if (child.first == "Grid") + gameObj->addComponent(parse(child.second)); + else if (child.first == "Sprite") gameObj->addComponent(parse(child.second)); @@ -449,6 +504,13 @@ model::GameObject* parse (boost::property_tree::ptree& root) else if (child.first == "Weapon") gameObj->addComponent(parse(child.second)); + // Components::plants + else if (child.first == "Plant") + gameObj->addComponent(parse(child.second)); + + else if (child.first == "Seed") + gameObj->addComponent(parse(child.second)); + // Components::player else if (child.first == "PlayerInventory") gameObj->addComponent(parse(child.second)); @@ -457,12 +519,15 @@ model::GameObject* parse (boost::property_tree::ptree& root) gameObj->addComponent(parse(child.second)); // Components - else if (child.first == "Map") - gameObj->addComponent(parse(child.second)); - else if (child.first == "DebugController") gameObj->addComponent(parse(child.second)); + else if (child.first == "GameTime") + gameObj->addComponent(parse(child.second)); + + else if (child.first == "Map") + gameObj->addComponent(parse(child.second)); + // Graphics else if (child.first == "MapRenderer") gameObj->addComponent(parse(child.second)); @@ -479,7 +544,7 @@ model::GameObject* parse (boost::property_tree::ptree& root) template <> model::Configuration* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Configuration") root = root.front().second; @@ -492,7 +557,7 @@ model::Configuration* parse (boost::property_tree::ptree& template <> model::Scene* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "Scene") root = root.front().second; @@ -515,7 +580,7 @@ model::Scene* parse (boost::property_tree::ptree& root) template<> model::TileSet* parse (boost::property_tree::ptree& root) { - // Ensure we are on the model::Scene node (property tree seems to add root of its own) + // Ensure we are on the correct node (property tree seems to add root of its own) if (root.front().first == "TileSet") root = root.front().second; diff --git a/src/storage/Parsers.h b/src/storage/Parsers.h index 367aadd..054f0e0 100644 --- a/src/storage/Parsers.h +++ b/src/storage/Parsers.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -62,6 +65,9 @@ namespace storage { template <> components::DebugController* parse (boost::property_tree::ptree& root); + template <> + components::GameTime* parse (boost::property_tree::ptree& root); + template <> components::items::Axe* parse (boost::property_tree::ptree& root); @@ -86,6 +92,12 @@ namespace storage { template <> components::items::Weapon* parse (boost::property_tree::ptree& root); + template <> + components::plants::Plant* parse (boost::property_tree::ptree& root); + + template <> + components::plants::Seed* parse (boost::property_tree::ptree& root); + template <> components::player::PlayerInventory* parse (boost::property_tree::ptree& root);