From: Daniel Karbach Date: Sun, 30 Sep 2012 15:08:29 +0000 (+0200) Subject: added entity orientation awareness X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1d6a9f01b8db0f5212b6a02603dd0670c6da38c7;p=l2e.git added entity orientation awareness --- diff --git a/src/main.cpp b/src/main.cpp index ce45985..8fa1318 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; } diff --git a/src/map/Entity.cpp b/src/map/Entity.cpp index c14c9a0..0e1b822 100644 --- a/src/map/Entity.cpp +++ b/src/map/Entity.cpp @@ -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(); + return; + } + switch (orientation) { + case ORIENTATION_NORTH: + velocity = Vector(0.0f, -speed); + break; + case ORIENTATION_EAST: + velocity = Vector(speed, 0.0f); + break; + case ORIENTATION_SOUTH: + velocity = Vector(0.0f, speed); + break; + case ORIENTATION_WEST: + velocity = Vector(-speed, 0.0f); + break; + } +} + + bool Entity::TileLock(int width, int height) const { Vector tilePosition( position.X() - (width / 2), @@ -34,7 +69,7 @@ void Entity::Render(SDL_Surface *dest, const Vector &offset) const { if (animation.Running()) { animation.DrawCenterBottom(dest, offset + position); } else { - sprite->DrawCenterBottom(dest, offset + position); + sprite->DrawCenterBottom(dest, offset + position, orientation); } } diff --git a/src/map/Entity.h b/src/map/Entity.h index d6ba5f3..2bc6aac 100644 --- a/src/map/Entity.h +++ b/src/map/Entity.h @@ -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 &Position() { return position; } const geometry::Vector &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 &offset) const; +private: + void UpdateVelocity(); + private: const graphics::Sprite *sprite; graphics::AnimationRunner animation; geometry::Vector position; geometry::Vector velocity; + Orientation orientation; + float speed; }; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index 5477999..6ef2be7 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -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 offset(camera.CalculateOffset()); map->Render(screen, offset); diff --git a/src/map/MapState.h b/src/map/MapState.h index e180d48..cde5ec7 100644 --- a/src/map/MapState.h +++ b/src/map/MapState.h @@ -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 tempTarget; graphics::Camera camera; std::vector entities; + float walkingSpeed; };