2011-08-28 15:17:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
|
|
|
|
*
|
|
|
|
* 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-15 21:15:49 +02:00
|
|
|
#include "datmanager.h"
|
2011-08-26 07:07:23 +02:00
|
|
|
#include "localplayer.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include <framework/platform/platform.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
#include <framework/graphics/graphics.h>
|
2011-08-21 05:21:35 +02:00
|
|
|
#include <framework/graphics/fontmanager.h>
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
Creature::Creature() : Thing(Otc::Creature)
|
2011-08-15 16:11:24 +02:00
|
|
|
{
|
2011-08-15 23:02:52 +02:00
|
|
|
m_healthPercent = 0;
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::South;
|
2011-08-26 07:07:23 +02:00
|
|
|
m_animation = 0;
|
|
|
|
|
|
|
|
m_walking = false;
|
|
|
|
m_walkOffsetX = 0;
|
|
|
|
m_walkOffsetY = 0;
|
|
|
|
m_lastTicks = g_platform.getTicks();
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Creature::draw(int x, int y)
|
|
|
|
{
|
2011-08-26 07:07:23 +02:00
|
|
|
//dump << (int)m_speed;
|
|
|
|
|
|
|
|
// gspeed = 100
|
|
|
|
// pspeed = 234
|
|
|
|
// (recorded) 400 - 866 = 466
|
|
|
|
// (calc by ot eq) 1000 * 100 / 234 = 427
|
|
|
|
|
|
|
|
// gspeed = 150
|
|
|
|
// pspeed = 234
|
|
|
|
// (recorded) 934 - 1597 = 663
|
|
|
|
// (calc by ot eq) 1000 * 150 / 234 = 641
|
|
|
|
|
|
|
|
// gspeed = 100
|
|
|
|
// pspeed = 110
|
|
|
|
// (recorded) 900 - 1833 = 933
|
|
|
|
// (calc by ot eq) 1000 * 100 / 110 = 909
|
|
|
|
|
|
|
|
// 1000 * groundSpeed / playerSpeed
|
|
|
|
|
2011-08-26 17:44:29 +02:00
|
|
|
// TODO 1: FIX RENDER STEP 2
|
|
|
|
// TODO 2: FIX SHAKY EFFECT
|
|
|
|
// TODO 3: ADD ANIMATION
|
2011-08-26 17:48:23 +02:00
|
|
|
// TODO 4: ADD DIAGONAL WALKING
|
2011-08-15 16:11:24 +02:00
|
|
|
|
2011-08-19 14:26:26 +02:00
|
|
|
const ThingAttributes& attributes = getAttributes();
|
2011-08-26 07:07:23 +02:00
|
|
|
|
|
|
|
// we must walk 32 pixels in m_speed miliseconds
|
|
|
|
if(m_walking && attributes.animcount > 1) {
|
2011-08-26 17:44:29 +02:00
|
|
|
double offset = (32.0 / m_walkTime) * (g_platform.getTicks() - m_lastTicks);
|
2011-08-26 07:07:23 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
if(m_direction == Otc::North)
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetY = std::max(m_walkOffsetY - offset, 0.0);
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(m_direction == Otc::East)
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetX = std::min(m_walkOffsetX + offset, 0.0);
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(m_direction == Otc::South)
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetY = std::min(m_walkOffsetY + offset, 0.0);
|
2011-08-28 18:02:26 +02:00
|
|
|
else if(m_direction == Otc::West)
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetX = std::max(m_walkOffsetX - offset, 0.0);
|
2011-08-26 07:07:23 +02:00
|
|
|
|
|
|
|
/*if(g_platform.getTicks() - m_lastTicks > m_speed / 4) {
|
|
|
|
if(m_animation+1 == attributes.animcount)
|
|
|
|
m_animation = 1;
|
|
|
|
else
|
|
|
|
m_animation++;
|
|
|
|
|
|
|
|
m_lastTicks = g_platform.getTicks();
|
|
|
|
}*/
|
|
|
|
|
2011-08-26 17:44:29 +02:00
|
|
|
if(((m_walkOffsetX == 0 && m_walkOffsetY == 0) && m_walkOffsetX != m_walkOffsetY) ||
|
|
|
|
((m_walkOffsetX == 0 || m_walkOffsetY == 0) && m_walkOffsetX == m_walkOffsetY)) {
|
2011-08-26 07:07:23 +02:00
|
|
|
m_walking = false;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetX = 0;
|
2011-08-26 07:07:23 +02:00
|
|
|
m_walkOffsetY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//m_lastTicks = g_platform.getTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lastTicks = g_platform.getTicks();
|
|
|
|
|
|
|
|
x += m_walkOffsetX;
|
|
|
|
y += m_walkOffsetY;
|
|
|
|
|
|
|
|
|
2011-08-24 05:58:23 +02:00
|
|
|
for(int ydiv = 0; ydiv < attributes.ydiv; ydiv++) {
|
|
|
|
|
|
|
|
// continue if we dont have this addon.
|
|
|
|
if(ydiv > 0 && !(m_outfit.addons & (1 << (ydiv-1))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// draw white item
|
2011-08-26 07:07:23 +02:00
|
|
|
internalDraw(x, y, 0, m_direction, ydiv, 0, m_animation);
|
2011-08-24 05:58:23 +02:00
|
|
|
|
|
|
|
// draw mask if exists
|
|
|
|
if(attributes.blendframes > 1) {
|
2011-08-28 20:06:47 +02:00
|
|
|
// switch to blend color mode
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindBlendFunc(Fw::BlendColorzing);
|
2011-08-19 14:26:26 +02:00
|
|
|
|
2011-08-28 20:06:47 +02:00
|
|
|
// head
|
|
|
|
g_graphics.bindColor(Otc::OutfitColors[m_outfit.head]);
|
|
|
|
internalDraw(x, y, 1, m_direction, ydiv, 0, m_animation, Otc::SpriteYellowMask);
|
2011-08-24 05:58:23 +02:00
|
|
|
|
2011-08-28 20:06:47 +02:00
|
|
|
// body
|
|
|
|
g_graphics.bindColor(Otc::OutfitColors[m_outfit.body]);
|
|
|
|
internalDraw(x, y, 1, m_direction, ydiv, 0, m_animation, Otc::SpriteRedMask);
|
2011-08-24 05:58:23 +02:00
|
|
|
|
2011-08-28 20:06:47 +02:00
|
|
|
// legs
|
|
|
|
g_graphics.bindColor(Otc::OutfitColors[m_outfit.legs]);
|
|
|
|
internalDraw(x, y, 1, m_direction, ydiv, 0, m_animation, Otc::SpriteGreenMask);
|
2011-08-24 05:58:23 +02:00
|
|
|
|
2011-08-28 20:06:47 +02:00
|
|
|
// feet
|
|
|
|
g_graphics.bindColor(Otc::OutfitColors[m_outfit.feet]);
|
|
|
|
internalDraw(x, y, 1, m_direction, ydiv, 0, m_animation, Otc::SpriteBlueMask);
|
|
|
|
|
|
|
|
// restore default blend func
|
|
|
|
g_graphics.bindBlendFunc(Fw::BlendDefault);
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindColor(Fw::white);
|
2011-08-24 05:58:23 +02:00
|
|
|
}
|
2011-08-19 14:26:26 +02:00
|
|
|
}
|
2011-08-21 05:21:35 +02:00
|
|
|
}
|
2011-08-19 15:23:35 +02:00
|
|
|
|
2011-08-21 21:09:23 +02:00
|
|
|
void Creature::drawInformation(int x, int y, bool useGray)
|
2011-08-21 05:21:35 +02:00
|
|
|
{
|
2011-08-21 21:09:23 +02:00
|
|
|
Color fillColor = Color(96, 96, 96);
|
|
|
|
|
|
|
|
if(!useGray) {
|
2011-08-26 07:07:23 +02:00
|
|
|
// health bar according to yatc
|
2011-08-28 18:02:26 +02:00
|
|
|
fillColor = Fw::black;
|
2011-08-26 07:07:23 +02:00
|
|
|
if(m_healthPercent > 92) {
|
|
|
|
fillColor.setGreen(188);
|
|
|
|
}
|
|
|
|
else if(m_healthPercent > 60) {
|
|
|
|
fillColor.setRed(80);
|
|
|
|
fillColor.setGreen(161);
|
|
|
|
fillColor.setBlue(80);
|
|
|
|
}
|
|
|
|
else if(m_healthPercent > 30) {
|
|
|
|
fillColor.setRed(161);
|
|
|
|
fillColor.setGreen(161);
|
|
|
|
}
|
|
|
|
else if(m_healthPercent > 8) {
|
|
|
|
fillColor.setRed(160);
|
|
|
|
fillColor.setGreen(39);
|
|
|
|
fillColor.setBlue(39);
|
|
|
|
}
|
|
|
|
else if(m_healthPercent > 3) {
|
|
|
|
fillColor.setRed(160);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fillColor.setRed(79);
|
2011-08-21 21:09:23 +02:00
|
|
|
}
|
2011-08-19 15:23:35 +02:00
|
|
|
}
|
|
|
|
|
2011-08-22 03:01:59 +02:00
|
|
|
Rect backgroundRect = Rect(x-(14.5), y, 27, 4);
|
2011-08-21 21:09:23 +02:00
|
|
|
Rect healthRect = backgroundRect.expanded(-1);
|
|
|
|
healthRect.setWidth((m_healthPercent/100.0)*25);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
g_graphics.bindColor(Fw::black);
|
2011-08-21 21:09:23 +02:00
|
|
|
g_graphics.drawFilledRect(backgroundRect);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-21 21:09:23 +02:00
|
|
|
g_graphics.bindColor(fillColor);
|
|
|
|
g_graphics.drawFilledRect(healthRect);
|
2011-08-20 22:30:41 +02:00
|
|
|
|
2011-08-21 05:21:35 +02:00
|
|
|
// name
|
2011-08-21 21:09:23 +02:00
|
|
|
FontPtr font = g_fonts.getFont("tibia-12px-rounded");
|
2011-08-28 18:02:26 +02:00
|
|
|
font->renderText(m_name, Rect(x-100, y-15, 200, 15), Fw::AlignTopCenter, fillColor);
|
2011-08-15 16:11:24 +02:00
|
|
|
}
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2011-08-26 07:07:23 +02:00
|
|
|
void Creature::walk(const Position& position)
|
|
|
|
{
|
2011-08-26 17:44:29 +02:00
|
|
|
// set walking state
|
2011-08-26 07:07:23 +02:00
|
|
|
m_walking = true;
|
|
|
|
m_walkOffsetX = 0;
|
|
|
|
m_walkOffsetY = 0;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkingFromPosition = m_position;
|
|
|
|
|
|
|
|
// update map tiles
|
|
|
|
g_map.removeThingByPtr(asThing());
|
|
|
|
m_position = position;
|
|
|
|
g_map.addThing(asThing());
|
2011-08-26 07:07:23 +02:00
|
|
|
|
2011-08-26 17:44:29 +02:00
|
|
|
// set new direction
|
|
|
|
if(m_walkingFromPosition + Position(0, -1, 0) == m_position) {
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::North;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetY = 32;
|
|
|
|
}
|
|
|
|
else if(m_walkingFromPosition + Position(1, 0, 0) == m_position) {
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::East;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetX = -32;
|
|
|
|
}
|
|
|
|
else if(m_walkingFromPosition + Position(0, 1, 0) == m_position) {
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::South;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetY = -32;
|
|
|
|
}
|
|
|
|
else if(m_walkingFromPosition + Position(-1, 0, 0) == m_position) {
|
2011-08-28 18:02:26 +02:00
|
|
|
m_direction = Otc::West;
|
2011-08-26 17:44:29 +02:00
|
|
|
m_walkOffsetX = 32;
|
|
|
|
}
|
2011-08-26 07:07:23 +02:00
|
|
|
else { // Teleport
|
2011-08-26 17:44:29 +02:00
|
|
|
// we teleported, dont walk or change direction
|
2011-08-26 07:07:23 +02:00
|
|
|
m_walking = false;
|
|
|
|
}
|
2011-08-26 17:44:29 +02:00
|
|
|
|
|
|
|
// get walk speed
|
|
|
|
int groundSpeed = 0;
|
|
|
|
|
|
|
|
ThingPtr ground = g_map.getThing(m_position, 0);
|
|
|
|
if(ground)
|
|
|
|
groundSpeed = ground->getAttributes().speed;
|
|
|
|
|
|
|
|
m_walkTime = 1000.0 * (float)groundSpeed / m_speed;
|
|
|
|
m_walkTime = m_walkTime == 0 ? 1000 : m_walkTime;
|
2011-08-26 07:07:23 +02:00
|
|
|
}
|
|
|
|
|
2011-08-15 23:02:52 +02:00
|
|
|
const ThingAttributes& Creature::getAttributes()
|
|
|
|
{
|
|
|
|
return g_dat.getCreatureAttributes(m_outfit.type);
|
|
|
|
}
|