diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-03-17 22:17:30 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-03-17 22:17:30 -0400 |
commit | 19ade83b74aa66c80129409d97c2c98fa8b8534c (patch) | |
tree | ed8fdfb53a302d1e236cb74b7f0413f83f55020d | |
parent | bf01660ab468f49d63a133c28131ab21bba3a1a1 (diff) |
bow & arrow draft
-rw-r--r-- | config/items.xml | 4 | ||||
-rw-r--r-- | include/inventory.hpp | 22 | ||||
-rw-r--r-- | include/vector2.hpp | 15 | ||||
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | src/attack.cpp | 3 | ||||
-rw-r--r-- | src/components.cpp | 2 | ||||
-rw-r--r-- | src/player.cpp | 40 | ||||
-rw-r--r-- | src/ui.cpp | 2 |
8 files changed, 79 insertions, 13 deletions
diff --git a/config/items.xml b/config/items.xml index 6767bc0..3601c18 100644 --- a/config/items.xml +++ b/config/items.xml @@ -17,9 +17,9 @@ <!--##########--> <!-- WEAPONS --> -<item name="Wood Sword" type="Sword" damage="3" maxStackSize="1" sprite="assets/items/SWORD_WOOD.png" /> +<item name="Wood Sword" type="Sword" damage="3" maxStackSize="1" sound="assets/sounds/longSwing.wav" sprite="assets/items/SWORD_WOOD.png" /> <item name="Hunters Bow" type="Bow" damage="2" maxStackSize="1" sprite="assets/items/bow.png"/> -<item name="Crude Arrow" type="Arrow" damage="1" maxStackSize="99" sprite="assets/items/arrow_crude.png"/> +<item name="Arrow" type="Arrow" damage="1" maxStackSize="99" sprite="assets/items/arrow_crude.png"/> <!-- UTILITIES --> <!--<item name="Rusty Lantern" type="Light" radius="150" color="255|255|255" sprite="assets/items/rusty_lantern.png"/>--> diff --git a/include/inventory.hpp b/include/inventory.hpp index 1052b20..adf16f1 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -7,6 +7,7 @@ #include <GL/glew.h> #include <SDL2/SDL_opengl.h> +#include <SDL2/SDL_mixer.h> #include <string> #include <vector> @@ -28,6 +29,8 @@ struct Item { int value; /**< The value/worth of the item */ int stackSize; /**< The stack size of the item */ Texture sprite; /**< The texture for the item (in inventory) */ + Mix_Chunk* sound; /**< The sound to play on item use */ + int cooldown; Item(void) : value(0), stackSize(1) {} @@ -36,7 +39,7 @@ struct Item { * Constructs an item from XML. * @param the xml element (<item />) */ - Item(XMLElement *e) { + Item(XMLElement* e) { name = e->StrAttribute("name"); type = e->StrAttribute("type"); @@ -46,6 +49,19 @@ struct Item { e->QueryIntAttribute("maxStackSize", &stackSize); sprite = Texture(e->StrAttribute("sprite")); + + if (e->Attribute("sound") != nullptr) + sound = Mix_LoadWAV(e->Attribute("sound")); + else + sound = nullptr; + + cooldown = 250; + e->QueryIntAttribute("cooldown", &cooldown); + } + + ~Item(void) { + if (sound != nullptr) + Mix_FreeChunk(sound); } }; @@ -128,6 +144,10 @@ public: * @return true if the operation could be completed */ bool take(const std::string& name, int count); + + inline Item getItem(const std::string& s) { + return itemList[s]; + } }; #endif // INVENTORY_HPP_ diff --git a/include/vector2.hpp b/include/vector2.hpp index 5671ccd..beb2787 100644 --- a/include/vector2.hpp +++ b/include/vector2.hpp @@ -48,6 +48,11 @@ struct vector2 { return vector2<T>(x - n, y - n); } + vector2<T> operator-=(const vector2<T>& v) { + x -= v.x, y -= v.y; + return *this; + } + // multiplication vector2<T> operator*(const vector2<T>& v) const { return vector2<T>(x * v.x, y * v.y); @@ -57,6 +62,11 @@ struct vector2 { return vector2<T>(x * n, y * n); } + vector2<T> operator*=(const vector2<T>& v) { + x *= v.x, y *= v.y; + return *this; + } + vector2<T> operator*=(const T& n) { x *= n, y *= n; return *this; @@ -71,6 +81,11 @@ struct vector2 { return vector2<T>(x / n, y / n); } + vector2<T> operator/=(const vector2<T>& v) { + x /= v.x, y /= v.y; + return *this; + } + vector2<T> operator/=(const T& n) { x /= n, y /= n; return *this; @@ -117,7 +117,9 @@ int main(int argc, char *argv[]) ///////////////////////////// - game::engine.getSystem<InventorySystem>()->add("Wood Sword", 9); + game::engine.getSystem<InventorySystem>()->add("Wood Sword", 1); + game::engine.getSystem<InventorySystem>()->add("Hunters Bow", 1); + game::engine.getSystem<InventorySystem>()->add("Arrow", 40); std::list<SDL_Event> eventQueue; diff --git a/src/attack.cpp b/src/attack.cpp index fd171a0..4047b0a 100644 --- a/src/attack.cpp +++ b/src/attack.cpp @@ -34,6 +34,7 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, for (const auto& a : attacks) { switch (a.type) { case AttackType::ShortSlash: + case AttackType::LongSlash: en.each<Position, Solid, Health>( [&a](entityx::Entity e, Position& pos, Solid& dim, Health& h) { (void)e; @@ -48,8 +49,6 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, } ); break; - case AttackType::LongSlash: - break; default: break; } diff --git a/src/components.cpp b/src/components.cpp index af746ab..6571e2f 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -50,7 +50,7 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e // make the entity wander // TODO initialX and range? - if (entity.has_component<Aggro>()) { + if (0 && entity.has_component<Aggro>()) { auto ppos = game::engine.getSystem<PlayerSystem>()->getPosition(); if (ppos.x > position.x && ppos.x < position.x + entity.component<Solid>()->width) { auto& h = entity.component<Health>()->health; diff --git a/src/player.cpp b/src/player.cpp index 3d959ea..2fb7c44 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -211,10 +211,40 @@ vec2 PlayerSystem::getPosition(void) const void PlayerSystem::receive(const UseItemEvent& uie) { - if (uie.item->type == "Sword") { - auto loc = getPosition(); - auto &solid = *player.component<Solid>().get(); - loc.x += solid.width / 2, loc.y += solid.height / 2; - game::events.emit<AttackEvent>(loc, AttackType::ShortSlash); + static std::atomic_bool cool (true); + + if (cool.load()) { + if (uie.item->sound != nullptr) + Mix_PlayChannel(0, uie.item->sound, 0); + + if (uie.item->type == "Sword") { + auto loc = getPosition(); + auto &solid = *player.component<Solid>().get(); + loc.x += solid.width / 2, loc.y += solid.height / 2; + game::events.emit<AttackEvent>(loc, AttackType::ShortSlash); + } else if (uie.item->type == "Bow") { + if (game::engine.getSystem<InventorySystem>()->take("Arrow", 1)) { + auto e = game::entities.create(); + auto pos = getPosition(); + e.assign<Position>(pos.x, pos.y + 10); + + auto angle = std::atan2(uie.curs.y - pos.y, uie.curs.x - pos.x); + e.assign<Direction>(1 * std::cos(angle), 1 * std::sin(angle)); + + e.assign<Visible>(-5); + e.assign<Physics>(); + auto sprite = e.assign<Sprite>(); + auto tex = game::engine.getSystem<InventorySystem>()->getItem("Arrow"); + sprite->addSpriteSegment(SpriteData(tex.sprite), 0); + auto dim = HLINES(sprite->getSpriteSize()); + e.assign<Solid>(dim.x, dim.y); + } + } + + /*cool.store(false); + std::thread([&](void) { + std::this_thread::sleep_for(std::chrono::milliseconds(uie.item->cooldown)); + cool.store(true); + }).detach();*/ } } @@ -1096,7 +1096,7 @@ namespace ui { fadeWhite = true; fadeFast = true; - Mix_PlayChannel(1, battleStart, 0); + //Mix_PlayChannel(1, battleStart, 0); } void takeScreenshot(GLubyte* pixels) { |