remove walk updates from render loop to fix some issues and have better performance

This commit is contained in:
Eduardo Bart 2011-11-05 19:37:36 -02:00
parent 63652bd3a3
commit d2c3e554dc
2 changed files with 55 additions and 38 deletions

View File

@ -29,6 +29,7 @@
#include <framework/platform/platform.h> #include <framework/platform/platform.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <framework/core/eventdispatcher.h>
Creature::Creature() : Thing() Creature::Creature() : Thing()
@ -45,8 +46,10 @@ Creature::Creature() : Thing()
void Creature::draw(int x, int y) void Creature::draw(int x, int y)
{ {
if(m_walking) {
x += m_walkOffsetX; x += m_walkOffsetX;
y += m_walkOffsetY; y += m_walkOffsetY;
}
const ThingType& type = getType(); const ThingType& type = getType();
@ -86,42 +89,6 @@ void Creature::draw(int x, int y)
g_graphics.bindColor(Fw::white); g_graphics.bindColor(Fw::white);
} }
} }
// Update animation and position
if(m_walking && type.animationPhases > 1) {
int elapsedTicks = g_platform.getTicks() - m_walkStartTicks;
int totalPixelsWalked = std::min((int)std::round(elapsedTicks / m_walkTimePerPixel), 32);
if(m_inverseWalking) {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffsetY = 32 - totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = totalPixelsWalked - 32;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = totalPixelsWalked - 32;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffsetX = 32 - totalPixelsWalked;
if(m_walkOffsetX == 0 && m_walkOffsetY == 0)
cancelWalk(m_direction);
}
else {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffsetY = -totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = totalPixelsWalked;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = totalPixelsWalked;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffsetX = -totalPixelsWalked;
}
int totalWalkTileTicks = (int)m_walkTimePerPixel*32;
m_animation = (g_platform.getTicks() % totalWalkTileTicks) / (totalWalkTileTicks / (type.animationPhases - 1)) + 1;
} else
m_animation = 0;
} }
void Creature::drawInformation(int x, int y, bool useGray, const Rect& rect) void Creature::drawInformation(int x, int y, bool useGray, const Rect& rect)
@ -225,6 +192,9 @@ void Creature::walk(const Position& position, bool inverse)
else { // Teleport else { // Teleport
// we teleported, dont walk or change direction // we teleported, dont walk or change direction
m_walking = false; m_walking = false;
m_walkOffsetX = 0;
m_walkOffsetY = 0;
m_animation = 0;
} }
if(!m_inverseWalking) { if(!m_inverseWalking) {
@ -257,12 +227,57 @@ void Creature::walk(const Position& position, bool inverse)
m_walkTimePerPixel = walkTime / 32.0; m_walkTimePerPixel = walkTime / 32.0;
if(!sameWalk) if(!sameWalk)
m_walkStartTicks = g_platform.getTicks(); m_walkStartTicks = g_platform.getTicks();
g_dispatcher.scheduleEvent(std::bind(&Creature::updateWalk, asCreature()), m_walkTimePerPixel);
}
}
void Creature::updateWalk()
{
const ThingType& type = getType();
if(m_walking) {
int elapsedTicks = g_platform.getTicks() - m_walkStartTicks;
int totalPixelsWalked = std::min((int)std::round(elapsedTicks / m_walkTimePerPixel), 32);
if(m_inverseWalking) {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffsetY = 32 - totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = totalPixelsWalked - 32;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = totalPixelsWalked - 32;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffsetX = 32 - totalPixelsWalked;
if(m_walkOffsetX == 0 && m_walkOffsetY == 0)
cancelWalk(m_direction);
}
else {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
m_walkOffsetY = -totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = totalPixelsWalked;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = totalPixelsWalked;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffsetX = -totalPixelsWalked;
}
int totalWalkTileTicks = (int)m_walkTimePerPixel*32;
m_animation = (g_platform.getTicks() % totalWalkTileTicks) / (totalWalkTileTicks / (type.animationPhases - 1)) + 1;
g_dispatcher.scheduleEvent(std::bind(&Creature::updateWalk, asCreature()), m_walkTimePerPixel);
if(totalPixelsWalked == 32)
cancelWalk(m_direction);
} }
} }
void Creature::cancelWalk(Otc::Direction direction) void Creature::cancelWalk(Otc::Direction direction)
{ {
m_walking = false; m_walking = false;
m_walkStartTicks = 0;
m_walkOffsetX = 0; m_walkOffsetX = 0;
m_walkOffsetY = 0; m_walkOffsetY = 0;
m_animation = 0; m_animation = 0;

View File

@ -80,6 +80,8 @@ public:
CreaturePtr asCreature() { return std::static_pointer_cast<Creature>(shared_from_this()); } CreaturePtr asCreature() { return std::static_pointer_cast<Creature>(shared_from_this()); }
protected: protected:
void updateWalk();
std::string m_name; std::string m_name;
uint8 m_healthPercent; uint8 m_healthPercent;
Otc::Direction m_direction; Otc::Direction m_direction;