Brunot
Loading...
Searching...
No Matches
ActionList.h
Go to the documentation of this file.
1
10// ____ __ __ __
11// /\__ _\/\ \ /\ \/\ \
12// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
13// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
14// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
15// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
16// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
17
18
19
20#pragma once
21#include "Framework/Component.h"
22
23#include "Actions/Action.h"
24
29class ActionList : public Component
30{
31public:
32 static auto getEnum() -> ComponentTypeEnum
33 {
34 return cActionList;
35 }
36 static auto bindLuaTypes(sol::state& lua) -> void {}
37
38 ActionList();
39 ActionList(const ActionList&);
40 auto operator=(const ActionList&) -> ActionList&;
41 ~ActionList() override = default;
42 ActionList(ActionList&& other) noexcept = default;
43 auto operator=(ActionList&& other) noexcept -> ActionList& = default;
44
45 auto operator[](int index) const -> Action&;
46
47 auto parentTo(GameObject* newParent) -> bool override;
48 auto clone() const -> std::unique_ptr<GameObject> override;
49 auto componentShowMenu() -> void override; // for debug ImGUI
50 auto onUpdate(float dt) -> void override;
51 auto endWindow() -> void override; // for debug ImGUI
52 auto onRender() -> void override;
53 auto load(const Stream& stream) -> void override;
54 auto save(Stream& stream) const -> void override;
55
56 friend auto to_json(json& j, const ActionList& a) -> void;
57 friend auto from_json(const json& j, ActionList& a) -> void;
58
66 auto getAction(const std::string& targetAction) -> Action*;
67
74 auto removeAction(const std::string& targetAction) -> void;
75
76 auto front() const -> Action*;
77 auto back() const -> Action*;
78
79 auto pushBack(const std::unique_ptr<Action>& newAction) -> void;
80 auto pushFront(const std::unique_ptr<Action>& newAction) -> void;
81
82 auto clear() -> void;
83
84 template <typename ActionType>
85 auto pushBack(const ActionType& newAction) -> void
86 {
87 allActions.emplace_back(std::make_unique<ActionType>(newAction));
88 allActions.back()->setOwner(this->getEntityParent());
89 }
90
91 template <typename ActionType>
92 auto pushFront(const ActionType& newAction) -> void
93 {
94 allActions.insert(allActions.begin(),std::make_unique<ActionType>(newAction));
95 allActions.front()->setOwner(this->getEntityParent());
96 }
97
98 template <typename ActionType>
99 auto getAction(const std::string& actionName) -> ActionType*
100 {
101 auto isPresent = std::ranges::find_if(allActions, [&actionName](const auto& action)
102 {
103 return action->getName() == actionName;
104 });
105
106 if (isPresent != allActions.end())
107 {
108 return dynamic_cast<ActionType*>(isPresent->get());
109 }
110
111 return nullptr;
112 }
113
114
115private:
116 std::vector<std::unique_ptr<Action>> allActions;
117
118 // Lambda for checking if an action has finished
119 inline static auto HasActionExpired = [](const auto& item)
120 {
121 return item->shouldRemove();
122 };
123};
A class that changes a variable over a set duration.
sys::Json Stream
Definition AudioObject.h:20
The base class for components, holding all of their shared All components should inherit from this.
nlohmann::json json
Definition Json.cpp:19
Component that stores all actions currently affecting the parent entity.
Definition ActionList.h:30
auto front() const -> Action *
Definition ActionList.cpp:195
auto getAction(const std::string &actionName) -> ActionType *
Definition ActionList.h:99
friend auto to_json(json &j, const ActionList &a) -> void
Definition ActionList.cpp:155
auto removeAction(const std::string &targetAction) -> void
Removes the first action of a specific type.
Definition ActionList.cpp:182
auto parentTo(GameObject *newParent) -> bool override
Sets the GameObject as a child of another GameObject.
Definition ActionList.cpp:62
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition ActionList.cpp:145
auto onRender() -> void override
called every frame after update has been called for every object.
Definition ActionList.cpp:141
std::vector< std::unique_ptr< Action > > allActions
Definition ActionList.h:116
auto componentShowMenu() -> void override
Definition ActionList.cpp:79
auto pushBack(const std::unique_ptr< Action > &newAction) -> void
Definition ActionList.cpp:205
auto getAction(const std::string &targetAction) -> Action *
Returns the first action of a specific type.
Definition ActionList.cpp:167
ActionList()
Definition ActionList.cpp:26
static auto bindLuaTypes(sol::state &lua) -> void
Definition ActionList.h:36
auto onUpdate(float dt) -> void override
called once every frame.
Definition ActionList.cpp:121
auto operator=(const ActionList &) -> ActionList &
Definition ActionList.cpp:41
auto clear() -> void
Definition ActionList.cpp:217
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition ActionList.cpp:150
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition ActionList.cpp:137
auto operator=(ActionList &&other) noexcept -> ActionList &=default
static auto getEnum() -> ComponentTypeEnum
Definition ActionList.h:32
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition ActionList.cpp:74
auto back() const -> Action *
Definition ActionList.cpp:200
ActionList(ActionList &&other) noexcept=default
friend auto from_json(const json &j, ActionList &a) -> void
Definition ActionList.cpp:162
auto operator[](int index) const -> Action &
Definition ActionList.cpp:52
auto pushFront(const ActionType &newAction) -> void
Definition ActionList.h:92
static auto HasActionExpired
Definition ActionList.h:119
~ActionList() override=default
auto pushFront(const std::unique_ptr< Action > &newAction) -> void
Definition ActionList.cpp:211
Definition Action.h:131
ComponentTypeEnum
This enum lists out every type of component we have.
Definition Component.h:39
Component(ComponentTypeEnum type, const char *typeName)
Definition Component.cpp:72
auto getEntityParent() const -> Entity *
gets the parent as an Entity.
Definition Component.cpp:106
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24