From b6d813660943c3b9744d7d88774f306bcb0fbd1d Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Thu, 3 May 2012 02:19:55 +0200 Subject: [PATCH] implement obstracles callbacks --- src/level.c | 19 ++++++++++++++++++- src/main.h | 17 ++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/level.c b/src/level.c index 8155e80..0f949f5 100644 --- a/src/level.c +++ b/src/level.c @@ -72,6 +72,20 @@ bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state) return true; } +void Obst_MoveUpDown(struct Game *game, struct Obstracle *obstracle) { + if (obstracle->data) { + obstracle->y -= 0.5; + if (obstracle->y<=0) { + obstracle->data=NULL; + } + } else { + obstracle->y += 0.5; + if (obstracle->y>=100) { + obstracle->data++; + } + } +} + bool GenerateObstracles(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { /*float* tmp; bool* in;*/ int* count; @@ -98,6 +112,8 @@ bool GenerateObstracles(struct Game *game, struct TM_Action *action, enum TM_Act obst->speed = 0; obst->bitmap = game->level.obst_bmps.pie; obst->callback = NULL; + obst->data = NULL; + if (rand()%100<=50) obst->callback=Obst_MoveUpDown; if (game->level.obstracles) { game->level.obstracles->prev = obst; obst->next = game->level.obstracles; @@ -172,8 +188,9 @@ void Level_Draw(struct Game *game) { if (tmp->x > -10) { al_draw_bitmap(tmp->bitmap, (tmp->x/100.0)*al_get_display_width(game->display), (tmp->y/100.0)*al_get_display_height(game->display), 0); tmp->x -= game->level.speed*310; + if (tmp->callback) tmp->callback(game, tmp); } else { - /* Delete from queue... */ + /* TODO: Delete from queue... */ } tmp = tmp->next; } diff --git a/src/main.h b/src/main.h index 1051f04..0d8ca70 100644 --- a/src/main.h +++ b/src/main.h @@ -50,13 +50,7 @@ struct Moonwalk { double derpy_pos; /*!< Position of Derpy on screen. */ }; -struct Obstracle { - ALLEGRO_BITMAP *bitmap; - float x, y, speed; - int points; - void *callback; - struct Obstracle *prev, *next; -}; +struct Obstracle; /*! \brief Resources used by Level state. */ struct Level { @@ -223,6 +217,15 @@ struct Game { } audio; }; +struct Obstracle { + ALLEGRO_BITMAP *bitmap; + float x, y, speed; + int points; + void (*callback)(struct Game*, struct Obstracle*); + void *data; + struct Obstracle *prev, *next; +}; + /*! \brief Draws text with shadow. * * Draws given text two times: once with color (0,0,0,128) and 1px off in both x and y axis,