]> git.localhorst.tv Git - orbi.git/blobdiff - src/app/Application.cpp
orientation for entities
[orbi.git] / src / app / Application.cpp
index 01568d91fbd6ca2d01068b494dba3ebd93ea1e8e..048ec263a4c56be5a779e90e793a0c6602044022 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "../graphics/Canvas.h"
 #include "../graphics/Color.h"
+#include "../world/Character.h"
 #include "../world/Entity.h"
 #include "../world/Tileset.h"
 #include "../world/World.h"
@@ -13,8 +14,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 +25,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 +97,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_DOWN:
-                       focus.MoveDown();
+               case SDLK_s:
+                       target.MoveDown();
                        break;
-               case SDLK_LEFT:
-                       focus.MoveLeft();
+               case SDLK_a:
+                       ctrl.MoveLeft();
+                       target.MoveLeft();
                        break;
-               case SDLK_RIGHT:
-                       focus.MoveRight();
+               case SDLK_d:
+                       ctrl.MoveRight();
+                       target.MoveRight();
+                       break;
+               case SDLK_SPACE:
+                       ctrl.StartJump();
                        break;
                case SDLK_p:
                        paused = !paused;
@@ -107,17 +124,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 +149,18 @@ 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);
+
+       // testing arm rotation
+       //dynamic_cast<Character *>(*world.Entities().begin())->armAngle -= 3 * delta;
 }
 
 
@@ -142,9 +173,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<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(world.TileSize()));
 }
 
 void Application::RenderWorld() {
@@ -159,25 +194,60 @@ void Application::RenderWorld() {
 }
 
 void Application::RenderEntities() {
-       constexpr Color entityColor(0x00, 0xFA, 0x00);
-       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());
-               canvas.OutlineRect(cam.ToScreen(pos), cam.ToScale(size));
+       constexpr Color boundsColor(0xFA, 0x00, 0x00);
+       constexpr Color vboxColor(0xFA, 0xFA, 0x00);
+       constexpr Color hboxColor(0x00, 0xFA, 0x00);
+       constexpr Color dotColor(0x00, 0xFA, 0xFA);
+
+       for (const Entity *e : world.Entities()) {
+               canvas.SetColor(boundsColor);
+               canvas.OutlineRect(
+                       cam.ToScreen(e->bounds.LeftTop()),
+                       cam.ToScale(e->bounds.Size())
+               );
+               if (const Character *c = dynamic_cast<const Character *>(e)) {
+                       if (c->orient == Entity::LOOKS_LEFT) {
+                               canvas.OutlineRectRot(
+                                       Rect<float>(
+                                               cam.ToScreen(c->bounds.LeftTop() + c->arm.LeftTop()),
+                                               cam.ToScale(c->arm.Size())
+                                       ),
+                                       cam.ToScreen(c->bounds.LeftTop() + c->armOrigin),
+                                       c->armAngle
+                               );
+                               //canvas.SetColor(dotColor);
+                               //canvas.Cross(cam.ToScreen(c->bounds.LeftTop() + c->armOrigin), 5);
+                       } else {
+                               canvas.OutlineRectRot(
+                                       Rect<float>(
+                                               cam.ToScreen(c->bounds.RightTop() + MirrorY(c->arm.RightTop())),
+                                               cam.ToScale(c->arm.Size())
+                                       ),
+                                       cam.ToScreen(c->bounds.RightTop() + MirrorY(c->armOrigin)),
+                                       -c->armAngle
+                               );
+                               //canvas.SetColor(dotColor);
+                               //canvas.Cross(cam.ToScreen(c->bounds.RightTop() + MirrorY(c->armOrigin)), 5);
+                       }
+               }
+               canvas.SetColor(vboxColor);
+               canvas.OutlineRect(
+                       cam.ToScreen(e->vbox.LeftTop()),
+                       cam.ToScale(e->vbox.Size())
+               );
+               canvas.SetColor(hboxColor);
+               canvas.OutlineRect(
+                       cam.ToScreen(e->hbox.LeftTop()),
+                       cam.ToScale(e->hbox.Size())
+               );
        }
 }
 
 void Application::RenderUI() {
-       constexpr Color outlineColor(0x00, 0x00, 0xFA);
-       constexpr Color focusColor(0xFA, 0xFA, 0x00);
-
-       canvas.SetColor(outlineColor);
-       canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(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);
 }
 
 }