some reworks

This commit is contained in:
Henrique Santiago 2011-12-26 04:14:57 -02:00
parent 3abbf5255e
commit 37a6a38ca6
9 changed files with 197 additions and 117 deletions

View File

@ -65,34 +65,61 @@ void Particle::update(double elapsedTime)
} }
updateColor(); updateColor();
updateSize();
updatePosition(elapsedTime);
// update size m_elapsedTime += elapsedTime;
m_size = m_startSize + (m_finalSize - m_startSize) / m_duration * m_elapsedTime; }
void Particle::updatePosition(double elapsedTime)
{
bool mustRedraw = false;
// update position
if(m_ignorePhysicsAfter < 0 || m_elapsedTime < m_ignorePhysicsAfter ) { if(m_ignorePhysicsAfter < 0 || m_elapsedTime < m_ignorePhysicsAfter ) {
// update position // update position
PointF delta = m_velocity * elapsedTime; PointF delta = m_velocity * elapsedTime;
delta.y *= -1; // painter orientate Y axis in the inverse direction delta.y *= -1; // painter orientate Y axis in the inverse direction
PointF position = m_position + delta;
if(m_position != position) {
mustRedraw = true;
m_position += delta; m_position += delta;
}
// update acceleration // update acceleration
m_velocity += m_acceleration * elapsedTime; m_velocity += m_acceleration * elapsedTime;
} }
m_rect.moveTo((int)m_position.x - m_size.width() / 2, (int)m_position.y - m_size.height() / 2); m_rect.moveTo((int)m_position.x - m_size.width() / 2, (int)m_position.y - m_size.height() / 2);
m_rect.resize(m_size); }
m_elapsedTime += elapsedTime; void Particle::updateSize()
{
bool mustRedraw = false;
Size size = m_startSize + (m_finalSize - m_startSize) / m_duration * m_elapsedTime;
if(m_size != size) {
mustRedraw = true;
m_size = size;
}
m_rect.resize(m_size);
} }
void Particle::updateColor() void Particle::updateColor()
{ {
bool mustRedraw = false;
if(m_elapsedTime < m_colorsStops[1]) { if(m_elapsedTime < m_colorsStops[1]) {
m_color.setRGBA(m_colors[0].r() + (m_colors[1].r() - m_colors[0].r()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]), Color color = Color(m_colors[0].r() + (m_colors[1].r() - m_colors[0].r()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]),
m_colors[0].g() + (m_colors[1].g() - m_colors[0].g()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]), m_colors[0].g() + (m_colors[1].g() - m_colors[0].g()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]),
m_colors[0].b() + (m_colors[1].b() - m_colors[0].b()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]), m_colors[0].b() + (m_colors[1].b() - m_colors[0].b()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]),
m_colors[0].a() + (m_colors[1].a() - m_colors[0].a()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0])); m_colors[0].a() + (m_colors[1].a() - m_colors[0].a()) / (m_colorsStops[1] - m_colorsStops[0]) * (m_elapsedTime - m_colorsStops[0]));
if(m_color != color) {
mustRedraw = true;
m_color = color;
}
} }
else { else {
if(m_colors.size() > 1) { if(m_colors.size() > 1) {
@ -100,7 +127,10 @@ void Particle::updateColor()
m_colorsStops.erase(m_colorsStops.begin()); m_colorsStops.erase(m_colorsStops.begin());
} }
else { else {
if(m_color != m_colors[0]) {
mustRedraw = true;
m_color = m_colors[0]; m_color = m_colors[0];
} }
} }
}
} }

View File

@ -44,6 +44,8 @@ public:
private: private:
void updateColor(); void updateColor();
void updatePosition(double elapsedTime);
void updateSize();
Color m_color; Color m_color;
std::vector<Color> m_colors; std::vector<Color> m_colors;

View File

@ -304,7 +304,8 @@ namespace Otc
NorthEast, NorthEast,
SouthEast, SouthEast,
SouthWest, SouthWest,
NorthWest NorthWest,
InvalidDirection
}; };
enum FluidsColor { enum FluidsColor {

View File

@ -43,6 +43,7 @@ Creature::Creature() : Thing()
m_walkTimePerPixel = 1000.0/32.0; m_walkTimePerPixel = 1000.0/32.0;
m_walking = false; m_walking = false;
m_inverseWalking = true;
m_informationFont = g_fonts.getFont("verdana-11px-rounded"); m_informationFont = g_fonts.getFont("verdana-11px-rounded");
} }
@ -173,45 +174,42 @@ void Creature::walk(const Position& position, bool inverse)
int walkTimeFactor = 1; int walkTimeFactor = 1;
// set new direction // set new direction
if(m_position + Position(0, -1, 0) == position) { if(m_position.isInRange(position, 1, 1, 0)) {
setDirection(Otc::North); Otc::Direction direction = m_position.getDirectionFromPosition(position);
setDirection(direction);
if(direction == Otc::NorthWest) {
m_walkOffset.x = 32;
m_walkOffset.y = 32;
walkTimeFactor = 2;
}
else if(direction == Otc::North) {
m_walkOffset.y = 32; m_walkOffset.y = 32;
} }
else if(m_position + Position(1, 0, 0) == position) { else if(direction == Otc::NorthEast) {
setDirection(Otc::East);
m_walkOffset.x = -32;
}
else if(m_position + Position(0, 1, 0) == position) {
setDirection(Otc::South);
m_walkOffset.y = -32;
}
else if(m_position + Position(-1, 0, 0) == position) {
setDirection(Otc::West);
m_walkOffset.x = 32;
}
else if(m_position + Position(1, -1, 0) == position) {
setDirection(Otc::NorthEast);
m_walkOffset.x = -32; m_walkOffset.x = -32;
m_walkOffset.y = 32; m_walkOffset.y = 32;
walkTimeFactor = 2; walkTimeFactor = 2;
} }
else if(m_position + Position(1, 1, 0) == position) { else if(direction == Otc::East) {
setDirection(Otc::SouthEast); m_walkOffset.x = -32;
}
else if(direction == Otc::SouthEast) {
m_walkOffset.x = -32; m_walkOffset.x = -32;
m_walkOffset.y = -32; m_walkOffset.y = -32;
walkTimeFactor = 2; walkTimeFactor = 2;
} }
else if(m_position + Position(-1, 1, 0) == position) { else if(direction == Otc::South) {
setDirection(Otc::SouthWest); m_walkOffset.y = -32;
}
else if(direction == Otc::SouthWest) {
m_walkOffset.x = 32; m_walkOffset.x = 32;
m_walkOffset.y = -32; m_walkOffset.y = -32;
walkTimeFactor = 2; walkTimeFactor = 2;
} }
else if(m_position + Position(-1, -1, 0) == position) { else if(direction == Otc::West) {
setDirection(Otc::NorthWest);
m_walkOffset.x = 32; m_walkOffset.x = 32;
m_walkOffset.y = 32; }
walkTimeFactor = 2;
} }
else { // Teleport else { // Teleport
// we teleported, dont walk or change direction // we teleported, dont walk or change direction
@ -260,9 +258,6 @@ void Creature::updateWalk()
m_walkOffset.x = totalPixelsWalked - 32; m_walkOffset.x = totalPixelsWalked - 32;
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest) else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
m_walkOffset.x = 32 - totalPixelsWalked; m_walkOffset.x = 32 - totalPixelsWalked;
if(m_walkOffset.x == 0 && m_walkOffset.y == 0)
cancelWalk(m_direction);
} }
else { else {
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest) if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
@ -281,10 +276,12 @@ void Creature::updateWalk()
m_animation = (g_clock.ticks() % totalWalkTileTicks) / (totalWalkTileTicks / (m_type->dimensions[ThingType::AnimationPhases] - 1)) + 1; m_animation = (g_clock.ticks() % totalWalkTileTicks) / (totalWalkTileTicks / (m_type->dimensions[ThingType::AnimationPhases] - 1)) + 1;
else else
m_animation = 0; m_animation = 0;
g_dispatcher.scheduleEvent(std::bind(&Creature::updateWalk, asCreature()), m_walkTimePerPixel);
if(totalPixelsWalked == 32) if(totalPixelsWalked == 32)
cancelWalk(m_direction); cancelWalk(m_direction);
else
g_dispatcher.scheduleEvent(std::bind(&Creature::updateWalk, asCreature()), m_walkTimePerPixel);
} }
} }

View File

@ -91,7 +91,6 @@ protected:
ticks_t m_walkStartTicks; ticks_t m_walkStartTicks;
bool m_walking, m_inverseWalking; bool m_walking, m_inverseWalking;
float m_walkTimePerPixel; float m_walkTimePerPixel;
Position m_walkingFromPosition;
Point m_walkOffset; Point m_walkOffset;
}; };

View File

@ -33,12 +33,22 @@ Effect::Effect() : Thing()
} }
void Effect::draw(const Point& p) void Effect::draw(const Point& p)
{
internalDraw(p, 0);
}
void Effect::updateAnimation()
{ {
int animationPhase = (g_clock.ticks() - m_animationStartTicks) / TICKS_PER_FRAME; int animationPhase = (g_clock.ticks() - m_animationStartTicks) / TICKS_PER_FRAME;
if(animationPhase < getAnimationPhases()) { if(animationPhase < getAnimationPhases())
m_animation = animationPhase; m_animation = animationPhase;
internalDraw(p, 0);
if(animationPhase < getAnimationPhases() - 1) {
auto self = asEffect();
g_dispatcher.scheduleEvent([self]() {
self->updateAnimation();
}, TICKS_PER_FRAME);
} }
} }
@ -46,8 +56,14 @@ void Effect::startAnimation()
{ {
m_animationStartTicks = g_clock.ticks(); m_animationStartTicks = g_clock.ticks();
// schedule removal
auto self = asEffect(); auto self = asEffect();
// schedule update
g_dispatcher.scheduleEvent([self]() {
self->updateAnimation();
}, TICKS_PER_FRAME);
// schedule removal
g_dispatcher.scheduleEvent([self]() { g_dispatcher.scheduleEvent([self]() {
g_map.getTile(self->getPosition())->removeEffect(self); g_map.getTile(self->getPosition())->removeEffect(self);
}, TICKS_PER_FRAME * getAnimationPhases()); }, TICKS_PER_FRAME * getAnimationPhases());

View File

@ -40,75 +40,51 @@ void Missile::draw(const Point& p)
void Missile::setPath(const Position& fromPosition, const Position& toPosition) void Missile::setPath(const Position& fromPosition, const Position& toPosition)
{ {
m_position = fromPosition; Otc::Direction direction = fromPosition.getDirectionFromPosition(toPosition);
m_positionDelta = toPosition - fromPosition;
if(m_positionDelta.x == 0 && m_positionDelta.y == 0) { if(direction == Otc::NorthWest) {
m_xPattern = 1; m_xPattern = 0;
m_yPattern = 1;
}
else if(m_positionDelta.x == 0) {
m_xPattern = 1;
if(m_positionDelta.y < 0) {
m_yPattern = 0; m_yPattern = 0;
} }
else if(m_positionDelta.y > 0) { else if(direction == Otc::North) {
m_xPattern = 1;
m_yPattern = 0;
}
else if(direction == Otc::NorthEast) {
m_xPattern = 2;
m_yPattern = 0;
}
else if(direction == Otc::East) {
m_xPattern = 2;
m_yPattern = 1;
}
else if(direction == Otc::SouthEast) {
m_xPattern = 2;
m_yPattern = 2; m_yPattern = 2;
} }
else if(direction == Otc::South) {
m_xPattern = 1;
m_yPattern = 2;
} }
else if(m_positionDelta.y == 0) { else if(direction == Otc::SouthWest) {
m_yPattern = 1;
if(m_positionDelta.x < 0) {
m_xPattern = 0; m_xPattern = 0;
m_yPattern = 2;
} }
else if(m_positionDelta.x > 0) { else if(direction == Otc::West) {
m_xPattern = 2; m_xPattern = 0;
} m_yPattern = 1;
} }
else { else {
float angle = std::atan2(m_positionDelta.y * -1, m_positionDelta.x) * 180.0 / 3.141592; m_xPattern = 1;
if(angle < 0)
angle += 360;
if(angle >= 360 - 22.5 || angle < 0 + 22.5) {
m_xPattern = 2;
m_yPattern = 1; m_yPattern = 1;
} }
else if(angle >= 45 - 22.5 && angle < 45 + 22.5) {
m_xPattern = 2;
m_yPattern = 0;
}
else if(angle >= 90 - 22.5 && angle < 90 + 22.5) {
m_xPattern = 1;
m_yPattern = 0;
}
else if(angle >= 135 - 22.5 && angle < 135 + 22.5) {
m_xPattern = 0;
m_yPattern = 0;
}
else if(angle >= 180 - 22.5 && angle < 180 + 22.5) {
m_xPattern = 0;
m_yPattern = 1;
}
else if(angle >= 225 - 22.5 && angle < 225 + 22.5) {
m_xPattern = 0;
m_yPattern = 2;
}
else if(angle >= 270 - 22.5 && angle < 270 + 22.5) {
m_xPattern = 1;
m_yPattern = 2;
}
else if(angle >= 315 - 22.5 && angle < 315 + 22.5) {
m_xPattern = 2;
m_yPattern = 2;
}
}
m_duration = 150 * std::sqrt(Point(m_positionDelta.x, m_positionDelta.y).length());
m_positionDelta.x *= 32;
m_positionDelta.y *= 32;
m_position = fromPosition;
m_positionDelta = toPosition - fromPosition;
m_startTicks = g_clock.ticks(); m_startTicks = g_clock.ticks();
m_duration = 150 * std::sqrt(Point(m_positionDelta.x, m_positionDelta.y).length());
m_positionDelta.x *= Map::NUM_TILE_PIXELS;
m_positionDelta.y *= Map::NUM_TILE_PIXELS;
// schedule removal // schedule removal
auto self = asMissile(); auto self = asMissile();

View File

@ -25,6 +25,8 @@
#include <framework/otml/otml.h> #include <framework/otml/otml.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <otclient/core/tile.h> #include <otclient/core/tile.h>
#include <otclient/core/missile.h>
#include <otclient/core/effect.h>
UIMap::UIMap() UIMap::UIMap()
{ {
@ -67,6 +69,21 @@ bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button)
TilePtr tile = g_map.getTile(tilePos); TilePtr tile = g_map.getTile(tilePos);
if(tile) if(tile)
tile->useItem(); tile->useItem();
// cool testing \/
if(button == Fw::MouseLeftButton) {
MissilePtr shot = MissilePtr(new Missile());
shot->setId(1);
shot->setPath(g_map.getCentralPosition(), tilePos);
g_map.addThing(shot, g_map.getCentralPosition());
}
else if(button == Fw::MouseRightButton) {
EffectPtr effect = EffectPtr(new Effect());
effect->setId(6);
effect->startAnimation();
if(tile)
tile->addEffect(effect);
}
} }
return UIWidget::onMousePress(mousePos, button); return UIWidget::onMousePress(mousePos, button);

View File

@ -56,6 +56,48 @@ public:
} }
} }
Otc::Direction getDirectionFromPosition(const Position& position) const {
Position positionDelta = position - *this;
if(positionDelta.x == 0 && positionDelta.y == 0)
return Otc::InvalidDirection;
else if(positionDelta.x == 0) {
if(positionDelta.y < 0)
return Otc::North;
else if(positionDelta.y > 0)
return Otc::South;
}
else if(positionDelta.y == 0) {
if(positionDelta.x < 0)
return Otc::West;
else if(positionDelta.x > 0)
return Otc::East;
}
else {
float angle = std::atan2(positionDelta.y * -1, positionDelta.x) * 180.0 / 3.141592;
if(angle < 0)
angle += 360;
if(angle >= 360 - 22.5 || angle < 0 + 22.5)
return Otc::East;
else if(angle >= 45 - 22.5 && angle < 45 + 22.5)
return Otc::NorthEast;
else if(angle >= 90 - 22.5 && angle < 90 + 22.5)
return Otc::North;
else if(angle >= 135 - 22.5 && angle < 135 + 22.5)
return Otc::NorthWest;
else if(angle >= 180 - 22.5 && angle < 180 + 22.5)
return Otc::West;
else if(angle >= 225 - 22.5 && angle < 225 + 22.5)
return Otc::SouthWest;
else if(angle >= 270 - 22.5 && angle < 270 + 22.5)
return Otc::South;
else if(angle >= 315 - 22.5 && angle < 315 + 22.5)
return Otc::SouthEast;
}
return Otc::InvalidDirection;
}
bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x < 65536 && y < 65536 && z < 15; } bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x < 65536 && y < 65536 && z < 15; }
Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z); } Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z); }