Added automatic weapons.

This commit is contained in:
Felix Ableitner 2012-12-24 01:14:22 +01:00
parent 5433e14f51
commit f0a9c3320d
8 changed files with 75 additions and 14 deletions

View file

@ -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));

View file

@ -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();

View file

@ -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();
}
/**

View file

@ -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();

View file

@ -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;
}
}
}

View file

@ -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;
};

View file

@ -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();
}
/**

View file

@ -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);