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
end
-- FIXME: the event function can be collected
-- and the dispatcher would call an invalid function, generating an warning
function removeEvent(id)
if id and eventsTable[id] then
eventsTable[id] = nil
return true
end
end

View File

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

View File

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

View File

@ -29,31 +29,30 @@
Effect::Effect() : Thing()
{
m_lastTicks = g_platform.getTicks();
m_finished = false;
m_animationStartTicks = 0;
}
void Effect::draw(int x, int y)
{
if(!m_finished) {
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();
}
int animationPhase = (g_platform.getTicks() - m_animationStartTicks) / TICKS_PER_FRAME;
if(animationPhase < getAnimationPhases()) {
m_animation = animationPhase;
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()
{
return g_thingsType.getEffectType(m_id);

View File

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

View File

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

View File

@ -48,6 +48,7 @@ public:
Position getPosition() const { return m_position; }
int getStackPriority();
virtual const ThingType& getType() = 0;
int getAnimationPhases() { return getType().animationPhases; }
virtual void onIdChange(int) {}
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);
// 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);
}
}
}
}
// effects
for(const EffectPtr& effect : m_effects)

View File

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

View File

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