1212
1313#include " Renderer/renderer.h"
1414
15+ #include " Helpers/fsm.h"
16+
1517using namespace pg ;
1618
1719#ifdef PG_AUTO_CONVERT_EVENTS_TO_STANDARD
@@ -28,11 +30,11 @@ int test = 0;
2830StandardSystemImpl* createEventNotificationSystem ()
2931{
3032 return createStandardSystem (" EventNotification" )
31- .onInit ([](StandardSystemHandle* sys ) {
33+ .onInit ([](StandardSystemHandle*) {
3234 // Initialize system
3335 LOG_INFO (" EventNotification" , " System initialized" );
3436 })
35- .onEvent (" BasicEvent" , [](StandardSystemHandle* sys , const StandardEvent& event) {
37+ .onEvent (" BasicEvent" , [](StandardSystemHandle*, const StandardEvent& event) {
3638 bool verbose = true ;
3739
3840 test++;
@@ -70,7 +72,7 @@ StandardSystemImpl* createEventNotificationSystem()
7072StandardSystemImpl* createScriptEventNotificationSystem ()
7173{
7274 return createStandardSystem (" ScriptEventNotification" )
73- .onInit ([](StandardSystemHandle* sys ) {
75+ .onInit ([](StandardSystemHandle*) {
7476 // Initialize system
7577 LOG_INFO (" ScriptEventNotification" , " System initialized" );
7678 })
@@ -82,7 +84,7 @@ StandardSystemImpl* createScriptEventNotificationSystem()
8284StandardSystemImpl* createSimplePositionSystem ()
8385{
8486 return createStandardSystem (" SimplePosition" )
85- .onInit ([](StandardSystemHandle* sys ) {
87+ .onInit ([](StandardSystemHandle*) {
8688 // Initialize system
8789 LOG_INFO (" SimplePosition" , " System initialized" );
8890 })
@@ -96,7 +98,7 @@ StandardSystemImpl* createSimplePositionSystem()
9698StandardSystemImpl* createSimpleExecSystem ()
9799{
98100 return createStandardSystem (" SimpleExec" )
99- .onInit ([](StandardSystemHandle* sys ) {
101+ .onInit ([](StandardSystemHandle*) {
100102 // Initialize system
101103 LOG_INFO (" SimpleExec" , " System initialized" );
102104 })
@@ -107,7 +109,7 @@ StandardSystemImpl* createSimpleExecSystem()
107109StandardSystemImpl* createCompExecSystem ()
108110{
109111 return createStandardSystem (" CompExec" )
110- .onInit ([](StandardSystemHandle* sys ) {
112+ .onInit ([](StandardSystemHandle*) {
111113 // Initialize system
112114 LOG_INFO (" CompExec" , " System initialized" );
113115 })
@@ -119,7 +121,7 @@ StandardSystemImpl* createCompExecSystem()
119121StandardSystemImpl* createCompExecReactorSystem ()
120122{
121123 return createStandardSystem (" CompExecReactor" )
122- .onInit ([](StandardSystemHandle* sys ) {
124+ .onInit ([](StandardSystemHandle*) {
123125 // Initialize system
124126 LOG_INFO (" CompExec" , " System initialized" );
125127 })
@@ -130,7 +132,7 @@ StandardSystemImpl* createCompExecReactorSystem()
130132StandardSystemImpl* createMouseClickHandlerSystem ()
131133{
132134 return createStandardSystem (" MouseClickHandler" )
133- .onInit ([](StandardSystemHandle* sys ) {
135+ .onInit ([](StandardSystemHandle*) {
134136 // Initialize system
135137 LOG_INFO (" CompExec" , " System initialized" );
136138 })
@@ -141,7 +143,7 @@ StandardSystemImpl* createMouseClickHandlerSystem()
141143StandardSystemImpl* createKeyHandlerSystem ()
142144{
143145 return createStandardSystem (" MouseClickHandler" )
144- .onInit ([](StandardSystemHandle* sys ) {
146+ .onInit ([](StandardSystemHandle*) {
145147 // Initialize system
146148 LOG_INFO (" CompExec" , " System initialized" );
147149 })
@@ -213,6 +215,9 @@ GameApp::GameApp(const std::string &appName) : engine(appName)
213215{
214216 engine.setSetupFunction ([this ](EntitySystem& ecs, Window& window)
215217 {
218+ // Todo O3 optimization breaks the event system for some reason
219+ ecs.setVMOptimizationLevel (VmOptimizationLevel::O0 );
220+
216221 ecs.registerSystem (createEventNotificationSystem ());
217222
218223 // Basic Event without a custom value
@@ -343,6 +348,61 @@ GameApp::GameApp(const std::string &appName) : engine(appName)
343348
344349 ecs.registerSystem (createFPSSystem ());
345350
351+ ecs.createSystem <FSMSystem>();
352+
353+ auto ent = ecs.createEntity ();
354+
355+ auto fsm = ent.attach <FiniteStateMachine>();
356+
357+ FSMState idleState (" idle" );
358+
359+ idleState.setEnterCallback ([](FSMState&) {
360+ LOG_INFO (" FSM" , " Entering idle state" );
361+ });
362+
363+ idleState.setExitCallback ([](FSMState&) {
364+ LOG_INFO (" FSM" , " Exiting idle state" );
365+ });
366+
367+ idleState.setEventCallback (" OnSDLScanCode" , [](const StandardEvent& event, FSMState& state) {
368+ auto key = event.getElement (" key" ).toString ();
369+
370+ LOG_INFO (" FSM" , " Idle State received scan code: " << key);
371+
372+ if (key == " A" )
373+ {
374+ state.getFSM ()->setState (" base" , " base" );
375+ }
376+ });
377+
378+ FSMState baseState (" base" );
379+
380+ baseState.setEnterCallback ([](FSMState&) {
381+ LOG_INFO (" FSM" , " Entering base state" );
382+ });
383+
384+ baseState.setExitCallback ([](FSMState&) {
385+ LOG_INFO (" FSM" , " Exiting base state" );
386+ });
387+
388+ baseState.setEventCallback (" OnSDLScanCode" , [](const StandardEvent& event, FSMState& state) {
389+ auto key = event.getElement (" key" ).toString ();
390+
391+ LOG_INFO (" FSM" , " Idle State received scan code: " << key);
392+
393+ if (key == " A" or key == " D" )
394+ {
395+ state.getFSM ()->setState (" base" , " idle" );
396+ }
397+ });
398+
399+ fsm->registerState (idleState);
400+ fsm->registerState (baseState);
401+
402+ fsm->setState (" base" , " idle" );
403+
404+ // fsm->setState("base", "base");
405+
346406 // window.receivedQuitRequest();
347407
348408 });
0 commit comments