13 #include "../math/Fixed.h"
14 #include "../math/Vector.h"
15 #include "../graphics/Animation.h"
22 /// An entity that can be placed on a map, moved around, animated, and possibly
23 /// interact with the player.
27 static const int TYPE_ID = 605;
35 ORIENTATION_NORTH = 0,
37 ORIENTATION_SOUTH = 2,
41 FLAG_NONBLOCKING = 0x01,
43 FLAG_FIXED_ORIENTATION = 0x04,
47 /// Pixel resolved position of the entity's top left corner on the map.
48 math::Vector<math::Fixed<8> > &Position() { return position; }
49 const math::Vector<math::Fixed<8> > &Position() const { return position; }
51 /// Velocity of the entity in pixels per second.
52 math::Vector<math::Fixed<8> > &Velocity() { return velocity; }
53 const math::Vector<math::Fixed<8> > &Velocity() const { return velocity; }
55 /// Offset of the entity's sprite's to left corner relative to Position().
56 math::Vector<int> &SpriteOffset() { return spriteOffset; }
57 const math::Vector<int> &SpriteOffset() const { return spriteOffset; }
59 /// Reset the entity to the stored tile coordinates (usually set when
60 /// loading game data).
61 void ResetPosition(const math::Vector<int> &tileSize) { position = tilePosition * tileSize; }
63 /// Set the animation to use for animated entities.
64 /// For orientable entities, the animation should have north, south, east,
65 /// and west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively.
66 /// If the entity can carry, row offset 2 is used.
67 /// If the entity can push, row offset 4 is used.
68 void SetAnimation(const graphics::Animation *a);
69 /// Start the animation on a global timer.
70 void StartAnimation(app::Application &ctrl);
71 /// Start the animation on a state timer.
72 void StartAnimation(app::State &ctrl);
73 /// Stop the animation.
75 /// Check if an animation is running.
76 bool AnimationRunning() const { return runner.Running(); }
78 /// Set the sprite used for the non-animated state.
79 /// For orientable entities, the sprite should have north, south, east, and
80 /// west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively.
81 void SetSprite(const graphics::Sprite *s) { sprite = s; }
83 /// Change the entity's facing direction.
84 /// If the entity is moving, velocity is untouched.
85 void SetOrientation(Orientation);
86 Orientation GetOrientation() const { return orientation; }
87 /// Change the entity's orientation to given one.
88 /// If the entity is moving, velocity is changed accordingly.
89 /// Also changes the orientation to given direction.
90 void SetDirection(Orientation);
91 Orientation GetDirection() const { return direction; }
92 /// Set the entity's speed in pixels per second.
93 /// This speed is then combined with the direction to form a velocity.
94 void SetSpeed(math::Fixed<8>);
96 /// Change to a natural, relaxed animation state (row offset 0).
98 /// Change animation to represent a carrying thingamabob (row offset 2).
100 /// Set a pushy animation state (row offset 4).
103 /// Set some basic boolean properties.
104 /// Parameter should be a combination from the Flags enum.
105 void SetFlags(int f) { flags = f; }
106 /// Check if the entity is blocking other entities from occupying its tile.
107 bool Blocking() const { return !(flags & FLAG_NONBLOCKING); }
108 /// Check if a battle should be launched when stepping onto a neighboring
110 bool Hostile() const { return partyLayout && numMonsters > 0; }
111 /// Check if this entity can be pushed around.
112 bool Pushable() const { return flags & FLAG_PUSHABLE; }
113 /// Check if the entity's orientation has any effect on the column rendered
114 /// from the animation or sprite.
115 bool CanTurn() const { return !(flags & FLAG_FIXED_ORIENTATION); }
117 /// Set a layout in battle for the party described by SetMonsters().
118 void SetPartyLayout(battle::PartyLayout *l) { partyLayout = l; }
119 /// Get the layout in battle for the party described by
120 /// Monsters{Begin,End}().
121 battle::PartyLayout *PartyLayout() { return partyLayout; }
123 /// Add monsters. This will cause the entity to be Hostile() and result in a
124 /// battle scene with given monsters when touched.
125 void SetMonsters(battle::Monster **m, int num) { monsters = m; numMonsters = num; }
126 battle::Monster **MonstersBegin() { return monsters; }
127 battle::Monster **MonstersEnd() { return monsters + numMonsters; }
129 /// Get an entity that should follow in this one's steps or 0 if none.
130 Entity *Follower() { return follower; }
131 const Entity *Follower() const { return follower; }
132 /// Add an entity that follows this one.
133 /// If this already has a follower, it is added to that one instead.
134 void AddFollower(Entity *);
135 /// Remove given entity from this entity or its follower.
136 void RemoveFollower(Entity *);
138 /// Check if position locks into grid defined by given tileSize.
139 bool TileLock(const math::Vector<int> &tileSize) const;
141 /// Integrate this entity's physical properties over given time interval.
142 void Update(Uint32 deltaT);
144 void Render(SDL_Surface *, const math::Vector<int> &offset) const;
146 static void CreateTypeDescription();
147 static void Construct(void *);
148 static void Load(void *);
151 void UpdateVelocity();
155 const graphics::Animation *animation;
156 const graphics::Sprite *sprite;
157 battle::PartyLayout *partyLayout;
158 battle::Monster **monsters;
160 graphics::AnimationRunner runner;
161 math::Vector<int> spriteOffset;
162 math::Vector<int> tilePosition;
163 math::Vector<math::Fixed<8> > position;
164 math::Vector<math::Fixed<8> > velocity;
165 Orientation direction;
166 Orientation orientation;
167 math::Fixed<8> speed;