Improved onThink function structure.

This commit is contained in:
Felix Ableitner 2013-03-09 16:25:04 +01:00
parent 3d74314d22
commit 394ebc04c7
8 changed files with 35 additions and 39 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -15,7 +15,3 @@ Enemy::Enemy(World& collection, Pathfinder& pathfinder,
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
config) {
}
void
Enemy::onThink(float elapsedTime) {
}

View file

@ -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_ */

View file

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

View file

@ -51,8 +51,7 @@ public:
// Private functions.
private:
void onCollide(Sprite& other);
void onThink(float elapsedTime);
void onThink(int elapsed);
// Private variables.
private: