Added knife.

This commit is contained in:
Felix Ableitner 2013-07-06 13:22:55 +02:00
parent 60518e1eda
commit 203a34f0a0
9 changed files with 44 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B

View file

@ -0,0 +1,5 @@
name: Bullet
texture: transparent.png
radius: 5
size: [1, 1]

View file

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

11
resources/yaml/knife.yaml Normal file
View file

@ -0,0 +1,11 @@
name: Knife
bullet: bullet_invisible.yaml
projectile_speed: 1000
damage: 10
fire_interval: 2000
automatic: false
spread: 0
spread_moving: 0
max_range: 50
requires_no_ammo: true

View file

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

View file

@ -22,12 +22,14 @@
*/
Bullet::Bullet(const sf::Vector2f& position, Character& shooter,
sf::Vector2f direction, const Yaml& config, float speed,
float damage) :
float damage, float maxRange) :
Circle(position, CATEGORY_PARTICLE, ~CATEGORY_PARTICLE,
config, thor::rotatedVector(direction, -90.0f)),
mShooter(shooter),
mDamage(damage),
mSpeed(speed) {
mSpeed(speed),
mMaxRangeSquared((maxRange == 0) ? std::numeric_limits<float>::max() : maxRange * maxRange),
mStartPoint(getPosition()) {
setSpeed(thor::rotatedVector(direction, -90.0f), mSpeed);
}
@ -37,9 +39,13 @@ Bullet::Bullet(const sf::Vector2f& position, Character& shooter,
*/
void
Bullet::onCollide(std::shared_ptr<Sprite> other) {
if (thor::squaredLength(getPosition() - mStartPoint) >= mMaxRangeSquared) {
setDelete(true);
return;
}
// Make sure we do not damage twice.
if (!getDelete() && (&*other != &mShooter)) {
// Call onShot on other, with damage as param.
if (!getDelete() && (&*other != &mShooter)) {
if (other->getCategory() == CATEGORY_ACTOR) {
std::shared_ptr<Character> character = std::static_pointer_cast<Character>(other);
character->onDamage(mDamage);

View file

@ -20,14 +20,16 @@ class Bullet : public Circle {
public:
explicit Bullet(const sf::Vector2f& position, Character& shooter,
sf::Vector2f direction, const Yaml& config, float speed,
float damage);
float damage, float maxRange);
void onCollide(std::shared_ptr<Sprite> other);
private:
const Character& mShooter;
const int mDamage;
const float mSpeed;
const float mSpeed;
const float mMaxRangeSquared;
sf::Vector2f mStartPoint;
};
#endif /* DG_BULLET_H_ */

View file

@ -31,7 +31,9 @@ Weapon::Weapon(World& world, Character& holder, const Yaml& config) :
mPelletSpread(config.get("pellet_spread", 0.0f)),
mReloadSingle(config.get("reload_single", false)),
mSpread(config.get("spread", 0.0f)),
mSpreadMoving(config.get("spread_moving", 0.0f)) {
mSpreadMoving(config.get("spread_moving", 0.0f)),
mMaxRange(config.get("max_range", 0.0f)),
mRequiresAmmo(!config.get("requires_no_ammo", false)) {
}
/**
@ -80,13 +82,13 @@ Weapon::onThink(int elapsed) {
mIsReloading = false;
}
if (mFiring && mMagazineAmmo != 0) {
if (mFiring && (!mRequiresAmmo || mMagazineAmmo != 0)) {
fire();
if (!mAutomatic)
mFiring = false;
}
if (mMagazineAmmo == 0 && mTotalAmmo != 0)
if (mRequiresAmmo && mMagazineAmmo == 0 && mTotalAmmo != 0)
reload();
}
@ -96,7 +98,8 @@ Weapon::onThink(int elapsed) {
void
Weapon::fire() {
mTimer.restart(sf::milliseconds(mFireInterval));
mMagazineAmmo--;
if (mRequiresAmmo)
mMagazineAmmo--;
if (mPellets == 0)
@ -155,6 +158,6 @@ Weapon::insertProjectile(float angle) {
std::shared_ptr<Sprite> projectile(new Bullet(mHolder.getPosition() + offset,
mHolder, direction, mProjectile, mProjectileSpeed,
mDamage));
mDamage, mMaxRange));
mWorld.insert(projectile);
}

View file

@ -60,6 +60,8 @@ private:
const bool mReloadSingle;
const float mSpread;
const float mSpreadMoving;
const float mMaxRange;
const float mRequiresAmmo;
std::default_random_engine mGenerator;
};