]> git.localhorst.tv Git - l2e.git/commitdiff
added entity orientation awareness
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 30 Sep 2012 15:08:29 +0000 (17:08 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 30 Sep 2012 15:08:29 +0000 (17:08 +0200)
src/main.cpp
src/map/Entity.cpp
src/map/Entity.h
src/map/MapState.cpp
src/map/MapState.h

index ce45985f3a647d33323ab2e37e374a5d54a2710b..8fa1318f6e6d55dfd7554e066c893365da9430a3 100644 (file)
@@ -94,6 +94,8 @@ int main(int argc, char **argv) {
        const int width = 800;
        const int height = 480;
 
+       const float walkSpeed = 128.0f;
+
        const bool battle(false);
 
 //     std::srand(std::time(0));
@@ -345,6 +347,7 @@ int main(int argc, char **argv) {
                        MapState *mapState(new MapState(&map));
                        mapState->AddEntity(&mapMaxim);
                        mapState->ControlEntity(&mapMaxim);
+                       mapState->SetWalkingSpeed(walkSpeed);
                        state = mapState;
                }
 
index c14c9a0e7321a25b43e5fa64875545c3be974dd5..0e1b822ba32256e920e4767051a5e8f68fbecaec 100644 (file)
@@ -12,11 +12,46 @@ using geometry::Vector;
 namespace map {
 
 Entity::Entity()
-: sprite(0) {
+: sprite(0)
+, orientation(ORIENTATION_NORTH)
+, speed(0) {
 
 }
 
 
+void Entity::SetOrientation(Orientation o) {
+       orientation = o;
+       UpdateVelocity();
+       animation.SetColOffset(orientation);
+}
+
+void Entity::SetSpeed(float s) {
+       speed = s;
+       UpdateVelocity();
+}
+
+void Entity::UpdateVelocity() {
+       if (speed == 0.0f) {
+               velocity = Vector<float>();
+               return;
+       }
+       switch (orientation) {
+               case ORIENTATION_NORTH:
+                       velocity = Vector<float>(0.0f, -speed);
+                       break;
+               case ORIENTATION_EAST:
+                       velocity = Vector<float>(speed, 0.0f);
+                       break;
+               case ORIENTATION_SOUTH:
+                       velocity = Vector<float>(0.0f, speed);
+                       break;
+               case ORIENTATION_WEST:
+                       velocity = Vector<float>(-speed, 0.0f);
+                       break;
+       }
+}
+
+
 bool Entity::TileLock(int width, int height) const {
        Vector<int> tilePosition(
                        position.X() - (width / 2),
@@ -34,7 +69,7 @@ void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
        if (animation.Running()) {
                animation.DrawCenterBottom(dest, offset + position);
        } else {
-               sprite->DrawCenterBottom(dest, offset + position);
+               sprite->DrawCenterBottom(dest, offset + position, orientation);
        }
 }
 
index d6ba5f3a3763dc2acd3aefd4697fde9d33bef8c5..2bc6aac6a863913eaece225598bf8ed85e5cd969 100644 (file)
@@ -23,6 +23,14 @@ public:
        Entity();
        ~Entity() { }
 
+public:
+       enum Orientation {
+               ORIENTATION_NORTH = 0,
+               ORIENTATION_EAST = 1,
+               ORIENTATION_SOUTH = 2,
+               ORIENTATION_WEST = 3,
+       };
+
 public:
        geometry::Vector<float> &Position() { return position; }
        const geometry::Vector<float> &Position() const { return position; }
@@ -34,17 +42,25 @@ public:
        graphics::AnimationRunner &Animation() { return animation; }
        const graphics::AnimationRunner &Animation() const { return animation; }
 
+       void SetOrientation(Orientation);
+       void SetSpeed(float);
+
        bool TileLock(int width, int height) const;
 
        void Update(float deltaT);
 
        void Render(SDL_Surface *, const geometry::Vector<int> &offset) const;
 
+private:
+       void UpdateVelocity();
+
 private:
        const graphics::Sprite *sprite;
        graphics::AnimationRunner animation;
        geometry::Vector<float> position;
        geometry::Vector<float> velocity;
+       Orientation orientation;
+       float speed;
 
 };
 
index 5477999899905d2015e99d4afe96a30ff07b90c0..6ef2be748deb8e1dc874e3588f0b4326f561aa8a 100644 (file)
@@ -23,7 +23,8 @@ MapState::MapState(const Map *map)
 : map(map)
 , controlled(0)
 , tempTarget(20, 20)
-, camera(100, 100, &tempTarget) {
+, camera(100, 100, &tempTarget)
+, walkingSpeed(64) {
 
 }
 
@@ -52,6 +53,22 @@ void MapState::Resize(int width, int height) {
 void MapState::HandleEvents(const Input &input) {
        if (!controlled) return;
        if (!controlled->TileLock(map->Tileset()->Width(), map->Tileset()->Height())) return;
+
+       if (input.IsDown(Input::PAD_UP)) {
+               controlled->SetOrientation(Entity::ORIENTATION_NORTH);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_RIGHT)) {
+               controlled->SetOrientation(Entity::ORIENTATION_EAST);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_DOWN)) {
+               controlled->SetOrientation(Entity::ORIENTATION_SOUTH);
+               controlled->SetSpeed(walkingSpeed);
+       } else if (input.IsDown(Input::PAD_LEFT)) {
+               controlled->SetOrientation(Entity::ORIENTATION_WEST);
+               controlled->SetSpeed(walkingSpeed);
+       } else {
+               controlled->SetSpeed(0.0f);
+       }
 }
 
 void MapState::UpdateWorld(float deltaT) {
@@ -61,6 +78,8 @@ void MapState::UpdateWorld(float deltaT) {
 }
 
 void MapState::Render(SDL_Surface *screen) {
+       SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
+
        Vector<int> offset(camera.CalculateOffset());
        map->Render(screen, offset);
 
index e180d4832b6b3c1aead07b2dd45cffd86896af65..cde5ec7449d0d9892164c7333db8ef3324540e7a 100644 (file)
@@ -40,6 +40,8 @@ public:
        void AddEntity(Entity *e) { entities.push_back(e); }
        void ControlEntity(Entity *e) { controlled = e; camera.SetTarget(&e->Position()); }
 
+       void SetWalkingSpeed(float s) { walkingSpeed = s; }
+
 private:
        static bool ZCompare(const Entity *lhs, const Entity *rhs);
 
@@ -49,6 +51,7 @@ private:
        geometry::Vector<float> tempTarget;
        graphics::Camera camera;
        std::vector<Entity *> entities;
+       float walkingSpeed;
 
 };