summaryrefslogtreecommitdiffstats
path: root/src/Game.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Game.cpp41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index 927cdc2..a3f2b50 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -12,6 +12,7 @@ State state;
std::unique_ptr<NetworkClient> nc;
std::unique_ptr<GameState> gs;
std::unique_ptr<Render> render;
+std::unique_ptr<LoopExecutionTimeController> timer;
EventListener listener;
void InitEvents() {
@@ -126,6 +127,7 @@ void InitEvents() {
default:
break;
}
+ LOG(INFO) << "Changed key";
});
listener.RegisterHandler("MouseMove", [](const Event& eventData) {
@@ -136,23 +138,33 @@ void InitEvents() {
});
listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) {
+ if (!gs)
+ return;
std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>();
gs->UpdatePacket(packet);
});
listener.RegisterHandler("LmbPressed",[](const Event& eventData) {
+ if (!gs)
+ return;
gs->StartDigging();
});
listener.RegisterHandler("LmbReleased",[](const Event& eventData) {
+ if (!gs)
+ return;
gs->CancelDigging();
});
listener.RegisterHandler("RmbPressed", [](const Event& eventData) {
+ if (!gs)
+ return;
gs->PlaceBlock();
});
listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) {
+ if (!gs)
+ return;
//TODO:
//gs->CancelDigging();
});
@@ -161,17 +173,32 @@ void InitEvents() {
void RunGame() {
InitEvents();
+ timer = std::make_unique<LoopExecutionTimeController>(std::chrono::milliseconds(16));
+
render = std::make_unique<Render>(900, 480, "AltCraft");
- SetState(State::MainMenu);
- LoopExecutionTimeController time(std::chrono::milliseconds(16));
+ SetState(State::MainMenu);
while (isRunning) {
listener.HandleAllEvents();
- if (gs)
- gs->Update(time.GetDeltaS());
+ if (gs) {
+ if (GetState() == State::Playing) {
+ if (isMoving[GameState::FORWARD])
+ gs->HandleMovement(GameState::FORWARD, timer->GetRealDeltaS());
+ if (isMoving[GameState::BACKWARD])
+ gs->HandleMovement(GameState::BACKWARD, timer->GetRealDeltaS());
+ if (isMoving[GameState::LEFT])
+ gs->HandleMovement(GameState::LEFT, timer->GetRealDeltaS());
+ if (isMoving[GameState::RIGHT])
+ gs->HandleMovement(GameState::RIGHT, timer->GetRealDeltaS());
+ if (isMoving[GameState::JUMP])
+ gs->HandleMovement(GameState::JUMP, timer->GetRealDeltaS());
+ }
+
+ gs->Update(timer->GetRealDeltaS());
+ }
render->Update();
- time.Update();
+ timer->Update();
}
render.reset();
@@ -198,3 +225,7 @@ Render *GetRender() {
NetworkClient *GetNetworkClient() {
return nc.get();
}
+
+LoopExecutionTimeController* GetTime() {
+ return timer.get();
+}