]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.h
added tile ladder flag
[l2e.git] / src / map / Entity.h
1 #ifndef MAP_ENTITY_H_
2 #define MAP_ENTITY_H_
3
4 namespace app {
5         class Application;
6         class State;
7 }
8 namespace battle {
9         class Monster;
10         class PartyLayout;
11 }
12
13 #include "../math/Fixed.h"
14 #include "../math/Vector.h"
15 #include "../graphics/Animation.h"
16
17 #include <functional>
18 #include <SDL.h>
19
20 namespace map {
21
22 /// An entity that can be placed on a map, moved around, animated, and possibly
23 /// interact with the player.
24 class Entity {
25
26 public:
27         static const int TYPE_ID = 605;
28
29 public:
30         Entity();
31         ~Entity() { }
32
33 public:
34         enum Orientation {
35                 ORIENTATION_NORTH = 0,
36                 ORIENTATION_EAST = 1,
37                 ORIENTATION_SOUTH = 2,
38                 ORIENTATION_WEST = 3,
39         };
40         enum Flags {
41                 FLAG_NONBLOCKING = 0x01,
42                 FLAG_PUSHABLE = 0x02,
43                 FLAG_FIXED_ORIENTATION = 0x04,
44         };
45
46 public:
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; }
50
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; }
54
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; }
58
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; }
62
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.
74         void StopAnimation();
75         /// Check if an animation is running.
76         bool AnimationRunning() const { return runner.Running(); }
77
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; }
82
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>);
95
96         /// Change to a natural, relaxed animation state (row offset 0).
97         void SetHandsFree();
98         /// Change animation to represent a carrying thingamabob (row offset 2).
99         void SetCarrying();
100         /// Set a pushy animation state (row offset 4).
101         void SetPushing();
102
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
109         /// tile.
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); }
116
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; }
122
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; }
128
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 *);
137
138         /// Check if position locks into grid defined by given tileSize.
139         bool TileLock(const math::Vector<int> &tileSize) const;
140
141         /// Integrate this entity's physical properties over given time interval.
142         void Update(Uint32 deltaT);
143
144         void Render(SDL_Surface *, const math::Vector<int> &offset) const;
145
146         static void CreateTypeDescription();
147         static void Construct(void *);
148         static void Load(void *);
149
150 private:
151         void UpdateVelocity();
152
153 private:
154         Entity *follower;
155         const graphics::Animation *animation;
156         const graphics::Sprite *sprite;
157         battle::PartyLayout *partyLayout;
158         battle::Monster **monsters;
159         int numMonsters;
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;
168         int flags;
169
170 };
171
172 }
173
174 #endif