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 speed: 100
radius: 25 radius: 25
size: [50, 50] size: [50, 50]
first_weapon: assault_rifle.yaml first_weapon: pistol.yaml
second_weapon: pistol.yaml second_weapon: knife.yaml
faction: 1 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 speed: 100
radius: 25 radius: 25
size: [50, 50] size: [50, 50]
first_weapon: hmg.yaml first_weapon: pistol.yaml
second_weapon: shotgun.yaml second_weapon: knife.yaml
faction: 0 faction: 0

View file

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

View file

@ -20,7 +20,7 @@ class Bullet : public Circle {
public: public:
explicit Bullet(const sf::Vector2f& position, Character& shooter, explicit Bullet(const sf::Vector2f& position, Character& shooter,
sf::Vector2f direction, const Yaml& config, float speed, sf::Vector2f direction, const Yaml& config, float speed,
float damage); float damage, float maxRange);
void onCollide(std::shared_ptr<Sprite> other); void onCollide(std::shared_ptr<Sprite> other);
@ -28,6 +28,8 @@ private:
const Character& mShooter; const Character& mShooter;
const int mDamage; const int mDamage;
const float mSpeed; const float mSpeed;
const float mMaxRangeSquared;
sf::Vector2f mStartPoint;
}; };
#endif /* DG_BULLET_H_ */ #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)), mPelletSpread(config.get("pellet_spread", 0.0f)),
mReloadSingle(config.get("reload_single", false)), mReloadSingle(config.get("reload_single", false)),
mSpread(config.get("spread", 0.0f)), 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; mIsReloading = false;
} }
if (mFiring && mMagazineAmmo != 0) { if (mFiring && (!mRequiresAmmo || mMagazineAmmo != 0)) {
fire(); fire();
if (!mAutomatic) if (!mAutomatic)
mFiring = false; mFiring = false;
} }
if (mMagazineAmmo == 0 && mTotalAmmo != 0) if (mRequiresAmmo && mMagazineAmmo == 0 && mTotalAmmo != 0)
reload(); reload();
} }
@ -96,7 +98,8 @@ Weapon::onThink(int elapsed) {
void void
Weapon::fire() { Weapon::fire() {
mTimer.restart(sf::milliseconds(mFireInterval)); mTimer.restart(sf::milliseconds(mFireInterval));
mMagazineAmmo--; if (mRequiresAmmo)
mMagazineAmmo--;
if (mPellets == 0) if (mPellets == 0)
@ -155,6 +158,6 @@ Weapon::insertProjectile(float angle) {
std::shared_ptr<Sprite> projectile(new Bullet(mHolder.getPosition() + offset, std::shared_ptr<Sprite> projectile(new Bullet(mHolder.getPosition() + offset,
mHolder, direction, mProjectile, mProjectileSpeed, mHolder, direction, mProjectile, mProjectileSpeed,
mDamage)); mDamage, mMaxRange));
mWorld.insert(projectile); mWorld.insert(projectile);
} }

View file

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