From 98fb433dbf32f96d9f4cc4af7ccf98570f428661 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 12 Sep 2012 20:10:29 +0200 Subject: [PATCH] Added direct movement via WASD keys. --- source/Game.cpp | 24 +++++++++++++++++++ source/sprite/Player.cpp | 50 ++++++++++++++++++++++++++++++++++++---- source/sprite/Player.h | 20 ++++++++++++++-- 3 files changed, 87 insertions(+), 7 deletions(-) diff --git a/source/Game.cpp b/source/Game.cpp index 930de45..f97de06 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -132,6 +132,18 @@ Game::keyUp(const sf::Event& event) { case sf::Keyboard::Space: mPaused = !mPaused; break; + case sf::Keyboard::W: + mPlayer.setDirection(Player::DIRECTION_UP, true); + break; + case sf::Keyboard::S: + mPlayer.setDirection(Player::DIRECTION_DOWN, true); + break; + case sf::Keyboard::A: + mPlayer.setDirection(Player::DIRECTION_LEFT, true); + break; + case sf::Keyboard::D: + mPlayer.setDirection(Player::DIRECTION_RIGHT, true); + break; default: break; } @@ -143,6 +155,18 @@ Game::keyUp(const sf::Event& event) { void Game::keyDown(const sf::Event& event) { switch (event.key.code) { + case sf::Keyboard::W: + mPlayer.setDirection(Player::DIRECTION_UP, false); + break; + case sf::Keyboard::S: + mPlayer.setDirection(Player::DIRECTION_DOWN, false); + break; + case sf::Keyboard::A: + mPlayer.setDirection(Player::DIRECTION_LEFT, false); + break; + case sf::Keyboard::D: + mPlayer.setDirection(Player::DIRECTION_RIGHT, false); + break; default: break; } diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index b668b43..22203d2 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -23,7 +23,9 @@ Player::Player(b2World& world, Collection& collection, const Vector2f& position) CATEGORY_ACTOR, MASK_ALL, true)), Actor(100), mWeapon(*this, collection, world, SIZE), - mDestination(Vector2i(50, 50)) { + mDestination(Vector2i(position)), + mDirection(0), + mDirectInput(false) { } /** @@ -35,8 +37,9 @@ void Player::setCrosshairPosition(const Vector2f& position) { mCrosshairPosition = position - getPosition(); } + /** - * Fire the attacked Weapon, emitting a Bullet object. + * Fires the attached Weapon, emitting a Bullet object. */ void Player::fire() { @@ -45,6 +48,7 @@ Player::fire() { /** * Moves the player to a destination point. + * Disables any previous calls to Player::setDirection(). * * @param destination Absolute world coordinate of the destination point. */ @@ -52,13 +56,49 @@ void Player::move(const Vector2f& destination) { mDestination = destination; // Convert to relative destination. - setSpeed(mDestination - getPosition(), SPEED); + setSpeed(mDestination - getPosition(), SPEED); + mDirectInput = false; } +/** + * Sets the movement direction. This is destined for input via keys (eg. WASD). + * Disables any previous commands via Player::move(). + * + * @param direction The direction to move to. + * @param unset False to start movement into the direction, true to stop it. + */ +void +Player::setDirection(Direction direction, bool unset) { + if (unset) { + mDirection = mDirection & ~direction; + } else { + mDirection = mDirection | direction; + } + // Convert directions into a vector. + Vector2f dirVec(0, 0); + if (mDirection & DIRECTION_RIGHT) { + dirVec.x += 1.0f; + } + if (mDirection & DIRECTION_LEFT) { + dirVec.x += - 1.0f; + } + if (mDirection & DIRECTION_DOWN) { + dirVec.y += 1.0f; + } + if (mDirection & DIRECTION_UP) { + dirVec.y += - 1.0f; + } + setSpeed(dirVec, SPEED); + mDirectInput = true; +} + +/** + * Check if we arrived at destination, turn towards cursor. + */ void Player::onThink(float elapsedTime) { - // Stop if we are close enough. - if (thor::length(mDestination - getPosition()) < 1.0f) { + // Stop if we are close enough to destination. + if (!mDirectInput && (thor::length(mDestination - getPosition()) < 1.0f)) { setSpeed(Vector2f(), 0); } // Look towards crosshair. diff --git a/source/sprite/Player.h b/source/sprite/Player.h index bb37051..0a08c6c 100644 --- a/source/sprite/Player.h +++ b/source/sprite/Player.h @@ -22,6 +22,18 @@ class Sprite; * Player object. */ class Player : public Sprite, public Actor { +// Public types. +public: + /** + * Movement directions that can be set via Player::setDirection(). + */ + enum Direction { + DIRECTION_RIGHT = 1 << 0, + DIRECTION_LEFT = 1 << 1, + DIRECTION_UP = 1 << 2, + DIRECTION_DOWN = 1 << 3 + }; + // Public functions. public: Player(b2World& world, Collection& collection, const Vector2f& position); @@ -29,9 +41,10 @@ public: void setCrosshairPosition(const Vector2f& position); void fire(); void move(const Vector2f& destination); + void setDirection(Direction direction, bool unset); -// Protected functions. -protected: +// Private functions. +private: void onCollide(Physical& other, uint16 category); void onThink(float elapsedTime); @@ -43,6 +56,9 @@ private: Weapon mWeapon; //< Weapon object used for Player::fire(). Vector2f mDestination; //< Absolute position of the movement destination. Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor). + uint8 mDirection; //< Current movement direction for direct control. + bool mDirectInput; //< True when using keyboard input (directions), false when using + // destination point. }; #endif /* DG_PLAYER_H_ */