revamp argument handling of timeline actions

This commit is contained in:
Sebastian Krzyszkowiak 2012-04-07 13:04:55 +02:00
parent f1e26219c4
commit 27ddfaafc9
3 changed files with 55 additions and 21 deletions

View file

@ -25,8 +25,6 @@
#include "level.h" #include "level.h"
#include "timeline.h" #include "timeline.h"
int x = 0, x2 = 0;
void Level_Passed(struct Game *game) { void Level_Passed(struct Game *game) {
if (game->level.current_level<6) { if (game->level.current_level<6) {
int available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1")); int available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1"));
@ -51,24 +49,48 @@ void Level_Draw(struct Game *game) {
} }
} }
bool napis(struct Game *game, struct TM_Action *lol) { bool napis(struct Game *game, struct TM_Action *action, enum TM_ActionState state) {
al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x, 20, 0, "wat"); if (!action->arguments) {
x+=10; action->arguments = malloc(sizeof(struct TM_Arguments));
if (x>1000) { x=0; return true; } action->arguments->value = malloc(sizeof(int));
action->arguments->next = NULL;
}
int* tmp;
tmp = (int*)action->arguments->value;
if (state == TM_ACTIONSTATE_INIT) {
*tmp = 0;
} else if (state == TM_ACTIONSTATE_RUNNING) {
al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), *tmp, 20, 0, "wat");
*tmp+=10;
if (*tmp>1000) { *tmp=0; return true; }
}
return false; return false;
} }
bool napis2(struct Game *game, struct TM_Action *lol) { bool napis2(struct Game *game, struct TM_Action *action, enum TM_ActionState state) {
al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), x2, 100, 0, "lol"); if (!action->arguments) {
x2+=5; action->arguments = malloc(sizeof(struct TM_Arguments));
if (x2>1000) { x2=0; return true; } action->arguments->value = malloc(sizeof(int));
action->arguments->next = NULL;
}
int* tmp;
tmp = (int*)action->arguments->value;
if (state == TM_ACTIONSTATE_INIT) {
*tmp = 0;
} else if (state == TM_ACTIONSTATE_RUNNING) {
al_draw_text_with_shadow(game->font, al_map_rgb(255,255,255), *tmp, 100, 0, "lol");
*tmp+=5;
if (*tmp>1000) { *tmp=0; return true; }
}
return false; return false;
} }
bool wyjscie(struct Game *game, struct TM_Action *lol) { bool wyjscie(struct Game *game, struct TM_Action *action, enum TM_ActionState state) {
Level_Passed(game); if (state == TM_ACTIONSTATE_DESTROY) {
game->gamestate = GAMESTATE_LOADING; Level_Passed(game);
game->loadstate = GAMESTATE_MAP; game->gamestate = GAMESTATE_LOADING;
game->loadstate = GAMESTATE_MAP;
}
return true; return true;
} }

View file

@ -42,9 +42,11 @@ void TM_Process() {
// if returns true, then delete it // if returns true, then delete it
if (queue) { if (queue) {
if (*queue->function) { if (*queue->function) {
if ((*queue->function)(game, queue)) { if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) {
(*queue->function)(game, queue, TM_ACTIONSTATE_DESTROY);
struct TM_Action *tmp = queue; struct TM_Action *tmp = queue;
queue = queue->next; queue = queue->next;
if (tmp->arguments) free(tmp->arguments); // TODO: cascade destroy
free(tmp); free(tmp);
} }
} else { } else {
@ -57,7 +59,8 @@ void TM_Process() {
while (pom!=NULL) { while (pom!=NULL) {
if (pom->active) { if (pom->active) {
if (*pom->function) { if (*pom->function) {
if ((*pom->function)(game, pom)) { if ((*pom->function)(game, pom, TM_ACTIONSTATE_RUNNING)) {
(*pom->function)(game, pom, TM_ACTIONSTATE_DESTROY);
if (tmp) { if (tmp) {
tmp->next = pom->next; tmp->next = pom->next;
} else { } else {
@ -72,6 +75,7 @@ void TM_Process() {
tmp = pom; tmp = pom;
pom = pom->next; pom = pom->next;
} else { } else {
if (pom->arguments) free(pom->arguments); // TODO: cascade destroy
free(pom); free(pom);
tmp2 = tmp; tmp2 = tmp;
if (!tmp) pom=background->next; if (!tmp) pom=background->next;
@ -88,7 +92,7 @@ void TM_HandleEvent(ALLEGRO_EVENT *ev) {
} }
void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args) { void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args) {
struct TM_Action *action = malloc(sizeof(struct TM_Action)); struct TM_Action *action = malloc(sizeof(struct TM_Action));
if (queue) { if (queue) {
struct TM_Action *pom = queue; struct TM_Action *pom = queue;
@ -105,9 +109,10 @@ void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Argum
action->timer = NULL; action->timer = NULL;
action->active = false; action->active = false;
action->delay = 0; action->delay = 0;
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
} }
void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay) { void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay) {
struct TM_Action *action = malloc(sizeof(struct TM_Action)); struct TM_Action *action = malloc(sizeof(struct TM_Action));
if (background) { if (background) {
struct TM_Action *pom = background; struct TM_Action *pom = background;
@ -124,6 +129,7 @@ void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struc
action->timer = NULL; action->timer = NULL;
action->active = true; action->active = true;
action->delay = 0; action->delay = 0;
(*action->function)(game, action, TM_ACTIONSTATE_INIT);
} }
void TM_AddDelay(int delay) { void TM_AddDelay(int delay) {

View file

@ -21,6 +21,12 @@
#include "main.h" #include "main.h"
enum TM_ActionState {
TM_ACTIONSTATE_INIT,
TM_ACTIONSTATE_RUNNING,
TM_ACTIONSTATE_DESTROY
};
struct TM_Arguments { struct TM_Arguments {
void *value; void *value;
struct TM_Arguments *next; struct TM_Arguments *next;
@ -28,7 +34,7 @@ struct TM_Arguments {
}; };
struct TM_Action { struct TM_Action {
bool (*function)(struct Game*, struct TM_Action*); bool (*function)(struct Game*, struct TM_Action*, enum TM_ActionState);
struct TM_Arguments *arguments; struct TM_Arguments *arguments;
ALLEGRO_TIMER *timer; ALLEGRO_TIMER *timer;
bool active; bool active;
@ -40,8 +46,8 @@ struct TM_Action {
void TM_Init(struct Game* game); void TM_Init(struct Game* game);
void TM_Process(); void TM_Process();
void TM_HandleEvent(ALLEGRO_EVENT *ev); void TM_HandleEvent(ALLEGRO_EVENT *ev);
void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args); void TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args);
void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*), struct TM_Arguments* args, int delay); void TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay);
void TM_AddDelay(int delay); void TM_AddDelay(int delay);
void TM_Pause(bool pause); void TM_Pause(bool pause);
void TM_Destroy(); void TM_Destroy();