From 3f4f8a92f64df08119a40da4d196b3e92ecdc637 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 18 Dec 2013 17:48:41 +0100 Subject: [PATCH] controlled entity --- src/app/Application.cpp | 57 +++++++++++++++++++++++++++++++---------- src/app/Application.h | 6 +++-- src/entity/Entity.h | 30 ++++++++++++++++++++++ src/math/Vector.h | 4 +++ src/world/Sector.cpp | 7 +++-- src/world/Sector.h | 8 +++--- src/world/Universe.cpp | 40 +++++++++++++++++++++++++---- src/world/Universe.h | 32 ++++++++++++++++++----- 8 files changed, 151 insertions(+), 33 deletions(-) create mode 100644 src/entity/Entity.h diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 247819c..b2ef916 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -9,12 +9,12 @@ namespace space { Application::Application(InitScreen &s) : screen(s) -, univ(10, 10, 10, 10, 10) +, univ(Vector(10, 10), Vector(10, 10), Vector(10, 10), 10) , focus(Vector(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(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 areaSize(10, 10); - constexpr Vector sectSize(areaSize * 10); - constexpr Vector 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 offset = cam.Offset(); + const Vector begin = cam.Offset(); + const Vector end = + begin + (univ.size * univ.secSize * univ.areaSize) + Vector(1, 1); Fill(dst, background); - Grid(dst, offset, offset + univSize + Vector(1, 1), areaSize, sectGrid); - Grid(dst, offset, offset + univSize + Vector(1, 1), sectSize, univGrid); - Cross(dst, offset + Vector(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(focus.Pos()), 15, focusColor); + + for (const Entity &e : univ.Entities()) { + Cross(dst, begin + (e.area * univ.areaSize) + Vector(e.pos), 10, entityColor); + } } } diff --git a/src/app/Application.h b/src/app/Application.h index a852696..61c3f45 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -12,6 +12,7 @@ namespace space { class InitScreen; +class Entity; class Application { @@ -30,8 +31,6 @@ private: void Update(int delta_ms); - Vector FocusVel() const; - void Render(); private: @@ -42,6 +41,9 @@ private: Moveable focus; Camera cam; + Entity *controlled; + Vector control; + Uint32 last; bool running; diff --git a/src/entity/Entity.h b/src/entity/Entity.h new file mode 100644 index 0000000..9fd2dff --- /dev/null +++ b/src/entity/Entity.h @@ -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 area; + Vector pos; + Vector vel; + Vector acc; + +public: + void Update(float delta) { + pos += vel * delta; + vel += acc * delta; + } + +}; + +} + +#endif diff --git a/src/math/Vector.h b/src/math/Vector.h index 5fa2eb3..57159bf 100644 --- a/src/math/Vector.h +++ b/src/math/Vector.h @@ -61,6 +61,10 @@ template constexpr Vector operator *(Scalar lhs, Vector rhs) { return rhs * lhs; } +template +constexpr Vector operator *(Vector lhs, Vector rhs) { + return Vector(lhs.x * rhs.x, lhs.y * rhs.y); +} } diff --git a/src/world/Sector.cpp b/src/world/Sector.cpp index bbe0bf4..781f23d 100644 --- a/src/world/Sector.cpp +++ b/src/world/Sector.cpp @@ -5,11 +5,10 @@ namespace space { -Sector::Sector(int w, int h, int numres) -: w(w) -, h(h) +Sector::Sector(Vector 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)); diff --git a/src/world/Sector.h b/src/world/Sector.h index fa27b7f..d5c101c 100644 --- a/src/world/Sector.h +++ b/src/world/Sector.h @@ -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 size, int numres); ~Sector(); Sector(const Sector &) = delete; Sector &operator =(const Sector &) = delete; private: - int w; - int h; + Vector size; int numres; int total; int *res_begin; diff --git a/src/world/Universe.cpp b/src/world/Universe.cpp index ec307a5..ec71fd1 100644 --- a/src/world/Universe.cpp +++ b/src/world/Universe.cpp @@ -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 size, Vector secSize, Vector areaSize, int numres) +: size(size) +, secSize(secSize) +, areaSize(areaSize) , numres(numres) -, total(w * h) +, total(size.x * size.y) , sec_begin(reinterpret_cast(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(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; + } + } +} + } diff --git a/src/world/Universe.h b/src/world/Universe.h index 29e4ecb..563bfd9 100644 --- a/src/world/Universe.h +++ b/src/world/Universe.h @@ -1,6 +1,12 @@ #ifndef SPACE_UNIVERSE_H_ #define SPACE_UNIVERSE_H_ +#include "../entity/Entity.h" +#include "../math/Vector.h" + +#include + + 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 size, Vector secSize, Vector areaSize, int numres); ~Universe(); -private: - Universe(const Universe &); - Universe &operator =(const Universe &); + + Universe(const Universe &) = delete; + Universe &operator =(const Universe &) = delete; + +public: + const Vector size; + const Vector secSize; + const Vector areaSize; + +public: + Entity *AddEntity(const Entity &); + const std::list &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 entities; + }; } -- 2.39.2