Abstract base class for game scenes. More...
#include <Scene.h>
Public Types | |
using | Ptr = std::shared_ptr< Scene > |
Shared Scene pointer. More... | |
Public Member Functions | |
Scene () | |
Constructor. More... | |
Scene (Scene &&) noexcept | |
Move constructor. More... | |
Scene & | operator= (Scene &&) noexcept |
Move assignment operator. More... | |
void | init (Engine &engine) |
virtual void | onEnter ()=0 |
Enter the scene. More... | |
virtual void | handleEvent (Event event) |
Handle a system event. More... | |
virtual void | update (Time deltaTime) |
Update the scene. More... | |
virtual void | fixedUpdate (Time deltaTime) |
Update the scene in fixed time steps. More... | |
virtual void | postUpdate (Time deltaTime) |
Post update the scene. More... | |
virtual void | onPause () |
Pause the scene. More... | |
virtual void | onResume () |
Resume scene. More... | |
virtual void | onExit () |
Exit a scene. More... | |
std::string | getClassName () const override |
Get the name of the scene. More... | |
bool | isVisibleOnPause () const |
Check if the scene is visible when paused or not. More... | |
bool | isEntered () const |
Check if the scene has been entered or not. More... | |
template<typename ... Args> | |
int | on_ (const std::string &event, Callback< Args... > callback) |
bool | unsubscribe_ (const std::string &event, int id) |
~Scene () override | |
Destructor. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the alias of the object. More... | |
unsigned int | getObjectId () const |
Get the id of the object. More... | |
virtual std::string | getClassType () const |
Get the name of the direct parent of an object instance. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback) |
Add an event listener to a specific property change event. More... | |
void | onPropertyChange (const Callback< Property > &callback) |
Add an event listener to a property change event. More... | |
int | onEvent (const std::string &event, const Callback<> &callback) |
Add an event listener to an event. More... | |
bool | unsubscribe (const std::string &event, int id) |
Remove an event listener from an event. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | removeDestructionListener (int id) |
Remove a destruction listener form the object. More... | |
bool | operator== (const Object &rhs) const |
Check if two objects are the same object or not. More... | |
bool | operator!= (const Object &rhs) const |
Check if two objects are not the same object. More... | |
Protected Member Functions | |
void | setVisibleOnPause (bool show) |
Set whether or not the scene is hidden or rendered when it is paused. More... | |
void | setTimescale (float timescale) |
Set the scene timescale. More... | |
float | getTimescale () const |
Get the scenes timescale. More... | |
Engine & | engine () const |
Get a reference to the game engine. More... | |
Camera & | camera () |
Get the scene camera. More... | |
World & | physics () |
Get the physics simulation. More... | |
GridMoverContainer & | gridMovers () |
Get the scenes grid mover container. More... | |
EventEmitter & | eventEmitter () |
Get the scenes event event emitter. More... | |
EventDispatcher & | globalEventEmitter () |
Get the global event emitter. More... | |
input::InputManager & | input () |
Get the scenes input manager. More... | |
audio::AudioManager & | audio () |
Get the scenes audio manager. More... | |
TimerManager & | timer () |
Get the scenes timer manager. More... | |
PropertyContainer & | cache () |
Get the global cache. More... | |
RenderLayerContainer & | renderLayers () |
Get the scene render layers. More... | |
TileMap & | tilemap () |
Get the scene Tilemap. More... | |
ui::GuiContainer & | gui () |
Get the scene gui container. More... | |
ShapeContainer & | shapes () |
Get the scene geometry shape container. More... | |
GameObjectContainer & | gameObjects () |
Get the scene game object container. More... | |
SpriteContainer & | sprites () |
Get the scene sprite container. More... | |
void | createWorld (Vector2f gravity) |
Create a physics simulation. More... | |
void | createTilemap (unsigned int tileWidth, unsigned int tileHeight) |
Create tilemap instance. More... | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emit (const std::string &event) |
Dispatch an action event. More... | |
Friends | |
class | SceneManager |
Pre updates the scene. More... | |
Abstract base class for game scenes.
A scene represents a distinct state of your game, for example loading, main menu, gameplay, paused and so on. All game scenes exist in isolation and have no knowledge of each other. Only one scene can be active at a time. For example, the game cannot be in a main menu state and a gameplay state at the same time.
The transition between scenes is managed using the Last In First Out (LIFO) technique (The same way std::stack works). Therefore you cannot transition between scenes at random. The order in which scenes are added to the Engine is important.
For example, if while in the "gameplay" scene, you push a "pause" scene to the game engine, the "gameplay" scene will be paused (onPause called) and the "pause" scene will be entered (onEnter called on the pause scene instance) and the "pause" scene will become the active scene (gets system events, updates and rendered). If you pop the "pause" scene, the engine will destroy it (onExit called on the pause scene instance) and return to the "gameplay" scene (onResume called on the gameplay scene instance). However, if you push (transition to) another scene while in the "pause" scene, the process repeats; the "pause" scene gets paused and the the new scene becomes active)
using ime::Scene::Ptr = std::shared_ptr<Scene> |
ime::Scene::Scene | ( | ) |
Constructor.
engine | A reference to the game engine |
|
noexcept |
Move constructor.
|
override |
Destructor.
|
protected |
Get the scenes audio manager.
The audio manager is local the scene instance. For global audio, use the global audio manager in the Engine class
|
protected |
Get the global cache.
Data stored in the cache persists from scene to scene. This means that another scene can access or modify data stored by another scene. The data can also be accessed using the engine instance
|
protected |
Get the scene camera.
|
protected |
Create tilemap instance.
tileWidth | The width of the tilemap |
tileHeight | The height of the tilemap |
Note that this function only creates a tilemap instance so that it can be used. You still need to construct the tilemap using the appropriate member function
|
protected |
Create a physics simulation.
gravity | Acceleration of bodies in the simulation due to gravity |
This function should be called by scenes that require a physics simulation
|
protectedinherited |
Dispatch an action event.
event | The name of the event to be dispatched |
This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protected |
Get a reference to the game engine.
|
protected |
Get the scenes event event emitter.
The event emitter is local to the scene instance. This means that events registered on it are only dispatched when the scene is active and de-registered when the scene is destroyed. For global events use the global event event emitter in the Engine class or the EventDispatcher
|
inlinevirtual |
Update the scene in fixed time steps.
deltaTime | Time passed since last update |
This function will be called by the game engine after the scene has handled system events and external inputs (Keyboard and mouse). The function may be called once per frame, multiple times per frame or not called at all. The delta passed to it is always the same and is independent of the games frame rate. The delta time is always 1.0f / FPS_LIMIT, where FPS_LIMIT is the games Frames Per Second limit
Updates that are frame-rate independent must be in this function. Note that implementing this function is optional and must be overridden if needed. IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
|
protected |
Get the scene game object container.
This class stores game objects that belong to this scene
|
overridevirtual |
|
virtualinherited |
Get the name of the direct parent of an object instance.
In contrast to getClassName which returns the name of the concrete class, this function returns the name of the concrete class's base class. This function is implemented by all derived classes of Object which also serve as base classes for other Objects. For classes whose direct parent is this class, this function will return the name of this class
Reimplemented in ime::SpriteImage, ime::Shape, ime::Drawable, ime::GridMover, ime::Joint, ime::Collider, and ime::GameObject.
|
inherited |
Get the id of the object.
Each object has a unique id
|
inherited |
|
protected |
Get the scenes timescale.
|
protected |
Get the global event emitter.
The global event emitter is available to anything that needs it (a class, function etc...). Events registered to it are always dispatched regardless of the active scene. As a result you must remove event listeners that are local to the scene when the scene is destroyed. For example, a lambda that captures "this" will result in undefined behavior if not removed after the scene is destroyed
|
protected |
Get the scenes grid mover container.
Note that you can use this container to store a GridMover or you can use your own container. The advantage of using this container is that the grid mover is updated on your behalf
|
protected |
Get the scene gui container.
The gui container is provided here for convenience, you can manually instantiate as many gui containers as you desire. Note that the gui does not belong to any render layer and is always rendered on top
|
inlinevirtual |
Handle a system event.
event | System event to be handled |
This function will be called by the game engine before the scene is updated. Override this function if you want to handle a window specific events such as window resize event. Do not use it to handle input related events. Use the scenes input manager or the global input manager found in the Engine. The function is called once per frame
Note that IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
|
protected |
Get the scenes input manager.
The input manager is local to the scene instance. This means that input listeners registered on it are only invoked when the scene is active and de-registered when the scene is destroyed To register global inputs, use the global input manager in the Engine class
bool ime::Scene::isEntered | ( | ) | const |
Check if the scene has been entered or not.
bool ime::Scene::isVisibleOnPause | ( | ) | const |
Check if the scene is visible when paused or not.
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time
|
pure virtual |
Enter the scene.
This function will be called by the game engine when the scene is entered for the first time. Note that a scene cannot be entered more than once, in other words this function will only be called once
|
inherited |
Add an event listener to an event.
event | The name of the event to add an an event listener to |
callback | The function to be executed when the event takes place |
Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:
|
inlinevirtual |
Exit a scene.
This function will be called by the game engine before the scene is removed from the game. The function is optional and may be overridden if you want to perform cleanup or something before the scene is destroyed
Note that implementing this function is optional and must be overridden if you want to do something when the scene is resumed. IME will never put anything inside this function. therefore you don't have to call the base class method in your implementation
|
inlinevirtual |
Pause the scene.
This function will be called by the game engine if another scene is pushed while this scene is active
Note that implementing this function is optional and must be overridden if you want to do something when the scene is paused. IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
Add an event listener to a property change event.
callback | The function to be executed when the property changes |
Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to listen for |
callback | The function to be executed when the property changes |
A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, for the setTag function, the property that the function modifies is Tag.
Note that when adding a property change event listener, the name of the property must be in lowercase:
Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time
|
inlinevirtual |
Resume scene.
This function will be called by the game engine when the current active scene is removed and this scene was paused
Note that implementing this function is optional and must be overridden if you want to do something when the scene is resumed. IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
|
inherited |
Check if two objects are not the same object.
rhs | Object to compare against this object |
Two objects are different from each other if they have different object id's
|
inherited |
Check if two objects are the same object or not.
rhs | Object to compare against this object |
Two objects are the same object if they have the same object id. Recall that each object instance has a unique id
|
protected |
Get the physics simulation.
The physics simulation is responsible for creating, managing, colliding and updating all of the bodies within it.
|
inlinevirtual |
Post update the scene.
deltaTime | Time passed since last update |
This function is called after fixed update and update but before the scene is rendered. It may be useful if you want to do something after all time updates (timer updates, physics updates, animation updates, gui updates etc...) have completed.
Note that implementing this function is optional and must be overridden if needed. IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
|
inherited |
Remove a destruction listener form the object.
The | id of the destruction listener to be removed |
|
protected |
Get the scene render layers.
Render layers allow the scene to be rendered in separate layers which are then composed back together. By default the scene has a "default" layer at index 0. When a drawable object is added to the scene without an explicit layer, it will be added to the default layer. You can add objects to the "default" layer or even remove the "default" layer from the render layer container, however you mus not forget to reallocate the objects in the "default" layer to another layer, otherwise they will not be drawn to the screen
|
inherited |
Assign the object an alias.
name | The alias of the object |
This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.
By default, the tag is an empty string
|
protected |
Set the scene timescale.
timescale | The new scene timescale |
The timescale can be used to speed up or slow down the scene without changing the FPS limit. Values above 1.0f speed up the scene whilst values below 1.0f slow it down A timescale of zero freezes the scene.
By default the timescale is 1.0f (real-time)
|
protected |
Set whether or not the scene is hidden or rendered when it is paused.
True | to show or false to hide |
When the scene is shown on pause, it is rendered behind the current active scene but it does not receive any events or updates. Note that the scene will only be rendered if it has been entered and it is the next scene (It is the scene that will run if the current one is popped)
By default the scene is hidden when it is paused
|
protected |
Get the scene geometry shape container.
You may use this class to create geometry shape instead of using their respective constructors. Consult the ShapeContainer class definition for more info
|
protected |
Get the scene sprite container.
This class stores the sprites in the scene. The sprite's animator will automatically be updated
|
protected |
Get the scene Tilemap.
Note that only one tilemap can be created per scene
|
protected |
Get the scenes timer manager.
The timer manager is local to the scene instance. This means that callbacks scheduled on it will only be dispatched when the scene is active
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove event listener from |
id | The unique id of the event listener to be removed |
|
inlinevirtual |
Update the scene.
deltaTime | Time passed since last update |
This function will be called by the game engine after the scene has handled system events and external inputs (Keyboard and mouse). The function is called once per frame and the delta passed to it is frame rate dependent. This means that it depends on how long the previous frame took to complete.
All updates that should be synced with the render fps must be in this function. Note that implementing this function is optional and must be overridden if needed. IME will never put anything inside this function, therefore you don't have to call the base class method in your implementation
|
friend |