change effects animation calculation

This commit is contained in:
Eduardo Bart 2011-11-07 15:20:13 -02:00
parent 5b1b170165
commit ce67fd1733
11 changed files with 47 additions and 38 deletions

View File

@ -18,9 +18,12 @@ function scheduleEvent(func, delay)
return id return id
end end
-- FIXME: the event function can be collected
-- and the dispatcher would call an invalid function, generating an warning
function removeEvent(id) function removeEvent(id)
if id and eventsTable[id] then if id and eventsTable[id] then
eventsTable[id] = nil eventsTable[id] = nil
return true
end end
end end

View File

@ -2,8 +2,7 @@ CenterLabel < Label
font: verdana-11px-rounded font: verdana-11px-rounded
height: 16 height: 16
align: center align: center
anchors.top: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.bottom: parent.verticalCenter
anchors.left: parent.left anchors.left: parent.left
anchors.right: parent.right anchors.right: parent.right

View File

@ -663,6 +663,7 @@ std::string LuaInterface::functionSourcePath()
// gets function source path // gets function source path
lua_Debug ar; lua_Debug ar;
memset(&ar, 0, sizeof(ar));
lua_getinfo(L, ">Sn", &ar); lua_getinfo(L, ">Sn", &ar);
if(ar.source) { if(ar.source) {
// scripts coming from files has source beginning with '@' // scripts coming from files has source beginning with '@'

View File

@ -29,31 +29,30 @@
Effect::Effect() : Thing() Effect::Effect() : Thing()
{ {
m_lastTicks = g_platform.getTicks(); m_animationStartTicks = 0;
m_finished = false;
} }
void Effect::draw(int x, int y) void Effect::draw(int x, int y)
{ {
if(!m_finished) { int animationPhase = (g_platform.getTicks() - m_animationStartTicks) / TICKS_PER_FRAME;
if(g_platform.getTicks() - m_lastTicks > 75) {
const ThingType& type = getType();
if(m_animation+1 == type.animationPhases) {
EffectPtr self = asEffect();
g_dispatcher.addEvent([self] {
g_map.getTile(self->getPosition())->removeEffect(self);
});
m_finished = true;
}
else
m_animation++;
m_lastTicks = g_platform.getTicks();
}
if(animationPhase < getAnimationPhases()) {
m_animation = animationPhase;
internalDraw(x, y, 0); internalDraw(x, y, 0);
} }
} }
void Effect::startAnimation()
{
m_animationStartTicks = g_platform.getTicks();
// schedule removal
auto self = asEffect();
g_dispatcher.scheduleEvent([self]() {
g_map.getTile(self->getPosition())->removeEffect(self);
}, TICKS_PER_FRAME * getAnimationPhases());
}
const ThingType& Effect::getType() const ThingType& Effect::getType()
{ {
return g_thingsType.getEffectType(m_id); return g_thingsType.getEffectType(m_id);

View File

@ -28,20 +28,24 @@
class Effect : public Thing class Effect : public Thing
{ {
enum {
TICKS_PER_FRAME = 75
};
public: public:
Effect(); Effect();
void draw(int x, int y); void draw(int x, int y);
bool finished() { return m_finished; } void startAnimation();
void updateAnimation();
const ThingType& getType(); const ThingType& getType();
EffectPtr asEffect() { return std::static_pointer_cast<Effect>(shared_from_this()); } EffectPtr asEffect() { return std::static_pointer_cast<Effect>(shared_from_this()); }
private: private:
int m_lastTicks; int m_animationStartTicks;
bool m_finished;
}; };
#endif #endif

View File

@ -73,7 +73,6 @@ private:
Light m_light; Light m_light;
Position m_centralPosition; Position m_centralPosition;
FrameBufferPtr m_framebuffer; FrameBufferPtr m_framebuffer;
}; };

View File

@ -48,6 +48,7 @@ public:
Position getPosition() const { return m_position; } Position getPosition() const { return m_position; }
int getStackPriority(); int getStackPriority();
virtual const ThingType& getType() = 0; virtual const ThingType& getType() = 0;
int getAnimationPhases() { return getType().animationPhases; }
virtual void onIdChange(int) {} virtual void onIdChange(int) {}
virtual void onPositionChange(const Position&) {} virtual void onPositionChange(const Position&) {}

View File

@ -72,11 +72,12 @@ void Tile::draw(const Point& p)
Rect thisTileRect(p.x, p.y, 32, 32); Rect thisTileRect(p.x, p.y, 32, 32);
// only render creatures where bottom right is inside our rect // only render creatures where bottom right is inside our rect
if(thisTileRect.contains(creatureRect.bottomRight())) if(thisTileRect.contains(creatureRect.bottomRight())) {
creature->draw(p.x + xi*32 - m_drawElevation, p.y + yi*32 - m_drawElevation); creature->draw(p.x + xi*32 - m_drawElevation, p.y + yi*32 - m_drawElevation);
} }
} }
} }
}
// effects // effects
for(const EffectPtr& effect : m_effects) for(const EffectPtr& effect : m_effects)

View File

@ -516,6 +516,7 @@ void ProtocolGame::parseMagicEffect(InputMessage& msg)
int effectId = msg.getU8(); int effectId = msg.getU8();
EffectPtr effect = EffectPtr(new Effect()); EffectPtr effect = EffectPtr(new Effect());
effect->setId(effectId); effect->setId(effectId);
effect->startAnimation();
TilePtr tile = g_map.getTile(pos); TilePtr tile = g_map.getTile(pos);
tile->addEffect(effect); tile->addEffect(effect);

View File

@ -120,6 +120,7 @@ void OTClient::run()
poll(); poll();
while(!m_stopping) { while(!m_stopping) {
//g_platform.sleep(150);
g_platform.updateTicks(); g_platform.updateTicks();
frameTicks = g_platform.getTicks(); frameTicks = g_platform.getTicks();