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.
27 static const int TYPE_ID = 605;
35 ORIENTATION_NORTH = 0,
37 ORIENTATION_SOUTH = 2,
41 FLAG_NONBLOCKING = 0x01,
43 FLAG_FIXED_ORIENTATION = 0x04,
47 /// Pixel resolved position of the entity's top left corner on the map.
48 geometry::Vector<float> &Position() { return position; }
49 const geometry::Vector<float> &Position() const { return position; }
51 /// Velocity of the entity in pixels per second.
52 geometry::Vector<float> &Velocity() { return velocity; }
53 const geometry::Vector<float> &Velocity() const { return velocity; }
55 /// Offset of the entity's sprite's to left corner relative to Position().
56 geometry::Vector<int> &SpriteOffset() { return spriteOffset; }
57 const geometry::Vector<int> &SpriteOffset() const { return spriteOffset; }
59 /// Reset the entity to the stored tile coordinates (usually set when
60 /// loading game data).
61 void ResetPosition(const geometry::Vector<int> &tileSize) { position = tilePosition * tileSize; }
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.
75 /// Check if an animation is running.
76 bool AnimationRunning() const { return runner.Running(); }
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; }
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.
91 /// Change to a natural, relaxed animation state (row offset 0).
93 /// Change animation to represent a carrying thingamabob (row offset 2).
95 /// Set a pushy animation state (row offset 4).
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
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); }
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; }
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; }
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 *);
133 /// Check if position locks into grid defined by given tileSize.
134 bool TileLock(const geometry::Vector<int> &tileSize) const;
136 /// Integrate this entity's physical properties over given time interval.
137 void Update(float deltaT);
139 void Render(SDL_Surface *, const geometry::Vector<int> &offset) const;
141 static void CreateTypeDescription();
142 static void Construct(void *);
143 static void Load(void *);
146 void UpdateVelocity();
150 const graphics::Animation *animation;
151 const graphics::Sprite *sprite;
152 battle::PartyLayout *partyLayout;
153 battle::Monster *monsters;
155 graphics::AnimationRunner runner;
156 geometry::Vector<int> spriteOffset;
157 geometry::Vector<int> tilePosition;
158 geometry::Vector<float> position;
159 geometry::Vector<float> velocity;
160 Orientation orientation;
168 #endif /* MAP_ENTITY_H_ */