Added secondary weapon.

This commit is contained in:
Felix Ableitner 2013-06-25 22:00:40 +02:00
parent ef0fa839b9
commit 60518e1eda
12 changed files with 61 additions and 111 deletions

View file

@ -5,5 +5,6 @@ health: 100
speed: 100 speed: 100
radius: 25 radius: 25
size: [50, 50] size: [50, 50]
weapon: assault_rifle.yaml first_weapon: assault_rifle.yaml
second_weapon: pistol.yaml
faction: 1 faction: 1

View file

@ -5,5 +5,6 @@ health: 100
speed: 100 speed: 100
radius: 25 radius: 25
size: [50, 50] size: [50, 50]
weapon: hmg.yaml first_weapon: hmg.yaml
second_weapon: shotgun.yaml
faction: 0 faction: 0

View file

@ -116,6 +116,9 @@ Game::input() {
mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x, mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x,
event.mouseMove.y)); event.mouseMove.y));
break; break;
case sf::Event::MouseWheelMoved:
mPlayer->toggleWeapon();
break;
default: default:
break; break;
} }
@ -172,6 +175,12 @@ Game::keyDown(const sf::Event& event) {
case sf::Keyboard::R: case sf::Keyboard::R:
mPlayer->reload(); mPlayer->reload();
break; break;
case sf::Keyboard::Num1:
mPlayer->selectFirstWeapon();
break;
case sf::Keyboard::Num2:
mPlayer->selectSecondWeapon();
break;
default: default:
break; break;
} }

View file

@ -31,8 +31,11 @@ Character::Character(const sf::Vector2f& position, Category category,
mMaxHealth(config.get("health", 100)), mMaxHealth(config.get("health", 100)),
mCurrentHealth(mMaxHealth), mCurrentHealth(mMaxHealth),
mMovementSpeed(config.get("speed", 0.0f)), mMovementSpeed(config.get("speed", 0.0f)),
mWeapon(new Weapon(world, *this, mFirstWeapon(new Weapon(world, *this,
Yaml(config.get("weapon", std::string())))), Yaml(config.get("first_weapon", std::string())))),
mSecondWeapon(new Weapon(world, *this,
Yaml(config.get("second_weapon", std::string())))),
mActiveWeapon(mFirstWeapon),
mLastPosition(getPosition()), mLastPosition(getPosition()),
mFaction((Faction) config.get("faction", 1)) { mFaction((Faction) config.get("faction", 1)) {
} }
@ -64,7 +67,7 @@ Character::onDamage(int damage) {
*/ */
void void
Character::onThink(int elapsed) { Character::onThink(int elapsed) {
mWeapon->onThink(elapsed); mActiveWeapon->onThink(elapsed);
move(); move();
} }
@ -98,7 +101,7 @@ Character::getMovementSpeed() const {
*/ */
void void
Character::pullTrigger() { Character::pullTrigger() {
mWeapon->pullTrigger(); mActiveWeapon->pullTrigger();
} }
/** /**
@ -106,7 +109,7 @@ Character::pullTrigger() {
*/ */
void void
Character::releaseTrigger() { Character::releaseTrigger() {
mWeapon->releaseTrigger(); mActiveWeapon->releaseTrigger();
} }
/** /**
@ -171,15 +174,35 @@ Character::getCharacters() const {
int int
Character::getMagazineAmmo() const { Character::getMagazineAmmo() const {
return mWeapon->getMagazineAmmo(); return mActiveWeapon->getMagazineAmmo();
} }
int int
Character::getTotalAmmo() const { Character::getTotalAmmo() const {
return mWeapon->getTotalAmmo(); return mActiveWeapon->getTotalAmmo();
} }
void void
Character::reload() { Character::reload() {
mWeapon->reload(); mActiveWeapon->reload();
}
void
Character::toggleWeapon() {
mActiveWeapon->cancelReload();
mActiveWeapon = (mActiveWeapon == mFirstWeapon)
? mSecondWeapon
: mFirstWeapon;
}
void
Character::selectFirstWeapon() {
mActiveWeapon->cancelReload();
mActiveWeapon = mFirstWeapon;
}
void
Character::selectSecondWeapon() {
mActiveWeapon->cancelReload();
mActiveWeapon = mSecondWeapon;
} }

View file

@ -50,6 +50,9 @@ protected:
int getMagazineAmmo() const; int getMagazineAmmo() const;
int getTotalAmmo() const; int getTotalAmmo() const;
void reload(); void reload();
void toggleWeapon();
void selectFirstWeapon();
void selectSecondWeapon();
private: private:
void move(); void move();
@ -65,7 +68,9 @@ private:
const int mMaxHealth; const int mMaxHealth;
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth. int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
const float mMovementSpeed; const float mMovementSpeed;
std::unique_ptr<Weapon> mWeapon; std::shared_ptr<Weapon> mFirstWeapon;
std::shared_ptr<Weapon> mSecondWeapon;
std::shared_ptr<Weapon> mActiveWeapon;
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination. std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
sf::Vector2f mLastPosition; sf::Vector2f mLastPosition;
Faction mFaction; Faction mFaction;

View file

@ -125,6 +125,13 @@ Weapon::reload() {
mTimer.restart(sf::milliseconds(mReloadTime)); mTimer.restart(sf::milliseconds(mReloadTime));
} }
void
Weapon::cancelReload() {
mIsReloading = false;
// To make sure time isn't skipped.
mTimer.restart(sf::milliseconds(mFireInterval));
}
/** /**
* Creates a new projectile and inserts it into the world. * Creates a new projectile and inserts it into the world.
* *

View file

@ -32,6 +32,7 @@ public:
int getMagazineAmmo() const; int getMagazineAmmo() const;
int getTotalAmmo() const; int getTotalAmmo() const;
void reload(); void reload();
void cancelReload();
private: private:
void fire(); void fire();

View file

@ -1,25 +0,0 @@
/*
* Emitter.cpp
*
* Created on: 15.08.2012
* Author: Felix
*/
#include "Emitter.h"
#include "../World.h"
Emitter::Emitter(World& world) :
mWorld(world) {
}
Emitter::~Emitter() {
}
/**
* Inserts a new particle into the system, using createParticle().
*/
void
Emitter::emit() {
mWorld.insert(createParticle());
}

View file

@ -1,31 +0,0 @@
/*
* Emitter.h
*
* Created on: 15.08.2012
* Author: Felix
*/
#ifndef DG_EMITTER_H_
#define DG_EMITTER_H_
#include <memory>
class World;
class Particle;
class Sprite;
class Emitter {
public:
explicit Emitter(World& world);
virtual ~Emitter();
protected:
void emit();
/// Creates a particle. Allows to use a user-defined particle class and custom settings.
virtual std::shared_ptr<Sprite> createParticle() = 0;
private:
World& mWorld;
};
#endif /* DG_EMITTER_H_ */

View file

@ -1,18 +0,0 @@
/*
* Particle.cpp
*
* Created on: 15.08.2012
* Author: Felix
*/
#include "Particle.h"
Particle::Particle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction) :
Circle(position, category, mask, config, direction) {
}
Particle::~Particle() {
}

View file

@ -1,26 +0,0 @@
/*
* Particle.h
*
* Created on: 15.08.2012
* Author: Felix
*/
#ifndef DG_PARTICLE_H_
#define DG_PARTICLE_H_
#include "../abstract/Circle.h"
class Yaml;
/**
* Prototype for a particle.
*/
class Particle : public Circle {
public:
explicit Particle(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config,
const sf::Vector2f& direction);
virtual ~Particle();
};
#endif /* DG_PARTICLE_H_ */

View file

@ -39,6 +39,9 @@ public:
using Character::getMagazineAmmo; using Character::getMagazineAmmo;
using Character::getTotalAmmo; using Character::getTotalAmmo;
using Character::reload; using Character::reload;
using Character::toggleWeapon;
using Character::selectFirstWeapon;
using Character::selectSecondWeapon;
private: private:
void onThink(int elapsed) override; void onThink(int elapsed) override;