diff --git a/source/Game.cpp b/source/Game.cpp index 50e60ea..798dc8b 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -104,6 +104,9 @@ Game::input() { case sf::Event::KeyReleased: keyUp(event); break; + case sf::Event::MouseButtonPressed: + mouseDown(event); + break; case sf::Event::MouseButtonReleased: mouseUp(event); break; @@ -176,6 +179,16 @@ Game::convertCoordinates(int x, int y) { return mWindow.convertCoords(sf::Vector2i(x, y), mView); } +void +Game::mouseDown(const sf::Event& event) { + switch(event.mouseButton.button) { + case sf::Mouse::Left: + mPlayer->pullTrigger(); + break; + default: + break; + } +} /** * Handles mouse key up events. */ @@ -183,7 +196,7 @@ void Game::mouseUp(const sf::Event& event) { switch (event.mouseButton.button) { case sf::Mouse::Left: - mPlayer->fire(); + mPlayer->releaseTrigger(); break; case sf::Mouse::Right: mPlayer->move(convertCoordinates(event.mouseButton.x, event.mouseButton.y)); diff --git a/source/Game.h b/source/Game.h index 2cc3d47..f399de7 100644 --- a/source/Game.h +++ b/source/Game.h @@ -41,6 +41,7 @@ private: void keyDown(const sf::Event& event); void keyUp(const sf::Event& event); + void mouseDown(const sf::Event& event); void mouseUp(const sf::Event& event); void generate(); diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index a74340c..85c41e2 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -59,6 +59,7 @@ Character::~Character() { void Character::think(float elapsedTime) { for (auto i : mCharacterInstances) { + i->mWeapon.think(); i->onThink(elapsedTime); } } @@ -104,11 +105,19 @@ Character::getMovementSpeed() const { } /** - * Fire the attached weapon. + * Pull the trigger on the attached weapon. */ void -Character::fire() { - mWeapon.fire(); +Character::pullTrigger() { + mWeapon.pullTrigger(); +} + +/** + * Release the trigger on the attached weapon. + */ +void +Character::releaseTrigger() { + mWeapon.releaseTrigger(); } /** diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 0cb2671..d53197b 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -43,7 +43,8 @@ protected: virtual void onThink(float elapsedTime); virtual void onDeath(); float getMovementSpeed() const; - void fire(); + void pullTrigger(); + void releaseTrigger(); bool setDestination(const sf::Vector2f& destination); void move(); diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index e02ddb1..f2bf499 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -18,13 +18,17 @@ const std::string Weapon::KEY_BULLET = "bullet"; const std::string Weapon::DEFAULT_BULLET = "bullet.yaml"; const std::string Weapon::KEY_INTERVAL = "interval"; const int Weapon::DEFAULT_INTERVAL = 250; +const std::string Weapon::KEY_AUTOMATIC = "automatic"; +const bool Weapon::DEFAULT_AUTOMATIC = false; Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) : Emitter(world), mWorld(world), mHolder(holder), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), - mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) { + mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))), + mFire(false), + mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) { sf::Vector2f holderSize = mHolder.getSize(); Yaml bullet(mBullet); sf::Vector2f bulletSize = bullet.get(Sprite::KEY_SIZE, sf::Vector2f()); @@ -37,11 +41,29 @@ Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) : * Pull the trigger. */ void -Weapon::fire() { - // Only call if has ammo, consider firing rate etc. - if (mTimer.isExpired()) { +Weapon::pullTrigger() { + mFire = true; +} + +/** + * Release the trigger. + */ +void +Weapon::releaseTrigger() { + mFire = false; +} + +/** + * Fire if the trigger has been pulled, time between bullets is over, has ammo etc. + */ +void +Weapon::think() { + if (mFire && mTimer.isExpired()) { emit(); mTimer.start(); + if (!mAutomatic) { + mFire = false; + } } } diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 0b4375b..b5cd9c0 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -31,7 +31,9 @@ class Weapon : public Emitter { public: Weapon(World& world, Sprite& holder, const Yaml& config); - void fire(); + void pullTrigger(); + void releaseTrigger(); + void think(); // Protected functions. protected: @@ -43,6 +45,8 @@ private: static const std::string DEFAULT_BULLET; static const std::string KEY_INTERVAL; static const int DEFAULT_INTERVAL; + static const std::string KEY_AUTOMATIC; + static const bool DEFAULT_AUTOMATIC; World& mWorld; Sprite& mHolder; @@ -50,6 +54,8 @@ private: sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). const std::string mBullet; Timer mTimer; + bool mFire; + bool mAutomatic; }; diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index 0a50113..e2044aa 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -35,11 +35,19 @@ Player::setCrosshairPosition(const sf::Vector2f& position) { } /** - * Fires the attached Weapon, emitting a Bullet object. + * Pull the trigger on the attached weapon. */ void -Player::fire() { - Character::fire(); +Player::pullTrigger() { + Character::pullTrigger(); +} + +/** + * Release the trigger on the attached weapon. + */ +void +Player::releaseTrigger() { + Character::releaseTrigger(); } /** diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 5344d25..1a2527c 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -44,7 +44,8 @@ public: const sf::Vector2f& position, const Yaml& config); void setCrosshairPosition(const sf::Vector2f& position); - void fire(); + void pullTrigger(); + void releaseTrigger(); void move(const sf::Vector2f& destination); void setDirection(Direction direction, bool unset);