Dumb-ways-to-memorize
2D game
parsepowerup.c
Go to the documentation of this file.
1 #include "globals.h"
2 #include "parsefunction.h"
3 #include "parsepowerup.h"
4 #include "parseobject.h"
5 #include "entity.h"
6 #include "mystrings.h"
7 #include <jsmn.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include "dumb_physics.h"
11 #include "player.h"
12 
13 char *InteractionNames[] = {"move", "destroy", "spawn", "edit", "nullify", 0};
15 
16 power_t *gCurrentPowerUp = NULL;
17 vec2_t *mousePos = NULL;
18 int *keyPower = NULL;
19 
20 void Move(entity_t *targ, entity_t *info)
21 {
22  if(!targ || !info)
23  {
24  printf("Failed to do move , invalid target/info \n");
25  return;
26  }
27  targ->mPosition = info->mPosition;
28 }
29 
30 
31 //No other access necessary
32 void Destroy(entity_t *targ, entity_t *info)
33 {
34  if(targ)
35  {
36  FreeEntity(targ);
37  }
38 }
39 
40 //Needs access to parseEntity
41 void Spawn(entity_t *targ, entity_t *info)
42 {
43  entity_t *spawned;
44  if(!targ || !info)
45  {
46  printf("Spawn given blank targ/info \n");
47  return;
48  }
49  spawned = InitNewEntity();
50  if (!spawned)
51  {
52  printf("Max entities reached can't spawn any more \n");
53  return;
54  }
55  memcpy(spawned, info, sizeof(entity_t));
56  Vec2Add(&targ->mPosition, &spawned->mPosition, &spawned->mPosition);
57  if(targ->mDirection == ENTITY_DIR_RIGHT)
58  {
59  spawned->mPosition.x += targ->mSprites[ANIMATION_IDLE]->mSize.x;
60  spawned->mVelocity.x += PHYSICS_BASE_SPEED_X;
61  } else
62  {
63  spawned->mPosition.x -= targ->mSprites[ANIMATION_IDLE]->mSize.x;
64  spawned->mVelocity.x -= PHYSICS_BASE_SPEED_X;
65  }
66 }
67 
68 void Edit(entity_t *targ, entity_t *info)
69 {
70  int i, entity_size;
71  int *dst = (int*) targ;
72  int *value = (int*) info;
73  if(!targ || !info)
74  {
75  printf("Null edit, not doing \n");
76  return;
77  }
78  entity_size = sizeof(entity_t)/sizeof(int);
79  //iterate through members
80  for(i = 0; i < entity_size; i++)
81  {
82  if(*&value[i] && (*&dst[i] != *&value[i]))
83  {
84  *&dst[i] = *&value[i];
85  }
86  }
87 
88 }
89 
90 void Nullify(entity_t *targ, entity_t *info)
91 {
92  if(!targ)
93  {
94  printf("Null given null target \n");
95  }
96  targ->Think = NULL;
97 }
98 
99 void UpdateNormal(power_t* power)
100 {
101  if(!gPlayer)
102  {
103  printf("Player not set \n");
104  return;
105  }
106  if(!power)
107  {
108  printf("power not set \n");
109  return;
110  }
111  power->GetTarg((entity_t*) gPlayer, &power->target);
112  power->uses--;
113  if(power->uses == 0)
114  {
115  //free(power); Destroy Power
116  }
117 }
118 
119 void UpdateInfinite(power_t* power)
120 {
121  power->GetTarg( (entity_t*) gPlayer, &power->target);
122 }
123 
124 int GetUseType(const char *var, int *useType)
125 {
126  if(!strcmp("infinite", var))
127  {
128  *useType = -1;
129  return 0;
130  } else if(!strcmp("static", var))
131  {
132  *useType = 0;
133  return 0;
134  } else if(strpbrk(var, "-0123456789"))
135  {
136  sscanf(var, "%d", useType);
137  return 0;
138  }
139  return -1;
140 }
141 
142 void CallInfo(power_t *self)
143 {
144  switch (self->info_type)
145  {
146  case INFO_BOTH:
147  GetXMouse((entity_t*) gPlayer, keyPower, mousePos); break;
148  case INFO_BUTTON:
149  GetX((entity_t*) gPlayer, keyPower); break;
150  case INFO_MOUSE:
151  GetMousePos((entity_t*) gPlayer, mousePos); break;
152  default:
153  break;
154  }
155 }
156 
157 power_t* ParseToPowerUp(object_t* power, char* g_str)
158 {
159  int i;
160  char *temp_str;
161  jsmntok_t *temp_tok;
162  entity_t *temp_ent;
163  power_t *retVal;
164  retVal = (power_t*) malloc(sizeof(power_t));
165  if(!retVal)
166  {
167  printf("Power up malloc error \n");
168  return NULL;
169  }
170  memset(retVal, 0, sizeof(power_t));
171  if(!power || !g_str)
172  {
173  printf("Power Up tried to parse NULL \n");
174  return NULL;
175  }
176  if (!power->name)
177  {
178  printf("Tried to parse Power Up with No Name");
179  return NULL;
180  }
181  retVal->name = power->name;
182 
183  if( (temp_str = FindValue(power, POWER_TARGET_STR, g_str)) != NULL )
184  {
185  retVal->GetTarg = ParseToFunction(temp_str);
186  if(temp_str) free(temp_str);
187  }
188  //Use Type
189  if( (temp_str = FindValue(power, POWER_USE_TYPE_STR, g_str)) != NULL )
190  {
191  GetUseType(temp_str, &retVal->uses);
192  if(temp_str) free(temp_str);
193  }
194  switch(retVal->uses)
195  {
196  case INFINITE:
197  retVal->UpdateUse = UpdateInfinite;
198  break;
199  case STATIC:
200  retVal->UpdateUse = NULL;
201  break;
202  default:
203  retVal->UpdateUse = UpdateNormal;
204  }
205 
206  //Input Type
207  if( (temp_str = FindValue(power, POWER_INPUT_TYPE_STR, g_str)) != NULL )
208  {
209  for(i = 0; i < INFO_BOTH; i++ )
210  {
211  if(!strcmp(FunctionNames[i], temp_str))
212  {
213  retVal->info_type = (info_type_t) (INFO_BOTH - i);
214  break;
215  }
216  retVal->info_type = INFO_NONE;
217  }
218  if(temp_str) free(temp_str);
219  }
220 
221  if(retVal->info_type)
222  {
223  retVal->UpdateInput = CallInfo;
224  keyPower = (int*) malloc(sizeof(int));
225  memset(keyPower,0, sizeof(int));
226  mousePos = (vec2_t*) malloc(sizeof(vec2_t));
227  memset(mousePos,0, sizeof(vec2_t));
228  }
229 
230  //Interaction
231  if( (temp_str = FindValue(power, POWER_INTERACTION_STR, g_str)) != NULL )
232  {
233  for(i = 0; InteractionNames[i]; i++ )
234  {
235  if(!strcmp(InteractionNames[i], temp_str))
236  {
237  retVal->DoPower = (void(*)(entity_t *targ, entity_t *info)) InteractionSymbols[i];
238  break;
239  }
240  retVal->DoPower = NULL;
241  }
242  if(temp_str) free(temp_str);
243  }
244 
245  //Info
246  if( (temp_str = FindValue(power, POWER_ENTITY_STR, g_str)) != NULL )
247  {
248  if( (temp_ent = FindCachedEntity( temp_str )) != NULL )
249  {
250  retVal->info = temp_ent;
251  } else
252  {
253  printf("Failed to identify/find entity in power : %s \n", power->name);
254  retVal->info = NULL;
255  }
256  } else
257  {
258  retVal->info = NULL;//ParseToEntity(power, g_str);
259  }
260 
261  return retVal;
262 }
263 
264 power_t* FindPower(char* str)
265 {
266  int i, count;
267  if(!gPowerUps)
268  {
269  printf("No PowerUps Loaded");
270  return NULL;
271  }
272  count = CountMem(gPowerUps, sizeof(power_t));
273  for(i = 0; i < count; i++)
274  {
275  if(!gPowerUps[i].name)
276  continue;
277  if(!strcmp(gPowerUps[i].name, str))
278  {
279  return &gPowerUps[i];
280  }
281  }
282  return NULL;
283 }
284 
285 void UsePower(power_t* power)
286 {
287  if(!power)
288  {
289  return;
290  }
291  if(power->UpdateInput)
292  {
293  power->UpdateInput(power);
294  }
295  if(power->UpdateUse)
296  {
297  power->UpdateUse(power);
298  }
299  if(power->DoPower)
300  {
301  power->DoPower(power->target, power->info);
302  }
303 
304 }
void CallInfo(power_t *self)
Definition: parsepowerup.c:142
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
power_t * ParseToPowerUp(object_t *power, char *g_str)
Definition: parsepowerup.c:157
#define POWER_TARGET_STR
Definition: parsepowerup.h:7
void Destroy(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:32
void GetXMouse(entity_t *self, int *button, vec2_t *pos)
Definition: parsefunction.c:59
void Edit(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:68
void GetMousePos(entity_t *self, vec2_t *pos)
Definition: parsefunction.c:53
entity_t * InitNewEntity()
Definition: entity.c:235
vec2_t * mousePos
Definition: parsepowerup.c:17
void UpdateInfinite(power_t *power)
Definition: parsepowerup.c:119
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
void Spawn(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:41
void UpdateNormal(power_t *power)
Definition: parsepowerup.c:99
void GetX(entity_t *self, int *button)
Definition: parsefunction.c:47
void Move(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:20
power_t * gPowerUps
Definition: game.c:42
#define POWER_INPUT_TYPE_STR
Definition: parsepowerup.h:10
char * InteractionNames[]
Definition: parsepowerup.c:13
void UsePower(power_t *power)
Definition: parsepowerup.c:285
int GetUseType(const char *var, int *useType)
Definition: parsepowerup.c:124
void Nullify(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:90
#define POWER_INTERACTION_STR
Definition: parsepowerup.h:9
#define POWER_USE_TYPE_STR
Definition: parsepowerup.h:8
info_type_t
Definition: parsepowerup.h:21
#define POWER_ENTITY_STR
Definition: parsepowerup.h:11
char * FunctionNames[]
Definition: parsefunction.c:8
#define ENTITY_DIR_RIGHT
Definition: entity.h:10
int * keyPower
Definition: parsepowerup.c:18
void * ParseToFunction(const char *name)
Definition: parsefunction.c:11
power_t * FindPower(char *str)
Definition: parsepowerup.c:264
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
void FreeEntity(entity_t *ent)
Definition: entity.c:379
void InteractionSymbols
Definition: parsepowerup.c:14
power_t * gCurrentPowerUp
Definition: parsepowerup.c:16
entity_t * gPlayer
Definition: player.c:9
Definition: globals.h:19