summaryrefslogtreecommitdiffstats
path: root/src/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/control')
-rw-r--r--src/control/MapContext.cpp21
-rw-r--r--src/control/MapContext.hpp15
-rw-r--r--src/control/RPGEdit.cpp9
3 files changed, 30 insertions, 15 deletions
diff --git a/src/control/MapContext.cpp b/src/control/MapContext.cpp
index 5c715db..e3f9a49 100644
--- a/src/control/MapContext.cpp
+++ b/src/control/MapContext.cpp
@@ -31,8 +31,19 @@ namespace RPGEdit {
namespace Control {
-MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<const Model::Map> &map0)
- : eventBus(eventBus0), inputHandler(inputHandler0), map(map0) {
+MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, View::MapView *view0, const std::shared_ptr<const Model::Map> &map0)
+ : eventBus(eventBus0), inputHandler(inputHandler0), view(view0), map(map0) {
+ for (const Model::Entity &entity : map->getEntities())
+ entities.emplace_back(new Model::Entity(entity));
+
+ playerEntity = new Model::Entity("square");
+ playerEntity->moveTo(Model::Position{8, 8});
+
+ entities.emplace_back(playerEntity);
+
+ view->updateEntities(entities);
+
+
inputHandler->registerListener(
[this] (uint16_t key, bool pressed, uint64_t time) {
if (pressed)
@@ -42,14 +53,14 @@ MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const s
}
void MapContext::movePlayer(Model::Direction dir, uint64_t time) {
- if (map->getPlayerEntity()->hasTransition())
+ if (playerEntity->hasTransition())
return;
- map->getPlayerEntity()->move(dir, time, time+250);
+ playerEntity->move(dir, time, time+250);
eventBus->enqueue(
[=] {
- map->getPlayerEntity()->finishTransition();
+ playerEntity->finishTransition();
movePlayerContinue(time+250);
},
time+250
diff --git a/src/control/MapContext.hpp b/src/control/MapContext.hpp
index 37a38ea..a9481a6 100644
--- a/src/control/MapContext.hpp
+++ b/src/control/MapContext.hpp
@@ -44,19 +44,26 @@ private:
EventBus *const eventBus;
InputHandler *const inputHandler;
+ View::MapView *const view;
+
std::shared_ptr<const Model::Map> map;
- uint64_t totalTicks = 0;
+ std::vector<std::unique_ptr<Model::Entity>> entities;
+ Model::Entity *playerEntity;
void movePlayer(Model::Direction dir, uint64_t time);
void movePlayerContinue(uint64_t time);
void keyPressed(uint16_t key, uint64_t time);
+ Model::Position getViewPosition(uint64_t time) {
+ return playerEntity->getPosition(time);
+ }
+
public:
- MapContext(EventBus *eventBus0, InputHandler *inputHandler0, const std::shared_ptr<const Model::Map> &map0);
+ MapContext(EventBus *eventBus0, InputHandler *inputHandler0, View::MapView *view0, const std::shared_ptr<const Model::Map> &map0);
- Model::Position getViewPosition(uint64_t time) {
- return map->getPlayerEntity()->getPosition(time);
+ void render(uint64_t time) {
+ view->render(entities, getViewPosition(time), time);
}
};
diff --git a/src/control/RPGEdit.cpp b/src/control/RPGEdit.cpp
index 31998b2..e22d8fa 100644
--- a/src/control/RPGEdit.cpp
+++ b/src/control/RPGEdit.cpp
@@ -74,10 +74,7 @@ void RPGEdit::systemLoop() {
{
std::unique_lock<std::mutex> lock(modelMutex);
- uint64_t time = std::min(timeProvider.now(), handledTime);
-
- Model::Position pos = ctx->getViewPosition(time);
- mapView->render(pos.x, pos.y, time);
+ ctx->render(std::min(timeProvider.now(), handledTime));
}
SDL_RenderPresent(window->getRenderer());
@@ -104,11 +101,11 @@ void RPGEdit::eventLoop() {
void RPGEdit::run() {
std::shared_ptr<Model::Map> map = Model::Map::load("test");
- ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, map);
-
window = std::make_shared<View::Window>();
mapView = std::make_shared<View::MapView>(window, map);
+ ctx = std::make_shared<MapContext>(&eventBus, &inputHandler, mapView.get(), map);
+
eventThread = std::thread([this] { eventLoop(); });
systemLoop();