AireEngine
A 3D Open-World Game Engine
Loading...
Searching...
No Matches
myEvent.h
1#pragma once
2
3#include "../gameplay/event.h"
4#include "../gameplay/player.h"
5#include "../gameplay/stateMachine.h"
6
7#include "../rendering/hud.h"
8
9#include "../utils/log.h"
10#include "../utils/ai.h"
11
12using namespace Aire;
13
14//############################//
15//### Events Example Start ###//
16//############################//
17
18// Inherit from the main Event class only
19// if you want to pass additional arguments to an event.
20// Declare them as class members and initialize them with desired
21// values before dispatching the event.
22
23struct CollisionEvent : public Event {
25 CollisionEvent(const std::string& eventType, float collisionDamage) : Event(eventType), collisionDamage(collisionDamage) {}
26};
27
28struct GameOverEvent : public Event {
29 static Aire::HUD::HUD_TEXT* winText;
30 static Aire::HUD::HUD_TEXT* deathText;
31 static Aire::AudioSource* winSound;
32 static Aire::AudioSource* deathSound;
33 GameOverEvent(const std::string& eventType) : Event(eventType) {}
34};
35
36Aire::HUD::HUD_TEXT* GameOverEvent::winText = nullptr;
37Aire::HUD::HUD_TEXT* GameOverEvent::deathText = nullptr;
38Aire::AudioSource* GameOverEvent::winSound = nullptr;
39Aire::AudioSource* GameOverEvent::deathSound = nullptr;
40
41struct BoidEvent : public Event {
42 int numBoids;
43 BoidEvent(const std::string& eventType, int numBoids) : Event(eventType), numBoids(numBoids) {}
44};
45
47public:
48 BoidController(Aire::BoidManager* boidManager) : boidManager(boidManager) {
49 Aire::EventManager::registerForEvent(this, "dispatchBoids");
50 }
51
52 virtual void onEvent(const Aire::Event& eventInstance) override {
53 if (eventInstance.type == "dispatchBoids") {
54 auto const& boidEvent = static_cast<BoidEvent const&>(eventInstance);
55 if (boidManager) boidManager->addBoids(boidEvent.numBoids);
56 }
57 }
58
59 Aire::BoidManager* boidManager;
60};
61
62
63// TODO maybe we'll need to handle boid<->object collision
64// this would require a custom boid class to receive the event
65// this does not fit very well with current implementation
66
67// If you want an object to be responsive to a game event,
68// it must inherit from the GameObject class and override the
69// onEvent() method. Inside the method, catch the desired event
70// and perform action.
71//
72// Make sure to register the object to the event type, otherwise
73// it won't be notified when the event gets dispatched.
74class MyPlayer : public Aire::Player, public Aire::GameObject {
75public:
77 Aire::EventManager::registerForEvent(this, "playerDied");
79 Aire::EventManager::registerForEvent(this, "collision");
80 }
81
82 virtual void onEvent(const Aire::Event& eventInstance) override {
83 if (eventInstance.type == "playerDied" && isAlive) {
84 // If we want to access the members of a custom event, we need to cast it as follows:
85 //auto const& eventCasted = static_cast<MyEvent const&>(eventInstance);
86 //
87 // This is required only if we need to pass additional arguments to the event.
88
89 // log death
90 AIRE_ERROR("Player died.");
91
92 GameOverEvent::deathText->active = true;
93 isAlive = false;
94
95 GameOverEvent::deathSound->play();
96
97 // respawn the player at the centre
98 rb->position = glm::vec3(0.f);
99 //TODO handle player died event
100
101 } else if (eventInstance.type == "gameOver") {
102 // log game over
103 AIRE_WARN("Game over.");
104 if (health > 0.0) {
105 // Player won
106 GameOverEvent::winText->active = true;
107 isAlive = false;
108
109 GameOverEvent::winSound->play();
110
111 } else EventManager::dispatchEvent({ "playerDied" });
112 } else if (eventInstance.type == "collision") {
113 // General-purpose collision event
114 auto const& collision = static_cast<CollisionEvent const&>(eventInstance);
115 health -= collision.collisionDamage;
116 if (health<=0.0) EventManager::dispatchEvent(Event("playerDied"));
117 }
118 }
119
120 float health = 100.0f;
121 bool isAlive = true;
122};
123
124// This is a second example. It is there to demostrate how to do timer events.
125
126struct BoidStateMachine : public StateMachine, public GameObject {
128 EventManager::registerForEvent(this, "startChasing");
129 EventManager::registerForEvent(this, "stopChasing");
130 }
131 virtual void onEvent(const Aire::Event& eventInstance) override {
132 if (eventInstance.type == "startChasing") {
133 if (currentState && currentState->name == "peaceful") {
134 transitionTo("chasing");
135 }
136 } else if (eventInstance.type == "stopChasing") {
137 if (currentState && currentState->name == "chasing") {
138 transitionTo("peaceful");
139 }
140 }
141 }
142};
143
144struct BoidState : public State {
145 Aire::BoidManager* boidMan;
146
147 BoidState(const std::string& name, Aire::BoidManager* boidMan) : State{ name }, boidMan(boidMan) {}
148
149 virtual void onActive() override {
150 if (!boidMan) return;
151 if (name == "chasing") {
152 boidMan->setTrackPlayer(true);
153 }
154 else if (name == "peaceful") {
155 boidMan->setTrackPlayer(false);
156 }
157 }
158};
159
160//############################//
161//#### Events Example End ####//
162//############################//
AudioSource class to play sound effects.
Definition audioSource.h:17
void play()
Plays the audio.
Definition audioSource.cpp:43
Manager for the boids.
Definition ai.h:159
void setTrackPlayer(bool trackPlayer)
Make the boids track the player.
Definition ai.h:211
void addBoids(int numBoids)
Add a nmber of new boids.
Definition ai.cpp:174
Definition config.h:11
Definition player.h:12
Rigidbody * rb
Rigid body handles physics.
Definition player.h:26
Config config
Config attached to the player.
Definition player.h:24
Definition window.h:13
Definition myEvent.h:46
virtual void onEvent(const Aire::Event &eventInstance) override
Definition myEvent.h:52
Definition myEvent.h:74
virtual void onEvent(const Aire::Event &eventInstance) override
Definition myEvent.h:82
Definition event.h:11
std::string type
Type of the event. Serves as event identifier. Case sensitive.
Definition event.h:14
static void registerForEvent(GameObject *obj, std::string eventType)
Definition event.h:53
static void dispatchEvent(const Event &eventInstance)
Definition event.h:139
Definition event.h:22
Class to display text on the screen.
Definition hud.h:47
bool active
Flag to indicate if currently active.
Definition hud.h:50
Definition stateMachine.h:9
A state machine that controls transition between states.
Definition stateMachine.h:37
State * currentState
The current state that the state machine is in.
Definition stateMachine.h:38
void transitionTo(std::string dstState)
Definition stateMachine.h:46
Definition myEvent.h:41
Definition myEvent.h:144
virtual void onActive() override
Definition myEvent.h:149
Definition myEvent.h:126
virtual void onEvent(const Aire::Event &eventInstance) override
Definition myEvent.h:131
Definition myEvent.h:23
float collisionDamage
The amount of damage the collision will do.
Definition myEvent.h:24
Definition myEvent.h:28