2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CCreatureAnimation.h, 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
|
|
|
|
*
|
2009-04-16 14:14:13 +03:00
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../../lib/FunctionList.h"
|
|
|
|
#include "../widgets/Images.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "../render/CAnimation.h"
|
|
|
|
#include "../render/IImage.h"
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2023-01-30 19:55:32 +02:00
|
|
|
#include <SDL_pixels.h>
|
2023-01-30 00:12:43 +02:00
|
|
|
|
2011-12-17 21:59:59 +03:00
|
|
|
class CIntObject;
|
2022-12-09 13:26:17 +02:00
|
|
|
class CreatureAnimation;
|
2022-12-11 22:09:57 +02:00
|
|
|
class Canvas;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
/// Namespace for some common controls of animations
|
|
|
|
namespace AnimationControls
|
|
|
|
{
|
|
|
|
/// get SDL_Color for creature selection borders
|
|
|
|
SDL_Color getBlueBorder();
|
|
|
|
SDL_Color getGoldBorder();
|
|
|
|
SDL_Color getNoBorder();
|
|
|
|
|
2023-01-28 16:49:35 +02:00
|
|
|
/// returns animation speed factor according to game settings,
|
|
|
|
/// slow speed is considered to be "base speed" and will return 1.0
|
|
|
|
float getAnimationSpeedFactor();
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
/// creates animation object with preset speed control
|
2022-12-09 13:26:17 +02:00
|
|
|
std::shared_ptr<CreatureAnimation> getAnimation(const CCreature * creature);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
/// returns animation speed of specific group, taking in mind game setting (in frames per second)
|
2022-12-22 01:04:58 +02:00
|
|
|
float getCreatureAnimationSpeed(const CCreature * creature, const CreatureAnimation * anim, ECreatureAnimType groupID);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2023-01-27 23:16:02 +02:00
|
|
|
/// returns how far projectile should move per second, in pixels per second
|
2013-07-06 19:10:20 +03:00
|
|
|
float getProjectileSpeed();
|
|
|
|
|
2023-01-27 23:16:02 +02:00
|
|
|
/// returns how far projectile should move per second, in pixels per second
|
|
|
|
float getRayProjectileSpeed();
|
|
|
|
|
|
|
|
/// returns speed of catapult projectile, in pixels per second, on a straight line, without parabola correction
|
2022-12-01 22:06:42 +02:00
|
|
|
float getCatapultSpeed();
|
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
/// returns speed of any spell effects, including any special effects like morale (in frames per second)
|
|
|
|
float getSpellEffectSpeed();
|
|
|
|
|
2023-01-27 23:16:02 +02:00
|
|
|
/// returns speed of movement animation across the screen, in tiles per second
|
|
|
|
float getMovementDistance(const CCreature * creature);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2023-01-27 23:16:02 +02:00
|
|
|
/// returns speed of movement animation across the screen, in pixels per seconds
|
2013-07-06 19:10:20 +03:00
|
|
|
float getFlightDistance(const CCreature * creature);
|
2022-12-14 12:04:37 +02:00
|
|
|
|
|
|
|
/// Returns total time for full fade-in effect on newly summoned creatures, in seconds
|
|
|
|
float getFadeInDuration();
|
2022-12-21 17:02:53 +02:00
|
|
|
|
|
|
|
/// Returns animation speed for obstacles, in frames per second
|
|
|
|
float getObstaclesSpeed();
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
2010-08-17 17:58:13 +03:00
|
|
|
|
2011-02-22 13:52:36 +02:00
|
|
|
/// Class which manages animations of creatures/units inside battles
|
2013-07-06 19:10:20 +03:00
|
|
|
/// TODO: split into constant image container and class that does *control* of animation
|
2022-12-09 13:26:17 +02:00
|
|
|
class CreatureAnimation : public CIntObject
|
2009-04-16 14:14:13 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
public:
|
2022-12-22 01:04:58 +02:00
|
|
|
typedef std::function<float(CreatureAnimation *, ECreatureAnimType)> TSpeedController;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2009-04-16 14:14:13 +03:00
|
|
|
private:
|
2017-09-05 19:45:29 +02:00
|
|
|
std::string name;
|
2022-12-11 23:16:23 +02:00
|
|
|
|
|
|
|
/// animation for rendering stack in default orientation - facing right
|
2017-09-05 17:59:26 +02:00
|
|
|
std::shared_ptr<CAnimation> forward;
|
2022-12-11 23:16:23 +02:00
|
|
|
|
|
|
|
/// animation that has all its frames flipped for rendering stack facing left
|
2017-09-05 17:59:26 +02:00
|
|
|
std::shared_ptr<CAnimation> reverse;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2017-09-05 17:59:26 +02:00
|
|
|
int fullWidth;
|
|
|
|
int fullHeight;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// speed of animation, measure in frames per second
|
2013-07-06 19:10:20 +03:00
|
|
|
float speed;
|
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// currently displayed frame. Float to allow H3-style animations where frames
|
|
|
|
/// don't display for integer number of frames
|
2013-07-06 19:10:20 +03:00
|
|
|
float currentFrame;
|
2023-03-20 17:48:52 +02:00
|
|
|
float animationEnd;
|
2022-12-11 23:16:23 +02:00
|
|
|
|
|
|
|
/// cumulative, real-time duration of animation. Used for effects like selection border
|
2013-07-06 19:10:20 +03:00
|
|
|
float elapsedTime;
|
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
///type of animation being displayed
|
2022-12-22 01:04:58 +02:00
|
|
|
ECreatureAnimType type;
|
2022-12-11 23:16:23 +02:00
|
|
|
|
2022-12-14 12:04:37 +02:00
|
|
|
/// current value of shadow transparency
|
|
|
|
uint8_t shadowAlpha;
|
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// border color, disabled if alpha = 0
|
2013-07-06 19:10:20 +03:00
|
|
|
SDL_Color border;
|
|
|
|
|
|
|
|
TSpeedController speedController;
|
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// animation will be played once and the reset to idling
|
|
|
|
bool once;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
void endAnimation();
|
2011-03-22 15:19:07 +02:00
|
|
|
|
2022-12-14 12:04:37 +02:00
|
|
|
void genSpecialPalette(IImage::SpecialPalette & target);
|
2011-03-22 15:19:07 +02:00
|
|
|
public:
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// function(s) that will be called when animation ends, after reset to 1st frame
|
|
|
|
/// NOTE that these functions will be fired only once
|
2013-07-06 19:10:20 +03:00
|
|
|
CFunctionList<void()> onAnimationReset;
|
|
|
|
|
|
|
|
int getWidth() const;
|
|
|
|
int getHeight() const;
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
/// name - path to .def file, relative to SPRITES/ directory
|
|
|
|
/// controller - function that will return for how long *each* frame
|
|
|
|
/// in specified group of animation should be played, measured in seconds
|
2022-12-09 13:26:17 +02:00
|
|
|
CreatureAnimation(const std::string & name_, TSpeedController speedController);
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// sets type of animation and resets framecount
|
2022-12-22 01:04:58 +02:00
|
|
|
void setType(ECreatureAnimType type);
|
2022-12-11 23:16:23 +02:00
|
|
|
|
|
|
|
/// returns currently rendered type of animation
|
2022-12-22 01:04:58 +02:00
|
|
|
ECreatureAnimType getType() const;
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2022-12-15 23:24:03 +02:00
|
|
|
void nextFrame(Canvas & canvas, const ColorFilter & shifter, bool facingRight);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// should be called every frame, return true when animation was reset to beginning
|
2013-07-06 19:10:20 +03:00
|
|
|
bool incrementFrame(float timePassed);
|
2022-12-11 23:16:23 +02:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void setBorderColor(SDL_Color palette);
|
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// Gets the current frame ID within current group.
|
|
|
|
float getCurrentFrame() const;
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// plays once given type of animation, then resets to idle
|
2022-12-22 01:04:58 +02:00
|
|
|
void playOnce(ECreatureAnimType type);
|
2009-04-16 14:14:13 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// returns number of frames in selected animation type
|
2022-12-22 01:04:58 +02:00
|
|
|
int framesInGroup(ECreatureAnimType group) const;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2023-03-20 17:48:52 +02:00
|
|
|
void playUntil(size_t frameIndex);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2022-12-11 23:16:23 +02:00
|
|
|
/// helpers to classify current type of animation
|
2013-07-06 19:10:20 +03:00
|
|
|
bool isDead() const;
|
2022-11-28 22:35:38 +02:00
|
|
|
bool isDying() const;
|
|
|
|
bool isDeadOrDying() const;
|
2013-07-06 19:10:20 +03:00
|
|
|
bool isIdle() const;
|
2013-07-21 13:10:38 +03:00
|
|
|
bool isMoving() const;
|
|
|
|
bool isShooting() const;
|
2009-04-16 14:14:13 +03:00
|
|
|
};
|