Added direct movement via WASD keys.
This commit is contained in:
parent
a793cc8933
commit
98fb433dbf
3 changed files with 87 additions and 7 deletions
|
@ -132,6 +132,18 @@ Game::keyUp(const sf::Event& event) {
|
||||||
case sf::Keyboard::Space:
|
case sf::Keyboard::Space:
|
||||||
mPaused = !mPaused;
|
mPaused = !mPaused;
|
||||||
break;
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -143,6 +155,18 @@ Game::keyUp(const sf::Event& event) {
|
||||||
void
|
void
|
||||||
Game::keyDown(const sf::Event& event) {
|
Game::keyDown(const sf::Event& event) {
|
||||||
switch (event.key.code) {
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,9 @@ Player::Player(b2World& world, Collection& collection, const Vector2f& position)
|
||||||
CATEGORY_ACTOR, MASK_ALL, true)),
|
CATEGORY_ACTOR, MASK_ALL, true)),
|
||||||
Actor(100),
|
Actor(100),
|
||||||
mWeapon(*this, collection, world, SIZE),
|
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) {
|
Player::setCrosshairPosition(const Vector2f& position) {
|
||||||
mCrosshairPosition = position - getPosition();
|
mCrosshairPosition = position - getPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fire the attacked Weapon, emitting a Bullet object.
|
* Fires the attached Weapon, emitting a Bullet object.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Player::fire() {
|
Player::fire() {
|
||||||
|
@ -45,6 +48,7 @@ Player::fire() {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves the player to a destination point.
|
* Moves the player to a destination point.
|
||||||
|
* Disables any previous calls to Player::setDirection().
|
||||||
*
|
*
|
||||||
* @param destination Absolute world coordinate of the destination point.
|
* @param destination Absolute world coordinate of the destination point.
|
||||||
*/
|
*/
|
||||||
|
@ -52,13 +56,49 @@ void
|
||||||
Player::move(const Vector2f& destination) {
|
Player::move(const Vector2f& destination) {
|
||||||
mDestination = destination;
|
mDestination = destination;
|
||||||
// Convert to relative 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
|
void
|
||||||
Player::onThink(float elapsedTime) {
|
Player::onThink(float elapsedTime) {
|
||||||
// Stop if we are close enough.
|
// Stop if we are close enough to destination.
|
||||||
if (thor::length(mDestination - getPosition()) < 1.0f) {
|
if (!mDirectInput && (thor::length(mDestination - getPosition()) < 1.0f)) {
|
||||||
setSpeed(Vector2f(), 0);
|
setSpeed(Vector2f(), 0);
|
||||||
}
|
}
|
||||||
// Look towards crosshair.
|
// Look towards crosshair.
|
||||||
|
|
|
@ -22,6 +22,18 @@ class Sprite;
|
||||||
* Player object.
|
* Player object.
|
||||||
*/
|
*/
|
||||||
class Player : public Sprite, public Actor {
|
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 functions.
|
||||||
public:
|
public:
|
||||||
Player(b2World& world, Collection& collection, const Vector2f& position);
|
Player(b2World& world, Collection& collection, const Vector2f& position);
|
||||||
|
@ -29,9 +41,10 @@ public:
|
||||||
void setCrosshairPosition(const Vector2f& position);
|
void setCrosshairPosition(const Vector2f& position);
|
||||||
void fire();
|
void fire();
|
||||||
void move(const Vector2f& destination);
|
void move(const Vector2f& destination);
|
||||||
|
void setDirection(Direction direction, bool unset);
|
||||||
|
|
||||||
// Protected functions.
|
// Private functions.
|
||||||
protected:
|
private:
|
||||||
void onCollide(Physical& other, uint16 category);
|
void onCollide(Physical& other, uint16 category);
|
||||||
void onThink(float elapsedTime);
|
void onThink(float elapsedTime);
|
||||||
|
|
||||||
|
@ -43,6 +56,9 @@ private:
|
||||||
Weapon mWeapon; //< Weapon object used for Player::fire().
|
Weapon mWeapon; //< Weapon object used for Player::fire().
|
||||||
Vector2f mDestination; //< Absolute position of the movement destination.
|
Vector2f mDestination; //< Absolute position of the movement destination.
|
||||||
Vector2f mCrosshairPosition; //< Relative position of the point to fire at (mouse cursor).
|
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_ */
|
#endif /* DG_PLAYER_H_ */
|
||||||
|
|
Reference in a new issue