-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.cpp
More file actions
120 lines (99 loc) · 2.61 KB
/
Scene.cpp
File metadata and controls
120 lines (99 loc) · 2.61 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
#include "precomp.h"
#include "Scene.h"
#include "game.h"
#include "EntityManager.h"
#include "Camera.h"
#include "Terrain.h"
#include "Physics.h"
#include "SavedData.h"
#include "TimeManager.h"
Framework::Scene::Scene(Game& game, const std::string& levelFile, const std::string& levelName) :
mGame(game)
{
mPhysics = std::make_unique<Physics>(*this);
mCamera = std::make_unique<Camera>(*this);
mTerrain = std::make_unique<Terrain>(*this);
mEntityManager = std::make_unique<EntityManager>(*this);
if (!levelFile.empty())
{
mSceneData = std::make_unique<Data::SavedData>(levelFile, levelName);
}
}
Framework::Scene::~Scene() = default;
uchar Framework::Scene::Deserialize(const uchar progress)
{
if (mSceneData == nullptr)
{
return 100;
}
const Data::Scope& myScope = mSceneData->GetScope();
if (progress <= 4)
{
mCamera->Deserialize(myScope);
return 5;
}
else if (progress <= 29)
{
mTerrain->Deserialize(myScope);
return 30;
}
else if (progress <= 80)
{
constexpr uchar progressStart = 30;
constexpr uchar progressEnd = 80;
const float entityProgress = mEntityManager->Deserialize(myScope, 5);
if (entityProgress == 1.0f)
{
return progressEnd + 1;
}
return static_cast<uchar>(Math::lerp(static_cast<float>(progressStart), static_cast<float>(progressEnd), entityProgress));
}
else if (progress < 100)
{
constexpr uchar progressStart = 81;
constexpr uchar progressEnd = 99;
const float terrainProgress = mTerrain->GenerateTerrain(1);
if (terrainProgress == 1.0f)
{
return progressEnd + 1;
}
return static_cast<uchar>(Math::lerp(static_cast<float>(progressStart), static_cast<float>(progressEnd), terrainProgress));
}
return 100; // Just to silence warning
}
void Framework::Scene::Tick()
{
mEntityManager->DeconstructDestroyedEntities();
mEntityManager->Tick();
mPhysics->Tick();
}
void Framework::Scene::Draw()
{
mCamera->DrawScene();
DrawImGui();
}
void Framework::Scene::Unload()
{
mEntityManager->Clear();
mEntityManager.reset();
mCamera.reset();
mTerrain.reset();
mPhysics.reset();
TimeManager::SetTimeScale(1.0f);
}
void Framework::Scene::Serialize(const std::string& saveName) const
{
std::string filepath = "saves/" + saveName + ".dat";
Framework::Data::SavedData::MakeEmpty(filepath);
Framework::Data::SavedData saveData = { filepath };
Framework::Data::Scope& globalScope = saveData.GetScope();
Data::Scope& myScope = globalScope.AddChild(saveName);
Serialize(myScope);
saveData.Save();
}
void Framework::Scene::Serialize(Data::Scope& myScope) const
{
mCamera->Serialize(myScope);
mTerrain->Serialize(myScope);
mEntityManager->Serialize(myScope);
}