]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
bow & arrow draft
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 18 Mar 2017 02:17:30 +0000 (22:17 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 18 Mar 2017 02:17:30 +0000 (22:17 -0400)
config/items.xml
include/inventory.hpp
include/vector2.hpp
main.cpp
src/attack.cpp
src/components.cpp
src/player.cpp
src/ui.cpp

index 6767bc0cbac32f633672a6c8da27f4d913a18f85..3601c18b35ecf5f162cd173ad343ffa56b54cbd4 100644 (file)
@@ -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"/>-->
index 1052b206337daf2b718638cc89e6906a1f031479..adf16f1eb3f6dc58af6859b68b7a65a872a30b3d 100644 (file)
@@ -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(XMLElemente) {
                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_
index 5671ccd71a28166c739fa724410d997698f8b25a..beb278707fee81d4fa3ed9c8c6123e3cf6b45a48 100644 (file)
@@ -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;
index 3d82c44c8905855cf932c5344230b97b5be02b58..a46af338a42811cb7e0d3fd9604ab68ee5c55c2e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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;
 
index fd171a0e55d8249c439dcab3d8d0ee2c3188b4f1..4047b0a19b23d8ded0a49ca93de56440d822295b 100644 (file)
@@ -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;
                }
index af746abc0f7c6034c15d1c819ee1d0f148ab41b7..6571e2fc98b6ad9fe5a18d1617b95d3b8e995ab8 100644 (file)
@@ -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;
index 3d959ea16f57dc10aa3cef396aefa3ae4a45847a..2fb7c447af1423fa6966a68201da78a1c8ca9128 100644 (file)
@@ -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();*/
        }
 }
index 58a6b97d7cb837813c40a967ebbde0ab08cd6574..00e43a2ab31d4afa44e4ce85131b18343606960b 100644 (file)
@@ -1096,7 +1096,7 @@ namespace ui {
                fadeWhite   = true;
                fadeFast    = true;
 
-               Mix_PlayChannel(1, battleStart, 0);
+               //Mix_PlayChannel(1, battleStart, 0);
        }
 
     void takeScreenshot(GLubyte* pixels) {