]> git.localhorst.tv Git - space.git/commitdiff
controlled entity
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 18 Dec 2013 16:48:41 +0000 (17:48 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 18 Dec 2013 16:48:41 +0000 (17:48 +0100)
src/app/Application.cpp
src/app/Application.h
src/entity/Entity.h [new file with mode: 0644]
src/math/Vector.h
src/world/Sector.cpp
src/world/Sector.h
src/world/Universe.cpp
src/world/Universe.h

index 247819c41bd0088d9f1a51e9b3dc7745954ff17c..b2ef9161287b5cf77e30ca188386174f116d1390 100644 (file)
@@ -9,12 +9,12 @@ namespace space {
 
 Application::Application(InitScreen &s)
 : screen(s)
-, univ(10, 10, 10, 10, 10)
+, univ(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(10, 10), 10)
 , focus(Vector<float>(500, 500), 500)
 , cam(800, 800, focus.Pos())
 , last(SDL_GetTicks())
 , running(false) {
-
+       controlled = univ.AddEntity(Entity());
 }
 
 
@@ -75,6 +75,18 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
                case SDLK_RIGHT:
                        focus.MoveRight();
                        break;
+               case SDLK_w:
+                       control.y -= 1;
+                       break;
+               case SDLK_s:
+                       control.y += 1;
+                       break;
+               case SDLK_a:
+                       control.x -= 1;
+                       break;
+               case SDLK_d:
+                       control.x += 1;
+                       break;
                default:
                        break;
        }
@@ -94,6 +106,18 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
                case SDLK_RIGHT:
                        focus.StopRight();
                        break;
+               case SDLK_w:
+                       control.y += 1;
+                       break;
+               case SDLK_s:
+                       control.y -= 1;
+                       break;
+               case SDLK_a:
+                       control.x += 1;
+                       break;
+               case SDLK_d:
+                       control.x -= 1;
+                       break;
                default:
                        break;
        }
@@ -101,26 +125,33 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
 
 
 void Application::Update(int dt) {
-       focus.Update(dt / 1e3);
+       const float delta = dt / 1e3;
+       controlled->acc = Vector<float>(control * 10);
+       univ.Update(delta);
+       focus.Update(delta);
 }
 
 
 void Application::Render() {
        constexpr Color background(0x00, 0x00, 0x00);
-       constexpr Color univGrid(0xFF, 0xFF, 0xFF);
-       constexpr Color sectGrid(0xAA, 0xAA, 0xAA);
-
-       constexpr Vector<int> areaSize(10, 10);
-       constexpr Vector<int> sectSize(areaSize * 10);
-       constexpr Vector<int> univSize(sectSize * 10);
+       constexpr Color univGrid(0xEE, 0xEE, 0xEE);
+       constexpr Color secGrid(0x77, 0x77, 0x77);
+       constexpr Color entityColor(0x00, 0xAA, 0xAA);
+       constexpr Color focusColor(0xFA, 0xFA, 0x00);
 
        SDL_Surface *dst = screen.Screen();
-       Vector<int> offset = cam.Offset();
+       const Vector<int> begin = cam.Offset();
+       const Vector<int> end =
+               begin + (univ.size * univ.secSize * univ.areaSize) + Vector<int>(1, 1);
 
        Fill(dst, background);
-       Grid(dst, offset, offset + univSize + Vector<int>(1, 1), areaSize, sectGrid);
-       Grid(dst, offset, offset + univSize + Vector<int>(1, 1), sectSize, univGrid);
-       Cross(dst, offset + Vector<int>(focus.Pos()), 15, Color(0xFF, 0xFF, 0x00));
+       Grid(dst, begin, end, univ.areaSize, secGrid);
+       Grid(dst, begin, end, univ.secSize * univ.areaSize, univGrid);
+       Cross(dst, begin + Vector<int>(focus.Pos()), 15, focusColor);
+
+       for (const Entity &e : univ.Entities()) {
+               Cross(dst, begin + (e.area * univ.areaSize) + Vector<int>(e.pos), 10, entityColor);
+       }
 }
 
 }
index a85269612257269349b6955fb4c85f2968cc4224..61c3f45a5ab7e8ebe3ed740643e7097e17ee1246 100644 (file)
@@ -12,6 +12,7 @@
 namespace space {
 
 class InitScreen;
+class Entity;
 
 class Application {
 
@@ -30,8 +31,6 @@ private:
 
        void Update(int delta_ms);
 
-       Vector<float> FocusVel() const;
-
        void Render();
 
 private:
@@ -42,6 +41,9 @@ private:
        Moveable<float> focus;
        Camera cam;
 
+       Entity *controlled;
+       Vector<int> control;
+
        Uint32 last;
        bool running;
 
diff --git a/src/entity/Entity.h b/src/entity/Entity.h
new file mode 100644 (file)
index 0000000..9fd2dff
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef SPACE_ENTITY_H_
+#define SPACE_ENTITY_H_
+
+#include "../math/Vector.h"
+
+
+namespace space {
+
+class Entity {
+
+public:
+       constexpr Entity() { }
+
+public:
+       Vector<int> area;
+       Vector<float> pos;
+       Vector<float> vel;
+       Vector<float> acc;
+
+public:
+       void Update(float delta) {
+               pos += vel * delta;
+               vel += acc * delta;
+       }
+
+};
+
+}
+
+#endif
index 5fa2eb31760bd71a7f867d5b4323cad1a9bd8afe..57159bff2e0e01193e4a8ea16952eace97e9e4b7 100644 (file)
@@ -61,6 +61,10 @@ template<class Scalar>
 constexpr Vector<Scalar> operator *(Scalar lhs, Vector<Scalar> rhs) {
        return rhs * lhs;
 }
+template<class Scalar>
+constexpr Vector<Scalar> operator *(Vector<Scalar> lhs, Vector<Scalar> rhs) {
+       return Vector<Scalar>(lhs.x * rhs.x, lhs.y * rhs.y);
+}
 
 }
 
index bbe0bf413aa6b154b74aa96b4b7d23b2243e3bf0..781f23d7af62589a002944042e4a12ae29197cb6 100644 (file)
@@ -5,11 +5,10 @@
 
 namespace space {
 
-Sector::Sector(int w, int h, int numres)
-: w(w)
-, h(h)
+Sector::Sector(Vector<int> size, int numres)
+: size(size)
 , numres(numres)
-, total(w * h * numres)
+, total(size.x * size.y * numres)
 , res_begin(new int[total])
 , res_end(res_begin + total) {
        std::memset(res_begin, 0, total * sizeof(int));
index fa27b7fa5f1edb9c427a9ddc136bf1f6a96fc8f2..d5c101cc180695076589334417cb7c4e82f929e2 100644 (file)
@@ -1,20 +1,22 @@
 #ifndef SPACE_SECTOR_H_
 #define SPACE_SECTOR_H_
 
+#include "../math/Vector.h"
+
+
 namespace space {
 
 class Sector {
 
 public:
-       Sector(int w, int h, int numres);
+       Sector(Vector<int> size, int numres);
        ~Sector();
 
        Sector(const Sector &) = delete;
        Sector &operator =(const Sector &) = delete;
 
 private:
-       int w;
-       int h;
+       Vector<int> size;
        int numres;
        int total;
        int *res_begin;
index ec307a52eb3fc952e34f2125be6d3250730363d7..ec71fd1acc665a83e42ff80023b3195e0b78f8af 100644 (file)
@@ -8,15 +8,16 @@
 
 namespace space {
 
-Universe::Universe(int w, int h, int sec_w, int sec_h, int numres)
-: w(w)
-, h(h)
+Universe::Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize, int numres)
+: size(size)
+, secSize(secSize)
+, areaSize(areaSize)
 , numres(numres)
-, total(w * h)
+, total(size.x * size.y)
 , sec_begin(reinterpret_cast<Sector *>(new char[total * sizeof(Sector)]))
 , sec_end(sec_begin + total) {
        for (Sector *i = sec_begin; i < sec_end; ++i) {
-               new (i) Sector(sec_w, sec_h, numres);
+               new (i) Sector(secSize, numres);
        }
 }
 
@@ -24,4 +25,33 @@ Universe::~Universe() {
        delete[] reinterpret_cast<char *>(sec_begin);
 }
 
+
+Entity *Universe::AddEntity(const Entity &e) {
+       entities.emplace_back(e);
+       return &entities.back();
+}
+
+
+void Universe::Update(float delta) {
+       for (Entity &e : entities) {
+               e.Update(delta);
+               while (e.pos.x > areaSize.x) {
+                       e.pos.x -= areaSize.x;
+                       ++e.area.x;
+               }
+               while (e.pos.x < 0) {
+                       e.pos.x += areaSize.x;
+                       --e.area.x;
+               }
+               while (e.pos.y > areaSize.y) {
+                       e.pos.y -= areaSize.y;
+                       ++e.area.y;
+               }
+               while (e.pos.y < 0) {
+                       e.pos.y += areaSize.y;
+                       --e.area.y;
+               }
+       }
+}
+
 }
index 29e4ecb92b726f6e97bf65e38d6ea4aafecdacd1..563bfd90000b545d56bf754778b5f39d1827ab4a 100644 (file)
@@ -1,6 +1,12 @@
 #ifndef SPACE_UNIVERSE_H_
 #define SPACE_UNIVERSE_H_
 
+#include "../entity/Entity.h"
+#include "../math/Vector.h"
+
+#include <list>
+
+
 namespace space {
 
 class Sector;
@@ -8,20 +14,34 @@ class Sector;
 class Universe {
 
 public:
-       Universe(int w, int h, int sec_w, int sec_h, int numres);
+       Universe(Vector<int> size, Vector<int> secSize, Vector<int> areaSize, int numres);
        ~Universe();
-private:
-       Universe(const Universe &);
-       Universe &operator =(const Universe &);
+
+       Universe(const Universe &) = delete;
+       Universe &operator =(const Universe &) = delete;
+
+public:
+       const Vector<int> size;
+       const Vector<int> secSize;
+       const Vector<int> areaSize;
+
+public:
+       Entity *AddEntity(const Entity &);
+       const std::list<Entity> &Entities() const { return entities; }
+
+public:
+       void Update(float deltaT);
 
 private:
-       int w;
-       int h;
+
        int numres;
        int total;
+
        Sector *sec_begin;
        Sector *sec_end;
 
+       std::list<Entity> entities;
+
 };
 
 }