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 0000000..ffa9cdc
Binary files /dev/null and b/assets/sprites/items/hoe.png differ
diff --git a/assets_original/hoe.xcf b/assets_original/hoe.xcf
new file mode 100644
index 0000000..81b669c
Binary files /dev/null and b/assets_original/hoe.xcf differ
diff --git a/src/components/items/Hoe.cpp b/src/components/items/Hoe.cpp
new file mode 100644
index 0000000..d524fe3
--- /dev/null
+++ b/src/components/items/Hoe.cpp
@@ -0,0 +1,43 @@
+/*
+ * Hoe.cpp
+ *
+ * Created on: Dec 2, 2016
+ * Author: tibi
+ */
+
+#include
+
+#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);