X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;fp=src%2Fapp%2FApplication.cpp;h=cbfafd374537935625dbaad63955d4d7edacbcd6;hb=dbc08d84d9de1a77cba0dd97e4701f4ac99d056e;hp=01568d91fbd6ca2d01068b494dba3ebd93ea1e8e;hpb=79a34acdc1beff20213f03c326a4aa03c5d47c92;p=orbi.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 01568d9..cbfafd3 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -13,8 +13,10 @@ Application::Application(Canvas &c, World &w, Tileset &t) : canvas(c) , world(w) , tiles(t) -, focus(Vector(5, 5), 2) -, cam(c.Size(), focus.Pos()) +, ctrl() +, focus(5, 5) +, target(focus, 2) +, cam(c.Size(), focus) , last(SDL_GetTicks()) , running(false) , paused(false) { @@ -22,6 +24,15 @@ Application::Application(Canvas &c, World &w, Tileset &t) } +void Application::Control(Entity &e) { + ctrl.Control(e); +} + +void Application::Relinquish() { + ctrl.Relinquish(); +} + + void Application::Run() { running = true; while (running) { @@ -85,17 +96,22 @@ void Application::HandleEvents() { void Application::OnKeyDown(const SDL_KeyboardEvent &e) { switch (e.keysym.sym) { - case SDLK_UP: - focus.MoveUp(); + case SDLK_w: + target.MoveUp(); + break; + case SDLK_s: + target.MoveDown(); break; - case SDLK_DOWN: - focus.MoveDown(); + case SDLK_a: + ctrl.MoveLeft(); + target.MoveLeft(); break; - case SDLK_LEFT: - focus.MoveLeft(); + case SDLK_d: + ctrl.MoveRight(); + target.MoveRight(); break; - case SDLK_RIGHT: - focus.MoveRight(); + case SDLK_SPACE: + ctrl.StartJump(); break; case SDLK_p: paused = !paused; @@ -107,17 +123,22 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) { void Application::OnKeyUp(const SDL_KeyboardEvent &e) { switch (e.keysym.sym) { - case SDLK_UP: - focus.StopUp(); + case SDLK_w: + target.StopUp(); break; - case SDLK_DOWN: - focus.StopDown(); + case SDLK_s: + target.StopDown(); break; - case SDLK_LEFT: - focus.StopLeft(); + case SDLK_a: + ctrl.StopLeft(); + target.StopLeft(); break; - case SDLK_RIGHT: - focus.StopRight(); + case SDLK_d: + ctrl.StopRight(); + target.StopRight(); + break; + case SDLK_SPACE: + ctrl.StopJump(); break; default: break; @@ -127,9 +148,15 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) { void Application::Update(int dt) { const float delta = dt / 1e3; + ctrl.Update(delta); + target.Update(delta); + focus = ctrl.Controlling() + ? ctrl.Controlled().bounds.Center() + : target.Pos(); cam.Update(delta); - world.Update(dt); - focus.Update(delta); + for (int i = 0; i < dt; ++i) { + world.Update(1e-3); + } } @@ -163,21 +190,21 @@ void Application::RenderEntities() { canvas.SetColor(entityColor); for (const Entity &e : world.Entities()) { - const Vector pos(e.Bounds().Left(), e.Bounds().Top()); - const Vector size(e.Bounds().Size()); + const Vector pos(e.bounds.Left(), e.bounds.Top()); + const Vector size(e.bounds.Size()); canvas.OutlineRect(cam.ToScreen(pos), cam.ToScale(size)); } } void Application::RenderUI() { constexpr Color outlineColor(0x00, 0x00, 0xFA); - constexpr Color focusColor(0xFA, 0xFA, 0x00); + constexpr Color targetColor(0xFA, 0xFA, 0x00); canvas.SetColor(outlineColor); canvas.Grid(cam.ToScreen(Vector(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector(1, 1))); - canvas.SetColor(focusColor); - canvas.Cross(cam.ToScreen(focus.Pos()), 15); + canvas.SetColor(targetColor); + canvas.Cross(cam.ToScreen(target.Pos()), 15); } }