]> git.localhorst.tv Git - orbi.git/commitdiff
rotatable arm for character entities
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 8 May 2014 18:47:25 +0000 (20:47 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 8 May 2014 18:47:25 +0000 (20:47 +0200)
src/app/Application.cpp
src/graphics/Canvas.cpp
src/graphics/Canvas.h
src/graphics/Vector.h
src/orbi.cpp
src/world/Character.h [new file with mode: 0644]
src/world/Entity.h

index 3ba8485db63554d012f05fd0de5a56ca9b37f283..3ddb41443a3d4bd6860603cd69a7e1073b670a87 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"
@@ -157,6 +158,9 @@ void Application::Update(int dt) {
                ? ctrl.Controlled().vbox.Center()
                : target.Pos();
        cam.Update(delta);
+
+       // testing arm rotation
+       //dynamic_cast<Character *>(*world.Entities().begin())->armAngle -= 3 * delta;
 }
 
 
@@ -197,26 +201,28 @@ void Application::RenderEntities() {
        for (const Entity *e : world.Entities()) {
                canvas.SetColor(boundsColor);
                canvas.OutlineRect(
-                       cam.ToScreen(Vector<float>(e->bounds.Left(), e->bounds.Top())),
-                       cam.ToScale(Vector<float>(e->bounds.Size()))
+                       cam.ToScreen(e->bounds.Position()),
+                       cam.ToScale(e->bounds.Size())
                );
+               if (const Character *c = dynamic_cast<const Character *>(e)) {
+                       canvas.OutlineRectRot(
+                               Rect<float>(
+                                       cam.ToScreen(c->bounds.Position() + c->arm.Position()),
+                                       cam.ToScale(c->arm.Size())
+                               ),
+                               cam.ToScreen(c->bounds.Position() + c->armOrigin),
+                               c->armAngle
+                       );
+               }
                canvas.SetColor(vboxColor);
-               canvas.Line(
-                       cam.ToScreen(Vector<float>(e->vbox.Left(), e->vbox.Top())),
-                       cam.ToScreen(Vector<float>(e->vbox.Right(), e->vbox.Top())) - Vector<int>(1, 0)
-               );
-               canvas.Line(
-                       cam.ToScreen(Vector<float>(e->vbox.Left(), e->vbox.Bottom())) - Vector<int>(0, 1),
-                       cam.ToScreen(Vector<float>(e->vbox.Right(), e->vbox.Bottom())) - Vector<int>(1, 1)
+               canvas.OutlineRect(
+                       cam.ToScreen(e->vbox.Position()),
+                       cam.ToScale(e->vbox.Size())
                );
                canvas.SetColor(hboxColor);
-               canvas.Line(
-                       cam.ToScreen(Vector<float>(e->hbox.Left(), e->hbox.Top())),
-                       cam.ToScreen(Vector<float>(e->hbox.Left(), e->hbox.Bottom())) - Vector<int>(0, 1)
-               );
-               canvas.Line(
-                       cam.ToScreen(Vector<float>(e->hbox.Right(), e->hbox.Top())) - Vector<int>(1, 0),
-                       cam.ToScreen(Vector<float>(e->hbox.Right(), e->hbox.Bottom())) - Vector<int>(1, 1)
+               canvas.OutlineRect(
+                       cam.ToScreen(e->hbox.Position()),
+                       cam.ToScale(e->hbox.Size())
                );
        }
 }
index 3f1c3d117ef1473446d91dc5096b146698d34b93..9a163e968d4b7321abc9c1520abd5731697e81b2 100644 (file)
@@ -81,22 +81,26 @@ void Canvas::Line(Vector<int> from, Vector<int> to) {
        SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y);
 }
 
-void Canvas::FillRect(Vector<int> pos, Vector<int> size) {
-       SDL_Rect destRect;
-       destRect.x = pos.x;
-       destRect.y = pos.y;
-       destRect.w = size.x;
-       destRect.h = size.y;
-       SDL_RenderFillRect(canv, &destRect);
+void Canvas::FillRect(Rect<int> rect) {
+       SDL_RenderFillRect(canv, &rect);
 }
 
-void Canvas::OutlineRect(Vector<int> pos, Vector<int> size) {
-       SDL_Rect destRect;
-       destRect.x = pos.x;
-       destRect.y = pos.y;
-       destRect.w = size.x;
-       destRect.h = size.y;
-       SDL_RenderDrawRect(canv, &destRect);
+void Canvas::OutlineRect(Rect<int> rect) {
+       SDL_RenderDrawRect(canv, &rect);
+}
+
+void Canvas::OutlineRectRot(Rect<float> rect, Vector<float> origin, float rot) {
+       const Vector<float> topLeft(rect.x, rect.y);
+       const Vector<float> topRight(rect.x + rect.w - 1, rect.y);
+       const Vector<float> botLeft(rect.x, rect.y + rect.h - 1);
+       const Vector<float> botRight(rect.x + rect.w - 1, rect.y + rect.h - 1);
+
+       Quad(
+               Rotate(topLeft - origin, rot) + origin,
+               Rotate(topRight - origin, rot) + origin,
+               Rotate(botRight - origin, rot) + origin,
+               Rotate(botLeft - origin, rot) + origin
+       );
 }
 
 
index 021bcedff936c4f5d2655ff977c2b9f80e26592b..fdcc5a03d840b18f34c9bb4af18758d069eff916 100644 (file)
@@ -2,6 +2,7 @@
 #define ORBI_CANVAS_H_
 
 #include "Color.h"
+#include "Rect.h"
 #include "Texture.h"
 #include "Vector.h"
 
@@ -44,8 +45,16 @@ public:
        void Outline();
 
        void Line(Vector<int> from, Vector<int> to);
-       void FillRect(Vector<int> pos, Vector<int> size);
-       void OutlineRect(Vector<int> pos, Vector<int> size);
+       void FillRect(Rect<int>);
+       void OutlineRect(Rect<int>);
+       void OutlineRectRot(Rect<float>, Vector<float> origin, float rot);
+
+       void FillRect(Vector<int> pos, Vector<int> size) {
+               FillRect(Rect<int>(pos, size));
+       }
+       void OutlineRect(Vector<int> pos, Vector<int> size) {
+               OutlineRect(Rect<int>(pos, size));
+       }
 
        void Dot(Vector<int> pos);
        void Cross(Vector<int> pos, int extent);
index 6259b97f0cb9a78fab1b389347113b28a94e9a33..958be232b74ea991f3471d9de2e5c7ef229d085c 100644 (file)
@@ -210,6 +210,7 @@ template<class Scalar>
 constexpr Vector<Scalar> Rotate270(Vector<Scalar> v) {
        return Vector<Scalar>(v.y, -v.x);
 }
+// angle given in radians
 template<class Scalar, class Float>
 inline Vector<Scalar> Rotate(Vector<Scalar> v, Float by) {
        Float sine(std::sin(by));
index 67409e6888f9122a0aeca50389ed24ba44c1f59b..87e3866645203e08941b5bec2db69497386b3456 100644 (file)
@@ -5,6 +5,7 @@
 #include "graphics/Canvas.h"
 #include "graphics/Window.h"
 #include "world/AABB.h"
+#include "world/Character.h"
 #include "world/Entity.h"
 #include "world/Tile.h"
 #include "world/Tileset.h"
@@ -52,12 +53,14 @@ int main(int argc, const char *argv[]) {
        world.SetTile(Vector<int>(2, 9), Tile(0));
        world.SetTile(Vector<int>(3, 9), Tile(0));
 
-       Entity e;
-       e.bounds = AABB(0, 0, 2, 3);
-       e.vbox = AABB(.1, 0, 1.8, 3);
-       e.hbox = AABB(0, .1, 2, 1.8);
-       e.Move(Vector<float>(5, 0));
-       world.AddEntity(e);
+       Character player;
+       player.bounds = AABB(0, 0, 2, 3);
+       player.vbox = AABB(.1, 0, 1.8, 3);
+       player.hbox = AABB(0, .1, 2, 1.8);
+       player.arm = AABB(.5, 1.4, 1, .5);
+       player.armOrigin = Vector<float>(1.4, 1.65);
+       player.Move(Vector<float>(5, 0));
+       world.AddEntity(player);
 
        Entity mob;
        mob.bounds = AABB(0, 0, 2, 1.5);
@@ -67,7 +70,7 @@ int main(int argc, const char *argv[]) {
        world.AddEntity(mob);
 
        Application app(canv, world, tiles);
-       app.Control(e);
+       app.Control(player);
        app.Run();
 
        return 0;
diff --git a/src/world/Character.h b/src/world/Character.h
new file mode 100644 (file)
index 0000000..8985e7a
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef ORBI_CHARACTER_H_
+#define ORBI_CHARACTER_H_
+
+#include "Entity.h"
+
+
+namespace orbi {
+
+class Character
+: public Entity {
+
+public:
+       constexpr Character() { }
+
+public:
+       AABB arm;
+       Vector<float> armOrigin;
+       float armAngle = 0;
+
+       int heldItem = -1;
+
+};
+
+}
+
+#endif
index a7e45d292b277490c41d9657c68d2082e9f375b0..21663d31c216f230c1f5cc960f865589ba0c530a 100644 (file)
@@ -11,6 +11,7 @@ class Entity {
 
 public:
        constexpr Entity() { }
+       virtual ~Entity() { }
 
 public:
        void Update(float dt, Vector<float> extAcc, Vector<float> tv);