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