diff options
Diffstat (limited to 'src/control/MapContext.cpp')
-rw-r--r-- | src/control/MapContext.cpp | 74 |
1 files changed, 49 insertions, 25 deletions
diff --git a/src/control/MapContext.cpp b/src/control/MapContext.cpp index f4dd31e..a981e8a 100644 --- a/src/control/MapContext.cpp +++ b/src/control/MapContext.cpp @@ -31,8 +31,8 @@ namespace RPGEdit { namespace Control { -MapContext::MapContext(ImageLoader *imageLoader0, const std::shared_ptr<const Model::Map> &map0) - : imageLoader(imageLoader0), map(map0) { +MapContext::MapContext(EventBus *eventBus0, InputHandler *inputHandler0, ImageLoader *imageLoader0, const std::shared_ptr<const Model::Map> &map0) + : eventBus(eventBus0), inputHandler(inputHandler0), imageLoader(imageLoader0), map(map0) { const std::vector<std::string> &tileset = map->getTileset(); tiles.resize(tileset.size()); @@ -43,38 +43,62 @@ MapContext::MapContext(ImageLoader *imageLoader0, const std::shared_ptr<const Mo for (const std::shared_ptr<Model::Entity> &entity : mapEntities) entities[entity->getName()] = imageLoader->get("entity/" + entity->getName()); + + inputHandler->registerListener( + [this] (uint16_t key, bool pressed, uint64_t time) { + if (pressed) + keyPressed(key, time); + } + ); } -void MapContext::advance(InputHandler *inputHandler, uint32_t ticks) { - uint64_t totalTicksOld = totalTicks; - totalTicks += ticks; +void MapContext::movePlayer(Model::Direction dir, uint64_t time) { + if (map->getPlayerEntity()->hasTransition()) + return; - unsigned advance = totalTicks - totalTicksOld; + map->getPlayerEntity()->move(dir, time, time+250); - while (advance) { - advance = map->getPlayerEntity().advance(advance); + eventBus->enqueue( + [=] { + map->getPlayerEntity()->finishTransition(); + movePlayerContinue(time+250); + }, + time+250 + ); +} - if (map->getPlayerEntity().hasTransition()) - break; +void MapContext::movePlayerContinue(uint64_t time) { + if (inputHandler->isKeyPressed(SDL_SCANCODE_UP)) + movePlayer(Model::NORTH, time); + else if (inputHandler->isKeyPressed(SDL_SCANCODE_RIGHT)) + movePlayer(Model::EAST, time); + else if (inputHandler->isKeyPressed(SDL_SCANCODE_DOWN)) + movePlayer(Model::SOUTH, time); + else if (inputHandler->isKeyPressed(SDL_SCANCODE_LEFT)) + movePlayer(Model::WEST, time); +} - if (inputHandler->isKeyPressed(SDL_SCANCODE_UP)) { - map->getPlayerEntity().move(Model::NORTH, 250); - } - else if (inputHandler->isKeyPressed(SDL_SCANCODE_RIGHT)) { - map->getPlayerEntity().move(Model::EAST, 250); - } - else if (inputHandler->isKeyPressed(SDL_SCANCODE_DOWN)) { - map->getPlayerEntity().move(Model::SOUTH, 250); - } - else if (inputHandler->isKeyPressed(SDL_SCANCODE_LEFT)) { - map->getPlayerEntity().move(Model::WEST, 250); - } - else { - break; - } + +void MapContext::keyPressed(uint16_t key, uint64_t time) { + switch (key) { + case SDL_SCANCODE_UP: + movePlayer(Model::NORTH, time); + break; + + case SDL_SCANCODE_RIGHT: + movePlayer(Model::EAST, time); + break; + + case SDL_SCANCODE_DOWN: + movePlayer(Model::SOUTH, time); + break; + + case SDL_SCANCODE_LEFT: + movePlayer(Model::WEST, time); } } + } } |