2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CCreatureAnimation.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2008-06-07 20:16:52 +03:00
|
|
|
#include "CCreatureAnimation.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
#include "../gui/SDL_Extensions.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
|
|
|
|
static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
|
|
|
|
static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
SDL_Color AnimationControls::getBlueBorder()
|
|
|
|
{
|
|
|
|
return creatureBlueBorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Color AnimationControls::getGoldBorder()
|
|
|
|
{
|
|
|
|
return creatureGoldBorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Color AnimationControls::getNoBorder()
|
|
|
|
{
|
|
|
|
return creatureNoBorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCreatureAnimation * AnimationControls::getAnimation(const CCreature * creature)
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
|
2013-07-06 19:10:20 +03:00
|
|
|
return new CCreatureAnimation(creature->animDefName, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t group)
|
|
|
|
{
|
2013-07-17 01:59:39 +03:00
|
|
|
CCreatureAnim::EAnimType type = CCreatureAnim::EAnimType(group);
|
|
|
|
|
|
|
|
assert(creature->animation.walkAnimationTime != 0);
|
|
|
|
assert(creature->animation.attackAnimationTime != 0);
|
|
|
|
assert(anim->framesInGroup(type) != 0);
|
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
// possible new fields for creature format:
|
|
|
|
//split "Attack time" into "Shoot Time" and "Cast Time"
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
// a lot of arbitrary multipliers, mostly to make animation speed closer to H3
|
2013-07-17 01:59:39 +03:00
|
|
|
const float baseSpeed = 0.1;
|
|
|
|
const float speedMult = settings["battle"]["animationSpeed"].Float();
|
|
|
|
const float speed = baseSpeed / speedMult;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CCreatureAnim::MOVING:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 2 * creature->animation.walkAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::MOUSEON:
|
|
|
|
return baseSpeed;
|
2013-11-06 16:42:58 +03:00
|
|
|
case CCreatureAnim::HOLDING:
|
|
|
|
return baseSpeed * creature->animation.idleAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::SHOOT_UP:
|
|
|
|
case CCreatureAnim::SHOOT_FRONT:
|
|
|
|
case CCreatureAnim::SHOOT_DOWN:
|
|
|
|
case CCreatureAnim::CAST_UP:
|
|
|
|
case CCreatureAnim::CAST_FRONT:
|
|
|
|
case CCreatureAnim::CAST_DOWN:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 4 * creature->animation.attackAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
// as strange as it looks like "attackAnimationTime" does not affects melee attacks
|
2013-07-17 01:59:39 +03:00
|
|
|
// necessary because length of these animations must be same for all creatures for synchronization
|
2013-07-16 21:12:47 +03:00
|
|
|
case CCreatureAnim::ATTACK_UP:
|
|
|
|
case CCreatureAnim::ATTACK_FRONT:
|
|
|
|
case CCreatureAnim::ATTACK_DOWN:
|
2013-07-17 01:59:39 +03:00
|
|
|
case CCreatureAnim::HITTED:
|
2013-07-16 21:12:47 +03:00
|
|
|
case CCreatureAnim::DEFENCE:
|
|
|
|
case CCreatureAnim::DEATH:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 3 / anim->framesInGroup(type);
|
2013-07-16 21:12:47 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
case CCreatureAnim::TURN_L:
|
|
|
|
case CCreatureAnim::TURN_R:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed / 3;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::MOVE_START:
|
|
|
|
case CCreatureAnim::MOVE_END:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed / 3;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::DEAD:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationControls::getProjectileSpeed()
|
|
|
|
{
|
|
|
|
return settings["battle"]["animationSpeed"].Float() * 100;
|
|
|
|
}
|
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
float AnimationControls::getSpellEffectSpeed()
|
|
|
|
{
|
|
|
|
return settings["battle"]["animationSpeed"].Float() * 60;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
float AnimationControls::getMovementDuration(const CCreature * creature)
|
|
|
|
{
|
|
|
|
return settings["battle"]["animationSpeed"].Float() * 4 / creature->animation.walkAnimationTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationControls::getFlightDistance(const CCreature * creature)
|
|
|
|
{
|
|
|
|
return creature->animation.flightAnimationDistance * 200;
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
CCreatureAnim::EAnimType CCreatureAnimation::getType() const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
assert(type >= 0);
|
|
|
|
assert(framesInGroup(type) != 0);
|
|
|
|
|
2008-06-07 20:16:52 +03:00
|
|
|
this->type = type;
|
2013-07-06 19:10:20 +03:00
|
|
|
currentFrame = 0;
|
|
|
|
once = false;
|
|
|
|
|
|
|
|
play();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
CCreatureAnimation::CCreatureAnimation(std::string name, TSpeedController controller)
|
2017-09-05 17:59:26 +02:00
|
|
|
: speed(0.1),
|
|
|
|
currentFrame(0),
|
|
|
|
elapsedTime(0),
|
2013-07-07 11:27:27 +03:00
|
|
|
type(CCreatureAnim::HOLDING),
|
|
|
|
border(CSDL_Ext::makeColor(0, 0, 0, 0)),
|
2017-09-05 17:59:26 +02:00
|
|
|
speedController(controller),
|
|
|
|
once(false)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2017-09-05 17:59:26 +02:00
|
|
|
forward = std::make_shared<CAnimation>(name);
|
|
|
|
reverse = std::make_shared<CAnimation>(name);
|
2008-06-07 20:16:52 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
//todo: optimize
|
|
|
|
forward->preload();
|
|
|
|
reverse->preload();
|
|
|
|
reverse->verticalFlip();
|
2010-10-31 00:53:41 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
//TODO: get dimensions form CAnimation
|
|
|
|
IImage * first = forward->getImage(0, type, true);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
if(!first)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2017-09-05 17:59:26 +02:00
|
|
|
fullWidth = 0;
|
|
|
|
fullHeight = 0;
|
|
|
|
return;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2017-09-05 17:59:26 +02:00
|
|
|
fullWidth = first->width();
|
|
|
|
fullHeight = first->height();
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
// if necessary, add one frame into vcmi-only group DEAD
|
|
|
|
if(forward->size(CCreatureAnim::DEAD) == 0)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2017-09-05 17:59:26 +02:00
|
|
|
forward->duplicateImage(CCreatureAnim::DEATH, forward->size(CCreatureAnim::DEATH)-1, CCreatureAnim::DEAD);
|
|
|
|
reverse->duplicateImage(CCreatureAnim::DEATH, reverse->size(CCreatureAnim::DEATH)-1, CCreatureAnim::DEAD);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
play();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::endAnimation()
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
once = false;
|
|
|
|
auto copy = onAnimationReset;
|
|
|
|
onAnimationReset.clear();
|
|
|
|
copy();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
bool CCreatureAnimation::incrementFrame(float timePassed)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
elapsedTime += timePassed;
|
|
|
|
currentFrame += timePassed * speed;
|
|
|
|
if (currentFrame >= float(framesInGroup(type)))
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-16 21:12:47 +03:00
|
|
|
// just in case of extremely low fps (or insanely high speed)
|
2013-07-06 19:10:20 +03:00
|
|
|
while (currentFrame >= float(framesInGroup(type)))
|
|
|
|
currentFrame -= framesInGroup(type);
|
|
|
|
|
|
|
|
if (once)
|
|
|
|
setType(CCreatureAnim::HOLDING);
|
|
|
|
|
|
|
|
endAnimation();
|
|
|
|
return true;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
return false;
|
2008-08-07 03:38:44 +03:00
|
|
|
}
|
2008-09-24 19:25:14 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::setBorderColor(SDL_Color palette)
|
2008-09-24 19:25:14 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
border = palette;
|
2008-09-24 19:25:14 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
int CCreatureAnimation::getWidth() const
|
2011-05-26 17:47:45 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return fullWidth;
|
2011-05-26 17:47:45 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
int CCreatureAnimation::getHeight() const
|
2009-10-13 05:01:29 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return fullHeight;
|
2009-10-13 05:01:29 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
float CCreatureAnimation::getCurrentFrame() const
|
2009-03-05 19:57:26 +02:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return currentFrame;
|
2009-03-05 19:57:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
|
2009-02-21 17:43:28 +02:00
|
|
|
{
|
|
|
|
setType(type);
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
inline int getBorderStrength(float time)
|
|
|
|
{
|
2013-07-07 11:27:27 +03:00
|
|
|
float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
return borderStrength * 155 + 100; // scale to 0-255
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Color genShadow(ui8 alpha)
|
|
|
|
{
|
2013-07-07 11:27:27 +03:00
|
|
|
return CSDL_Ext::makeColor(0, 0, 0, alpha);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
|
|
|
|
{
|
2014-05-21 19:04:34 +03:00
|
|
|
return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
|
|
|
|
{
|
|
|
|
return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
|
|
|
|
{
|
2014-05-21 19:04:34 +03:00
|
|
|
return CSDL_Ext::makeColor(
|
|
|
|
mixChannels(over.r, base.r, over.a, base.a),
|
|
|
|
mixChannels(over.g, base.g, over.a, base.a),
|
|
|
|
mixChannels(over.b, base.b, over.a, base.a),
|
|
|
|
ui8(over.a + base.a * (255 - over.a) / 256)
|
|
|
|
);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
void CCreatureAnimation::genBorderPalette(IImage::BorderPallete & target)
|
2013-07-06 19:10:20 +03:00
|
|
|
{
|
2017-09-05 17:59:26 +02:00
|
|
|
target[0] = genBorderColor(getBorderStrength(elapsedTime), border);
|
|
|
|
target[1] = addColors(genShadow(128), genBorderColor(getBorderStrength(elapsedTime), border));
|
|
|
|
target[2] = addColors(genShadow(64), genBorderColor(getBorderStrength(elapsedTime), border));
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
2008-08-07 03:38:44 +03:00
|
|
|
|
2016-08-30 04:38:56 +02:00
|
|
|
|
2011-10-13 07:33:19 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
void CCreatureAnimation::nextFrame(SDL_Surface *dest, bool attacker)
|
|
|
|
{
|
|
|
|
size_t frame = floor(currentFrame);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
IImage * image = nullptr;
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
if(attacker)
|
|
|
|
image = forward->getImage(frame, type);
|
|
|
|
else
|
|
|
|
image = reverse->getImage(frame, type);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
IImage::BorderPallete borderPallete;
|
|
|
|
genBorderPalette(borderPallete);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
image->setBorderPallete(borderPallete);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
image->draw(dest, pos.x, pos.y);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2010-08-12 08:22:48 +03:00
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2017-09-05 17:59:26 +02:00
|
|
|
return forward->size(group);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
bool CCreatureAnimation::isDead() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::DEAD
|
|
|
|
|| getType() == CCreatureAnim::DEATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCreatureAnimation::isIdle() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::HOLDING
|
|
|
|
|| getType() == CCreatureAnim::MOUSEON;
|
|
|
|
}
|
|
|
|
|
2013-07-21 13:10:38 +03:00
|
|
|
bool CCreatureAnimation::isMoving() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::MOVE_START
|
|
|
|
|| getType() == CCreatureAnim::MOVING
|
|
|
|
|| getType() == CCreatureAnim::MOVE_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCreatureAnimation::isShooting() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::SHOOT_UP
|
|
|
|
|| getType() == CCreatureAnim::SHOOT_FRONT
|
|
|
|
|| getType() == CCreatureAnim::SHOOT_DOWN;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::pause()
|
|
|
|
{
|
|
|
|
speed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnimation::play()
|
|
|
|
{
|
2013-11-06 16:42:58 +03:00
|
|
|
speed = 0;
|
|
|
|
if (speedController(this, type) != 0)
|
|
|
|
speed = 1 / speedController(this, type);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|