]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.h
13044880121569cfa54c1ef4f7ce34dbc6a64457
[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 orientation to given one.
84         /// If the entity is moving, velocity is changed accordingly.
85         void SetOrientation(Orientation);
86         Orientation GetOrientation() const { return orientation; }
87         /// Set the entity's speed in pixels per second.
88         /// This speed is then combined with the orientation to form a velocity.
89         void SetSpeed(math::Fixed<8>);
90
91         /// Change to a natural, relaxed animation state (row offset 0).
92         void SetHandsFree();
93         /// Change animation to represent a carrying thingamabob (row offset 2).
94         void SetCarrying();
95         /// Set a pushy animation state (row offset 4).
96         void SetPushing();
97
98         /// Set some basic boolean properties.
99         /// Parameter should be a combination from the Flags enum.
100         void SetFlags(int f) { flags = f; }
101         /// Check if the entity is blocking other entities from occupying its tile.
102         bool Blocking() const { return !(flags & FLAG_NONBLOCKING); }
103         /// Check if a battle should be launched when stepping onto a neighboring
104         /// tile.
105         bool Hostile() const { return partyLayout && numMonsters > 0; }
106         /// Check if this entity can be pushed around.
107         bool Pushable() const { return flags & FLAG_PUSHABLE; }
108         /// Check if the entity's orientation has any effect on the column rendered
109         /// from the animation or sprite.
110         bool CanTurn() const { return !(flags & FLAG_FIXED_ORIENTATION); }
111
112         /// Set a layout in battle for the party described by SetMonsters().
113         void SetPartyLayout(battle::PartyLayout *l) { partyLayout = l; }
114         /// Get the layout in battle for the party described by
115         /// Monsters{Begin,End}().
116         battle::PartyLayout *PartyLayout() { return partyLayout; }
117
118         /// Add monsters. This will cause the entity to be Hostile() and result in a
119         /// battle scene with given monsters when touched.
120         void SetMonsters(battle::Monster **m, int num) { monsters = m; numMonsters = num; }
121         battle::Monster **MonstersBegin() { return monsters; }
122         battle::Monster **MonstersEnd() { return monsters + numMonsters; }
123
124         /// Get an entity that should follow in this one's steps or 0 if none.
125         Entity *Follower() { return follower; }
126         const Entity *Follower() const { return follower; }
127         /// Add an entity that follows this one.
128         /// If this already has a follower, it is added to that one instead.
129         void AddFollower(Entity *);
130         /// Remove given entity from this entity or its follower.
131         void RemoveFollower(Entity *);
132
133         /// Check if position locks into grid defined by given tileSize.
134         bool TileLock(const math::Vector<int> &tileSize) const;
135
136         /// Integrate this entity's physical properties over given time interval.
137         void Update(Uint32 deltaT);
138
139         void Render(SDL_Surface *, const math::Vector<int> &offset) const;
140
141         static void CreateTypeDescription();
142         static void Construct(void *);
143         static void Load(void *);
144
145 private:
146         void UpdateVelocity();
147
148 private:
149         Entity *follower;
150         const graphics::Animation *animation;
151         const graphics::Sprite *sprite;
152         battle::PartyLayout *partyLayout;
153         battle::Monster **monsters;
154         int numMonsters;
155         graphics::AnimationRunner runner;
156         math::Vector<int> spriteOffset;
157         math::Vector<int> tilePosition;
158         math::Vector<math::Fixed<8> > position;
159         math::Vector<math::Fixed<8> > velocity;
160         Orientation orientation;
161         math::Fixed<8> speed;
162         int flags;
163
164 };
165
166 }
167
168 #endif