bring back Map gamestate

This commit is contained in:
Sebastian Krzyszkowiak 2012-12-27 00:46:23 +01:00
parent 1e901cf51b
commit 5d3bb2d0d7
3 changed files with 97 additions and 82 deletions

View file

@ -13,7 +13,7 @@ ENDMACRO()
#GAMESTATE("menu") #GAMESTATE("menu")
GAMESTATE("disclaimer") GAMESTATE("disclaimer")
GAMESTATE("intro") GAMESTATE("intro")
#GAMESTATE("map") GAMESTATE("map")
##GAMESTATE("level") ##GAMESTATE("level")
GAMESTATE("about") GAMESTATE("about")
##GAMESTATE("pause") ##GAMESTATE("pause")

View file

@ -20,13 +20,16 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "../utils.h"
#include "../config.h" #include "../config.h"
#include "map.h" #include "map.h"
void Map_Draw(struct Game *game) { int Gamestate_ProgressCount = 0;
al_draw_bitmap(game->map.map, 0, 0, 0);
void Gamestate_Draw(struct Game *game, struct MapResources* data) {
al_draw_bitmap(data->map, 0, 0, 0);
float x=0,y=0; float x=0,y=0;
switch (game->map.selected) { switch (data->selected) {
case 1: case 1:
x=0.2; x=0.2;
y=0.25; y=0.25;
@ -52,104 +55,105 @@ void Map_Draw(struct Game *game) {
y=0.675; y=0.675;
break; break;
} }
al_draw_scaled_bitmap(game->map.arrow, 0, 0, al_get_bitmap_width(game->map.arrow), al_get_bitmap_height(game->map.arrow), (game->viewportWidth-game->viewportHeight*1.6)/2+game->viewportHeight*1.6*x, game->viewportHeight*y + ((sin(game->map.arrowpos)+0.5)/20.0)*game->viewportHeight, game->viewportHeight*1.6*0.1, game->viewportHeight*0.16, 0); al_draw_scaled_bitmap(data->arrow, 0, 0, al_get_bitmap_width(data->arrow), al_get_bitmap_height(data->arrow), (game->viewport.width-game->viewport.height*1.6)/2+game->viewport.height*1.6*x, game->viewport.height*y + ((sin(data->arrowpos)+0.5)/20.0)*game->viewport.height, game->viewport.height*1.6*0.1, game->viewport.height*0.16, 0);
} }
void Map_Logic(struct Game *game) { void Gamestate_Logic(struct Game *game, struct MapResources* data) {
game->map.arrowpos += 0.1; data->arrowpos += 0.1;
} }
void Map_Load(struct Game *game) { void Gamestate_Start(struct Game *game, struct MapResources* data) {
al_play_sample_instance(game->map.music); al_play_sample_instance(data->music);
FadeGameState(game, true); FadeGamestate(game, true);
} }
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev) { void Gamestate_ProcessEvent(struct Game *game, struct MapResources* data, ALLEGRO_EVENT *ev) {
if ((((game->map.selected<4) || (game->map.selected==6)) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((game->map.selected>4) && (game->map.selected!=6) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((game->map.selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_UP)) || ((game->map.selected==6) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN))) { if (ev->type!=ALLEGRO_EVENT_KEY_DOWN) return;
game->map.selected--; if ((((data->selected<4) || (data->selected==6)) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((data->selected>4) && (data->selected!=6) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((data->selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_UP)) || ((data->selected==6) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN))) {
al_play_sample_instance(game->map.click); data->selected--;
} else if (((game->map.selected<3) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((game->map.selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((game->map.selected==3) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) || ((game->map.selected==5) && (ev->keyboard.keycode==ALLEGRO_KEY_UP))) { al_play_sample_instance(data->click);
game->map.selected++; } else if (((data->selected<3) && (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT)) || ((data->selected==4) && (ev->keyboard.keycode==ALLEGRO_KEY_LEFT)) || ((data->selected==3) && (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) || ((data->selected==5) && (ev->keyboard.keycode==ALLEGRO_KEY_UP))) {
al_play_sample_instance(game->map.click); data->selected++;
al_play_sample_instance(data->click);
} else if ((ev->keyboard.keycode==ALLEGRO_KEY_LEFT) || (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT) || (ev->keyboard.keycode==ALLEGRO_KEY_UP) || (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) { } else if ((ev->keyboard.keycode==ALLEGRO_KEY_LEFT) || (ev->keyboard.keycode==ALLEGRO_KEY_RIGHT) || (ev->keyboard.keycode==ALLEGRO_KEY_UP) || (ev->keyboard.keycode==ALLEGRO_KEY_DOWN)) {
al_play_sample_instance(game->map.click); al_play_sample_instance(data->click);
} else if (ev->keyboard.keycode==ALLEGRO_KEY_ENTER) { } else if (ev->keyboard.keycode==ALLEGRO_KEY_ENTER) {
al_play_sample_instance(game->map.click); al_play_sample_instance(data->click);
game->level.input.current_level = game->map.selected; //game->level.input.current_level = data->selected;
PrintConsole(game, "Selecting level %d...", game->map.selected); PrintConsole(game, "Selecting level %d...", data->selected);
UnloadGameState(game); char gamestate[7] = {};
game->gamestate = GAMESTATE_LOADING; sprintf(gamestate, "level%d", data->selected);
game->loadstate = GAMESTATE_LEVEL; SwitchGamestate(game, "map", gamestate);
return 0; return;
} else if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) { } else if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
UnloadGameState(game); SwitchGamestate(game, "map", "menu");
game->loadstate = GAMESTATE_MENU; return;
LoadGameState(game); }
return 0; if (data->selected<1) data->selected=1;
} else { return 0; } if (data->selected>data->available) data->selected=data->available;
if (game->map.selected<1) game->map.selected=1;
if (game->map.selected>game->map.available) game->map.selected=game->map.available;
return 0;
} }
void Map_Preload(struct Game *game, void (*progress)(struct Game*, float)) { void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
PROGRESS_INIT(7);
game->map.available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1")); struct MapResources *data = malloc(sizeof(struct MapResources));
if ((game->map.available<1) || (game->map.available>6)) game->map.available=1; data->available = atoi(GetConfigOptionDefault(game, "MuffinAttack", "level", "1"));
game->map.selected = game->map.available; if ((data->available<1) || (data->available>6)) data->available=1;
PrintConsole(game, "Last level available: %d", game->map.selected); data->selected = data->available;
game->map.arrowpos = 0; PrintConsole(game, "Last level available: %d", data->selected);
data->arrowpos = 0;
game->map.map_bg = LoadScaledBitmap("map/background.png", game->viewportHeight*1.6, game->viewportHeight); data->map_bg = LoadScaledBitmap(game, "map/background.png", game->viewport.height*1.6, game->viewport.height);
PROGRESS;
char filename[30] = { }; char filename[30] = { };
sprintf(filename, "map/highlight%d.png", game->map.available); sprintf(filename, "map/highlight%d.png", data->available);
game->map.highlight = LoadScaledBitmap(filename, game->viewportHeight*1.6, game->viewportHeight); data->highlight = LoadScaledBitmap(game, filename, game->viewport.height*1.6, game->viewport.height);
PROGRESS;
game->map.arrow = al_load_bitmap( GetDataFilePath("map/arrow.png") ); data->arrow = al_load_bitmap( GetDataFilePath("map/arrow.png") );
PROGRESS;
game->map.click_sample = al_load_sample( GetDataFilePath("menu/click.flac") ); data->click_sample = al_load_sample( GetDataFilePath("menu/click.flac") );
PROGRESS; data->sample = al_load_sample( GetDataFilePath("map/map.flac") );
game->map.sample = al_load_sample( GetDataFilePath("map/map.flac") );
PROGRESS;
game->map.music = al_create_sample_instance(game->map.sample); data->music = al_create_sample_instance(data->sample);
al_attach_sample_instance_to_mixer(game->map.music, game->audio.music); al_attach_sample_instance_to_mixer(data->music, game->audio.music);
al_set_sample_instance_playmode(game->map.music, ALLEGRO_PLAYMODE_LOOP); al_set_sample_instance_playmode(data->music, ALLEGRO_PLAYMODE_LOOP);
game->map.click = al_create_sample_instance(game->map.click_sample); data->click = al_create_sample_instance(data->click_sample);
al_attach_sample_instance_to_mixer(game->map.click, game->audio.fx); al_attach_sample_instance_to_mixer(data->click, game->audio.fx);
al_set_sample_instance_playmode(game->map.click, ALLEGRO_PLAYMODE_ONCE); al_set_sample_instance_playmode(data->click, ALLEGRO_PLAYMODE_ONCE);
if (!game->map.sample){ if (!data->sample){
fprintf(stderr, "Audio clip sample not loaded!\n" ); fprintf(stderr, "Audio clip sample not loaded!\n" );
exit(-1); exit(-1);
} }
if (!game->map.click_sample){ if (!data->click_sample){
fprintf(stderr, "Audio clip sample#2 not loaded!\n" ); fprintf(stderr, "Audio clip sample#2 not loaded!\n" );
exit(-1); exit(-1);
} }
game->map.map = LoadScaledBitmap("table.png", game->viewportWidth, game->viewportHeight); data->map = LoadScaledBitmap(game, "table.png", game->viewport.width, game->viewport.height);
PROGRESS; al_set_target_bitmap(data->map);
al_set_target_bitmap(game->map.map); al_draw_bitmap(data->map_bg, (game->viewport.width-game->viewport.height*1.6)/2, 0 ,0);
al_draw_bitmap(game->map.map_bg, (game->viewportWidth-game->viewportHeight*1.6)/2, 0 ,0); al_draw_bitmap(data->highlight, (game->viewport.width-game->viewport.height*1.6)/2, 0 ,0);
al_draw_bitmap(game->map.highlight, (game->viewportWidth-game->viewportHeight*1.6)/2, 0 ,0);
al_set_target_bitmap(al_get_backbuffer(game->display)); al_set_target_bitmap(al_get_backbuffer(game->display));
PROGRESS;
return data;
} }
void Map_Unload(struct Game *game) { void Gamestate_Unload(struct Game *game, struct MapResources* data) {
FadeGameState(game, false); al_destroy_bitmap(data->map);
al_destroy_bitmap(game->map.map); al_destroy_bitmap(data->map_bg);
al_destroy_bitmap(game->map.map_bg); al_destroy_bitmap(data->highlight);
al_destroy_bitmap(game->map.highlight); al_destroy_bitmap(data->arrow);
al_destroy_bitmap(game->map.arrow); al_destroy_sample_instance(data->music);
al_destroy_sample_instance(game->map.music); al_destroy_sample(data->sample);
al_destroy_sample(game->map.sample); al_destroy_sample_instance(data->click);
al_destroy_sample_instance(game->map.click); al_destroy_sample(data->click_sample);
al_destroy_sample(game->map.click_sample);
} }
void Gamestate_Stop(struct Game *game, struct MapResources* data) {
FadeGamestate(game, false);
al_stop_sample_instance(data->music);
}
void Gamestate_Pause(struct Game *game, struct MapResources* data) {}
void Gamestate_Resume(struct Game *game, struct MapResources* data) {}
void Gamestate_Reload(struct Game *game, struct MapResources* data) {}

View file

@ -18,11 +18,22 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include "../main.h"
void Map_Draw(struct Game *game); #include <allegro5/allegro.h>
void Map_Logic(struct Game *game); #include <allegro5/allegro_font.h>
void Map_Preload(struct Game *game, void (*progress)(struct Game*, float)); #include <allegro5/allegro_audio.h>
void Map_Unload(struct Game *game);
void Map_Load(struct Game *game); /*! \brief Resources used by Map state. */
int Map_Keydown(struct Game *game, ALLEGRO_EVENT *ev); struct MapResources {
ALLEGRO_BITMAP *map; /*!< Background table bitmap. */
ALLEGRO_BITMAP *map_bg; /*!< Map bitmap. */
ALLEGRO_BITMAP *highlight; /*!< Level highlights bitmap. */
ALLEGRO_BITMAP *arrow; /*!< Arrow bitmap. */
int selected; /*!< Number of currently selected level. */
int available; /*!< Number of highest available level. */
float arrowpos; /*!< Vertical position of the arrow. */
ALLEGRO_SAMPLE *sample; /*!< Sample with backgrond music. */
ALLEGRO_SAMPLE *click_sample; /*!< Sample with click sound. */
ALLEGRO_SAMPLE_INSTANCE *music; /*!< Sample instance with background music. */
ALLEGRO_SAMPLE_INSTANCE *click; /*!< Sample instance with click sound. */
};