Skip to content

Commit 28b1f6b

Browse files
committed
Need to add menu buttons and stuff
Signed-off-by: Dan Printzell <[email protected]>
1 parent 35145e2 commit 28b1f6b

File tree

12 files changed

+135
-68
lines changed

12 files changed

+135
-68
lines changed

docs/States.org

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class World {
1212
class State {
1313
+State()
1414
+getWorld(): World&
15-
+onEnter(State& prev):
16-
+onLeave(State& next):
15+
+onEnter(State* prev):
16+
+onLeave(State* next):
1717
}
1818

1919
class MainMenuState

docs/States.png

-160 Bytes
Loading

src/engine.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "world/renderpass/textrenderpass.hpp"
3333

3434
#include "state/ingamestate.hpp"
35+
#include "state/mainmenustate.hpp"
3536

3637
Engine::~Engine() {
3738
TTF_Quit();
@@ -77,7 +78,7 @@ int Engine::run(bool vsync) {
7778
_height = event.window.data2;
7879
_system_resize(_width, _height);
7980

80-
World& world = _states[getCurrentState()]->getWorld();
81+
World& world = getState().getWorld();
8182
for (std::unique_ptr<Entity>& entity : world.getEntities()) {
8283
CameraComponent* cc = entity->getComponent<CameraComponent>();
8384
if (!cc)
@@ -110,6 +111,8 @@ int Engine::run(bool vsync) {
110111
_system_tick(delta);
111112
ImGui::Render();
112113
SDL_GL_SwapWindow(_window);
114+
if (!getStatePtr())
115+
break;
113116
}
114117
return 0;
115118
}
@@ -124,8 +127,12 @@ void Engine::_init(bool vsync) {
124127
_hidInput = std::make_shared<HIDInput>();
125128
_textFactory = std::make_shared<TextFactory>("assets/fonts/font.png");
126129

127-
_currentState = std::make_unique<std::type_index>(typeid(InGameState));
130+
_currentState = std::make_unique<std::type_index>(std::type_index(typeid(nullptr)));
131+
132+
_states[std::type_index(typeid(nullptr))] = std::unique_ptr<State>();
128133
_states[std::type_index(typeid(InGameState))] = std::make_unique<InGameState>();
134+
_states[std::type_index(typeid(MainMenuState))] = std::make_unique<MainMenuState>();
135+
setState<MainMenuState>();
129136
_setupSystems();
130137
}
131138

@@ -283,7 +290,7 @@ void Engine::_setupSystems() {
283290
}
284291

285292
void Engine::_system_tick(float delta) {
286-
World& world = _states[getCurrentState()]->getWorld();
293+
World& world = getState().getWorld();
287294
for (const std::unique_ptr<System>& system : _systems)
288295
system->update(world, delta);
289296
}

src/engine.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,25 @@ class Engine {
4444
inline std::vector<std::unique_ptr<System>>& getSystems() { return _systems; }
4545

4646
template <typename T>
47-
inline std::unique_ptr<T>& getState() {
48-
return _states[std::type_index(typeid(T))];
47+
inline std::unordered_map<std::type_index, std::unique_ptr<State>>& getStates() {
48+
return _states;
4949
}
5050

51-
inline const std::type_index getCurrentState() { return *_currentState; }
51+
inline State& getState() { return *_states[*_currentState]; }
52+
inline State* getStatePtr() { return _states[*_currentState].get(); }
53+
54+
inline const std::type_index getStateType() { return *_currentState; }
5255

5356
template <typename T>
54-
inline void setCurrentState() {
57+
inline void setState() {
58+
State* prev = getStatePtr();
5559
*_currentState = std::type_index(typeid(T));
60+
State* next = getStatePtr();
61+
if (next)
62+
next->onEnter(prev);
63+
64+
if (prev)
65+
prev->onLeave(next);
5666
}
5767

5868
private:

src/state/ingamestate.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../engine.hpp"
55

66
#include "../lib/glad.h"
7+
#include "../lib/imgui.h"
78
#include "../gl/shader.hpp"
89

910
#include "../world/component/transformcomponent.hpp"
@@ -19,24 +20,22 @@
1920
InGameState::InGameState() {
2021
auto& engine = Engine::getInstance();
2122

22-
Entity* camera = _world.addEntity(sole::rebuild("f8bb5ea8-e3fb-4ec7-939d-5d70ae3e9d12"), "Camera");
23-
Entity* player = _world.addEntity(sole::rebuild("31bcc9bd-78bb-45b7-bb86-1917bcf5df6d"), "Player");
24-
Entity* floor = _world.addEntity(sole::rebuild("b056cfea-b2cd-4c91-b921-5b8ee6b286d6"), "Floor");
23+
_camera = _world.addEntity(sole::rebuild("f8bb5ea8-e3fb-4ec7-939d-5d70ae3e9d12"), "Camera");
24+
_player = _world.addEntity(sole::rebuild("31bcc9bd-78bb-45b7-bb86-1917bcf5df6d"), "Player");
25+
_floor = _world.addEntity(sole::rebuild("b056cfea-b2cd-4c91-b921-5b8ee6b286d6"), "Floor");
2526

2627
{ // Adding Camera
27-
camera->addComponent<TransformComponent>();
28-
camera->addComponent<CameraComponent>();
29-
auto lookAt = camera->addComponent<LookAtComponent>();
30-
lookAt->target = player;
31-
lookAt->followMode = FollowMode::followByOffset;
32-
lookAt->offsetFromTarget = glm::vec3(0, 2.5, -5);
28+
_camera->addComponent<TransformComponent>();
29+
_camera->addComponent<CameraComponent>();
30+
_addLookAt();
31+
_camera->registerImGui = &InGameState::_registerImGUI;
3332
}
3433

3534
{ // Adding Player
36-
auto transform = player->addComponent<TransformComponent>();
35+
auto transform = _player->addComponent<TransformComponent>();
3736
transform->scale = glm::vec3(0.01);
3837
transform->recalculateMatrix();
39-
auto model = player->addComponent<ModelComponent>();
38+
auto model = _player->addComponent<ModelComponent>();
4039
model->meshData = engine.getMeshLoader()->getMesh("assets/objects/player.fbx");
4140
model->meshData->mesh
4241
->addBuffer("m",
@@ -53,22 +52,22 @@ InGameState::InGameState() {
5352
glBindBuffer(GL_ARRAY_BUFFER, 0);
5453
})
5554
.finalize();
56-
auto particle = player->addComponent<ParticleComponent>();
55+
auto particle = _player->addComponent<ParticleComponent>();
5756
particle->addEmitter(glm::vec3(0, 1, 0), 1024);
58-
player->addComponent<KBMouseInputComponent>();
59-
player->addComponent<PhysicsComponent>();
60-
auto text = player->addComponent<TextComponent>();
57+
_player->addComponent<KBMouseInputComponent>();
58+
_player->addComponent<PhysicsComponent>();
59+
auto text = _player->addComponent<TextComponent>();
6160
text->textRenderer = engine.getTextFactory()->makeRenderer("Hello, My name is Mr. Duck!\x01");
6261

6362
text->transform.position = glm::vec3(0, 2, 0);
6463
text->transform.scale = glm::vec3(100 * 2); // To counteract transform->scale
6564
text->transform.recalculateMatrix();
6665
}
6766

68-
{ // Adding Floor
67+
{ // Adding Floor
6968
constexpr int gridSize = 8; // will be gridSize*gridSize
7069

71-
auto transform = floor->addComponent<FloorTransformComponent>();
70+
auto transform = _floor->addComponent<FloorTransformComponent>();
7271
transform->gridSize = gridSize;
7372
transform->scale = glm::vec3(1, 0.1, 1);
7473
transform->recalculateMatrices();
@@ -115,7 +114,7 @@ InGameState::InGameState() {
115114
point[backwards] = std::min(minFloor, point[backwards]);
116115
}
117116

118-
auto model = floor->addComponent<ModelComponent>();
117+
auto model = _floor->addComponent<ModelComponent>();
119118
model->meshData = engine.getMeshLoader()->getMesh("assets/objects/box.obj");
120119
model->meshData->mesh
121120
->addBuffer("m",
@@ -157,5 +156,25 @@ InGameState::InGameState() {
157156
delete[] topData;
158157
delete[] neighborData;
159158
}
159+
}
160+
161+
void InGameState::onEnter(State* prev) {}
162+
void InGameState::onLeave(State* next) {}
160163

164+
void InGameState::_addLookAt() {
165+
auto lookAt = _camera->addComponent<LookAtComponent>();
166+
lookAt->target = _player;
167+
lookAt->followMode = FollowMode::followByOffset;
168+
lookAt->offsetFromTarget = glm::vec3(0, 2.5, -5);
161169
}
170+
171+
void InGameState::_registerImGUI(Entity& self, State& state) {
172+
static bool follow = true;
173+
InGameState& this_ = static_cast<InGameState&>(state);
174+
if (ImGui::Checkbox("Follow target", &follow)) {
175+
if (follow)
176+
this_._addLookAt();
177+
else
178+
self.removeComponent<LookAtComponent>();
179+
}
180+
}

src/state/ingamestate.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,16 @@
55
class InGameState : public State {
66
public:
77
InGameState();
8+
9+
virtual void onEnter(State* prev);
10+
virtual void onLeave(State* next);
11+
12+
private:
13+
Entity* _camera;
14+
Entity* _player;
15+
Entity* _floor;
16+
17+
void _addLookAt();
18+
19+
static void _registerImGUI(Entity& self, State& state);
820
};

src/state/mainmenustate.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "mainmenustate.hpp"
2+
3+
#include "../world/component/transformcomponent.hpp"
4+
#include "../world/component/cameracomponent.hpp"
5+
#include "../world/component/particlecomponent.hpp"
6+
#include "../world/component/textcomponent.hpp"
7+
#include "../world/component/modelcomponent.hpp"
8+
9+
MainMenuState::MainMenuState() {
10+
_camera = _world.addEntity(sole::uuid4(), "Camera");
11+
_title = _world.addEntity(sole::uuid4(), "Title");
12+
_play = _world.addEntity(sole::uuid4(), "Play");
13+
_quit = _world.addEntity(sole::uuid4(), "Quit");
14+
15+
{
16+
_camera->addComponent<TransformComponent>();
17+
_camera->addComponent<CameraComponent>();
18+
}
19+
20+
{ auto transform = _title->addComponent<TransformComponent>(); }
21+
22+
{ auto transform = _play->addComponent<TransformComponent>(); }
23+
24+
{ auto transform = _quit->addComponent<TransformComponent>(); }
25+
}
26+
27+
void MainMenuState::onEnter(State* prev) {}
28+
void MainMenuState::onLeave(State* next) {}

src/state/mainmenustate.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "state.hpp"
4+
5+
class MainMenuState : public State {
6+
public:
7+
MainMenuState();
8+
9+
virtual void onEnter(State* prev);
10+
virtual void onLeave(State* next);
11+
12+
private:
13+
Entity* _camera;
14+
Entity* _title;
15+
Entity* _play;
16+
Entity* _quit;
17+
};

src/state/state.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
class State {
77
public:
8-
//virtual void onEnter(State& prev);
9-
//virtual void onLeave(State& next);
8+
virtual void onEnter(State* prev) = 0;
9+
virtual void onLeave(State* next) = 0;
1010

1111
inline World& getWorld() { return _world; }
1212

src/world/entity.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// Kinda like GameObject - https://docs.unity3d.com/ScriptReference/GameObject.html
1414

1515
class World;
16+
class State;
1617

1718
class Entity {
1819
public:
@@ -48,25 +49,26 @@ class Entity {
4849

4950
template <typename T, typename std::enable_if<std::is_base_of<IComponent, T>::value>::type* = nullptr>
5051
void removeComponent() {
51-
auto list = T::getActiveComponents();
52-
for (auto it = list.begin(); it != list.end(); it++) {
53-
auto com = std::dynamic_pointer_cast<T>(*it);
52+
for (auto it = _components.begin(); it != _components.end(); it++) {
53+
T* com = dynamic_cast<T*>(*it);
5454
if (!com)
5555
continue;
56-
list.erase(it);
56+
_components.erase(it);
5757
break;
5858
}
5959

60-
for (auto it = _components.begin(); it != _components.end(); it++) {
61-
auto com = dynamic_cast<T*>(*it);
60+
auto& list = T::getActiveComponents();
61+
for (auto it = list.begin(); it != list.end(); it++) {
62+
T* com = dynamic_cast<T*>(it->get());
6263
if (!com)
6364
continue;
64-
_components.erase(it);
65+
list.erase(it);
6566
break;
6667
}
6768
}
6869

69-
void (*registerImGui)(Entity* entity) = nullptr;
70+
typedef void (*registerImGui_f)(Entity& self, State& state);
71+
registerImGui_f registerImGui = nullptr;
7072

7173
inline sole::uuid& getUUID() { return _uuid; }
7274
inline std::string& getName() { return _name; }

0 commit comments

Comments
 (0)