2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
#include "creature.h"
|
2011-08-31 17:03:33 +02:00
|
|
|
#include "thingstype.h"
|
2011-08-26 07:07:23 +02:00
|
|
|
#include "localplayer.h"
|
|
|
|
#include "map.h"
|
2011-08-31 22:22:57 +02:00
|
|
|
#include "tile.h"
|
|
|
|
#include "item.h"
|
2011-12-28 12:26:52 +01:00
|
|
|
#include "game.h"
|
2012-01-10 01:36:18 +01:00
|
|
|
#include "effect.h"
|
2011-08-31 22:22:57 +02:00
|
|
|
|
2011-08-15 16:11:24 +02:00
|
|
|
#include <framework/graphics/graphics.h>
|
2011-11-05 22:37:36 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
2011-12-01 23:25:32 +01:00
|
|
|
#include <framework/core/clock.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-12-08 18:28:29 +01:00
|
|
|
#include <framework/graphics/paintershaderprogram.h>
|
2012-04-19 01:03:43 +02:00
|
|
|
#include <framework/graphics/painterogl2_shadersources.h>
|
2012-01-07 00:54:17 +01:00
|
|
|
#include <framework/graphics/texturemanager.h>
|
2012-02-08 06:13:52 +01:00
|
|
|
#include <framework/graphics/framebuffer.h>
|
2011-12-08 18:28:29 +01:00
|
|
|
#include "spritemanager.h"
|
|
|
|
|
2011-08-31 22:22:57 +02:00
|
|
|
Creature::Creature() : Thing()
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2012-05-11 21:25:29 +02:00
|
|
|
m_id = 1;
|
2012-05-10 16:04:11 +02:00
|
|
|
m_healthPercent = 100;
|
|
|
|
m_speed = 200;
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::South;
|
2012-02-02 17:37:52 +01:00
|
|
|
m_walkAnimationPhase = 0;
|
2012-01-19 05:12:53 +01:00
|
|
|
m_walkInterval = 0;
|
2012-01-19 05:50:48 +01:00
|
|
|
m_walkAnimationInterval = 0;
|
2012-01-19 05:12:53 +01:00
|
|
|
m_walkTurnDirection = Otc::InvalidDirection;
|
2012-01-11 23:31:23 +01:00
|
|
|
m_skull = Otc::SkullNone;
|
|
|
|
m_shield = Otc::ShieldNone;
|
|
|
|
m_emblem = Otc::EmblemNone;
|
2012-06-09 02:40:22 +02:00
|
|
|
m_nameCache.setFont(g_fonts.getFont("verdana-11px-rounded"));
|
|
|
|
m_nameCache.setAlign(Fw::AlignTopCenter);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
/*
|
2011-12-08 18:28:29 +01:00
|
|
|
PainterShaderProgramPtr outfitProgram;
|
|
|
|
int HEAD_COLOR_UNIFORM = 10;
|
|
|
|
int BODY_COLOR_UNIFORM = 11;
|
|
|
|
int LEGS_COLOR_UNIFORM = 12;
|
|
|
|
int FEET_COLOR_UNIFORM = 13;
|
|
|
|
int MASK_TEXTURE_UNIFORM = 14;
|
2012-04-19 01:03:43 +02:00
|
|
|
*/
|
2011-12-08 18:28:29 +01:00
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
void Creature::draw(const Point& dest, float scaleFactor, bool animate)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
Point animationOffset = animate ? m_walkOffset : Point(0,0);
|
|
|
|
|
2012-02-08 03:11:57 +01:00
|
|
|
if(m_showTimedSquare && animate) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(m_timedSquareColor);
|
2012-06-03 22:35:07 +02:00
|
|
|
g_painter->drawBoundingRect(Rect(dest + (animationOffset - getDisplacement() + 2)*scaleFactor, Size(28, 28)*scaleFactor), std::max((int)(2*scaleFactor), 1));
|
2012-06-06 16:10:35 +02:00
|
|
|
g_painter->setColor(Color::white);
|
2011-11-13 23:23:21 +01:00
|
|
|
}
|
|
|
|
|
2012-02-02 17:37:52 +01:00
|
|
|
if(m_showStaticSquare && animate) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(m_staticSquareColor);
|
2012-06-03 22:35:07 +02:00
|
|
|
g_painter->drawBoundingRect(Rect(dest + (animationOffset - getDisplacement())*scaleFactor, Size(Otc::TILE_PIXELS, Otc::TILE_PIXELS)*scaleFactor), std::max((int)(2*scaleFactor), 1));
|
2012-06-06 16:10:35 +02:00
|
|
|
g_painter->setColor(Color::white);
|
2012-01-05 15:24:38 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 06:13:52 +01:00
|
|
|
internalDrawOutfit(dest + animationOffset * scaleFactor, scaleFactor, animate, animate, m_direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::internalDrawOutfit(const Point& dest, float scaleFactor, bool animateWalk, bool animateIdle, Otc::Direction direction)
|
|
|
|
{
|
2012-02-02 17:37:52 +01:00
|
|
|
// outfit is a real creature
|
2012-01-10 01:36:18 +01:00
|
|
|
if(m_outfit.getCategory() == ThingsType::Creature) {
|
2012-02-08 06:13:52 +01:00
|
|
|
int animationPhase = animateWalk ? m_walkAnimationPhase : 0;
|
|
|
|
|
|
|
|
if(isAnimateAlways() && animateIdle) {
|
2012-02-03 00:23:51 +01:00
|
|
|
int ticksPerFrame = 1000 / getAnimationPhases();
|
2012-06-02 16:43:27 +02:00
|
|
|
animationPhase = (g_clock.millis() % (ticksPerFrame * getAnimationPhases())) / ticksPerFrame;
|
2012-02-03 00:23:51 +01:00
|
|
|
}
|
2012-02-02 17:37:52 +01:00
|
|
|
|
|
|
|
// xPattern => creature direction
|
2012-06-09 02:40:22 +02:00
|
|
|
int xPattern;
|
2012-02-08 06:13:52 +01:00
|
|
|
if(direction == Otc::NorthEast || direction == Otc::SouthEast)
|
2012-02-02 17:37:52 +01:00
|
|
|
xPattern = Otc::East;
|
2012-02-08 06:13:52 +01:00
|
|
|
else if(direction == Otc::NorthWest || direction == Otc::SouthWest)
|
2012-02-02 17:37:52 +01:00
|
|
|
xPattern = Otc::West;
|
|
|
|
else
|
2012-02-08 06:13:52 +01:00
|
|
|
xPattern = direction;
|
2012-02-02 17:37:52 +01:00
|
|
|
|
|
|
|
// yPattern => creature addon
|
2012-06-09 02:40:22 +02:00
|
|
|
for(int yPattern = 0; yPattern < getNumPatternsY(); yPattern++) {
|
2012-01-10 01:36:18 +01:00
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
// continue if we dont have this addon
|
2012-02-02 17:37:52 +01:00
|
|
|
if(yPattern > 0 && !(m_outfit.getAddons() & (1 << (yPattern-1))))
|
2012-01-10 01:36:18 +01:00
|
|
|
continue;
|
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
m_type->draw(dest, scaleFactor, 0, xPattern, yPattern, 0, animationPhase);
|
|
|
|
|
|
|
|
if(getLayers() > 1) {
|
|
|
|
Color oldColor = g_painter->getColor();
|
|
|
|
Painter::CompositionMode oldComposition = g_painter->getCompositionMode();
|
|
|
|
g_painter->setCompositionMode(Painter::CompositionMode_Multiply);
|
|
|
|
g_painter->setColor(m_outfit.getHeadColor());
|
|
|
|
m_type->draw(dest, scaleFactor, ThingType::YellowMask, xPattern, yPattern, 0, animationPhase);
|
|
|
|
g_painter->setColor(m_outfit.getBodyColor());
|
|
|
|
m_type->draw(dest, scaleFactor, ThingType::RedMask, xPattern, yPattern, 0, animationPhase);
|
|
|
|
g_painter->setColor(m_outfit.getLegsColor());
|
|
|
|
m_type->draw(dest, scaleFactor, ThingType::GreenMask, xPattern, yPattern, 0, animationPhase);
|
|
|
|
g_painter->setColor(m_outfit.getFeetColor());
|
|
|
|
m_type->draw(dest, scaleFactor, ThingType::BlueMask, xPattern, yPattern, 0, animationPhase);
|
|
|
|
g_painter->setColor(oldColor);
|
|
|
|
g_painter->setCompositionMode(oldComposition);
|
2011-12-08 18:28:29 +01:00
|
|
|
}
|
2012-01-10 01:36:18 +01:00
|
|
|
}
|
2012-02-02 17:37:52 +01:00
|
|
|
// outfit is a creature imitating an item or the invisible effect
|
|
|
|
} else {
|
|
|
|
int animationPhase = 0;
|
|
|
|
int animationPhases = getAnimationPhases();
|
|
|
|
int animateTicks = Otc::ITEM_TICKS_PER_FRAME;
|
|
|
|
|
|
|
|
// when creature is an effect we cant render the first and last animation phase,
|
|
|
|
// instead we should loop in the phases between
|
|
|
|
if(m_outfit.getCategory() == ThingsType::Effect) {
|
|
|
|
animationPhases = std::max(1, animationPhases-2);
|
|
|
|
animateTicks = Otc::INVISIBLE_TICKS_PER_FRAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(animationPhases > 1) {
|
2012-02-08 06:13:52 +01:00
|
|
|
if(animateIdle)
|
2012-06-02 16:43:27 +02:00
|
|
|
animationPhase = (g_clock.millis() % (animateTicks * animationPhases)) / animateTicks;
|
2012-02-02 17:37:52 +01:00
|
|
|
else
|
|
|
|
animationPhase = animationPhases-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_outfit.getCategory() == ThingsType::Effect)
|
|
|
|
animationPhase = std::min(animationPhase+1, getAnimationPhases());
|
|
|
|
|
2012-05-13 17:05:17 +02:00
|
|
|
m_type->draw(dest, scaleFactor, 0, 0, 0, 0, animationPhase);
|
2012-02-08 06:13:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::drawOutfit(const Rect& destRect, bool resize)
|
|
|
|
{
|
2012-04-20 12:16:03 +02:00
|
|
|
if(g_graphics.canUseFBO()) {
|
|
|
|
static FrameBufferPtr outfitBuffer;
|
|
|
|
if(!outfitBuffer)
|
|
|
|
outfitBuffer = FrameBufferPtr(new FrameBuffer(Size(2*Otc::TILE_PIXELS, 2*Otc::TILE_PIXELS)));
|
|
|
|
|
|
|
|
outfitBuffer->bind();
|
2012-06-09 02:40:22 +02:00
|
|
|
g_painter->setAlphaWriting(true);
|
2012-06-06 16:10:35 +02:00
|
|
|
g_painter->clear(Color::alpha);
|
2012-04-20 12:16:03 +02:00
|
|
|
internalDrawOutfit(Point(Otc::TILE_PIXELS,Otc::TILE_PIXELS) + getDisplacement(), 1, false, true, Otc::South);
|
|
|
|
outfitBuffer->release();
|
|
|
|
|
2012-02-08 06:13:52 +01:00
|
|
|
Rect srcRect;
|
2012-04-20 12:16:03 +02:00
|
|
|
if(resize)
|
|
|
|
srcRect.resize(getExactSize(), getExactSize());
|
|
|
|
else
|
|
|
|
srcRect.resize(2*Otc::TILE_PIXELS*0.75f, 2*Otc::TILE_PIXELS*0.75f);
|
|
|
|
srcRect.moveBottomRight(Point(2*Otc::TILE_PIXELS - 1, 2*Otc::TILE_PIXELS - 1));
|
2012-02-08 06:13:52 +01:00
|
|
|
outfitBuffer->draw(destRect, srcRect);
|
|
|
|
} else {
|
2012-04-20 12:16:03 +02:00
|
|
|
float scaleFactor;
|
|
|
|
if(resize)
|
|
|
|
scaleFactor = destRect.width() / (float)getExactSize();
|
|
|
|
else
|
|
|
|
scaleFactor = destRect.width() / (float)(2*Otc::TILE_PIXELS*0.75f);
|
|
|
|
Point dest = destRect.bottomRight() - (Point(Otc::TILE_PIXELS,Otc::TILE_PIXELS) - getDisplacement())*scaleFactor;
|
|
|
|
internalDrawOutfit(dest, scaleFactor, false, true, Otc::South);
|
2012-01-10 01:36:18 +01:00
|
|
|
}
|
2011-08-21 05:21:35 +02:00
|
|
|
}
|
2011-08-19 15:23:35 +02:00
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
void Creature::drawInformation(const Point& point, bool useGray, const Rect& parentRect)
|
2011-08-21 05:21:35 +02:00
|
|
|
{
|
2011-08-21 21:09:23 +02:00
|
|
|
Color fillColor = Color(96, 96, 96);
|
|
|
|
|
2011-08-29 05:05:57 +02:00
|
|
|
if(!useGray)
|
|
|
|
fillColor = m_informationColor;
|
2011-08-19 15:23:35 +02:00
|
|
|
|
2011-11-04 05:53:00 +01:00
|
|
|
// calculate main rects
|
2012-01-30 01:00:12 +01:00
|
|
|
Rect backgroundRect = Rect(point.x-(13.5), point.y, 27, 4);
|
|
|
|
backgroundRect.bind(parentRect);
|
2011-11-04 05:53:00 +01:00
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
Size nameSize = m_nameCache.getTextSize();
|
|
|
|
Rect textRect = Rect(point.x - nameSize.width() / 2.0, point.y-12, nameSize);
|
2012-01-30 01:00:12 +01:00
|
|
|
textRect.bind(parentRect);
|
2011-11-04 05:53:00 +01:00
|
|
|
|
|
|
|
// distance them
|
2012-01-30 01:00:12 +01:00
|
|
|
if(textRect.top() == parentRect.top())
|
2011-12-29 08:08:02 +01:00
|
|
|
backgroundRect.moveTop(textRect.top() + 12);
|
2012-01-30 01:00:12 +01:00
|
|
|
if(backgroundRect.bottom() == parentRect.bottom())
|
2011-12-29 08:08:02 +01:00
|
|
|
textRect.moveTop(backgroundRect.top() - 12);
|
2011-11-04 05:53:00 +01:00
|
|
|
|
|
|
|
// health rect is based on background rect, so no worries
|
2011-08-21 21:09:23 +02:00
|
|
|
Rect healthRect = backgroundRect.expanded(-1);
|
2011-11-04 05:53:00 +01:00
|
|
|
healthRect.setWidth((m_healthPercent / 100.0) * 25);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-11-04 05:53:00 +01:00
|
|
|
// draw
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::black);
|
|
|
|
g_painter->drawFilledRect(backgroundRect);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(fillColor);
|
|
|
|
g_painter->drawFilledRect(healthRect);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2012-06-09 02:40:22 +02:00
|
|
|
m_nameCache.draw(textRect);
|
2012-01-07 00:54:17 +01:00
|
|
|
|
|
|
|
if(m_skull != Otc::SkullNone && m_skullTexture) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::white);
|
|
|
|
g_painter->drawTexturedRect(Rect(point.x + 12, point.y + 5, m_skullTexture->getSize()), m_skullTexture);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
2012-01-11 23:31:23 +01:00
|
|
|
if(m_shield != Otc::ShieldNone && m_shieldTexture && m_showShieldTexture) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::white);
|
|
|
|
g_painter->drawTexturedRect(Rect(point.x, point.y + 5, m_shieldTexture->getSize()), m_shieldTexture);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
|
|
|
if(m_emblem != Otc::EmblemNone && m_emblemTexture) {
|
2012-04-19 01:03:43 +02:00
|
|
|
g_painter->setColor(Color::white);
|
|
|
|
g_painter->drawTexturedRect(Rect(point.x + 12, point.y + 16, m_emblemTexture->getSize()), m_emblemTexture);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
void Creature::turn(Otc::Direction direction)
|
|
|
|
{
|
|
|
|
// if is not walking change the direction right away
|
|
|
|
if(!m_walking)
|
|
|
|
setDirection(direction);
|
|
|
|
// schedules to set the new direction when walk ends
|
|
|
|
else
|
|
|
|
m_walkTurnDirection = direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::walk(const Position& oldPos, const Position& newPos)
|
2011-08-26 07:07:23 +02:00
|
|
|
{
|
2012-05-10 00:19:05 +02:00
|
|
|
if(oldPos == newPos)
|
|
|
|
return;
|
|
|
|
|
2012-01-15 22:19:52 +01:00
|
|
|
// get walk direction
|
|
|
|
Otc::Direction direction = oldPos.getDirectionFromPosition(newPos);
|
2011-08-26 17:44:29 +02:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// set current walking direction
|
|
|
|
setDirection(direction);
|
2011-08-29 02:38:26 +02:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// starts counting walk
|
|
|
|
m_walking = true;
|
|
|
|
m_walkTimer.restart();
|
2012-01-15 22:19:52 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// calculates walk interval
|
2012-01-30 01:00:12 +01:00
|
|
|
int groundSpeed = 0;
|
|
|
|
TilePtr oldTile = g_map.getTile(oldPos);
|
|
|
|
if(oldTile)
|
|
|
|
groundSpeed = oldTile->getGroundSpeed();
|
|
|
|
|
2012-05-10 16:04:11 +02:00
|
|
|
float interval = 1000;
|
|
|
|
if(groundSpeed > 0 && m_speed > 0)
|
2012-01-19 05:50:48 +01:00
|
|
|
interval = (1000.0f * groundSpeed) / m_speed;
|
|
|
|
interval = std::ceil(interval / g_game.getServerBeat()) * g_game.getServerBeat();
|
|
|
|
|
|
|
|
m_walkAnimationInterval = interval;
|
|
|
|
m_walkInterval = interval;
|
2012-01-15 22:19:52 +01:00
|
|
|
|
2012-01-19 05:50:48 +01:00
|
|
|
// diagonal walking lasts 3 times more.
|
|
|
|
if(direction == Otc::NorthWest || direction == Otc::NorthEast || direction == Otc::SouthWest || direction == Otc::SouthEast)
|
|
|
|
m_walkInterval *= 3;
|
2011-11-05 22:37:36 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// no direction needs to be changed when the walk ends
|
|
|
|
m_walkTurnDirection = Otc::InvalidDirection;
|
|
|
|
|
|
|
|
// starts updating walk
|
|
|
|
nextWalkUpdate();
|
2011-12-28 12:00:09 +01:00
|
|
|
}
|
2011-11-05 22:37:36 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
void Creature::stopWalk()
|
2011-12-28 12:00:09 +01:00
|
|
|
{
|
|
|
|
if(!m_walking)
|
|
|
|
return;
|
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// reset walk animation states
|
2012-01-20 17:48:26 +01:00
|
|
|
m_walkOffset = Point(0,0);
|
2012-02-02 17:37:52 +01:00
|
|
|
m_walkAnimationPhase = 0;
|
2011-12-26 07:14:57 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// stops the walk right away
|
|
|
|
terminateWalk();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::updateWalkAnimation(int totalPixelsWalked)
|
|
|
|
{
|
|
|
|
// update outfit animation
|
2012-01-24 19:39:16 +01:00
|
|
|
if(m_outfit.getCategory() != ThingsType::Creature)
|
|
|
|
return;
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
if(totalPixelsWalked == 32 || totalPixelsWalked == 0 || getAnimationPhases() <= 1)
|
2012-02-02 17:37:52 +01:00
|
|
|
m_walkAnimationPhase = 0;
|
2012-01-30 01:00:12 +01:00
|
|
|
else if(getAnimationPhases() > 1)
|
2012-02-02 17:37:52 +01:00
|
|
|
m_walkAnimationPhase = 1 + ((totalPixelsWalked * 4) / Otc::TILE_PIXELS) % (getAnimationPhases() - 1);
|
2012-01-19 05:12:53 +01:00
|
|
|
}
|
2011-11-05 22:37:36 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
void Creature::updateWalkOffset(int totalPixelsWalked)
|
|
|
|
{
|
|
|
|
m_walkOffset = Point(0,0);
|
|
|
|
if(m_direction == Otc::North || m_direction == Otc::NorthEast || m_direction == Otc::NorthWest)
|
|
|
|
m_walkOffset.y = 32 - totalPixelsWalked;
|
|
|
|
else if(m_direction == Otc::South || m_direction == Otc::SouthEast || m_direction == Otc::SouthWest)
|
|
|
|
m_walkOffset.y = totalPixelsWalked - 32;
|
|
|
|
|
|
|
|
if(m_direction == Otc::East || m_direction == Otc::NorthEast || m_direction == Otc::SouthEast)
|
|
|
|
m_walkOffset.x = totalPixelsWalked - 32;
|
|
|
|
else if(m_direction == Otc::West || m_direction == Otc::NorthWest || m_direction == Otc::SouthWest)
|
|
|
|
m_walkOffset.x = 32 - totalPixelsWalked;
|
2011-08-30 16:37:48 +02:00
|
|
|
}
|
2011-08-29 02:38:26 +02:00
|
|
|
|
2012-01-30 22:28:08 +01:00
|
|
|
void Creature::updateWalkingTile()
|
|
|
|
{
|
|
|
|
// determine new walking tile
|
|
|
|
TilePtr newWalkingTile;
|
|
|
|
Rect virtualCreatureRect(Otc::TILE_PIXELS + (m_walkOffset.x - getDisplacementX()),
|
|
|
|
Otc::TILE_PIXELS + (m_walkOffset.y - getDisplacementY()),
|
|
|
|
Otc::TILE_PIXELS, Otc::TILE_PIXELS);
|
|
|
|
for(int xi = -1; xi <= 1 && !newWalkingTile; ++xi) {
|
|
|
|
for(int yi = -1; yi <= 1 && !newWalkingTile; ++yi) {
|
|
|
|
Rect virtualTileRect((xi+1)*Otc::TILE_PIXELS, (yi+1)*Otc::TILE_PIXELS, Otc::TILE_PIXELS, Otc::TILE_PIXELS);
|
|
|
|
|
|
|
|
// only render creatures where bottom right is inside tile rect
|
|
|
|
if(virtualTileRect.contains(virtualCreatureRect.bottomRight())) {
|
2012-05-11 23:40:40 +02:00
|
|
|
newWalkingTile = g_map.getOrCreateTile(m_position.translated(xi, yi, 0));
|
2012-01-30 22:28:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(newWalkingTile != m_walkingTile) {
|
|
|
|
if(m_walkingTile)
|
|
|
|
m_walkingTile->removeWalkingCreature(asCreature());
|
|
|
|
if(newWalkingTile)
|
|
|
|
newWalkingTile->addWalkingCreature(asCreature());
|
|
|
|
m_walkingTile = newWalkingTile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
void Creature::nextWalkUpdate()
|
2011-08-30 16:37:48 +02:00
|
|
|
{
|
2012-01-19 05:12:53 +01:00
|
|
|
// remove any previous scheduled walk updates
|
|
|
|
if(m_walkUpdateEvent)
|
|
|
|
m_walkUpdateEvent->cancel();
|
2012-01-15 22:19:52 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
// do the update
|
|
|
|
updateWalk();
|
|
|
|
|
|
|
|
// schedules next update
|
|
|
|
if(m_walking) {
|
|
|
|
auto self = asCreature();
|
2012-03-14 19:45:15 +01:00
|
|
|
m_walkUpdateEvent = g_eventDispatcher.scheduleEvent([self] {
|
2012-01-19 05:12:53 +01:00
|
|
|
self->m_walkUpdateEvent = nullptr;
|
|
|
|
self->nextWalkUpdate();
|
2012-01-19 05:50:48 +01:00
|
|
|
}, m_walkAnimationInterval / 32);
|
2012-01-19 05:12:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::updateWalk()
|
|
|
|
{
|
2012-01-19 05:50:48 +01:00
|
|
|
float walkTicksPerPixel = m_walkAnimationInterval / 32;
|
2012-01-19 05:12:53 +01:00
|
|
|
int totalPixelsWalked = std::min(m_walkTimer.ticksElapsed() / walkTicksPerPixel, 32.0f);
|
|
|
|
|
|
|
|
// update walk animation and offsets
|
|
|
|
updateWalkAnimation(totalPixelsWalked);
|
|
|
|
updateWalkOffset(totalPixelsWalked);
|
2012-01-30 22:28:08 +01:00
|
|
|
updateWalkingTile();
|
2012-01-19 05:12:53 +01:00
|
|
|
|
|
|
|
// terminate walk
|
|
|
|
if(m_walking && m_walkTimer.ticksElapsed() >= m_walkInterval)
|
|
|
|
terminateWalk();
|
|
|
|
}
|
2012-01-15 22:19:52 +01:00
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
void Creature::terminateWalk()
|
|
|
|
{
|
|
|
|
// remove any scheduled walk update
|
|
|
|
if(m_walkUpdateEvent) {
|
|
|
|
m_walkUpdateEvent->cancel();
|
|
|
|
m_walkUpdateEvent = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now the walk has ended, do any scheduled turn
|
|
|
|
if(m_walkTurnDirection != Otc::InvalidDirection) {
|
|
|
|
setDirection(m_walkTurnDirection);
|
|
|
|
m_walkTurnDirection = Otc::InvalidDirection;
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:28:08 +01:00
|
|
|
if(m_walkingTile) {
|
|
|
|
m_walkingTile->removeWalkingCreature(asCreature());
|
|
|
|
m_walkingTile = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-01-19 05:12:53 +01:00
|
|
|
m_walking = false;
|
2011-08-26 07:07:23 +02:00
|
|
|
}
|
|
|
|
|
2011-11-13 09:46:19 +01:00
|
|
|
void Creature::setName(const std::string& name)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
2012-06-09 02:40:22 +02:00
|
|
|
m_nameCache.setText(name);
|
2011-11-13 09:46:19 +01:00
|
|
|
m_name = name;
|
2011-08-15 23:02:52 +02:00
|
|
|
}
|
2011-08-29 05:05:57 +02:00
|
|
|
|
2011-11-13 09:46:19 +01:00
|
|
|
void Creature::setHealthPercent(uint8 healthPercent)
|
2011-08-29 05:05:57 +02:00
|
|
|
{
|
2012-03-20 16:17:10 +01:00
|
|
|
m_informationColor = Color::black;
|
2011-08-29 05:05:57 +02:00
|
|
|
|
2011-11-13 09:46:19 +01:00
|
|
|
if(healthPercent > 92) {
|
2011-08-29 05:05:57 +02:00
|
|
|
m_informationColor.setGreen(188);
|
|
|
|
}
|
2011-11-13 09:46:19 +01:00
|
|
|
else if(healthPercent > 60) {
|
2011-08-29 05:05:57 +02:00
|
|
|
m_informationColor.setRed(80);
|
|
|
|
m_informationColor.setGreen(161);
|
|
|
|
m_informationColor.setBlue(80);
|
|
|
|
}
|
2011-11-13 09:46:19 +01:00
|
|
|
else if(healthPercent > 30) {
|
2011-08-29 05:05:57 +02:00
|
|
|
m_informationColor.setRed(161);
|
|
|
|
m_informationColor.setGreen(161);
|
|
|
|
}
|
2011-11-13 09:46:19 +01:00
|
|
|
else if(healthPercent > 8) {
|
2011-08-29 05:05:57 +02:00
|
|
|
m_informationColor.setRed(160);
|
|
|
|
m_informationColor.setGreen(39);
|
|
|
|
m_informationColor.setBlue(39);
|
|
|
|
}
|
2011-11-13 09:46:19 +01:00
|
|
|
else if(healthPercent > 3) {
|
2011-08-29 05:05:57 +02:00
|
|
|
m_informationColor.setRed(160);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_informationColor.setRed(79);
|
|
|
|
}
|
2011-11-13 09:46:19 +01:00
|
|
|
|
|
|
|
m_healthPercent = healthPercent;
|
2011-08-29 05:05:57 +02:00
|
|
|
}
|
2011-11-06 22:45:42 +01:00
|
|
|
|
2011-11-13 09:46:19 +01:00
|
|
|
void Creature::setDirection(Otc::Direction direction)
|
2011-11-06 22:45:42 +01:00
|
|
|
{
|
2012-05-10 00:19:05 +02:00
|
|
|
assert(direction != Otc::InvalidDirection);
|
2011-11-13 09:46:19 +01:00
|
|
|
m_direction = direction;
|
|
|
|
}
|
|
|
|
|
2011-12-02 03:29:05 +01:00
|
|
|
void Creature::setOutfit(const Outfit& outfit)
|
|
|
|
{
|
2012-05-19 03:41:22 +02:00
|
|
|
m_walkAnimationPhase = 0; // might happen when player is walking and outfit is changed.
|
2012-01-24 19:39:16 +01:00
|
|
|
m_outfit = outfit;
|
2012-02-02 17:37:52 +01:00
|
|
|
m_type = g_thingsType.getThingType(outfit.getId(), outfit.getCategory());
|
2011-12-02 03:29:05 +01:00
|
|
|
}
|
|
|
|
|
2012-01-07 00:54:17 +01:00
|
|
|
void Creature::setSkull(uint8 skull)
|
|
|
|
{
|
|
|
|
m_skull = skull;
|
2012-02-08 23:58:27 +01:00
|
|
|
callLuaField("onSkullChange", m_skull);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::setShield(uint8 shield)
|
|
|
|
{
|
|
|
|
m_shield = shield;
|
2012-02-08 23:58:27 +01:00
|
|
|
callLuaField("onShieldChange", m_shield);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::setEmblem(uint8 emblem)
|
|
|
|
{
|
|
|
|
m_emblem = emblem;
|
2012-02-08 23:58:27 +01:00
|
|
|
callLuaField("onEmblemChange", m_emblem);
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::setSkullTexture(const std::string& filename)
|
|
|
|
{
|
|
|
|
m_skullTexture = g_textures.getTexture(filename);
|
|
|
|
}
|
|
|
|
|
2012-01-11 23:31:23 +01:00
|
|
|
void Creature::setShieldTexture(const std::string& filename, bool blink)
|
2012-01-07 00:54:17 +01:00
|
|
|
{
|
|
|
|
m_shieldTexture = g_textures.getTexture(filename);
|
2012-01-11 23:31:23 +01:00
|
|
|
m_showShieldTexture = true;
|
|
|
|
|
|
|
|
if(blink && !m_shieldBlink) {
|
|
|
|
auto self = asCreature();
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.scheduleEvent([self]() {
|
2012-01-11 23:31:23 +01:00
|
|
|
self->updateShield();
|
|
|
|
}, SHIELD_BLINK_TICKS);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shieldBlink = blink;
|
2012-01-07 00:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::setEmblemTexture(const std::string& filename)
|
|
|
|
{
|
|
|
|
m_emblemTexture = g_textures.getTexture(filename);
|
|
|
|
}
|
|
|
|
|
2012-02-08 03:11:57 +01:00
|
|
|
void Creature::addTimedSquare(uint8 color)
|
2012-01-05 15:24:38 +01:00
|
|
|
{
|
2012-02-08 03:11:57 +01:00
|
|
|
m_showTimedSquare = true;
|
|
|
|
m_timedSquareColor = Color::from8bit(color);
|
2012-01-05 15:24:38 +01:00
|
|
|
|
|
|
|
// schedule removal
|
|
|
|
auto self = asCreature();
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.scheduleEvent([self]() {
|
2012-02-08 03:11:57 +01:00
|
|
|
self->removeTimedSquare();
|
2012-01-05 15:24:38 +01:00
|
|
|
}, VOLATILE_SQUARE_DURATION);
|
|
|
|
}
|
|
|
|
|
2012-01-11 23:31:23 +01:00
|
|
|
void Creature::updateShield()
|
|
|
|
{
|
|
|
|
m_showShieldTexture = !m_showShieldTexture;
|
|
|
|
|
|
|
|
if(m_shield != Otc::ShieldNone && m_shieldBlink) {
|
|
|
|
auto self = asCreature();
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.scheduleEvent([self]() {
|
2012-01-11 23:31:23 +01:00
|
|
|
self->updateShield();
|
|
|
|
}, SHIELD_BLINK_TICKS);
|
|
|
|
}
|
|
|
|
else if(!m_shieldBlink)
|
|
|
|
m_showShieldTexture = true;
|
|
|
|
}
|
|
|
|
|
2012-02-02 22:20:34 +01:00
|
|
|
Point Creature::getDrawOffset()
|
|
|
|
{
|
|
|
|
Point drawOffset;
|
|
|
|
if(m_walking) {
|
|
|
|
if(m_walkingTile)
|
|
|
|
drawOffset -= Point(1,1) * m_walkingTile->getDrawElevation();
|
|
|
|
drawOffset += m_walkOffset;
|
|
|
|
} else {
|
|
|
|
const TilePtr& tile = getTile();
|
|
|
|
if(tile)
|
|
|
|
drawOffset -= Point(1,1) * tile->getDrawElevation();
|
|
|
|
}
|
|
|
|
return drawOffset;
|
|
|
|
}
|