KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
animation.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26
27#include "../world/object.hpp"
28#include "../engine/engine.hpp"
29#include "../engine/time/invocation.hpp"
30
37{
38public:
40 struct Instruction
41 {
42 enum class Type : uint8_t
43 {
44 ParentSetPosition, // Set position of the `Object`
45 ParentMove, // Move the `Object` (`Object::Move()`)
46 TextureSet, // Hide all other `Texture`s and show the given one
47 TextureSetPosition, // Set position of the given `Texture`
48 TextureMove, // Add to the position of the given `Texture`
49 TextureShow, // Show the given `Texture` (`Texture::visible = true`)
50 TextureHide, // Hide the given `Texture` (`Texture::visible = false`)
51 Delay
52 };
53
54 Type type;
55 size_t intData;
56 union // In all cases, it may hold only one of these:
57 {
58 Point pointData;
59 Time::Measurement timeMeasurement;
60 };
61
62 // TextureSet, TextureSetPosition, TextureShow, TextureHide
63 inline Instruction(Type type, size_t textureIndex, Point pointData = Point(0, 0))
64 : type(type), intData(textureIndex), pointData(pointData) {}
65
66 // ParentSetPosition, ParentMove
67 inline Instruction(Type type, Point pointData)
68 : type(type), pointData(pointData) {}
69
70 // Delay
71 inline Instruction(Type type, size_t time, Time::Measurement timeMeasurement)
72 : type(type), intData(time), timeMeasurement(timeMeasurement) {}
73 };
74
76
77 Animation(Engine& engine, const ID<Object>& object, const std::vector<Instruction>& instructions);
78 ~Animation();
79
80 auto Play() -> bool;
81 void Stop();
82
83private:
84 ID<Object> m_object;
85 std::vector<Instruction> m_instructions;
86 size_t m_i = 0;
87 Time::Invocation m_invocation;
88};
Wrapper for animating an Objects and its Textures.
Definition animation.hpp:37
void Stop()
Stop animation by canceling the invoked instructions, and prepare for a re-Play().
Definition animation.cpp:134
Animation(Engine &engine, const ID< Object > &object, const std::vector< Instruction > &instructions)
Construct an Animation, that will not play just yet.
Definition animation.cpp:36
~Animation()
Safely cancels invoked animation instructions.
Definition animation.cpp:40
auto Play() -> bool
Play the Animation.
Definition animation.cpp:52
Engine & engine
Parent Engine
Definition animation.hpp:75
Complete engine containing all engine components.
Definition engine.hpp:41
Measurement
Time measurement.
Definition time.hpp:39
Serializable world structure identifier.
Definition id.hpp:38
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
Calls a function after a given time.
Definition invocation.hpp:68