-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCInstance.cpp
More file actions
executable file
·89 lines (72 loc) · 2.12 KB
/
CInstance.cpp
File metadata and controls
executable file
·89 lines (72 loc) · 2.12 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
//
// CInstance.cpp
// Fifth
//
// Created by Didrik Munther on 06/05/15.
// Copyright (c) 2015 Didrik Munther. All rights reserved.
//
#include "CInstance.h"
#include "NFile.h"
#include "CAssetManager.h"
CInstance::CInstance(CGame* game)
: game(game)
, L(luaL_newstate())
, camera(new CCamera())
, gravity(3.0f)
, isPaused(false)
, doLoadLevel(false)
, levelToLoad("")
, player(nullptr), controller(nullptr)
, loadedMap("")
{
}
CInstance::~CInstance() {
delete camera;
}
void CInstance::onLoop() {
if(doLoadLevel) {
_loadLevel(levelToLoad);
levelToLoad = "";
}
}
void CInstance::loadAssets(std::string path) {
//CAssetManager::onCleanup(CLEAN_FLAGS::NOT_LUA_SCRIPTS);
NFile::loadAssets(path, this);
}
void CInstance::doLine(std::string line) {
luaL_dostring(L, line.c_str());
}
void CInstance::loadLevel(std::string fileName) {
levelToLoad = fileName;
doLoadLevel = true;
}
void CInstance::_loadLevel(std::string fileName) {
entityManager.onCleanup();
player = nullptr;
controller = nullptr;
camera->setTarget(nullptr);
doLoadLevel = false;
if(NFile::loadLevel(fileName, this) == -1) {
NFile::log(LogType::ALERT, "Reverting loaded map to: \"", loadedMap, "\".");
NFile::loadLevel(loadedMap, this);
} else {
loadedMap = fileName;
}
}
std::string CInstance::onSerialize() {
rapidjson::Document d;
d.Parse("{}");
rapidjson::Value values(rapidjson::kObjectType);
entityManager.onSerialize(&values, &d.GetAllocator(), this);
values.AddMember("player", rapidjson::Value(entityManager.getNameOfEntity(player).c_str(), d.GetAllocator()), d.GetAllocator());
values.AddMember("controller", rapidjson::Value(entityManager.getNameOfEntity(controller).c_str(), d.GetAllocator()), d.GetAllocator());
d.AddMember("this", values, d.GetAllocator());
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
d.Accept(writer);
return sb.GetString();
}
void CInstance::closeInstance() {
entityManager.onCleanup();
lua_close(L);
}