]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.h
d68861c7a60c5a7006aefe234abd39d26d3af294
[l2e.git] / src / map / Entity.h
1 #ifndef MAP_ENTITY_H_
2 #define MAP_ENTITY_H_
3
4 #include "../battle/fwd.h"
5 #include "../battle/Monster.h"
6 #include "../math/Fixed.h"
7 #include "../math/Vector.h"
8 #include "../graphics/fwd.h"
9 #include "../graphics/Animation.h"
10
11 #include <functional>
12 #include <SDL.h>
13
14 namespace map {
15
16 /// An entity that can be placed on a map, moved around, animated, and possibly
17 /// interact with the player.
18 class Entity {
19
20 public:
21         static const int TYPE_ID = 605;
22
23 public:
24         Entity();
25         ~Entity() { }
26
27 public:
28         enum Orientation {
29                 ORIENTATION_NORTH = 0,
30                 ORIENTATION_EAST = 1,
31                 ORIENTATION_SOUTH = 2,
32                 ORIENTATION_WEST = 3,
33         };
34         enum Flags {
35                 FLAG_NONBLOCKING = 0x01,
36                 FLAG_PUSHABLE = 0x02,
37                 FLAG_FIXED_ORIENTATION = 0x04,
38         };
39
40 public:
41         /// Pixel resolved position of the entity's top left corner on the map.
42         math::Vector<math::Fixed<8> > &Position() { return position; }
43         const math::Vector<math::Fixed<8> > &Position() const { return position; }
44
45         /// Velocity of the entity in pixels per second.
46         math::Vector<math::Fixed<8> > &Velocity() { return velocity; }
47         const math::Vector<math::Fixed<8> > &Velocity() const { return velocity; }
48
49         /// Offset of the entity's sprite's to left corner relative to Position().
50         math::Vector<int> &SpriteOffset() { return spriteOffset; }
51         const math::Vector<int> &SpriteOffset() const { return spriteOffset; }
52
53         /// Reset the entity to the stored tile coordinates (usually set when
54         /// loading game data).
55         void ResetPosition(const math::Vector<int> &tileSize) { position = tilePosition * tileSize; }
56
57         /// Set the animation to use for animated entities.
58         /// For orientable entities, the animation  should have north, south, east,
59         /// and west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively.
60         /// If the entity can carry, row offset 2 is used.
61         /// If the entity can push, row offset 4 is used.
62         void SetAnimation(const graphics::Animation *a);
63         /// Start the animation on a global timer.
64         void StartAnimation(app::Application &ctrl);
65         /// Start the animation on a state timer.
66         void StartAnimation(app::State &ctrl);
67         /// Stop the animation.
68         void StopAnimation();
69         /// Check if an animation is running.
70         bool AnimationRunning() const { return runner.Running(); }
71
72         /// Set the sprite used for the non-animated state.
73         /// For orientable entities, the sprite should have north, south, east, and
74         /// west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively.
75         void SetSprite(const graphics::Sprite *s) { sprite = s; }
76
77         /// Change the entity's orientation to given one.
78         /// If the entity is moving, velocity is changed accordingly.
79         void SetOrientation(Orientation);
80         Orientation GetOrientation() const { return orientation; }
81         /// Set the entity's speed in pixels per second.
82         /// This speed is then combined with the orientation to form a velocity.
83         void SetSpeed(math::Fixed<8>);
84
85         /// Change to a natural, relaxed animation state (row offset 0).
86         void SetHandsFree();
87         /// Change animation to represent a carrying thingamabob (row offset 2).
88         void SetCarrying();
89         /// Set a pushy animation state (row offset 4).
90         void SetPushing();
91
92         /// Set some basic boolean properties.
93         /// Parameter should be a combination from the Flags enum.
94         void SetFlags(int f) { flags = f; }
95         /// Check if the entity is blocking other entities from occupying its tile.
96         bool Blocking() const { return !(flags & FLAG_NONBLOCKING); }
97         /// Check if a battle should be launched when stepping onto a neighboring
98         /// tile.
99         bool Hostile() const { return partyLayout && numMonsters > 0; }
100         /// Check if this entity can be pushed around.
101         bool Pushable() const { return flags & FLAG_PUSHABLE; }
102         /// Check if the entity's orientation has any effect on the column rendered
103         /// from the animation or sprite.
104         bool CanTurn() const { return !(flags & FLAG_FIXED_ORIENTATION); }
105
106         /// Set a layout in battle for the party described by SetMonsters().
107         void SetPartyLayout(battle::PartyLayout *l) { partyLayout = l; }
108         /// Get the layout in battle for the party described by
109         /// Monsters{Begin,End}().
110         battle::PartyLayout *PartyLayout() { return partyLayout; }
111
112         /// Add monsters. This will cause the entity to be Hostile() and result in a
113         /// battle scene with given monsters when touched.
114         void SetMonsters(battle::Monster **m, int num) { monsters = m; numMonsters = num; }
115         battle::Monster **MonstersBegin() { return monsters; }
116         battle::Monster **MonstersEnd() { return monsters + numMonsters; }
117
118         /// Get an entity that should follow in this one's steps or 0 if none.
119         Entity *Follower() { return follower; }
120         const Entity *Follower() const { return follower; }
121         /// Add an entity that follows this one.
122         /// If this already has a follower, it is added to that one instead.
123         void AddFollower(Entity *);
124         /// Remove given entity from this entity or its follower.
125         void RemoveFollower(Entity *);
126
127         /// Check if position locks into grid defined by given tileSize.
128         bool TileLock(const math::Vector<int> &tileSize) const;
129
130         /// Integrate this entity's physical properties over given time interval.
131         void Update(Uint32 deltaT);
132
133         void Render(SDL_Surface *, const math::Vector<int> &offset) const;
134
135         static void CreateTypeDescription();
136         static void Construct(void *);
137         static void Load(void *);
138
139 private:
140         void UpdateVelocity();
141
142 private:
143         Entity *follower;
144         const graphics::Animation *animation;
145         const graphics::Sprite *sprite;
146         battle::PartyLayout *partyLayout;
147         battle::Monster **monsters;
148         int numMonsters;
149         graphics::AnimationRunner runner;
150         math::Vector<int> spriteOffset;
151         math::Vector<int> tilePosition;
152         math::Vector<math::Fixed<8> > position;
153         math::Vector<math::Fixed<8> > velocity;
154         Orientation orientation;
155         math::Fixed<8> speed;
156         int flags;
157
158 };
159
160 }
161
162 #endif /* MAP_ENTITY_H_ */