changes the way that walk calculates

This commit is contained in:
Eduardo Bart 2011-11-05 19:01:41 -02:00
parent fbda996b76
commit 63652bd3a3
2 changed files with 35 additions and 49 deletions

View File

@ -39,7 +39,6 @@ Creature::Creature() : Thing()
m_walking = false; m_walking = false;
m_walkOffsetX = 0; m_walkOffsetX = 0;
m_walkOffsetY = 0; m_walkOffsetY = 0;
m_lastWalkAnim = 1;
m_informationFont = g_fonts.getFont("verdana-11px-rounded"); m_informationFont = g_fonts.getFont("verdana-11px-rounded");
} }
@ -90,52 +89,39 @@ void Creature::draw(int x, int y)
// Update animation and position // Update animation and position
if(m_walking && type.animationPhases > 1) { if(m_walking && type.animationPhases > 1) {
if(g_platform.getTicks() - m_lastTicks >= m_walkTimePerPixel) { int elapsedTicks = g_platform.getTicks() - m_walkStartTicks;
int pixelsWalked = std::floor((g_platform.getTicks() - m_lastTicks) / m_walkTimePerPixel); int totalPixelsWalked = std::min((int)std::round(elapsedTicks / m_walkTimePerPixel), 32);
int remainingTime = (g_platform.getTicks() - m_lastTicks) % (int)m_walkTimePerPixel;
if(m_inverseWalking) { if(m_inverseWalking) {
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)
m_walkOffsetY = std::max(m_walkOffsetY - pixelsWalked, 0); m_walkOffsetY = 32 - totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest) else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = std::min(m_walkOffsetY + pixelsWalked, 0); m_walkOffsetY = totalPixelsWalked - 32;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast) if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = std::min(m_walkOffsetX + pixelsWalked, 0); m_walkOffsetX = 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_walkOffsetX = std::max(m_walkOffsetX - pixelsWalked, 0); m_walkOffsetX = 32 - totalPixelsWalked;
if(m_walkOffsetX == 0 && m_walkOffsetY == 0) if(m_walkOffsetX == 0 && m_walkOffsetY == 0)
cancelWalk(m_direction); 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)
m_walkOffsetY = std::max(m_walkOffsetY - pixelsWalked, -32); m_walkOffsetY = -totalPixelsWalked;
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest) else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
m_walkOffsetY = std::min(m_walkOffsetY + pixelsWalked, 32); m_walkOffsetY = totalPixelsWalked;
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast) if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
m_walkOffsetX = std::min(m_walkOffsetX + pixelsWalked, 32); m_walkOffsetX = totalPixelsWalked;
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_walkOffsetX = std::max(m_walkOffsetX - pixelsWalked, -32); m_walkOffsetX = -totalPixelsWalked;
}
if(std::abs(m_walkOffsetX) == 32 && std::abs(m_walkOffsetY) == 32) int totalWalkTileTicks = (int)m_walkTimePerPixel*32;
m_animation = (g_platform.getTicks() % totalWalkTileTicks) / (totalWalkTileTicks / (type.animationPhases - 1)) + 1;
} else
m_animation = 0; m_animation = 0;
}
int walkOffset = std::max(std::abs(m_walkOffsetX), std::abs(m_walkOffsetY)) + 32;
if(walkOffset % (int)std::ceil(32 / (float)type.animationPhases) == 0) {
if((m_lastWalkAnim+1) % type.animationPhases == 0)
m_lastWalkAnim = 1;
else
m_lastWalkAnim++;
}
if(m_walking)
m_animation = m_lastWalkAnim;
m_lastTicks = g_platform.getTicks() - remainingTime;
}
}
} }
void Creature::drawInformation(int x, int y, bool useGray, const Rect& rect) void Creature::drawInformation(int x, int y, bool useGray, const Rect& rect)
@ -190,6 +176,7 @@ void Creature::drawInformation(int x, int y, bool useGray, const Rect& rect)
void Creature::walk(const Position& position, bool inverse) void Creature::walk(const Position& position, bool inverse)
{ {
// set walking state // set walking state
bool sameWalk = m_walking && !m_inverseWalking && inverse;
m_walking = true; m_walking = true;
m_inverseWalking = inverse; m_inverseWalking = inverse;
int walkTimeFactor = 1; int walkTimeFactor = 1;
@ -258,18 +245,18 @@ void Creature::walk(const Position& position, bool inverse)
} }
// get walk speed // get walk speed
int groundSpeed = 0; int groundSpeed = 100;
ItemPtr ground = g_map.getTile(position)->getGround(); ItemPtr ground = g_map.getTile(position)->getGround();
if(ground) if(ground)
groundSpeed = ground->getType().groundSpeed; groundSpeed = ground->getType().groundSpeed;
float walkTime = walkTimeFactor * 1000.0 * (float)groundSpeed / m_speed; float walkTime = walkTimeFactor * 1000.0 * (float)groundSpeed / m_speed;
walkTime = walkTime == 0 ? 1000 : walkTime; walkTime = (walkTime == 0) ? 1000 : walkTime;
m_walkTimePerPixel = walkTime / 32.0; m_walkTimePerPixel = walkTime / 32.0;
if(!sameWalk)
m_lastTicks = g_platform.getTicks(); m_walkStartTicks = g_platform.getTicks();
} }
} }

View File

@ -94,11 +94,10 @@ protected:
FontPtr m_informationFont; FontPtr m_informationFont;
Color m_informationColor; Color m_informationColor;
int m_lastTicks; int m_walkStartTicks;
bool m_walking, m_inverseWalking; bool m_walking, m_inverseWalking;
float m_walkTimePerPixel; float m_walkTimePerPixel;
Position m_walkingFromPosition; Position m_walkingFromPosition;
int m_lastWalkAnim;
int m_walkOffsetX, m_walkOffsetY; int m_walkOffsetX, m_walkOffsetY;
}; };