]> git.localhorst.tv Git - l2e.git/commitdiff
start/stop animation of player character
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 2 Oct 2012 20:08:44 +0000 (22:08 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 2 Oct 2012 20:08:44 +0000 (22:08 +0200)
src/main.cpp
src/map/Entity.cpp
src/map/Entity.h
src/map/MapState.cpp

index fe975deb7dac4706f8310fed4b44d8841291316d..1979aac400499be06913e270804a1915c242d7fa 100644 (file)
@@ -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<float>(80, 128);
 
                InitScreen screen(width, height);
index 391c711665dc63e7495dedcc6f13cc6470ee2396..302ce5c195ac856ab8aafaaae7befb0b5634b226 100644 (file)
@@ -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<float>();
@@ -67,10 +73,10 @@ void Entity::Update(float deltaT) {
 
 void Entity::Render(SDL_Surface *dest, const Vector<int> &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);
        }
 }
 
index e1ede977e65888eea33360f20a37b3f91aa63a93..926c360fef144d740c4cc7f460372aadb3644b87 100644 (file)
@@ -38,9 +38,11 @@ public:
        geometry::Vector<float> &Velocity() { return velocity; }
        const geometry::Vector<float> &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<float> position;
        geometry::Vector<float> velocity;
        Orientation orientation;
index ec714d0588c646360746cad27003c96203c59634..8d9b278c89b8b52e4b30582d741145b58bdc325e 100644 (file)
@@ -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;