]> git.localhorst.tv Git - orbi.git/blobdiff - src/app/Application.cpp
simple controller
[orbi.git] / src / app / Application.cpp
index 01568d91fbd6ca2d01068b494dba3ebd93ea1e8e..cbfafd374537935625dbaad63955d4d7edacbcd6 100644 (file)
@@ -13,8 +13,10 @@ Application::Application(Canvas &c, World &w, Tileset &t)
 : canvas(c)
 , world(w)
 , tiles(t)
-, focus(Vector<float>(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<float> pos(e.Bounds().Left(), e.Bounds().Top());
-               const Vector<float> size(e.Bounds().Size());
+               const Vector<float> pos(e.bounds.Left(), e.bounds.Top());
+               const Vector<float> 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<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
 
-       canvas.SetColor(focusColor);
-       canvas.Cross(cam.ToScreen(focus.Pos()), 15);
+       canvas.SetColor(targetColor);
+       canvas.Cross(cam.ToScreen(target.Pos()), 15);
 }
 
 }