Improved onThink function structure.
This commit is contained in:
parent
3d74314d22
commit
394ebc04c7
8 changed files with 35 additions and 39 deletions
|
@ -52,13 +52,12 @@ Character::~Character() {
|
||||||
/**
|
/**
|
||||||
* Calls onThink on all Actor instances.
|
* Calls onThink on all Actor instances.
|
||||||
*
|
*
|
||||||
* @param elapsedTime Amount of time to simulate.
|
* @param elapsed Amount of time to simulate.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::think(float elapsedTime) {
|
Character::think(int elapsed) {
|
||||||
for (auto i : mCharacterInstances) {
|
for (auto i : mCharacterInstances) {
|
||||||
i->mWeapon.think();
|
i->onThink(elapsed);
|
||||||
i->onThink(elapsedTime);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
void
|
||||||
Character::onThink(float elapsedTime) {
|
Character::onThink(int elapsed) {
|
||||||
|
mWeapon.onThink(elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -34,13 +34,13 @@ public:
|
||||||
const Data& data, const Yaml& config);
|
const Data& data, const Yaml& config);
|
||||||
virtual ~Character() = 0;
|
virtual ~Character() = 0;
|
||||||
|
|
||||||
static void think(float elapsedTime);
|
static void think(int elapsed);
|
||||||
|
|
||||||
void onDamage(int damage);
|
void onDamage(int damage);
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
protected:
|
protected:
|
||||||
virtual void onThink(float elapsedTime);
|
virtual void onThink(int elapsed);
|
||||||
virtual void onDeath();
|
virtual void onDeath();
|
||||||
float getMovementSpeed() const;
|
float getMovementSpeed() const;
|
||||||
void pullTrigger();
|
void pullTrigger();
|
||||||
|
|
|
@ -26,7 +26,8 @@ Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
|
||||||
mWorld(world),
|
mWorld(world),
|
||||||
mHolder(holder),
|
mHolder(holder),
|
||||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
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),
|
mFire(false),
|
||||||
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
|
mAutomatic(config.get(KEY_AUTOMATIC, DEFAULT_AUTOMATIC)) {
|
||||||
sf::Vector2f holderSize = mHolder.getSize();
|
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.
|
* Fire if the trigger has been pulled, time between bullets is over, has ammo etc.
|
||||||
|
*
|
||||||
|
* @param elapsed Amount of time to simulate.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Weapon::think() {
|
Weapon::onThink(int elapsed) {
|
||||||
if (mFire && mTimer.isExpired()) {
|
// 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();
|
emit();
|
||||||
mTimer.start();
|
|
||||||
if (!mAutomatic) {
|
if (!mAutomatic) {
|
||||||
mFire = false;
|
mFire = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ public:
|
||||||
|
|
||||||
void pullTrigger();
|
void pullTrigger();
|
||||||
void releaseTrigger();
|
void releaseTrigger();
|
||||||
void think();
|
void onThink(int elapsed);
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
protected:
|
protected:
|
||||||
|
@ -52,10 +52,11 @@ private:
|
||||||
Sprite& mHolder;
|
Sprite& mHolder;
|
||||||
|
|
||||||
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
||||||
const std::string mBullet;
|
const std::string mBullet; //< Bullet config filename.
|
||||||
Timer mTimer;
|
int mLastShotWaitInterval; //< Remaining time left after firing last bullet before firing next one.
|
||||||
bool mFire;
|
const int mFireInterval; //< Time between firing bullets.
|
||||||
bool mAutomatic;
|
bool mFire; //< True if the trigger is pulled.
|
||||||
|
bool mAutomatic; //< True if the weapon continues firing after pulling the trigger once.
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,3 @@ Enemy::Enemy(World& collection, Pathfinder& pathfinder,
|
||||||
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||||
config) {
|
config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
Enemy::onThink(float elapsedTime) {
|
|
||||||
}
|
|
||||||
|
|
|
@ -24,10 +24,6 @@ class Enemy : public Character {
|
||||||
public:
|
public:
|
||||||
Enemy(World& collection, Pathfinder& pathfinder,
|
Enemy(World& collection, Pathfinder& pathfinder,
|
||||||
const sf::Vector2f& position, const Yaml& config);
|
const sf::Vector2f& position, const Yaml& config);
|
||||||
|
|
||||||
// Private functions.
|
|
||||||
private:
|
|
||||||
void onThink(float elapsedTime);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* DG_ENEMY_H_ */
|
#endif /* DG_ENEMY_H_ */
|
||||||
|
|
|
@ -96,7 +96,8 @@ Player::setDirection(Direction direction, bool unset) {
|
||||||
* Check if we arrived at destination, turn towards cursor.
|
* Check if we arrived at destination, turn towards cursor.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Player::onThink(float elapsedTime) {
|
Player::onThink(int elapsed) {
|
||||||
|
Character::onThink(elapsed);
|
||||||
if (!mDirection) {
|
if (!mDirection) {
|
||||||
// Only use path finding movement if no direct input movement active.
|
// Only use path finding movement if no direct input movement active.
|
||||||
Character::move();
|
Character::move();
|
||||||
|
@ -106,13 +107,3 @@ Player::onThink(float elapsedTime) {
|
||||||
setAngle(thor::polarAngle(mCrosshairPosition) + 90);
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -51,8 +51,7 @@ public:
|
||||||
|
|
||||||
// Private functions.
|
// Private functions.
|
||||||
private:
|
private:
|
||||||
void onCollide(Sprite& other);
|
void onThink(int elapsed);
|
||||||
void onThink(float elapsedTime);
|
|
||||||
|
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
|
|
Reference in a new issue