4 * Created on: Sep 29, 2012
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"
22 /// An entity that can be placed on a map, moved around, animated, and possibly
23 /// interact with the player.
32 ORIENTATION_NORTH = 0,
34 ORIENTATION_SOUTH = 2,
38 FLAG_NONBLOCKING = 0x01,
40 FLAG_FIXED_ORIENTATION = 0x04,
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; }
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; }
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; }
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; }
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.
72 /// Check if an animation is running.
73 bool AnimationRunning() const { return runner.Running(); }
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; }
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.
88 /// Change to a natural, relaxed animation state (row offset 0).
90 /// Change animation to represent a carrying thingamabob (row offset 2).
92 /// Set a pushy animation state (row offset 4).
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
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); }
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; }
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; }
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 *);
130 /// Check if position locks into grid defined by given tileSize.
131 bool TileLock(const geometry::Vector<int> &tileSize) const;
133 /// Integrate this entity's physical properties over given time interval.
134 void Update(float deltaT);
136 void Render(SDL_Surface *, const geometry::Vector<int> &offset) const;
138 static void CreateTypeDescription();
139 static void Construct(void *);
140 static void Load(void *);
143 void UpdateVelocity();
147 const graphics::Animation *animation;
148 const graphics::Sprite *sprite;
149 battle::PartyLayout *partyLayout;
150 battle::Monster *monsters;
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;
165 #endif /* MAP_ENTITY_H_ */