From df0f2febe6e9a0d13d66d4da203eba7207fcdb67 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Tue, 27 Dec 2011 00:18:15 -0200 Subject: [PATCH] more rework --- src/framework/const.h | 2 ++ src/framework/graphics/particleaffector.cpp | 2 -- src/framework/graphics/particleemitter.cpp | 2 -- src/otclient/core/animatedtext.cpp | 7 ++-- src/otclient/core/effect.cpp | 36 +++++++++++---------- src/otclient/core/effect.h | 2 +- src/otclient/core/item.cpp | 2 +- src/otclient/core/item.h | 1 - src/otclient/core/map.cpp | 20 ++++++++---- src/otclient/core/thing.cpp | 20 ++++-------- src/otclient/core/thing.h | 4 ++- src/otclient/core/thingtype.h | 16 +++++++++ src/otclient/net/protocolgameparse.cpp | 5 ++- src/otclient/ui/uimap.cpp | 2 +- src/otclient/util/position.h | 2 +- 15 files changed, 70 insertions(+), 53 deletions(-) diff --git a/src/framework/const.h b/src/framework/const.h index 5aef959e..61deb95e 100644 --- a/src/framework/const.h +++ b/src/framework/const.h @@ -25,6 +25,8 @@ #include "util/types.h" +#define DEG_TO_RAD (acos(-1)/180.0) + namespace Fw { static const double pi = 3.14159265; diff --git a/src/framework/graphics/particleaffector.cpp b/src/framework/graphics/particleaffector.cpp index 6a5dee52..f1d5ffe2 100644 --- a/src/framework/graphics/particleaffector.cpp +++ b/src/framework/graphics/particleaffector.cpp @@ -24,8 +24,6 @@ #include "particleaffector.h" #include -#define DEG_TO_RAD (acos(-1)/180.0) - ParticleAffector::ParticleAffector() { m_active = false; diff --git a/src/framework/graphics/particleemitter.cpp b/src/framework/graphics/particleemitter.cpp index 61594a26..e8acadd4 100644 --- a/src/framework/graphics/particleemitter.cpp +++ b/src/framework/graphics/particleemitter.cpp @@ -27,8 +27,6 @@ #include #include -#define DEG_TO_RAD (acos(-1)/180.0) - ParticleEmitter::ParticleEmitter(const ParticleSystemPtr& parent) { m_parent = parent; diff --git a/src/otclient/core/animatedtext.cpp b/src/otclient/core/animatedtext.cpp index c6bedddd..22db49b2 100644 --- a/src/otclient/core/animatedtext.cpp +++ b/src/otclient/core/animatedtext.cpp @@ -45,8 +45,8 @@ void AnimatedText::start() void AnimatedText::draw(const Point& p) { - assert(m_font); - m_font->renderText(m_text, Rect(p + Point(0, -20.0 * g_clock.timeElapsed(m_startTime) / (DURATION / 1000)), m_textSize), Fw::AlignTopCenter, m_color); + if(m_font) + m_font->renderText(m_text, Rect(p + Point(0, -20.0 * g_clock.timeElapsed(m_startTime) / (DURATION / 1000)), m_textSize), Fw::AlignTopCenter, m_color); } void AnimatedText::setColor(int color) @@ -56,6 +56,7 @@ void AnimatedText::setColor(int color) void AnimatedText::setText(const std::string& text) { - m_textSize = m_font->calculateTextRectSize(text); + if(m_font) + m_textSize = m_font->calculateTextRectSize(text); m_text = text; } diff --git a/src/otclient/core/effect.cpp b/src/otclient/core/effect.cpp index 5f36eefd..aa11f5b7 100644 --- a/src/otclient/core/effect.cpp +++ b/src/otclient/core/effect.cpp @@ -32,6 +32,25 @@ Effect::Effect() : Thing() m_animationStartTicks = 0; } +void Effect::start() +{ + m_animationStartTicks = g_clock.ticks(); + + auto self = asEffect(); + + // schedule update + if(getAnimationPhases() > 1) { + g_dispatcher.scheduleEvent([self]() { + self->updateAnimation(); + }, TICKS_PER_FRAME); + } + + // schedule removal + g_dispatcher.scheduleEvent([self]() { + g_map.getTile(self->getPosition())->removeEffect(self); + }, TICKS_PER_FRAME * getAnimationPhases()); +} + void Effect::draw(const Point& p) { internalDraw(p, 0); @@ -52,23 +71,6 @@ void Effect::updateAnimation() } } -void Effect::startAnimation() -{ - m_animationStartTicks = g_clock.ticks(); - - auto self = asEffect(); - - // schedule update - g_dispatcher.scheduleEvent([self]() { - self->updateAnimation(); - }, TICKS_PER_FRAME); - - // schedule removal - g_dispatcher.scheduleEvent([self]() { - g_map.getTile(self->getPosition())->removeEffect(self); - }, TICKS_PER_FRAME * getAnimationPhases()); -} - ThingType *Effect::getType() { return g_thingsType.getThingType(m_id, ThingsType::Effect); diff --git a/src/otclient/core/effect.h b/src/otclient/core/effect.h index b385790c..d4e4bd1a 100644 --- a/src/otclient/core/effect.h +++ b/src/otclient/core/effect.h @@ -37,7 +37,7 @@ public: void draw(const Point& p); - void startAnimation(); + void start(); void updateAnimation(); ThingType *getType(); diff --git a/src/otclient/core/item.cpp b/src/otclient/core/item.cpp index 08ff9d2b..8626adff 100644 --- a/src/otclient/core/item.cpp +++ b/src/otclient/core/item.cpp @@ -25,11 +25,11 @@ #include "spritemanager.h" #include "thing.h" #include +#include Item::Item() : Thing() { m_data = 0; - m_lastTicks = g_clock.ticks(); } void Item::draw(const Point& p) diff --git a/src/otclient/core/item.h b/src/otclient/core/item.h index 05f97634..4511e78a 100644 --- a/src/otclient/core/item.h +++ b/src/otclient/core/item.h @@ -47,7 +47,6 @@ public: private: int m_data; - ticks_t m_lastTicks; }; #endif diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index e00a46f6..0dcaa7d8 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -229,7 +229,16 @@ void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos) if(!thing) return; - if(MissilePtr shot = thing->asMissile()) { + TilePtr tile = getTile(pos); + + if(CreaturePtr creature = thing->asCreature()) { + tile->addThing(thing, stackPos); + m_creatures[creature->getId()] = creature; + } + else if(EffectPtr effect = thing->asEffect()) { + tile->addEffect(effect); + } + else if(MissilePtr shot = thing->asMissile()) { m_missilesAtFloor[shot->getPosition().z].push_back(shot); return; } @@ -238,12 +247,11 @@ void Map::addThing(const ThingPtr& thing, const Position& pos, int stackPos) m_animatedTexts.push_back(animatedText); return; } + else { + tile->addThing(thing, stackPos); + } - TilePtr tile = getTile(pos); - tile->addThing(thing, stackPos); - - if(CreaturePtr creature = thing->asCreature()) - m_creatures[creature->getId()] = creature; + thing->start(); } ThingPtr Map::getThing(const Position& pos, int stackPos) diff --git a/src/otclient/core/thing.cpp b/src/otclient/core/thing.cpp index cd237ded..5412f6b4 100644 --- a/src/otclient/core/thing.cpp +++ b/src/otclient/core/thing.cpp @@ -34,26 +34,18 @@ Thing::Thing() : m_id(0) m_type = g_thingsType.getEmptyThingType(); } -void Thing::internalDraw(const Point& p, int layers) +void Thing::internalDraw(const Point& p, int layer) { - for(int yi = 0; yi < m_type->dimensions[ThingType::Height]; yi++) { - for(int xi = 0; xi < m_type->dimensions[ThingType::Width]; xi++) { - int sprIndex = ((((((m_animation % m_type->dimensions[ThingType::AnimationPhases]) - * m_type->dimensions[ThingType::PatternZ] + m_zPattern) - * m_type->dimensions[ThingType::PatternY] + m_yPattern) - * m_type->dimensions[ThingType::PatternX] + m_xPattern) - * m_type->dimensions[ThingType::Layers] + layers) - * m_type->dimensions[ThingType::Height] + yi) - * m_type->dimensions[ThingType::Width] + xi; - - int spriteId = m_type->sprites[sprIndex]; + for(int h = 0; h < m_type->dimensions[ThingType::Height]; h++) { + for(int w = 0; w < m_type->dimensions[ThingType::Width]; w++) { + int spriteId = m_type->getSpriteId(w, h, layer, m_xPattern, m_yPattern, m_zPattern, m_animation); if(!spriteId) continue; TexturePtr spriteTex = g_sprites.getSpriteTexture(spriteId); - Rect drawRect((p.x - xi*32) - m_type->parameters[ThingType::DisplacementX], - (p.y - yi*32) - m_type->parameters[ThingType::DisplacementY], + Rect drawRect((p.x - w*32) - m_type->parameters[ThingType::DisplacementX], + (p.y - h*32) - m_type->parameters[ThingType::DisplacementY], 32, 32); g_painter.drawTexturedRect(drawRect, spriteTex); } diff --git a/src/otclient/core/thing.h b/src/otclient/core/thing.h index a731bb92..58e55b90 100644 --- a/src/otclient/core/thing.h +++ b/src/otclient/core/thing.h @@ -39,6 +39,8 @@ public: Thing(); virtual ~Thing() { } + virtual void start() {} + virtual void draw(const Point& p) = 0; void setId(uint32 id); @@ -64,7 +66,7 @@ public: virtual AnimatedTextPtr asAnimatedText() { return nullptr; } protected: - void internalDraw(const Point& p, int layers); + void internalDraw(const Point& p, int layer); uint32 m_id; Position m_position; diff --git a/src/otclient/core/thingtype.h b/src/otclient/core/thingtype.h index 56cafdf4..2d77e800 100644 --- a/src/otclient/core/thingtype.h +++ b/src/otclient/core/thingtype.h @@ -103,6 +103,22 @@ struct ThingType LastParameter }; std::array parameters; + + int getSpriteId(int w, int h, int l, int x, int y, int z, int a) + { + int sprIndex = ((((((a % dimensions[ThingType::AnimationPhases]) + * dimensions[ThingType::PatternZ] + z) + * dimensions[ThingType::PatternY] + y) + * dimensions[ThingType::PatternX] + x) + * dimensions[ThingType::Layers] + l) + * dimensions[ThingType::Height] + h) + * dimensions[ThingType::Width] + w; + + if(sprIndex >= 0 && sprIndex < (int)sprites.size()) + return sprites[sprIndex]; + + return 0; + } }; typedef std::vector ThingTypeList; diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 0a6da929..f86e10b1 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -526,10 +526,9 @@ void ProtocolGame::parseMagicEffect(InputMessage& msg) if(effectId != 37) { EffectPtr effect = EffectPtr(new Effect()); effect->setId(effectId); - effect->startAnimation(); + effect->setPosition(pos); - TilePtr tile = g_map.getTile(pos); - tile->addEffect(effect); + g_map.addThing(effect, pos); } else g_particleManager.load("particle.otpa"); diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index 1eb092a4..3ce44e3c 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -88,7 +88,7 @@ bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button) else if(button == Fw::MouseRightButton) { EffectPtr effect = EffectPtr(new Effect()); effect->setId(6); - effect->startAnimation(); + effect->start(); if(tile) tile->addEffect(effect); } diff --git a/src/otclient/util/position.h b/src/otclient/util/position.h index 7e395c54..7969cae9 100644 --- a/src/otclient/util/position.h +++ b/src/otclient/util/position.h @@ -74,7 +74,7 @@ public: return Otc::East; } else { - float angle = std::atan2(positionDelta.y * -1, positionDelta.x) * 180.0 / 3.141592; + float angle = std::atan2(positionDelta.y * -1, positionDelta.x) * DEG_TO_RAD; if(angle < 0) angle += 360;