nomlib
Loading...
Searching...
No Matches
IActionObject.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_ACTIONS_IACTION_OBJECT_HPP
30#define NOMLIB_ACTIONS_IACTION_OBJECT_HPP
31
32#include <functional>
33#include <vector>
34
35#include "nomlib/config.hpp"
36#include "nomlib/system/Timer.hpp"
37
38namespace nom {
39
41class IActionObject
42{
43 public:
44 typedef IActionObject self_type;
45
50 typedef
51 std::function<real32(real32, real32, real32, real32)> timing_curve_func;
52
61
63
64 virtual ~IActionObject();
65
67 const std::string& name() const;
68
72 real32 duration() const;
73
80 real32 speed() const;
81
88
90 void set_name(const std::string& action_id);
91
93 virtual void set_speed(real32 speed);
94
99
106 virtual std::unique_ptr<IActionObject> clone() const = 0;
107
117 virtual IActionObject::FrameState next_frame(real32 delta_time) = 0;
118
131 virtual IActionObject::FrameState prev_frame(real32 delta_time) = 0;
132
142 virtual void pause(real32 delta_time) = 0;
143
153 virtual void resume(real32 delta_time) = 0;
154
165 virtual void rewind(real32 delta_time) = 0;
166
174 virtual void release() = 0;
175
176 protected:
181
185 void set_duration(real32 seconds);
186
191
195 real32 elapsed_frames_ = 0.0f;
196
204
205 private:
207 std::string name_;
208 real32 duration_ = 0.0f;
209 real32 speed_ = 1.0f;
210 timing_curve_func timing_curve_ = nullptr;
211};
212
216typedef std::vector<std::shared_ptr<IActionObject>> action_list;
217
223template<typename ObjectType, typename... ObjectArgs>
224std::shared_ptr<ObjectType> create_action(ObjectArgs&&... args)
225{
226 // IMPORTANT: We risk object slicing if we use std::make_shared here! The
227 // problem occurs when the end-user tries to return the created action
228 // pointer by value.
229 return( std::shared_ptr<ObjectType>(
230 new ObjectType( std::forward<ObjectArgs>(args) ... ) ) );
231}
232
250template<typename ObjectType>
251std::shared_ptr<ObjectType>
253{
254 // IMPORTANT: We risk object slicing if we use std::make_shared here! The
255 // problem occurs when the end-user tries to return the created action
256 // pointer by value.
257 return( std::shared_ptr<ObjectType>( new ObjectType(actions) ) );
258}
259
260} // namespace nom
261
262#endif // include guard defined
263
Pure virtual base class interface for actions.
void set_duration(real32 seconds)
Set the duration of the action.
virtual void set_speed(real32 speed)
Set the speed factor of the action.
real32 elapsed_frames_
Internal frames counter.
Timer timer_
Internal time clock (milliseconds resolution).
virtual void release()=0
Free externally referenced resources held by the action.
const std::string & name() const
Get the unique identifier of the action.
std::shared_ptr< ObjectType > create_action(const action_list &actions)
Constructor function for creating an action that uses a collection of actions.
virtual IActionObject::FrameState prev_frame(real32 delta_time)=0
Play the action backwards in time by one time step.
void set_status(FrameState state)
Set the state of the action.
std::shared_ptr< ObjectType > create_action(ObjectArgs &&... args)
Constructor function for creating an action.
virtual IActionObject::FrameState next_frame(real32 delta_time)=0
Play the action forward in time by one time step.
FrameState
The update status of the action.
@ COMPLETED
The action has finished its update loop.
@ PLAYING
The action is still updating; duration remains.
std::function< real32(real32, real32, real32, real32)> timing_curve_func
A function pointer to a timing mode.
virtual std::unique_ptr< IActionObject > clone() const =0
Create a deep copy instance of the action.
virtual void resume(real32 delta_time)=0
Resume the internal state of the action.
virtual void rewind(real32 delta_time)=0
Reset the internal state of the action back to its initial starting values.
IActionObject::FrameState status() const
Get the current state of the action.
void set_name(const std::string &action_id)
Set the unique identifier of the action.
real32 speed() const
Get the speed modifier of the action.
virtual void pause(real32 delta_time)=0
Freeze the action's internal state.
std::vector< std::shared_ptr< IActionObject > > action_list
A collection of actions.
real32 duration() const
Get the duration of the action.
virtual void set_timing_curve(const IActionObject::timing_curve_func &mode)
Set the timing mode of the action.
const IActionObject::timing_curve_func & timing_curve() const
Get the timing mode function used by the action.