From 91c0da855b4fb2a97606e3e25454b3df0fd506e9 Mon Sep 17 00:00:00 2001 From: Tiberiu Chibici Date: Fri, 2 Dec 2016 22:48:37 +0200 Subject: [PATCH] Implemented hoe --- assets/items/Tools.items | 19 ++++++++- assets/sprites/items/HoeStone.sprite | 10 +++++ assets/sprites/items/hoe.png | Bin 0 -> 266 bytes assets_original/hoe.xcf | Bin 0 -> 1482 bytes src/components/items/Hoe.cpp | 43 ++++++++++++++++++++ src/components/items/Hoe.h | 34 ++++++++++++++++ src/components/items/Weapon.cpp | 14 +++---- src/components/items/Weapon.h | 2 +- src/components/player/PlayerController.cpp | 37 ++++++++--------- src/components/player/PlayerController.h | 4 +- src/resources/ResourceManager.cpp | 2 +- src/resources/Resources.g.h | 44 +++++++++++---------- src/storage/Parsers.cpp | 14 +++++++ src/storage/Parsers.h | 4 ++ 14 files changed, 174 insertions(+), 53 deletions(-) create mode 100644 assets/sprites/items/HoeStone.sprite create mode 100644 assets/sprites/items/hoe.png create mode 100644 assets_original/hoe.xcf create mode 100644 src/components/items/Hoe.cpp create mode 100644 src/components/items/Hoe.h diff --git a/assets/items/Tools.items b/assets/items/Tools.items index 9ad6e5e..bed8e7a 100644 --- a/assets/items/Tools.items +++ b/assets/items/Tools.items @@ -1,6 +1,21 @@ -Player + + + + + + + + + + + \ No newline at end of file diff --git a/assets/sprites/items/HoeStone.sprite b/assets/sprites/items/HoeStone.sprite new file mode 100644 index 0000000..a36b04b --- /dev/null +++ b/assets/sprites/items/HoeStone.sprite @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/assets/sprites/items/hoe.png b/assets/sprites/items/hoe.png new file mode 100644 index 0000000000000000000000000000000000000000..ffa9cdcb2469891c3e50bf3d81f619adb478e88a GIT binary patch literal 266 zcmeAS@N?(olHy`uVBq!ia0vp^3P3Et!3HGD8EPYe6kC$Fy9>jA5L~c#`DCC7XMsm# zF#`j)FbFd;%$g$s6l5>)^mS!_z{4aWCHEvHVh>PAvcxr_#5q4VH#M(>!MP|ku_QG` zp**uBL&4qCHz2%`PaLSI$kW9!#N+te$s2ha40u@dHG(%ZAC<|IP!zTPr!ccoQ^Sc{ z*O619j&c6`+=`%Sm0D}l`SwnitCIdRtx^8LR0hEVcexMjYWy9+pwhaG?M%-Up|orF z9CEXG?v*iz^B!Z)WjtYN{`!AN;}V6H=f$rqJNAH&f10*Jx&O>hK-(ESUHx3vIVCg! E06mdZ*#H0l literal 0 HcmV?d00001 diff --git a/assets_original/hoe.xcf b/assets_original/hoe.xcf new file mode 100644 index 0000000000000000000000000000000000000000..81b669c58751703155a1dca054e59b01f99ba2c2 GIT binary patch literal 1482 zcmeHF%}&BV5MD|}1c~^Y2%2m>!J&$p7`bYUX`;rf-b&e)P5MLHiu4^k_yFF03SYoS zGKM-+S}fAoaPj0M-^{n)?#ym>M(1Xq4poh6++YNZ0;)*BRsp#jxHZ56pele{2Nchq z2|<8u0K^DeQN*N8NWOqr(cunBwM~;*9;``N@!VmG$GY^9dp&w7U*Fa8tqCu<-Xo(f+0Ax!BFZ)LArD>$GTELXHS#IL#vRtX z!FpM4j(P`JFU!qQFTi?P4(bK91nNWx?(xGYARdN7I1H%%RsZoWw6y-`$6M&dl>Uo9 O`Y- + +#include + +namespace farmlands { +namespace components { +namespace items { + +Hoe::Hoe() +{ +} + +Hoe::~Hoe() +{ +} + +model::Component* Hoe::clone() +{ + return new Hoe(); +} + +void Hoe::dump(unsigned level) +{ + for (unsigned i = 0; i < level; i++) + std::cout<<" "; + + std::cout << " .Component: Hoe\n"; +} + +void Hoe::performAttack(float x, float y, model::Direction d) +{ +} + +} /* namespace items */ +} /* namespace components */ +} /* namespace farmlands */ diff --git a/src/components/items/Hoe.h b/src/components/items/Hoe.h new file mode 100644 index 0000000..fc4e3e7 --- /dev/null +++ b/src/components/items/Hoe.h @@ -0,0 +1,34 @@ +/* + * Hoe.h + * + * Created on: Dec 2, 2016 + * Author: tibi + */ + +#ifndef COMPONENTS_ITEMS_HOE_H_ +#define COMPONENTS_ITEMS_HOE_H_ + +#include +#include + +namespace farmlands { +namespace components { +namespace items { + + class Hoe: public model::Component + { + public: + Hoe(); + virtual ~Hoe(); + + virtual model::Component* clone() override; + virtual void dump(unsigned level) override; + + void performAttack(float x, float y, model::Direction d); + }; + +} /* namespace items */ +} /* namespace components */ +} /* namespace farmlands */ + +#endif /* COMPONENTS_ITEMS_HOE_H_ */ diff --git a/src/components/items/Weapon.cpp b/src/components/items/Weapon.cpp index db54ea2..8170d78 100644 --- a/src/components/items/Weapon.cpp +++ b/src/components/items/Weapon.cpp @@ -19,8 +19,8 @@ Weapon::Weapon() critProbability(0), critDamage(0), attackDuration(1.0f), - m_sprite(nullptr), - m_attackTimeLeft(0) + attackTimeLeft(0), + m_sprite(nullptr) { } @@ -46,22 +46,22 @@ void Weapon::onInitialize() void Weapon::onPreRender() { - m_sprite->setState(m_attackTimeLeft > 0); + m_sprite->setState(attackTimeLeft > 0); } void Weapon::onUpdateLogic() { - if (m_attackTimeLeft > 0) + if (attackTimeLeft > 0) { - m_attackTimeLeft -= GameState::current().elapsedTime; + attackTimeLeft -= GameState::current().elapsedTime; } } void Weapon::performAttack(float x, float y, model::Direction d) { - if (m_attackTimeLeft <= 0) + if (attackTimeLeft <= 0) { - m_attackTimeLeft = attackDuration; + attackTimeLeft = attackDuration; } } diff --git a/src/components/items/Weapon.h b/src/components/items/Weapon.h index 5cbefe3..46887a9 100644 --- a/src/components/items/Weapon.h +++ b/src/components/items/Weapon.h @@ -41,10 +41,10 @@ namespace items { float critDamage; float attackDuration; // In seconds + float attackTimeLeft; private: basic::Sprite* m_sprite; - float m_attackTimeLeft; }; } diff --git a/src/components/player/PlayerController.cpp b/src/components/player/PlayerController.cpp index 1ed230b..6fac3f0 100644 --- a/src/components/player/PlayerController.cpp +++ b/src/components/player/PlayerController.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -45,7 +46,8 @@ PlayerController::PlayerController() m_facingDirection(Direction::South), m_walking(false), m_running(false), - m_currentItem(nullptr) + m_currentItem(nullptr), + m_currentWeapon(nullptr) { } @@ -81,6 +83,11 @@ void PlayerController::handleAttackEvents(SDL_Event& event) if (weapon) weapon->performAttack(m_transform->x, m_transform->y, m_facingDirection); + // Tools + Hoe* hoe = m_currentItem->component(); + if (hoe) + hoe->performAttack(m_transform->x, m_transform->y, m_facingDirection); + // Gift behavior Giftable* giftable = m_currentItem->component(); if (giftable) @@ -115,11 +122,7 @@ void PlayerController::handleInventoryEvents(SDL_Event& event) // Instantiate new object m_currentItem = GameObject::instantiate(itemPrefab, "Current item", gameObject); - - // Print some information -// Item* item = itemPrefab->component(); -// std::string name = (item == nullptr) ? "unknown" : item->name; -// std::cout << "Instantiated object " << slot << " (" << name << ")\n"; + m_currentWeapon = m_currentItem->component(); } else if (0 <= slot) { @@ -135,19 +138,16 @@ void PlayerController::onUpdateLogic() // Compute movement velocity float velMultiplier = PlayerWalkVelocity; -// if (m_attackTimeLeft) -// { -// velMultiplier = PlayerAttackVelocity; -// --m_attackTimeLeft; -// } else - - if (Input::instance().pressed(GameKey::Run)) + if (m_currentWeapon && m_currentWeapon->attackTimeLeft > 0) + { + velMultiplier = PlayerAttackVelocity; + } + else if (Input::instance().pressed(GameKey::Run)) { velMultiplier = PlayerRunVelocity; m_running = true; } - // Make movement time independent velMultiplier *= GameState::current().elapsedTime; @@ -189,8 +189,8 @@ void PlayerController::onPreRender() // Set animation velocity float animVelocity = (m_running) ? 1.0f : 0.7f; -// if (m_attackTimeLeft) -// animVelocity = 0.1f; + if (m_currentWeapon && m_currentWeapon->attackTimeLeft > 0) + animVelocity = 0.1f; sprite->animationVelocity = animVelocity; @@ -222,11 +222,6 @@ Direction PlayerController::getDirection(float vx, float vy) return VelocitySignDirections[xx + 1][yy + 1]; } -void PlayerController::attack() -{ - // For now - nothing -} - void PlayerController::dump(unsigned level) { for (unsigned i = 0; i < level; i++) diff --git a/src/components/player/PlayerController.h b/src/components/player/PlayerController.h index 4c0f3ac..645468d 100644 --- a/src/components/player/PlayerController.h +++ b/src/components/player/PlayerController.h @@ -9,6 +9,7 @@ #define CONTROLLER_PLAYERCONTROLLER_H_ #include +#include #include #include @@ -39,13 +40,14 @@ namespace player { void handleInventoryEvents(SDL_Event& event); bool canMove(float x, float y); - void attack(); basic::Transform* m_transform; model::Direction m_facingDirection; bool m_walking, m_running; model::GameObject* m_currentItem; + items::Weapon* m_currentWeapon; + }; } diff --git a/src/resources/ResourceManager.cpp b/src/resources/ResourceManager.cpp index 10ab02c..f3cb994 100644 --- a/src/resources/ResourceManager.cpp +++ b/src/resources/ResourceManager.cpp @@ -83,7 +83,7 @@ void ResourceManager::loadGame() GameState::current().scene = storage::parse(R::Scenes::Game); GameState::current().scene->root.onCreate(); -// storage::parseCollection(R::Items::Tools, GameState::current().itemPrefabs); + storage::parseCollection(R::Items::Tools, GameState::current().itemPrefabs); storage::parseCollection(R::Items::Weapons, GameState::current().itemPrefabs); } diff --git a/src/resources/Resources.g.h b/src/resources/Resources.g.h index 434e3d3..1eb39ee 100644 --- a/src/resources/Resources.g.h +++ b/src/resources/Resources.g.h @@ -17,51 +17,53 @@ namespace resources { Player = 0, items_Lvl2Sword = 1, items_Stone_pickaxe = 2, - items_Lvl1Sword = 3, - items_Sword = 4, + items_HoeStone = 3, + items_Lvl1Sword = 4, + items_Sword = 5, + items_Hoe = 6, }; enum Scenes { - Game = 5, + Game = 7, }; enum Fonts { - DejaVuSans = 6, + DejaVuSans = 8, }; enum Tilesets { - PlayerTiles = 7, - Ground = 8, + PlayerTiles = 9, + Ground = 10, }; enum Ui { - Mini_inventory = 9, - Cursor = 10, + Mini_inventory = 11, + Cursor = 12, }; enum Levels { - Farm_Background = 11, - Farm = 12, + Farm_Background = 13, + Farm = 14, }; enum Config { - Default = 13, + Default = 15, }; enum Items { - Tools = 14, - Weapons = 15, + Tools = 16, + Weapons = 17, }; } const int RInfo_Sprites_Begin = 0; - const int RInfo_Scenes_Begin = 5; - const int RInfo_Fonts_Begin = 6; - const int RInfo_Tilesets_Begin = 7; - const int RInfo_Ui_Begin = 9; - const int RInfo_Levels_Begin = 11; - const int RInfo_Config_Begin = 13; - const int RInfo_Items_Begin = 14; + const int RInfo_Scenes_Begin = 7; + const int RInfo_Fonts_Begin = 8; + const int RInfo_Tilesets_Begin = 9; + const int RInfo_Ui_Begin = 11; + const int RInfo_Levels_Begin = 13; + const int RInfo_Config_Begin = 15; + const int RInfo_Items_Begin = 16; /** * This array contains the names of all the files, and the corresponding file type. @@ -70,8 +72,10 @@ namespace resources { { "sprites/Player.sprite", ResourceType::Sprite }, { "sprites/items/Lvl2Sword.sprite", ResourceType::Sprite }, { "sprites/items/stone_pickaxe.sprite", ResourceType::Sprite }, + { "sprites/items/HoeStone.sprite", ResourceType::Sprite }, { "sprites/items/Lvl1Sword.sprite", ResourceType::Sprite }, { "sprites/items/sword.png", ResourceType::Texture }, + { "sprites/items/hoe.png", ResourceType::Texture }, { "scenes/Game.scene", ResourceType::Scene }, { "fonts/DejaVuSans.ttf", ResourceType::Font }, { "tilesets/PlayerTiles.png", ResourceType::Texture }, diff --git a/src/storage/Parsers.cpp b/src/storage/Parsers.cpp index 0d0869e..03a4170 100644 --- a/src/storage/Parsers.cpp +++ b/src/storage/Parsers.cpp @@ -313,6 +313,9 @@ GameObject* parse (boost::property_tree::ptree& root) else if (child.first == "Giftable") gameObj->addComponent(parse(child.second)); + else if (child.first == "Hoe") + gameObj->addComponent(parse(child.second)); + else if (child.first == "Item") gameObj->addComponent(parse(child.second)); @@ -356,6 +359,17 @@ Configuration* parse (boost::property_tree::ptree& root) return config; } +template <> +Hoe* parse (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 (boost::property_tree::ptree& root) { diff --git a/src/storage/Parsers.h b/src/storage/Parsers.h index 9783472..0228ae0 100644 --- a/src/storage/Parsers.h +++ b/src/storage/Parsers.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,9 @@ namespace storage { template <> components::items::Giftable* parse (boost::property_tree::ptree& root); + template <> + components::items::Hoe* parse (boost::property_tree::ptree& root); + template <> components::items::Item* parse (boost::property_tree::ptree& root);