-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLockstep.cpp
More file actions
165 lines (122 loc) · 4.65 KB
/
Lockstep.cpp
File metadata and controls
165 lines (122 loc) · 4.65 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
159
160
161
162
163
164
#include "Lockstep.h"
#include "stormancer/IClientFactory.h"
#include "GameSessionViewModel.h"
#include "ViewModel.h"
LockstepViewModel::LockstepViewModel(GameSessionViewModel* parent)
:parent(parent)
, _clientId(parent->parent->id)
{
}
void LockstepViewModel::initialize()
{
currentState = "";
auto client = Stormancer::IClientFactory::GetClient(_clientId);
_onStepSubscription = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>()->onStep.subscribe([this](Stormancer::Gameplay::Frame step)
{
for (auto& cmd : step.commands)
{
char c = (char)cmd.content[0];
currentState = currentState + c;
}
});
_onRollbackSubscription = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>()->onRollback.subscribe([this](Stormancer::Gameplay::RollbackContext& ctx)
{
Snapshot& current = snapshots.front();
for (auto& snapshot : snapshots)
{
if (snapshot.frame > ctx.targetFrame)
{
break;
}
else
{
current = snapshot;
}
}
currentState = current.state;
ctx.restoredFrame = current.frame;
});
///Called when the peer needs to install a snapshot generated by another peer.
_onInstallSnapshotSubscription = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>()->onInstallSnapshot.subscribe([this](Stormancer::Gameplay::Snapshot& snapshot)
{
this->gameplayTime = snapshot.gameplayTimeSeconds;
this->currentState = std::string((const char*)snapshot.content.data(), snapshot.content.size());
});
//Called when the peer has to create a snapshot.
_onCreateSnapshotSubscription = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>()->onCreateSnapshot.subscribe([this](Stormancer::Gameplay::Snapshot& snapshot)
{
snapshot.content.resize(currentState.length());
std::memcpy(snapshot.content.data(), currentState.c_str(), currentState.length());
});
_onStartSubscription = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>()->onStart.subscribe([this]()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto logger = client->dependencyResolver().resolve<Stormancer::ILogger>();
logger->log(Stormancer::LogLevel::Info, "lockstep", "OnStart called on time " + std::to_string(this->gameplayTime));
});
}
bool LockstepViewModel::isEnabled()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->isEnabled();
}
void LockstepViewModel::Reset()
{
currentState = "";
snapshots.clear();
Snapshot snapshot;
snapshots.push_back(snapshot);
}
std::vector<::Stormancer::Gameplay::LockstepPlayer> LockstepViewModel::getPlayers()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->getPlayers();
}
void LockstepViewModel::addCommand(Stormancer::byte cmd)
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
api->pushCommand(&cmd, 1);
}
Stormancer::Gameplay::Time LockstepViewModel::getLockstepTime()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->getCurrentTime();
}
Stormancer::Gameplay::Time LockstepViewModel::getTargetTime()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->getTargetTime();
}
Stormancer::Gameplay::FrameDuration LockstepViewModel::getCommandLatency()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->getCommandLatency();
}
bool LockstepViewModel::isPaused()
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
return api->isPaused();
}
void LockstepViewModel::Pause(bool pause)
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
api->pause(pause);
}
void LockstepViewModel::tick(float delta)
{
auto client = Stormancer::IClientFactory::GetClient(_clientId);
auto api = client->dependencyResolver().resolve<Stormancer::Gameplay::LockstepApi>();
auto gameplayDeltaTime = api->adjustTick(delta, delta);
api->tick(gameplayDeltaTime,delta);
this->gameplayTime += gameplayDeltaTime;
this->realTime += delta;
api->endFrame();
}