From 394ebc04c75dde30a2603ba8b198ae1f3471cad6 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 9 Mar 2013 16:25:04 +0100 Subject: [PATCH] Improved onThink function structure. --- source/abstract/Character.cpp | 15 ++++++++------- source/abstract/Character.h | 4 ++-- source/items/Weapon.cpp | 20 ++++++++++++++++---- source/items/Weapon.h | 11 ++++++----- source/sprites/Enemy.cpp | 4 ---- source/sprites/Enemy.h | 4 ---- source/sprites/Player.cpp | 13 ++----------- source/sprites/Player.h | 3 +-- 8 files changed, 35 insertions(+), 39 deletions(-) diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index f9f6b69..fd8d626 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -52,13 +52,12 @@ Character::~Character() { /** * Calls onThink on all Actor instances. * - * @param elapsedTime Amount of time to simulate. + * @param elapsed Amount of time to simulate. */ void -Character::think(float elapsedTime) { +Character::think(int elapsed) { for (auto i : mCharacterInstances) { - i->mWeapon.think(); - i->onThink(elapsedTime); + i->onThink(elapsed); } } @@ -79,12 +78,14 @@ Character::onDamage(int damage) { } /** - * Implement this function for any (regular) AI computations. Default implementation does nothing. + * Implement this function for any (regular) AI computations. + * If overwritten, this function should always be called from the overwriting function. * - * @param elapsedTime Amount of time to simulate. + * @param elapsed Amount of time to simulate. */ void -Character::onThink(float elapsedTime) { +Character::onThink(int elapsed) { + mWeapon.onThink(elapsed); } /** diff --git a/source/abstract/Character.h b/source/abstract/Character.h index d53197b..c8c44e8 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -34,13 +34,13 @@ public: const Data& data, const Yaml& config); virtual ~Character() = 0; - static void think(float elapsedTime); + static void think(int elapsed); void onDamage(int damage); // Protected functions. protected: - virtual void onThink(float elapsedTime); + virtual void onThink(int elapsed); virtual void onDeath(); float getMovementSpeed() const; void pullTrigger(); diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index f2bf499..e7a4fca 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -26,7 +26,8 @@ Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) : mWorld(world), mHolder(holder), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), - mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))), + mLastShotWaitInterval(0), + mFireInterval(config.get(KEY_INTERVAL, DEFAULT_INTERVAL)), mFire(false), mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) { sf::Vector2f holderSize = mHolder.getSize(); @@ -55,12 +56,23 @@ Weapon::releaseTrigger() { /** * Fire if the trigger has been pulled, time between bullets is over, has ammo etc. + * + * @param elapsed Amount of time to simulate. */ void -Weapon::think() { - if (mFire && mTimer.isExpired()) { +Weapon::onThink(int elapsed) { + // Waiting for next shot, subtract time since last onThink. + if (mLastShotWaitInterval > 0) { + mLastShotWaitInterval -= elapsed; + } + // Only reset to zero if we didn't recently fire (allow catching up for missed bullets). + else { + mLastShotWaitInterval = 0; + } + // Loop just in case we miss a bullet to fire. + while (mFire && mLastShotWaitInterval <= 0) { + mLastShotWaitInterval += mFireInterval; emit(); - mTimer.start(); if (!mAutomatic) { mFire = false; } diff --git a/source/items/Weapon.h b/source/items/Weapon.h index b5cd9c0..9bbd3ec 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -33,7 +33,7 @@ public: void pullTrigger(); void releaseTrigger(); - void think(); + void onThink(int elapsed); // Protected functions. protected: @@ -52,10 +52,11 @@ private: Sprite& mHolder; sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center). - const std::string mBullet; - Timer mTimer; - bool mFire; - bool mAutomatic; + const std::string mBullet; //< Bullet config filename. + int mLastShotWaitInterval; //< Remaining time left after firing last bullet before firing next one. + const int mFireInterval; //< Time between firing bullets. + bool mFire; //< True if the trigger is pulled. + bool mAutomatic; //< True if the weapon continues firing after pulling the trigger once. }; diff --git a/source/sprites/Enemy.cpp b/source/sprites/Enemy.cpp index f7615c2..f5e7316 100644 --- a/source/sprites/Enemy.cpp +++ b/source/sprites/Enemy.cpp @@ -15,7 +15,3 @@ Enemy::Enemy(World& collection, Pathfinder& pathfinder, Data(position, 0, CATEGORY_ACTOR, MASK_ALL), config) { } - -void -Enemy::onThink(float elapsedTime) { -} diff --git a/source/sprites/Enemy.h b/source/sprites/Enemy.h index 1a85bb8..b880b68 100644 --- a/source/sprites/Enemy.h +++ b/source/sprites/Enemy.h @@ -24,10 +24,6 @@ class Enemy : public Character { public: Enemy(World& collection, Pathfinder& pathfinder, const sf::Vector2f& position, const Yaml& config); - -// Private functions. -private: - void onThink(float elapsedTime); }; #endif /* DG_ENEMY_H_ */ diff --git a/source/sprites/Player.cpp b/source/sprites/Player.cpp index e2044aa..1c22e27 100644 --- a/source/sprites/Player.cpp +++ b/source/sprites/Player.cpp @@ -96,7 +96,8 @@ Player::setDirection(Direction direction, bool unset) { * Check if we arrived at destination, turn towards cursor. */ void -Player::onThink(float elapsedTime) { +Player::onThink(int elapsed) { + Character::onThink(elapsed); if (!mDirection) { // Only use path finding movement if no direct input movement active. Character::move(); @@ -106,13 +107,3 @@ Player::onThink(float elapsedTime) { setAngle(thor::polarAngle(mCrosshairPosition) + 90); } } - -/** - * Stop movement if we collide with anything except bullets. - */ -void -Player::onCollide(Sprite& other) { - if (other.getCategory() != CATEGORY_PARTICLE) { - setDestination(getPosition()); - } -} diff --git a/source/sprites/Player.h b/source/sprites/Player.h index 1a2527c..96f7892 100644 --- a/source/sprites/Player.h +++ b/source/sprites/Player.h @@ -51,8 +51,7 @@ public: // Private functions. private: - void onCollide(Sprite& other); - void onThink(float elapsedTime); + void onThink(int elapsed); // Private variables. private: