Dumb-ways-to-memorize
2D game
Functions | Variables
parsepowerup.c File Reference
#include "globals.h"
#include "parsefunction.h"
#include "parsepowerup.h"
#include "parseobject.h"
#include "entity.h"
#include "mystrings.h"
#include <jsmn.h>
#include <string.h>
#include <stdio.h>
#include "dumb_physics.h"
#include "player.h"
+ Include dependency graph for parsepowerup.c:

Go to the source code of this file.

Functions

void Move (entity_t *targ, entity_t *info)
 
void Destroy (entity_t *targ, entity_t *info)
 
void Spawn (entity_t *targ, entity_t *info)
 
void Edit (entity_t *targ, entity_t *info)
 
void Nullify (entity_t *targ, entity_t *info)
 
void UpdateNormal (power_t *power)
 
void UpdateInfinite (power_t *power)
 
int GetUseType (const char *var, int *useType)
 
void CallInfo (power_t *self)
 
power_t * ParseToPowerUp (object_t *power, char *g_str)
 
power_t * FindPower (char *str)
 
void UsePower (power_t *power)
 

Variables

char * InteractionNames [] = {"move", "destroy", "spawn", "edit", "nullify", 0}
 
void InteractionSymbols = {Move, Destroy, Spawn, Edit, Nullify, 0}
 
power_t * gCurrentPowerUp = NULL
 
vec2_tmousePos = NULL
 
int * keyPower = NULL
 

Function Documentation

void CallInfo ( power_t *  self)

Call information functions for the power_up.

Parameters
[in,out]selfIf non-null, the class instance that this method operates on.
Author
Anthony Rios
Date
3/30/2016

Definition at line 142 of file parsepowerup.c.

References GetMousePos(), GetX(), GetXMouse(), gPlayer, INFO_BOTH, INFO_BUTTON, INFO_MOUSE, and keyPower.

Referenced by ParseToPowerUp().

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 }
void GetXMouse(entity_t *self, int *button, vec2_t *pos)
Definition: parsefunction.c:59
void GetMousePos(entity_t *self, vec2_t *pos)
Definition: parsefunction.c:53
vec2_t * mousePos
Definition: parsepowerup.c:17
void GetX(entity_t *self, int *button)
Definition: parsefunction.c:47
int * keyPower
Definition: parsepowerup.c:18
entity_t * gPlayer
Definition: player.c:9
void Destroy ( entity_t *  targ,
entity_t *  info 
)

Destroys target.

Parameters
[in,out]targIf non-null, the targ.
[in,out]infoIf non-null, the information.
Author
Anthony Rios
Date
3/30/2016

Definition at line 32 of file parsepowerup.c.

References FreeEntity().

33 {
34  if(targ)
35  {
36  FreeEntity(targ);
37  }
38 }
void FreeEntity(entity_t *ent)
Definition: entity.c:379
void Edit ( entity_t *  targ,
entity_t *  info 
)

Edits the target with values from info.

Parameters
[in,out]targIf non-null, the targ.
[in,out]infoIf non-null, the information.
Author
Anthony Rios
Date
3/30/2016

Definition at line 68 of file parsepowerup.c.

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 }
power_t* FindPower ( char *  str)

Searches for the first power with matching name str.

Parameters
[in,out]strIf non-null, the string.
Returns
null if it fails, else the found power.
Author
Anthony Rios
Date
3/30/2016

Definition at line 264 of file parsepowerup.c.

References CountMem(), and gPowerUps.

Referenced by UpdatePowerSelectMenu().

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 }
int CountMem(void *src, int size_type)
Definition: mymath.c:51
power_t * gPowerUps
Definition: game.c:42
int GetUseType ( const char *  var,
int *  useType 
)

Gets the usetype from var, and puts it in useType.

Parameters
varThe variable.
[in,out]useTypeIf non-null, type of the use.
Returns
The use type.
Author
Anthony Rios
Date
3/30/2016

Definition at line 124 of file parsepowerup.c.

Referenced by ParseToPowerUp().

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 }
void Move ( entity_t *  targ,
entity_t *  info 
)

Moves the target to position given by info.

Parameters
[in,out]targIf non-null, the targ.
[in,out]infoIf non-null, the information.
Author
Anthony Rios
Date
3/30/2016

Definition at line 20 of file parsepowerup.c.

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 }
void Nullify ( entity_t *  targ,
entity_t *  info 
)

Nullifies the entity by removing the think function of targ.

Parameters
[in,out]targIf non-null, the targ.
[in,out]infoIf non-null, the information.
Author
Anthony Rios
Date
3/30/2016

Definition at line 90 of file parsepowerup.c.

91 {
92  if(!targ)
93  {
94  printf("Null given null target \n");
95  }
96  targ->Think = NULL;
97 }
power_t* ParseToPowerUp ( object_t *  power,
char *  str 
)

Parses object/str data to a power up.

Parameters
[in,out]powerIf non-null, the power.
[in,out]strIf non-null, the string.
Returns
null if it fails, else a pointer to a power_t.
Author
Anthony Rios
Date
3/30/2016

Definition at line 157 of file parsepowerup.c.

References CallInfo(), FindCachedEntity(), FindValue(), FunctionNames, GetUseType(), INFINITE, INFO_BOTH, INFO_NONE, InteractionNames, InteractionSymbols, keyPower, ParseToFunction(), POWER_ENTITY_STR, POWER_INPUT_TYPE_STR, POWER_INTERACTION_STR, POWER_TARGET_STR, POWER_USE_TYPE_STR, STATIC, UpdateInfinite(), and UpdateNormal().

Referenced by LoadPowerUpData().

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 }
void CallInfo(power_t *self)
Definition: parsepowerup.c:142
Definition: jsmn.h:40
#define POWER_TARGET_STR
Definition: parsepowerup.h:7
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 UpdateNormal(power_t *power)
Definition: parsepowerup.c:99
#define POWER_INPUT_TYPE_STR
Definition: parsepowerup.h:10
char * InteractionNames[]
Definition: parsepowerup.c:13
int GetUseType(const char *var, int *useType)
Definition: parsepowerup.c:124
#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
int * keyPower
Definition: parsepowerup.c:18
void * ParseToFunction(const char *name)
Definition: parsefunction.c:11
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
void InteractionSymbols
Definition: parsepowerup.c:14
Definition: globals.h:19
void Spawn ( entity_t *  targ,
entity_t *  info 
)

Spawns entity from info to position of targ.

Parameters
[in,out]targIf non-null, the targ.
[in,out]infoIf non-null, the information.
Author
Anthony Rios
Date
3/30/2016

Definition at line 41 of file parsepowerup.c.

References ANIMATION_IDLE, ENTITY_DIR_RIGHT, InitNewEntity(), PHYSICS_BASE_SPEED_X, and Vec2Add().

Referenced by AttackAI().

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 }
entity_t * InitNewEntity()
Definition: entity.c:235
#define ENTITY_DIR_RIGHT
Definition: entity.h:10
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
void UpdateInfinite ( power_t *  power)

Updates an infinite use power_up

Parameters
[in,out]powerIf non-null, the power.
Author
Anthony Rios
Date
3/30/2016

Definition at line 119 of file parsepowerup.c.

References gPlayer.

Referenced by ParseToPowerUp().

120 {
121  power->GetTarg( (entity_t*) gPlayer, &power->target);
122 }
entity_t * gPlayer
Definition: player.c:9
void UpdateNormal ( power_t *  power)

Updates a normal use power_up, with uses > 0.

Parameters
[in,out]powerIf non-null, the power.
Author
Anthony Rios
Date
3/30/2016

Definition at line 99 of file parsepowerup.c.

References gPlayer.

Referenced by ParseToPowerUp().

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 }
entity_t * gPlayer
Definition: player.c:9
void UsePower ( power_t *  power)

Uses the power, more like a macro then anything.

Parameters
[in,out]powerIf non-null, the power.
Author
Anthony Rios
Date
3/30/2016

Definition at line 285 of file parsepowerup.c.

Referenced by InitPlayer().

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 }

Variable Documentation

power_t* gCurrentPowerUp = NULL

The currently loaded power_up

Definition at line 16 of file parsepowerup.c.

Referenced by DoPlayerThink(), ThinkPlayer(), and UpdatePowerSelectMenu().

char* InteractionNames[] = {"move", "destroy", "spawn", "edit", "nullify", 0}

The interaction names, which deal with what function power does

Definition at line 13 of file parsepowerup.c.

Referenced by ParseToPowerUp().

void InteractionSymbols = {Move, Destroy, Spawn, Edit, Nullify, 0}

The interaction symbol for the functions

Definition at line 14 of file parsepowerup.c.

Referenced by ParseToPowerUp().

int* keyPower = NULL

The key press related to using the power

Definition at line 18 of file parsepowerup.c.

Referenced by CallInfo(), and ParseToPowerUp().

vec2_t* mousePos = NULL

The mouse position

Definition at line 17 of file parsepowerup.c.