-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (145 loc) · 4.84 KB
/
Copy pathmain.cpp
File metadata and controls
158 lines (145 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <memory>
#include <iostream>
#include <stdexcept>
#include <chrono>
#include <thread>
#include <ctime>
#include <string>
#include <array>
#include "EventManager.hpp"
#include "SimpleFSM.hpp"
// To count memory allocation
static int m_allocationCount=0;
void* operator new(size_t size)
{
std::cout << "Allocated : " << size << " bytes\n";
m_allocationCount++;
return malloc(size);
}
////////////////////////////////
void OnPlayerJoined(int playerId)
{
std::cout << "Player Joined : " << playerId << '\n';
}
void OnPlayerLeft(int playerId)
{
std::cout << "Player Left :" << playerId << '\n';
}
template<typename T>
void OnReceivedData_TwoParam(T )
{
std::cout << __PRETTY_FUNCTION__ << '\n';
}
class Player
{
static int count;
int id = 0;
std::array<float,4> arr = {12.f,2.3f,4.3f,5.8f};
SimpleEventManager::Event<int,const std::array<float,4>&,std::string_view> event_threeParam;
public:
Player():id(++count)
{
event_threeParam.RegisterListener([](int a, const std::array<float,4>& InVec, std::string_view Name)
{
std::cout << "Player: " << a << '\n';
for(const auto& e : InVec)
{
std::cout << e << ", ";
}
std::cout << Name <<'\n';
});
}
explicit Player(ClassBasedEventManager* Dispatcher):id(++count)
{
if(Dispatcher == nullptr)
{
throw std::invalid_argument("eventDispatcher is null");
}
Dispatcher->Register("Joined",[this](int playerId)
{
if(playerId == id)
{
OnPlayerJoined(playerId);
}
});
Dispatcher->Register("Left",[this](int playerId)
{
if(playerId == id)
{
OnPlayerLeft(playerId);
std::cout << "I DIE :" << id << std::endl;
}
});
};
int GetId() const {return id;}
void Print()const {std::cout << "I am :" << id << std::endl;}
void ShootEvent()
{
event_threeParam.DispatchEvent(id,arr,"Somos tres");
}
};
int Player::count = 0;
int main()
{
ClassBasedEventManager ClassEventManager;
Player playerOne(&ClassEventManager);
ClassEventManager.DispatchEvent("Joined",playerOne.GetId());
Player playerTwo(&ClassEventManager);
ClassEventManager.DispatchEvent("Joined",playerTwo.GetId());
std::this_thread::sleep_for(std::chrono::seconds(2));
std::time_t currentTimeT = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::ctime(¤tTimeT) << std::endl;
ClassEventManager.DispatchEvent("Left",playerOne.GetId());
std::this_thread::sleep_for(std::chrono::seconds(2));
currentTimeT = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::ctime(¤tTimeT) << std::endl;
ClassEventManager.DispatchEvent("Left",playerTwo.GetId());
Player playerThree;
playerThree.ShootEvent();
// Testing EventManager variadic
SimpleEventManager::Event<int,std::string> event_twoParams;
event_twoParams.RegisterListener([](int a,const std::string& b)
{
std::cout << "Listener 1:" << a << "," << b << '\n';
std::cout << __PRETTY_FUNCTION__ << '\n';
});
event_twoParams.DispatchEvent(42,"Hey");
std::cout << m_allocationCount << " Allocations \n";
/*
TEST FSM
*/
std::cout << "TEST SSFM \n";
SSFM ssfm;
ssfm.addTransition(State::Idle,Event::StartMoving,State::Patrol,[]()
{
std::cout << "Action : start patrolling\n";
});
ssfm.addTransition(State::Patrol,Event::StopMoving,State::Attack,[]()
{
std::cout << "Action : Attack\n";
});
ssfm.addTransition(State::Attack,Event::StartMoving,State::Cover,[]()
{
std::cout << "Action : Taking Cover\n";
});
ssfm.addTransition(State::Cover,Event::StopMoving,State::Heal,[]()
{
std::cout << "Action : Healing\n";
});
// simulate events
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::StartMoving);
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::StopMoving);
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::Attack);
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::StartMoving);
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::StopMoving);
std::cout << "Current State : " << static_cast<int>(ssfm.getCurrentState()) << '\n';
ssfm.handleEvent(Event::StartHeal);
std::cout << "Press Enter to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}