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