X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=76ded2db2c9b15bc35ba637bf5567ccdfee369d4;hb=7fccdf2cd07afb3afdc0b854e9a03130ef202eec;hp=01568d91fbd6ca2d01068b494dba3ebd93ea1e8e;hpb=4a51a83bdff30d1e25a5867cfb19936adc0034b1;p=orbi.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 01568d9..76ded2d 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_s: + target.StopDown(); break; - case SDLK_DOWN: - focus.StopDown(); + case SDLK_a: + ctrl.StopLeft(); + target.StopLeft(); break; - case SDLK_LEFT: - focus.StopLeft(); + case SDLK_d: + ctrl.StopRight(); + target.StopRight(); break; - case SDLK_RIGHT: - focus.StopRight(); + 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; + for (int i = 0; i < dt; ++i) { + ctrl.Update(1e-3); + world.Update(1e-3); + } + target.Update(delta); + focus = ctrl.Controlling() + ? ctrl.Controlled().vbox.Center() + : target.Pos(); cam.Update(delta); - world.Update(dt); - focus.Update(delta); } @@ -142,9 +169,13 @@ void Application::Render() { void Application::RenderBackground() { constexpr Color background(0x00, 0x00, 0x00); + constexpr Color outlineColor(0x00, 0x00, 0xFA); canvas.SetColor(background); canvas.Fill(); + + canvas.SetColor(outlineColor); + canvas.Grid(cam.ToScreen(Vector(0, 0)), cam.ToScale(world.Size()), cam.ToScale(world.TileSize())); } void Application::RenderWorld() { @@ -159,25 +190,42 @@ void Application::RenderWorld() { } void Application::RenderEntities() { - constexpr Color entityColor(0x00, 0xFA, 0x00); - canvas.SetColor(entityColor); + constexpr Color boundsColor(0xFA, 0x00, 0x00); + constexpr Color vboxColor(0xFA, 0xFA, 0x00); + constexpr Color hboxColor(0x00, 0xFA, 0x00); for (const Entity &e : world.Entities()) { - const Vector pos(e.Bounds().Left(), e.Bounds().Top()); - const Vector size(e.Bounds().Size()); - canvas.OutlineRect(cam.ToScreen(pos), cam.ToScale(size)); + canvas.SetColor(boundsColor); + canvas.OutlineRect( + cam.ToScreen(Vector(e.bounds.Left(), e.bounds.Top())), + cam.ToScale(Vector(e.bounds.Size())) + ); + canvas.SetColor(vboxColor); + canvas.Line( + cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Top())), + cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Top())) - Vector(1, 0) + ); + canvas.Line( + cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Bottom())) - Vector(0, 1), + cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Bottom())) - Vector(1, 1) + ); + canvas.SetColor(hboxColor); + canvas.Line( + cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Top())), + cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Bottom())) - Vector(0, 1) + ); + canvas.Line( + cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Top())) - Vector(1, 0), + cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Bottom())) - Vector(1, 1) + ); } } void Application::RenderUI() { - constexpr Color outlineColor(0x00, 0x00, 0xFA); - constexpr Color focusColor(0xFA, 0xFA, 0x00); - - canvas.SetColor(outlineColor); - canvas.Grid(cam.ToScreen(Vector(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector(1, 1))); + constexpr Color targetColor(0xFA, 0xFA, 0x00); - canvas.SetColor(focusColor); - canvas.Cross(cam.ToScreen(focus.Pos()), 15); + canvas.SetColor(targetColor); + canvas.Cross(cam.ToScreen(target.Pos()), 15); } }