diff --git a/src/levels/actions.c b/src/levels/actions.c index 9f39724..aa1c744 100644 --- a/src/levels/actions.c +++ b/src/levels/actions.c @@ -169,8 +169,19 @@ bool PassLevel(struct Game *game, struct TM_Action *action, enum TM_ActionState } bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state == TM_ACTIONSTATE_DESTROY) { + if (state == TM_ACTIONSTATE_INIT) { + action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(float))); + float* f = (float*)action->arguments->value; + *f = 0; + action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(ALLEGRO_AUDIO_STREAM*))); + ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; + *stream = al_load_audio_stream(GetDataFilePath(GetLevelFilename(game, "levels/?/letter.flac")), 4, 1024); + al_attach_audio_stream_to_mixer(*stream, game->audio.voice); + al_set_audio_stream_playing(*stream, false); + al_set_audio_stream_gain(*stream, 2.00); + action->arguments->next->next = NULL; + } + else if (state == TM_ACTIONSTATE_DESTROY) { ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; al_set_audio_stream_playing(*stream, false); al_destroy_audio_stream(*stream); @@ -178,7 +189,7 @@ bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState sta free(action->arguments->value); TM_DestroyArgs(action->arguments); } - if (state == TM_ACTIONSTATE_DRAW) { + else if (state == TM_ACTIONSTATE_DRAW) { float* f = (float*)action->arguments->value; al_draw_tinted_bitmap(game->level.letter, al_map_rgba(*f,*f,*f,*f), (game->viewportWidth-al_get_bitmap_width(game->level.letter))/2.0, al_get_bitmap_height(game->level.letter)*-0.05, 0); return false; @@ -190,19 +201,11 @@ bool Letter(struct Game *game, struct TM_Action *action, enum TM_ActionState sta ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; al_set_audio_stream_playing(*stream, true); } - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!action->arguments) { - action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(float))); - float* f = (float*)action->arguments->value; - *f = 0; - action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(ALLEGRO_AUDIO_STREAM*))); + else if (state == TM_ACTIONSTATE_START) { ALLEGRO_AUDIO_STREAM** stream = (ALLEGRO_AUDIO_STREAM**)action->arguments->next->value; - *stream = al_load_audio_stream(GetDataFilePath(GetLevelFilename(game, "levels/?/letter.flac")), 4, 1024); - al_attach_audio_stream_to_mixer(*stream, game->audio.voice); al_set_audio_stream_playing(*stream, true); - al_set_audio_stream_gain(*stream, 2.00); - action->arguments->next->next = NULL; } + else if (state != TM_ACTIONSTATE_RUNNING) return false; float* f = (float*)action->arguments->value; *f+=5; if (*f>255) *f=255; diff --git a/src/levels/level6.c b/src/levels/level6.c index e98def8..04641f7 100644 --- a/src/levels/level6.c +++ b/src/levels/level6.c @@ -27,7 +27,9 @@ void Level6_Load(struct Game *game) { Moonwalk_Load(game); - TM_AddBackgroundAction(&PassLevel, NULL, 5000, "passlevel"); + TM_AddAction(&DoMoonwalk, NULL, "moonwalk"); + TM_AddBackgroundAction(&DoMoonwalk, NULL, 5000, "moonwalkDerp"); + TM_AddBackgroundAction(&PassLevel, NULL, 10000, "passlevel"); FadeGameState(game, true); } diff --git a/src/levels/modules/dodger/actions.c b/src/levels/modules/dodger/actions.c index 3f90fa3..f6b2344 100644 --- a/src/levels/modules/dodger/actions.c +++ b/src/levels/modules/dodger/actions.c @@ -28,76 +28,67 @@ bool Accelerate(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.speed+=0.000015; - if (game->level.speed<0.0025) return false; - return true; + if (game->level.speed>=0.0025) return true; + return false; } // TODO: make it configurable and move to generic actions bool Walk(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) SelectDerpySpritesheet(game, "walk"); - action->arguments++; + if (state == TM_ACTIONSTATE_START) SelectDerpySpritesheet(game, "walk"); + else if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.derpy_x+=0.001; - if (game->level.derpy_x<0.05) return false; - return true; + if (game->level.derpy_x>=0.05) return true; + return false; } // TODO: make it configurable and move to generic actions bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.speed=0.000345; - if (game->level.st_pos<0.275) return false; - return true; + if (game->level.st_pos>=0.275) return true; + return false; } bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) { + if (state == TM_ACTIONSTATE_START) { SelectDerpySpritesheet(game, "fly"); game->level.derpy_angle = -0.15; - /*game->level.gg = true;*/ TM_AddBackgroundAction(&ShowMeter, NULL, 0, "showmeter"); - action->arguments++; } + else if (state == TM_ACTIONSTATE_DESTROY) { + game->level.handle_input = true; + } + else if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.derpy_y-=0.004; - if (game->level.derpy_y>0.2) return false; - game->level.handle_input=true; - return true; + if (game->level.derpy_y<=0.2) return true; + return false; } bool Run(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; - if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) { + if (state == TM_ACTIONSTATE_START) { game->level.handle_input=false; game->level.speed_modifier=1; - action->arguments++; } + else if (state == TM_ACTIONSTATE_DESTROY) { + game->level.derpy_angle = 0; + SelectDerpySpritesheet(game, "run"); + } + else if (state != TM_ACTIONSTATE_RUNNING) return false; game->level.derpy_y+=0.0042; if (game->level.derpy_angle > 0) { game->level.derpy_angle -= 0.02; if (game->level.derpy_angle < 0) game->level.derpy_angle = 0; } if (game->level.derpy_angle < 0) { game->level.derpy_angle += 0.02; if (game->level.derpy_angle > 0) game->level.derpy_angle = 0; } - if (game->level.derpy_y<0.65) return false; - game->level.derpy_angle = 0; - SelectDerpySpritesheet(game, "run"); - return true; + if (game->level.derpy_y>=0.65) return true; + return false; } bool GenerateObstacles(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - /*float* tmp; bool* in;*/ int* count; if (!action->arguments) { action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(int))); - /* action->arguments = TM_AddToArgs(action->arguments, malloc(sizeof(bool))); */ } count = (int*)action->arguments->value; - /*tmp = (float*)action->arguments->value; - in = (bool*)action->arguments->next->value;*/ if (state == TM_ACTIONSTATE_INIT) { *count = 0; - /* *tmp = 0; - *in = true;*/ } else if (state == TM_ACTIONSTATE_RUNNING) { if (rand()%(10000/(int)(85*game->level.speed_modifier))<=3) { diff --git a/src/levels/modules/moonwalk.c b/src/levels/modules/moonwalk.c index 4b27542..145a46c 100644 --- a/src/levels/modules/moonwalk.c +++ b/src/levels/modules/moonwalk.c @@ -25,7 +25,7 @@ // TODO: use Walk action instead bool DoMoonwalk(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { - if (state == TM_ACTIONSTATE_INIT) { + if (state == TM_ACTIONSTATE_START) { SelectDerpySpritesheet(game, "walk"); game->level.sheet_speed_modifier = 0.94; game->level.moonwalk.derpy_pos = -0.2; diff --git a/src/timeline.c b/src/timeline.c index e61a2bf..cd2003b 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -41,6 +41,10 @@ void TM_Process() { if returns true, delete it */ if (queue) { if (*queue->function) { + if (!queue->active) { + PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name); + (*queue->function)(game, queue, TM_ACTIONSTATE_START); + } queue->active = true; if ((*queue->function)(game, queue, TM_ACTIONSTATE_RUNNING)) { PrintConsole(game, "Timeline Manager: queue: destroy action (%d - %s)", queue->id, queue->name); @@ -50,7 +54,6 @@ void TM_Process() { (*tmp->function)(game, tmp, TM_ACTIONSTATE_DESTROY); free(tmp->name); free(tmp); - if (queue) PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name); } } else { /* delay handling */ @@ -59,9 +62,11 @@ void TM_Process() { queue = queue->next; free(tmp->name); free(tmp); - if (queue) PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name); } else { - al_start_timer(queue->timer); + if (!al_get_timer_started(queue->timer)) { + PrintConsole(game, "Timeline Manager: queue: delay started %d ms (%d - %s)", queue->delay, queue->id, queue->name); + al_start_timer(queue->timer); + } } } } @@ -167,8 +172,8 @@ void TM_HandleEvent(ALLEGRO_EVENT *ev) { al_destroy_timer(queue->timer); queue->timer = NULL; if (queue->function) { - PrintConsole(game, "Timeline Manager: queue: init action (%d - %s)", queue->id, queue->name); - (*queue->function)(game, queue, TM_ACTIONSTATE_INIT); + PrintConsole(game, "Timeline Manager: queue: run action (%d - %s)", queue->id, queue->name); + (*queue->function)(game, queue, TM_ACTIONSTATE_START); } else { PrintConsole(game, "Timeline Manager: queue: delay reached (%d - %s)", queue->id, queue->name); } @@ -178,11 +183,11 @@ void TM_HandleEvent(ALLEGRO_EVENT *ev) { struct TM_Action *pom = background; while (pom) { if (ev->timer.source == pom->timer) { - PrintConsole(game, "Timeline Manager: background: delay reached, init and run action (%d - %s)", pom->id, pom->name); + PrintConsole(game, "Timeline Manager: background: delay reached, run action (%d - %s)", pom->id, pom->name); pom->active=true; al_destroy_timer(pom->timer); pom->timer = NULL; - (*pom->function)(game, pom, TM_ACTIONSTATE_INIT); + (*pom->function)(game, pom, TM_ACTIONSTATE_START); return; } pom=pom->next; @@ -216,7 +221,7 @@ struct TM_Action* TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enu return action; } -struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name) { +struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name) { struct TM_Action *action = malloc(sizeof(struct TM_Action)); if (background) { struct TM_Action *pom = background; @@ -235,15 +240,19 @@ struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Ac action->delay = delay; action->id = ++lastid; if (delay) { + PrintConsole(game, "Timeline Manager: background: init action with delay %d ms (%d - %s)", delay, action->id, action->name); + (*action->function)(game, action, TM_ACTIONSTATE_INIT); action->active = false; action->timer = al_create_timer(delay/1000.0); al_register_event_source(game->event_queue, al_get_timer_event_source(action->timer)); al_start_timer(action->timer); } else { + PrintConsole(game, "Timeline Manager: background: init action (%d - %s)", action->id, action->name); + (*action->function)(game, action, TM_ACTIONSTATE_INIT); action->timer = NULL; action->active = true; - PrintConsole(game, "Timeline Manager: background: init and run action (%d - %s)", action->id, action->name); - (*action->function)(game, action, TM_ACTIONSTATE_INIT); + PrintConsole(game, "Timeline Manager: background: run action (%d - %s)", action->id, action->name); + (*action->function)(game, action, TM_ACTIONSTATE_START); } return action; } @@ -251,18 +260,18 @@ struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Ac /*! \brief Predefined action used by TM_AddQueuedBackgroundAction */ bool runinbackground(struct Game* game, struct TM_Action* action, enum TM_ActionState state) { if (state != TM_ACTIONSTATE_RUNNING) return false; - float* delay = (float*) action->arguments->next->value; + int* delay = (int*) action->arguments->next->value; char* name = (char*) action->arguments->next->next->value; TM_AddBackgroundAction(action->arguments->value, action->arguments->next->next->next, *delay, name); free(name); return true; } -struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name) { +struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name) { struct TM_Arguments* arguments = TM_AddToArgs(NULL, (void*) func); - arguments = TM_AddToArgs(arguments, malloc(sizeof(float))); + arguments = TM_AddToArgs(arguments, malloc(sizeof(int))); arguments = TM_AddToArgs(arguments, NULL); - *(float*)(arguments->next->value) = delay; + *(int*)(arguments->next->value) = delay; arguments->next->next->value = malloc((strlen(name)+1)*sizeof(char)); strcpy(arguments->next->next->value, name); @@ -270,13 +279,13 @@ struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct return TM_AddAction(*runinbackground, arguments, "TM_BackgroundAction"); } -void TM_AddDelay(float delay) { +void TM_AddDelay(int delay) { /*int *tmp; tmp = malloc(sizeof(int)); *tmp = delay; TM_AddAction(NULL, TM_AddToArgs(NULL, tmp));*/ struct TM_Action* tmp = TM_AddAction(NULL, NULL, "TM_Delay"); - PrintConsole(game, "Timeline Manager: queue: adding delay %f ms (%d)", delay, tmp->id); + PrintConsole(game, "Timeline Manager: queue: adding delay %d ms (%d)", delay, tmp->id); tmp->delay = delay; tmp->timer = al_create_timer(delay/1000.0); al_register_event_source(game->event_queue, al_get_timer_event_source(tmp->timer)); diff --git a/src/timeline.h b/src/timeline.h index 801c340..7825b9b 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -26,6 +26,7 @@ /*! \brief State of the TM_Action. */ enum TM_ActionState { TM_ACTIONSTATE_INIT, + TM_ACTIONSTATE_START, TM_ACTIONSTATE_RUNNING, TM_ACTIONSTATE_DRAW, TM_ACTIONSTATE_DESTROY, @@ -66,11 +67,11 @@ void TM_HandleEvent(ALLEGRO_EVENT *ev); /*! \brief Add new action to main queue. */ struct TM_Action* TM_AddAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, char* name); /*! \brief Add new action to background queue. */ -struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name); +struct TM_Action* TM_AddBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name); /*! \brief Add new action to main queue, which adds specified action into background queue. */ -struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, float delay, char* name); +struct TM_Action* TM_AddQueuedBackgroundAction(bool (*func)(struct Game*, struct TM_Action*, enum TM_ActionState), struct TM_Arguments* args, int delay, char* name); /*! \brief Add delay to main queue. */ -void TM_AddDelay(float delay); +void TM_AddDelay(int delay); /*! \brief Destroy timeline. */ void TM_Destroy(); /*! \brief Add data to TM_Arguments queue. */