Dumb-ways-to-memorize
2D game
entity.h
Go to the documentation of this file.
1 #ifndef __ENTITY__H
2 #define __ENTITY__H
3 
4 #include "globals.h"
5 #include "graphics.h"
6 #include "ai_interpret.h"
7 
8 #define MAX_ENTITIES 250
9 #define ENTITY_DIR_LEFT 0
10 #define ENTITY_DIR_RIGHT 1
11 
12 typedef struct entity_s entity_t;
13 struct power_s;
14 /**
15  * The core structure of our our entity system.
16  *
17  * @author Anthony Rios
18  * @date 2/17/2016
19  */
20 
21 struct entity_s
22 {
23  int mHazards; /**< The hazards that the entity is immune to / deals*/
24  int mNextThink; /**< The next time the entity should think */
25  int mHealth; /**< The health of the entity, if below zero - destroy this entity*/
26  int mDamage; /**< The damage the entity should deal to other entities */
27  int mWeight; /**< The weight boolean of the entity, if gravity affects him*/
28  int mCurrentFrame; /**< The current frame */
29  int mDirection:1; /**< The direction entity is facing*/
30  collision_type_t mCollisionType; /**< Type of the collision the entity is */
31  entity_state_t mEntityState; /**< The state of the entity */
32  sprite_t **mSprites; /**< The sprites of the given entity */
33  sprite_t *mAnimation; /**< The current animation, the entity is running */
34  ai_function_t *mData; /**< The data that an entity stores for its ai */
35  char *mName; /**< The name of the entity*/
36  vec2_t mAccel; /**< The acceleration vector */
37  vec2_t mVelocity; /**< The velocity vector */
38  vec2_t mPosition; /**< The position vector, equal to on screen draw position */
39  void (*Think)(entity_t *self); /**< The think function which gets called to update the entity*/
40  void (*Touch)(entity_t *self, entity_t *other, int type); /**< The function that gets called when enitities collide*/
41  void (*Draw)(entity_t *self); /**< The function that gets called every frame to draw the entity*/
42  void (*PowerUp)(struct power_s *info); /**< The player specific function for power_up useage */
43 };
44 
45 extern entity_t *gEntityDictionary; /**< Entities loaded from files AKA cached entities */
46 extern entity_t *gEntities; /**< The memory for entities that are currently running in the game */
47 extern int gLastEntity; /**< The last entity that was free'd or created */
48 
49 /**
50  * Init entity system.
51  *
52  * @return An int of failure state. 0 if good, -1 if error.
53  *
54  * @author Anthony Rios
55  * @date 3/29/2016
56  */
57 extern int InitEntitySystem();
58 
59 /**
60  * Init new entity in the memory of global entity space.
61  *
62  * @return null if it fails, else a pointer to an entity_t.
63  *
64  * @author Anthony Rios
65  * @date 3/29/2016
66  */
67 extern entity_t *InitNewEntity();
68 
69 /**
70  * Searches for the next free cache position in gEntitiesDictionary.
71  *
72  * @return null if it fails, else the found free cache position.
73  *
74  * @author Anthony Rios
75  * @date 3/29/2016
76  */
77 extern entity_t *FindNextFreeCachePos();
78 
79 /**
80  * Searches for the cached entity whose name matches given str.
81  *
82  * @param name The name of the entity.
83  *
84  * @return null if it fails, else the found cached entity.
85  *
86  * @author Anthony Rios
87  * @date 3/29/2016
88  */
89 extern entity_t *FindCachedEntity(const char *name);
90 
91 /**
92  * Searches for the first entity with the name of given str.
93  *
94  * @param name The name of the searched for entity.
95  *
96  * @return null if it fails, else the found entity.
97  *
98  * @author Anthony Rios
99  * @date 3/29/2016
100  */
101 extern entity_t *FindEntity(const char *name);
102 
103 /**
104  * Searches for the first free entity.
105  *
106  * @param [in,out] position If non-null, the position.
107  *
108  * @return null if it fails, else the found free entity.
109  *
110  * @author Anthony Rios
111  * @date 3/29/2016
112  */
113 extern entity_t *FindFreeEntity(int* position);
114 
115 /**
116  * Look for entity at position.
117  *
118  * @param position The position.
119  *
120  * @return null if it fails, else a pointer to an entity_t.
121  *
122  * @author Anthony Rios
123  * @date 3/29/2016
124  */
125 extern entity_t *LookForEntityAtPos(vec2_t position);
126 
127 /**
128  * Distance 2 entity other from entity self.
129  *
130  * @param [in,out] self If non-null, the class instance that this method operates on.
131  * @param [in,out] other If non-null, the other entity.
132  *
133  * @return An int.
134  *
135  * @author Anthony Rios
136  * @date 3/29/2016
137  */
138 extern int Distance2Entity(entity_t *self, entity_t *other);
139 
140 /**
141  * Returns a function pointer to the think function, given ai_function data.
142  *
143  * @param [in,out] parameter1 If non-null, the first parameter.
144  *
145  * @return null if it fails, else a GetFunctionAI(ai_function_t *data.
146  *
147  * @author Anthony Rios
148  * @date 3/29/2016
149  */
150 extern void (*GetFunctionAI(ai_function_t *data))(entity_t *);
151 
152 /**
153  * Executes the entities think functions.
154  *
155  * @note I think I have the logic wrong..
156  * @author Anthony Rios
157  * @date 3/29/2016
158  */
159 void RunEntities();
160 
161 /**
162  * Draw entities, if they have draw functions.
163  *
164  * @author Anthony Rios
165  * @date 3/29/2016
166  */
167 void DrawEntities();
168 
169 /**
170  * Prints the entities data, used for debug.
171  *
172  * @param [in,out] ent If non-null, the ent.
173  *
174  * @author Anthony Rios
175  * @date 3/29/2016
176  */
177 extern void PrintEntity(entity_t *ent);
178 
179 /**
180  * Free entity.
181  *
182  * @note right now it just acts like memset(0) on entities in the global Entities scope.
183  * But it free's any other data that isn't globally set.
184  *
185  * @param [in,out] ent If non-null, the ent.
186  *
187  * @author Anthony Rios
188  * @date 3/29/2016
189  */
190 void FreeEntity(entity_t *ent);
191 
192 /**
193  * Frees the non player entities, use for Destroy World.
194  *
195  * @author Anthony Rios
196  * @date 3/29/2016
197  */
198 void FreeNonPlayerEntities();
199 
200 /**
201  * Shutdown entity system, frees all the entities.
202  *
203  * @author Anthony Rios
204  * @date 3/29/2016
205  */
206 void ShutdownEntitySystem();
207 
208 //Draw Functions
209 /**
210  * Draws the entity via self->mAnimation if set, or the first sprite which is idle.
211  *
212  * @param [in,out] self If non-null, the class instance that this method operates on.
213  *
214  * @author Anthony Rios
215  * @date 3/29/2016
216  */
217 void DrawGeneric(entity_t *self);
218 //Unused for now
219 void DrawPlayer(entity_t *self);
220 
221 //Think Functions
222 /**
223  * Generic think function, checks health and dies when health <= 0
224  *
225  * @param [in,out] self If non-null, the class instance that this method operates on.
226  *
227  * @author Anthony Rios
228  * @date 3/29/2016
229  */
230 void ThinkGeneric(entity_t *self);
231 
232 /**
233  * The player think function, handles lives ,gamestate switching , and input.
234  *
235  * @param [in,out] self If non-null, the class instance that this method operates on.
236  *
237  * @author Anthony Rios
238  * @date 3/29/2016
239  */
240 void ThinkPlayer(entity_t *self);
241 
242 /**
243  * The modular think function for enemy entities with an AI.
244  *
245  * @param [in,out] self If non-null, the class instance that this method operates on.
246  *
247  * @author Anthony Rios
248  * @date 3/29/2016
249  */
250 void ThinkEnemy(entity_t *self);
251 
252 /**
253  * Generic touch function,
254  * Receives pain from touch and inflict pain on touch depending on hazard types.
255  *
256  * @param [in,out] self If non-null, the class instance that this method operates on.
257  * @param [in,out] other If non-null, the other.
258  * @param type The type.
259  *
260  * @author Anthony Rios
261  * @date 3/29/2016
262  */
263 void TouchGeneric(entity_t *self, entity_t *other, int type);
264 
265 /**
266  * The player touch function called on collision.
267  * If he touches a hazard, he gets damaged.
268  *
269  * @param [in,out] self If non-null, the class instance that this method operates on.
270  * @param [in,out] other If non-null, the other.
271  * @param type The type.
272  *
273  * @author Anthony Rios
274  * @date 3/30/2016
275  */
276 void TouchPlayer(entity_t *self, entity_t *other, int type);
277 
278 /**
279  * The touch function for an enemy, currently does nothing.
280  *
281  * @param [in,out] self If non-null, the class instance that this method operates on.
282  * @param [in,out] other If non-null, the other.
283  * @param type The type.
284  *
285  * @author Anthony Rios
286  * @date 3/30/2016
287  */
288 
289 void TouchEnemy(entity_t *self, entity_t *other, int type);
290 
291 /**
292  * The touch function for the goal entity / flag.
293  * Switches gamestate based on if you won or should go to the next level.
294  *
295  * @param [in,out] self If non-null, the class instance that this method operates on.
296  * @param [in,out] other If non-null, the other entity it touched.
297  * @param type The type.
298  *
299  * @author Anthony Rios
300  * @date 3/29/2016
301  */
302 void TouchGoal(entity_t *self, entity_t *other, int type);
303 
304 
305 #endif
int mNextThink
Definition: entity.h:24
void DrawPlayer(entity_t *self)
Definition: entity.c:40
char * name
Definition: parsepowerup.h:41
collision_type_t mCollisionType
Definition: entity.h:30
void TouchPlayer(entity_t *self, entity_t *other, int type)
Definition: entity.c:168
void TouchGoal(entity_t *self, entity_t *other, int type)
Definition: entity.c:209
entity_t * LookForEntityAtPos(vec2_t position)
Definition: entity.c:355
vec2_t mPosition
Definition: entity.h:38
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
entity_t * gEntityDictionary
Definition: game.c:41
void TouchGeneric(entity_t *self, entity_t *other, int type)
Definition: entity.c:139
entity_t * FindNextFreeCachePos()
Definition: entity.c:250
void(* PowerUp)(struct power_s *info)
Definition: entity.h:42
int InitEntitySystem()
Definition: entity.c:217
void(* Draw)(entity_t *self)
Definition: entity.h:41
void(* Think)(entity_t *self)
Definition: entity.h:39
int mDamage
Definition: entity.h:26
int mCurrentFrame
Definition: entity.h:28
entity_t * info
Definition: parsepowerup.h:49
entity_t * gEntities
Definition: entity.c:11
int mWeight
Definition: entity.h:27
int mHazards
Definition: entity.h:23
entity_t * FindFreeEntity(int *position)
Definition: entity.c:331
void(* Touch)(entity_t *self, entity_t *other, int type)
Definition: entity.h:40
int gLastEntity
Definition: entity.c:12
entity_state_t
Definition: globals.h:45
void FreeEntity(entity_t *ent)
Definition: entity.c:379
void ThinkEnemy(entity_t *self)
Definition: entity.c:102
void FreeNonPlayerEntities()
Definition: entity.c:409
void ThinkPlayer(entity_t *self)
Definition: entity.c:59
int Distance2Entity(entity_t *self, entity_t *other)
Definition: entity.c:371
void PrintEntity(entity_t *ent)
Definition: parseentity.c:195
vec2_t mAccel
Definition: entity.h:36
entity_t * FindEntity(const char *name)
Definition: entity.c:316
int mHealth
Definition: entity.h:25
entity_t * InitNewEntity()
Definition: entity.c:235
void RunEntities()
Definition: entity.c:281
void DrawGeneric(entity_t *self)
Definition: entity.c:17
void ShutdownEntitySystem()
Definition: entity.c:427
sprite_t ** mSprites
Definition: entity.h:32
ai_function_t * mData
Definition: entity.h:34
entity_state_t mEntityState
Definition: entity.h:31
void TouchEnemy(entity_t *self, entity_t *other, int type)
Definition: entity.c:196
collision_type_t
Definition: globals.h:53
char * mName
Definition: entity.h:35
void DrawEntities()
Definition: entity.c:264
int mDirection
Definition: entity.h:29
void ThinkGeneric(entity_t *self)
Definition: entity.c:45
sprite_t * mAnimation
Definition: entity.h:33
void(*)(entity_t *) GetFunctionAI(ai_function_t *data)
Definition: ai_interpret.c:238
vec2_t mVelocity
Definition: entity.h:37
Definition: globals.h:19