From 72988b5f9bd449cffea97273bebfc788735ce14d Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 2 Oct 2012 22:08:44 +0200 Subject: [PATCH] start/stop animation of player character --- src/main.cpp | 3 ++- src/map/Entity.cpp | 16 +++++++++++----- src/map/Entity.h | 12 +++++++----- src/map/MapState.cpp | 4 ++++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fe975de..1979aac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -324,8 +324,9 @@ int main(int argc, char **argv) { SDL_Surface *mapMaximImg(IMG_Load("test-data/maxim-map.png")); Sprite mapMaximSprite(mapMaximImg, 32, 64); + SimpleAnimation mapMaximAnimation(&mapMaximSprite, 128, 2, 0, 0, true); Entity mapMaxim; - mapMaxim.SetSprite(&mapMaximSprite); + mapMaxim.SetAnimation(&mapMaximAnimation); mapMaxim.Position() = Vector(80, 128); InitScreen screen(width, height); diff --git a/src/map/Entity.cpp b/src/map/Entity.cpp index 391c711..302ce5c 100644 --- a/src/map/Entity.cpp +++ b/src/map/Entity.cpp @@ -12,7 +12,7 @@ using geometry::Vector; namespace map { Entity::Entity() -: sprite(0) +: animation(0) , orientation(ORIENTATION_NORTH) , speed(0) { @@ -22,7 +22,7 @@ Entity::Entity() void Entity::SetOrientation(Orientation o) { orientation = o; UpdateVelocity(); - animation.SetColOffset(orientation); + runner.SetColOffset(orientation); } void Entity::SetSpeed(float s) { @@ -30,6 +30,12 @@ void Entity::SetSpeed(float s) { UpdateVelocity(); } +void Entity::SetAnimation(const graphics::Animation *a) { + animation = a; + runner.ChangeAnimation(animation); +} + + void Entity::UpdateVelocity() { if (speed == 0.0f) { velocity = Vector(); @@ -67,10 +73,10 @@ void Entity::Update(float deltaT) { void Entity::Render(SDL_Surface *dest, const Vector &offset) const { // TODO: configurable sprite offsets - if (animation.Running()) { - animation.DrawCenter(dest, offset + position); + if (runner.Running()) { + runner.DrawCenter(dest, offset + position); } else { - sprite->DrawCenter(dest, offset + position, orientation); + animation->GetSprite()->DrawCenter(dest, offset + position, orientation); } } diff --git a/src/map/Entity.h b/src/map/Entity.h index e1ede97..926c360 100644 --- a/src/map/Entity.h +++ b/src/map/Entity.h @@ -38,9 +38,11 @@ public: geometry::Vector &Velocity() { return velocity; } const geometry::Vector &Velocity() const { return velocity; } - void SetSprite(const graphics::Sprite *s) { sprite = s; } - graphics::AnimationRunner &Animation() { return animation; } - const graphics::AnimationRunner &Animation() const { return animation; } + void SetAnimation(const graphics::Animation *a); + void StartAnimation(app::Application &ctrl) { runner.Start(ctrl); } + void StartAnimation(app::State &ctrl) { runner.Start(ctrl); } + void StopAnimation() { runner.Stop(); } + bool AnimationRunning() const { return runner.Running(); } void SetOrientation(Orientation); Orientation GetOrientation() const { return orientation; } @@ -56,8 +58,8 @@ private: void UpdateVelocity(); private: - const graphics::Sprite *sprite; - graphics::AnimationRunner animation; + const graphics::Animation *animation; + graphics::AnimationRunner runner; geometry::Vector position; geometry::Vector velocity; Orientation orientation; diff --git a/src/map/MapState.cpp b/src/map/MapState.cpp index ec714d0..8d9b278 100644 --- a/src/map/MapState.cpp +++ b/src/map/MapState.cpp @@ -102,8 +102,12 @@ void MapState::UpdateWorld(float deltaT) { } else { controlled->SetSpeed(0.0f); } + if (!controlled->AnimationRunning()) { + controlled->StartAnimation(*this); + } } else { controlled->SetSpeed(0.0f); + controlled->StopAnimation(); } if (nowLock != lastLock) { lastLock = nowLock; -- 2.39.2