1 //
2 //  SuperTuxKart - a fun racing game with go-kart
3 //  Copyright (C) 2012-2015 Joerg Henrichs
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 3
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 #ifndef HEADER_ABSTRACT_KART_ANIMATION_HPP
20 #define HEADER_ABSTRACT_KART_ANIMATION_HPP
21 
22 #include "LinearMath/btTransform.h"
23 
24 #include "config/stk_config.hpp"
25 #include "utils/no_copy.hpp"
26 #include "utils/vec3.hpp"
27 
28 #include <exception>
29 #include <limits>
30 #include <string>
31 
32 class AbstractKart;
33 class BareNetworkString;
34 
35 enum KartAnimationType : uint8_t
36 {
37     KAT_RESCUE = 0,
38     KAT_EXPLOSION = 1,
39     KAT_CANNON = 2
40 };
41 
42 /** Exception for kart animation creation in networking, so if thrown it will
43  *  tell the num of bytes skipping in the game state. */
44 class KartAnimationCreationException : public std::exception
45 {
46 public:
47     virtual int getSkippingOffset() const = 0;
48 };
49 
50 /** The base class for all kart animation, like rescue, explosion, or cannon.
51  *  Kart animations are done by removing the physics body from the physics
52  *  world, and instead modifying the rotation and position of the kart
53  *  directly. They are registered with the kart, and only one can be
54  *  used at the same time. The memory is handled by the kart object, so
55  *  there is no need to manage it. Sample usage:
56  *    new ExplosionAnimation(kart);
57  *  The object does not need to be stored.
58  */
59 class AbstractKartAnimation: public NoCopy
60 {
61 private:
62     /** Name of this animation, used for debug prints only. */
63     std::string m_name;
64 
65 protected:
66    /** A pointer to the kart which is animated by this class. */
67     AbstractKart *m_kart;
68 
69     /** Time in ticks for the animation which ends in world count up ticks. */
70     int m_end_ticks;
71 
72     /** Time in ticks for the animation creation. */
73     int m_created_ticks;
74 
75     /** Transformation by the time the animation was created, used for rewind
76      *  to recreate the animation with the same one. */
77     btTransform m_created_transform;
78 
79     /* Compressed values for server to send to avoid compressing everytime. */
80     int m_created_transform_compressed[4];
81 
82     void resetPowerUp();
83     // ------------------------------------------------------------------------
84     void restoreBasicState(BareNetworkString* buffer);
85     // ------------------------------------------------------------------------
86     float getMaximumHeight(const Vec3& up_vector, float height_remove);
87 
88 public:
89                  AbstractKartAnimation(AbstractKart* kart,
90                                        const std::string &name);
91     virtual     ~AbstractKartAnimation();
92     virtual void update(int ticks);
93     // ------------------------------------------------------------------------
94     virtual void updateGraphics(float dt);
95     // ------------------------------------------------------------------------
96     virtual float getAnimationTimer() const;
97     // ------------------------------------------------------------------------
98     /** To easily allow printing the name of the animation being used atm.
99      *  Used in AstractKart in case of an incorrect sequence of calls. */
getName() const100     virtual const std::string &getName() const { return m_name; }
101     // ------------------------------------------------------------------------
102     virtual KartAnimationType getAnimationType() const = 0;
103     // ------------------------------------------------------------------------
104     /* Used to ignore adding karts back to physics when destroying world. */
handleResetRace()105     void handleResetRace()   { m_end_ticks = std::numeric_limits<int>::max(); }
106     // ------------------------------------------------------------------------
107     /* Used to recreate animation in \ref KartRewinder. */
108     virtual void saveState(BareNetworkString* buffer);
109     // ------------------------------------------------------------------------
110     /* Called when kart animation is the same in kart state, which make sure
111      * for example the end or created ticks are the same. */
restoreState(BareNetworkString * buffer)112     virtual void restoreState(BareNetworkString* buffer)
113                                                  { restoreBasicState(buffer); }
114 };   // AbstractKartAnimation
115 
116 #endif
117