From a73a6dd63407b5f5ef5b0c635551ad27b27c95d6 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 8 May 2014 20:47:25 +0200 Subject: [PATCH] rotatable arm for character entities --- src/app/Application.cpp | 38 ++++++++++++++++++++++---------------- src/graphics/Canvas.cpp | 32 ++++++++++++++++++-------------- src/graphics/Canvas.h | 13 +++++++++++-- src/graphics/Vector.h | 1 + src/orbi.cpp | 17 ++++++++++------- src/world/Character.h | 26 ++++++++++++++++++++++++++ src/world/Entity.h | 1 + 7 files changed, 89 insertions(+), 39 deletions(-) create mode 100644 src/world/Character.h diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 3ba8485..3ddb414 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -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(*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(e->bounds.Left(), e->bounds.Top())), - cam.ToScale(Vector(e->bounds.Size())) + cam.ToScreen(e->bounds.Position()), + cam.ToScale(e->bounds.Size()) ); + if (const Character *c = dynamic_cast(e)) { + canvas.OutlineRectRot( + Rect( + 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(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.OutlineRect( + cam.ToScreen(e->vbox.Position()), + cam.ToScale(e->vbox.Size()) ); 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) + canvas.OutlineRect( + cam.ToScreen(e->hbox.Position()), + cam.ToScale(e->hbox.Size()) ); } } diff --git a/src/graphics/Canvas.cpp b/src/graphics/Canvas.cpp index 3f1c3d1..9a163e9 100644 --- a/src/graphics/Canvas.cpp +++ b/src/graphics/Canvas.cpp @@ -81,22 +81,26 @@ void Canvas::Line(Vector from, Vector to) { SDL_RenderDrawLine(canv, from.x, from.y, to.x, to.y); } -void Canvas::FillRect(Vector pos, Vector 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 rect) { + SDL_RenderFillRect(canv, &rect); } -void Canvas::OutlineRect(Vector pos, Vector 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 rect) { + SDL_RenderDrawRect(canv, &rect); +} + +void Canvas::OutlineRectRot(Rect rect, Vector origin, float rot) { + const Vector topLeft(rect.x, rect.y); + const Vector topRight(rect.x + rect.w - 1, rect.y); + const Vector botLeft(rect.x, rect.y + rect.h - 1); + const Vector 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 + ); } diff --git a/src/graphics/Canvas.h b/src/graphics/Canvas.h index 021bced..fdcc5a0 100644 --- a/src/graphics/Canvas.h +++ b/src/graphics/Canvas.h @@ -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 from, Vector to); - void FillRect(Vector pos, Vector size); - void OutlineRect(Vector pos, Vector size); + void FillRect(Rect); + void OutlineRect(Rect); + void OutlineRectRot(Rect, Vector origin, float rot); + + void FillRect(Vector pos, Vector size) { + FillRect(Rect(pos, size)); + } + void OutlineRect(Vector pos, Vector size) { + OutlineRect(Rect(pos, size)); + } void Dot(Vector pos); void Cross(Vector pos, int extent); diff --git a/src/graphics/Vector.h b/src/graphics/Vector.h index 6259b97..958be23 100644 --- a/src/graphics/Vector.h +++ b/src/graphics/Vector.h @@ -210,6 +210,7 @@ template constexpr Vector Rotate270(Vector v) { return Vector(v.y, -v.x); } +// angle given in radians template inline Vector Rotate(Vector v, Float by) { Float sine(std::sin(by)); diff --git a/src/orbi.cpp b/src/orbi.cpp index 67409e6..87e3866 100644 --- a/src/orbi.cpp +++ b/src/orbi.cpp @@ -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(2, 9), Tile(0)); world.SetTile(Vector(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(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(1.4, 1.65); + player.Move(Vector(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 index 0000000..8985e7a --- /dev/null +++ b/src/world/Character.h @@ -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 armOrigin; + float armAngle = 0; + + int heldItem = -1; + +}; + +} + +#endif diff --git a/src/world/Entity.h b/src/world/Entity.h index a7e45d2..21663d3 100644 --- a/src/world/Entity.h +++ b/src/world/Entity.h @@ -11,6 +11,7 @@ class Entity { public: constexpr Entity() { } + virtual ~Entity() { } public: void Update(float dt, Vector extAcc, Vector tv); -- 2.39.2