From ce67fd1733e99a8f157bb7724308676f61c058d1 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 7 Nov 2011 15:20:13 -0200 Subject: [PATCH] change effects animation calculation --- modules/core/dispatcher.lua | 31 +++++++++++++----------- modules/textmessage/textmessage.otui | 3 +-- src/framework/luascript/luainterface.cpp | 1 + src/otclient/core/effect.cpp | 31 ++++++++++++------------ src/otclient/core/effect.h | 10 +++++--- src/otclient/core/map.cpp | 2 +- src/otclient/core/map.h | 1 - src/otclient/core/thing.h | 1 + src/otclient/core/tile.cpp | 3 ++- src/otclient/net/protocolgameparse.cpp | 1 + src/otclient/otclient.cpp | 1 + 11 files changed, 47 insertions(+), 38 deletions(-) diff --git a/modules/core/dispatcher.lua b/modules/core/dispatcher.lua index 312344f1..bea374b8 100644 --- a/modules/core/dispatcher.lua +++ b/modules/core/dispatcher.lua @@ -8,33 +8,36 @@ function scheduleEvent(func, delay) eventId = eventId + 1 local id = eventId local function proxyFunc() - if eventsTable[id] then - func() - eventsTable[id] = nil - end + if eventsTable[id] then + func() + eventsTable[id] = nil + end end eventsTable[id] = proxyFunc orig.scheduleEvent(proxyFunc, 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 -- fix original addEvent function addEvent(func) - eventId = eventId + 1 - local id = eventId - local function proxyFunc() - if eventsTable[id] then - func() - eventsTable[id] = nil - end + eventId = eventId + 1 + local id = eventId + local function proxyFunc() + if eventsTable[id] then + func() + eventsTable[id] = nil end - eventsTable[id] = proxyFunc - orig.addEvent(proxyFunc) - return id + end + eventsTable[id] = proxyFunc + orig.addEvent(proxyFunc) + return id end \ No newline at end of file diff --git a/modules/textmessage/textmessage.otui b/modules/textmessage/textmessage.otui index 38e1135d..0e46a408 100644 --- a/modules/textmessage/textmessage.otui +++ b/modules/textmessage/textmessage.otui @@ -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 diff --git a/src/framework/luascript/luainterface.cpp b/src/framework/luascript/luainterface.cpp index a5737756..bf91a2e8 100644 --- a/src/framework/luascript/luainterface.cpp +++ b/src/framework/luascript/luainterface.cpp @@ -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 '@' diff --git a/src/otclient/core/effect.cpp b/src/otclient/core/effect.cpp index e5d60bc6..6f810edd 100644 --- a/src/otclient/core/effect.cpp +++ b/src/otclient/core/effect.cpp @@ -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); diff --git a/src/otclient/core/effect.h b/src/otclient/core/effect.h index e5037940..914575dc 100644 --- a/src/otclient/core/effect.h +++ b/src/otclient/core/effect.h @@ -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(shared_from_this()); } private: - int m_lastTicks; - bool m_finished; + int m_animationStartTicks; }; #endif diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index 4761108e..63f56a37 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -200,7 +200,7 @@ void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos) tile->addThing(thing, stackPos); if(CreaturePtr creature = thing->asCreature()) - m_creatures[creature ->getId()] = creature; + m_creatures[creature->getId()] = creature; } ThingPtr Map::getThing(const Position& pos, int stackPos) diff --git a/src/otclient/core/map.h b/src/otclient/core/map.h index e0ada9b2..dbc027ae 100644 --- a/src/otclient/core/map.h +++ b/src/otclient/core/map.h @@ -73,7 +73,6 @@ private: Light m_light; Position m_centralPosition; - FrameBufferPtr m_framebuffer; }; diff --git a/src/otclient/core/thing.h b/src/otclient/core/thing.h index a0396942..889fe167 100644 --- a/src/otclient/core/thing.h +++ b/src/otclient/core/thing.h @@ -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&) {} diff --git a/src/otclient/core/tile.cpp b/src/otclient/core/tile.cpp index ca84a67b..2c08eb8a 100644 --- a/src/otclient/core/tile.cpp +++ b/src/otclient/core/tile.cpp @@ -72,8 +72,9 @@ 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); + } } } } diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index c3736f5d..4a5e03e7 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -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); diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index e3110673..ede805b2 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -120,6 +120,7 @@ void OTClient::run() poll(); while(!m_stopping) { + //g_platform.sleep(150); g_platform.updateTicks(); frameTicks = g_platform.getTicks();