more rework

This commit is contained in:
Henrique Santiago 2011-12-27 00:18:15 -02:00
parent 5830a3dc3e
commit df0f2febe6
15 changed files with 70 additions and 53 deletions

View File

@ -25,6 +25,8 @@
#include "util/types.h"
#define DEG_TO_RAD (acos(-1)/180.0)
namespace Fw
{
static const double pi = 3.14159265;

View File

@ -24,8 +24,6 @@
#include "particleaffector.h"
#include <framework/core/clock.h>
#define DEG_TO_RAD (acos(-1)/180.0)
ParticleAffector::ParticleAffector()
{
m_active = false;

View File

@ -27,8 +27,6 @@
#include <framework/graphics/texturemanager.h>
#include <framework/util/tools.h>
#define DEG_TO_RAD (acos(-1)/180.0)
ParticleEmitter::ParticleEmitter(const ParticleSystemPtr& parent)
{
m_parent = parent;

View File

@ -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;
}

View File

@ -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);

View File

@ -37,7 +37,7 @@ public:
void draw(const Point& p);
void startAnimation();
void start();
void updateAnimation();
ThingType *getType();

View File

@ -25,11 +25,11 @@
#include "spritemanager.h"
#include "thing.h"
#include <framework/core/clock.h>
#include <framework/core/eventdispatcher.h>
Item::Item() : Thing()
{
m_data = 0;
m_lastTicks = g_clock.ticks();
}
void Item::draw(const Point& p)

View File

@ -47,7 +47,6 @@ public:
private:
int m_data;
ticks_t m_lastTicks;
};
#endif

View File

@ -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)

View File

@ -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);
}

View File

@ -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;

View File

@ -103,6 +103,22 @@ struct ThingType
LastParameter
};
std::array<int, LastParameter> 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<ThingType> ThingTypeList;

View File

@ -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");

View File

@ -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);
}

View File

@ -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;