diff --git a/TrustFall/TrustFall/cursor.cpp b/TrustFall/TrustFall/cursor.cpp index b43c230..0228e79 100644 --- a/TrustFall/TrustFall/cursor.cpp +++ b/TrustFall/TrustFall/cursor.cpp @@ -9,102 +9,102 @@ using std::string; using std::vector; -template -Cursor::Cursor() -{ - active = false; -} - -//Give items to the cursor, and set the selected item to be the first -template -void Cursor::activate(std::vector _items) -{ - items = _items; - selected = 0; - active = true; -} - -//Functionality that was not used for this game, may be used in future projects. -template -void Cursor::deactivate() -{ - active = false; -} - -//Draw the menu options on the screen based on the x and y coordinates for the menu to start at -template -void Cursor::draw(float _x_start, float _y_start, float _y_offset, ALLEGRO_FONT* _font) { - vector items_text = get_item_strings(); - - x_start = _x_start; - y_start = _y_start; - y_offset = _y_offset; - font = _font; - - for (unsigned int i = 0; i < items_text.size(); i++) { - al_draw_text(font, al_map_rgb(255, 255, 255), x_start, y_start + (y_offset*i), ALLEGRO_ALIGN_LEFT, items_text.at(i).c_str()); - } - - update_selector(); -} - -//Change the selection of the cursor -template -void Cursor::down() -{ - if (selected < items.size() - 1) { - selected++; - redraw(); - } -} - -template -void Cursor::up() -{ - if (selected > 0) { - selected--; - redraw(); - } -} - -template -T Cursor::get_selected() { - return items.at(selected); -} - -//Clear screen before drawing menu -template -void Cursor::redraw() -{ - al_clear_to_color(al_map_rgb(0, 0, 0)); - draw(x_start, y_start, y_offset, font); -} - -//Draw selector to highlight proper selected item -template -void Cursor::update_selector() { - ALLEGRO_COLOR white = al_map_rgb(255, 255, 255); - float offset = y_offset * selected; - float x1 = x_start - 5; - float y1 = y_start + offset; - float x2 = x_start - 5; - float y2 = y_start + 5 + offset; - float x3 = x_start - 1; - float y3 = y_start + 2.5 + offset; - - al_draw_filled_triangle(x1, y1, x2, y2, x3, y3, white); -} - -//Define how the template will function when given various types. Only string was needed for this project. -template Cursor::Cursor(); -template void Cursor::activate(std::vector _options); -template void Cursor::deactivate(); -template void Cursor::draw(float x_start, float y_start, float _y_offset, ALLEGRO_FONT* font); -template void Cursor::redraw(); -template void Cursor::up(); -template void Cursor::down(); -template string Cursor::get_selected(); -template void Cursor::update_selector(); -vector Cursor::get_item_strings() { - return items; -} \ No newline at end of file +template +Cursor::Cursor() +{ + active = false; +} + +//Give items to the cursor, and set the selected item to be the first +template +void Cursor::activate(std::vector _items) +{ + items = _items; + selected = 0; + active = true; +} + +//Functionality that was not used for this game, may be used in future projects. +template +void Cursor::deactivate() +{ + active = false; +} + +//Draw the menu options on the screen based on the x and y coordinates for the menu to start at +template +void Cursor::draw(float _x_start, float _y_start, float _y_offset, ALLEGRO_FONT* _font) { + vector items_text = get_item_strings(); + + x_start = _x_start; + y_start = _y_start; + y_offset = _y_offset; + font = _font; + + for (unsigned int i = 0; i < items_text.size(); i++) { + al_draw_text(font, al_map_rgb(255, 255, 255), x_start, y_start + (y_offset*i), ALLEGRO_ALIGN_LEFT, items_text.at(i).c_str()); + } + + update_selector(); +} + +//Change the selection of the cursor +template +void Cursor::down() +{ + if (selected < items.size() - 1) { + selected++; + redraw(); + } +} + +template +void Cursor::up() +{ + if (selected > 0) { + selected--; + redraw(); + } +} + +template +T Cursor::get_selected() { + return items.at(selected); +} + +//Clear screen before drawing menu +template +void Cursor::redraw() +{ + al_clear_to_color(al_map_rgb(0, 0, 0)); + draw(x_start, y_start, y_offset, font); +} + +//Draw selector to highlight proper selected item +template +void Cursor::update_selector() { + ALLEGRO_COLOR white = al_map_rgb(255, 255, 255); + float offset = y_offset * selected; + float x1 = x_start - 5; + float y1 = y_start + offset; + float x2 = x_start - 5; + float y2 = y_start + 5 + offset; + float x3 = x_start - 1; + float y3 = y_start + 2.5 + offset; + + al_draw_filled_triangle(x1, y1, x2, y2, x3, y3, white); +} + +//Define how the template will function when given various types. Only string was needed for this project. +template Cursor::Cursor(); +template void Cursor::activate(std::vector _options); +template void Cursor::deactivate(); +template void Cursor::draw(float x_start, float y_start, float _y_offset, ALLEGRO_FONT* font); +template void Cursor::redraw(); +template void Cursor::up(); +template void Cursor::down(); +template string Cursor::get_selected(); +template void Cursor::update_selector(); +vector Cursor::get_item_strings() { + return items; +} diff --git a/TrustFall/TrustFall/game.cpp b/TrustFall/TrustFall/game.cpp index 743e5cc..be19033 100644 --- a/TrustFall/TrustFall/game.cpp +++ b/TrustFall/TrustFall/game.cpp @@ -11,41 +11,41 @@ Game::Game() { } //Load all global data for the game: sprites, audio, and font. -void Game::init() { +void Game::init() { score = 0; - state = Start; - sprites.insert(pair("Title", al_load_bitmap("logo.bmp"))); - sprites.insert(pair("Mr. Man", al_load_bitmap("MrMan.bmp"))); - sprites.insert(pair("Mr. Manager", al_load_bitmap("BigMrManager.bmp"))); - sprites.insert(pair("Mr. ManagerSad", al_load_bitmap("mrmanagersad.bmp"))); - sprites.insert(pair("Mr. ManagerHappy", al_load_bitmap("mrmanagerhappy.bmp"))); - sprites.insert(pair("KeyUp", al_load_bitmap("small_key_up.bmp"))); - sprites.insert(pair("Spacebar", al_load_bitmap("spacebar.bmp"))); - sprites.insert(pair("Employee", al_load_bitmap("employee.bmp"))); - sprites.insert(pair("EmployeeHappy", al_load_bitmap("employee_happy.bmp"))); - sprites.insert(pair("EmployeeSad", al_load_bitmap("employee_sad.bmp"))); - sprites.insert(pair("Conveyor", al_load_bitmap("conveyor.bmp"))); - sprites.insert(pair("KeyCtrl", al_load_bitmap("small_key_ctrl.bmp"))); - sprites.insert(pair("KeyH", al_load_bitmap("small_key_h.bmp"))); - sprites.insert(pair("KeyM", al_load_bitmap("small_key_m.bmp"))); - sprites.insert(pair("KeyEsc", al_load_bitmap("small_key_esc.bmp"))); - sprites.insert(pair("MusicOn", al_load_bitmap("music_on.bmp"))); - sprites.insert(pair("MusicOff", al_load_bitmap("music_off.bmp"))); - - al_reserve_samples(4); - samples.insert(pair("Theme", al_load_sample("rasputin.wav"))); - samples.insert(pair("Move", al_load_sample("move.wav"))); - samples.insert(pair("Catch", al_load_sample("catch.wav"))); - samples.insert(pair("Fall", al_load_sample("fall.wav"))); - samples.insert(pair("Place", al_load_sample("place.wav"))); - samples.insert(pair("LevelUp", al_load_sample("level.wav"))); - - font = al_create_builtin_font(); -} - -//Was not used in this project -void Game::reset() { -} + state = Start; + sprites.insert(pair("Title", al_load_bitmap("logo.bmp"))); + sprites.insert(pair("Mr. Man", al_load_bitmap("MrMan.bmp"))); + sprites.insert(pair("Mr. Manager", al_load_bitmap("BigMrManager.bmp"))); + sprites.insert(pair("Mr. ManagerSad", al_load_bitmap("mrmanagersad.bmp"))); + sprites.insert(pair("Mr. ManagerHappy", al_load_bitmap("mrmanagerhappy.bmp"))); + sprites.insert(pair("KeyUp", al_load_bitmap("small_key_up.bmp"))); + sprites.insert(pair("Spacebar", al_load_bitmap("spacebar.bmp"))); + sprites.insert(pair("Employee", al_load_bitmap("employee.bmp"))); + sprites.insert(pair("EmployeeHappy", al_load_bitmap("employee_happy.bmp"))); + sprites.insert(pair("EmployeeSad", al_load_bitmap("employee_sad.bmp"))); + sprites.insert(pair("Conveyor", al_load_bitmap("conveyor.bmp"))); + sprites.insert(pair("KeyCtrl", al_load_bitmap("small_key_ctrl.bmp"))); + sprites.insert(pair("KeyH", al_load_bitmap("small_key_h.bmp"))); + sprites.insert(pair("KeyM", al_load_bitmap("small_key_m.bmp"))); + sprites.insert(pair("KeyEsc", al_load_bitmap("small_key_esc.bmp"))); + sprites.insert(pair("MusicOn", al_load_bitmap("music_on.bmp"))); + sprites.insert(pair("MusicOff", al_load_bitmap("music_off.bmp"))); + + al_reserve_samples(4); + samples.insert(pair("Theme", al_load_sample("rasputin.wav"))); + samples.insert(pair("Move", al_load_sample("move.wav"))); + samples.insert(pair("Catch", al_load_sample("catch.wav"))); + samples.insert(pair("Fall", al_load_sample("fall.wav"))); + samples.insert(pair("Place", al_load_sample("place.wav"))); + samples.insert(pair("LevelUp", al_load_sample("level.wav"))); + + font = al_create_builtin_font(); +} + +//Was not used in this project +void Game::reset() { +} //Run the game state machine void Game::run() { @@ -112,4 +112,4 @@ void Game::run() { for (it2 = samples.begin(); it2 != samples.end(); it2++) { al_destroy_sample(it2->second); } -} \ No newline at end of file +} diff --git a/TrustFall/TrustFall/game.h b/TrustFall/TrustFall/game.h index 1354ce1..5f9d629 100644 --- a/TrustFall/TrustFall/game.h +++ b/TrustFall/TrustFall/game.h @@ -1,9 +1,9 @@ -#pragma once -#include -#include -#include -#include -#include +#pragma once +#include +#include +#include +#include +#include #include #include @@ -33,4 +33,4 @@ public: private: void reset(); -}; \ No newline at end of file +}; diff --git a/TrustFall/TrustFall/game_screen.cpp b/TrustFall/TrustFall/game_screen.cpp index 3e39b6e..3084354 100644 --- a/TrustFall/TrustFall/game_screen.cpp +++ b/TrustFall/TrustFall/game_screen.cpp @@ -1,275 +1,275 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "game_screen.h" - -#define FPS 60 - -using std::vector; -using std::string; -using std::ostringstream; -using std::map; -using std::pair; - -GameScreen::GameScreen(std::map _sprites, std::map _samples) { - sprites = _sprites; - samples = _samples; - srand(time(NULL)); //Seed random number generator with current time so sequence is unique each run -} - -//Resets game to default values, and uses new given values -void GameScreen::reset(int _lines, int _max_catchers, int _difficulty) { - lines.clear(); - for (int i = 0; i < _lines; i++) { - Line new_line(SCREEN_W / 2, SCREEN_H / _lines * i, sprites); - lines.push_back(new_line); - } - - selected = 0; - max_catchers = _max_catchers; - catchers = 0; - score = 0; - difficulty = _difficulty; - music = true; -} - -//If a sample needs to be played while it is still being played, it will be stopped first. -//Admittedly this was a hack solution to the fact that my sound effect samples are too long. -//In future projects I will use a better program for sound effect creation that does not have a large minimum length for audio export. -void play(ALLEGRO_SAMPLE_INSTANCE* x) { - if (al_get_sample_instance_playing(x)) { - al_stop_sample_instance(x); - } - al_play_sample_instance(x); -} - -void GameScreen::run(ALLEGRO_FONT* font) { - //Map that represents difficulty scale - map difficulty_map; - difficulty_map.insert(pair(1, 15)); - difficulty_map.insert(pair(2, 12)); - difficulty_map.insert(pair(3, 10)); - difficulty_map.insert(pair(4, 8)); - difficulty_map.insert(pair(5, 5)); - difficulty_map.insert(pair(6, 3)); - difficulty_map.insert(pair(7, 2)); - - ALLEGRO_EVENT_QUEUE* event_queue = NULL; - event_queue = al_create_event_queue(); - al_register_event_source(event_queue, al_get_keyboard_event_source()); - - ALLEGRO_TIMER* timer = NULL; - timer = al_create_timer(0.1); //Universally a "tick" in the context of the game is 1/10 of a second. - al_register_event_source(event_queue, al_get_timer_event_source(timer)); - - redraw(font); - al_flip_display(); - - //Create map of sample instances. This is more memory efficient than play samples over and over again individually, - //and allows the anti-overlap functionality implemented above. - map instances; - map::iterator it; - for (it = samples.begin(); it != samples.end(); it++) { - instances.insert(pair(it->first, al_create_sample_instance(it->second))); - al_attach_sample_instance_to_mixer(instances[it->first], al_get_default_mixer()); - } - - al_set_sample_instance_playmode(instances["Theme"], ALLEGRO_PLAYMODE_LOOP); - play(instances["Theme"]); - - int ticks = 0; //Counts the number of ticks, this is used to determine when to move the lines - bool exit_screen = false; - bool ctrl = false; //whether ctrl key is currently held - bool help = false; //whether help screen is currently displayed - - al_start_timer(timer); - while (!exit_screen) { - ALLEGRO_EVENT ev; - al_wait_for_event(event_queue, &ev); - if (ev.type == ALLEGRO_EVENT_TIMER) { - if (ticks == difficulty_map[difficulty]) { //If at the current difficulty level, the lines are due to move - int add_to = rand() % (lines.size() + 1); //Randomly decide which line to add to, with a 1/lines.size() chance of not adding - for (unsigned int i = 0; i < lines.size(); i++) { - if (i == add_to) { - lines.at(i).add_employee(); - } - lines.at(i).move(); //Every line will move regardless of any factors - if (lines.at(i).fall) { //If an employee has fallen, the game is over - map::iterator in_it; - for (in_it = instances.begin(); in_it != instances.end(); in_it++) { - al_stop_sample_instance(in_it->second); - } - al_play_sample_instance(instances["Fall"]); - exit_screen = true; - al_clear_to_color(al_map_rgb(0, 0, 0)); - redraw(font); - al_flip_display(); - al_rest(2.0); - } - else if (lines.at(i).caught) { //If an employee is caught, add to the score and then check if difficulty should be adjusted - play(instances["Catch"]); - catchers--; - score++; - if ((score == 10 || score == 20 || score == 30 || score == 40 || score == 50 || score == 75 || score == 100) && difficulty < 7) { - play(instances["LevelUp"]); - difficulty++; - } - } - } - //Refresh screen - al_clear_to_color(al_map_rgb(0, 0, 0)); - redraw(font); - al_flip_display(); - ticks = 0; //reset ticks - } - else { - //if not time to move, add to ticks - ticks++; - } - } - if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { - switch (ev.keyboard.keycode) { - case ALLEGRO_KEY_DOWN: - play(instances["Move"]); - if (selected < lines.size() - 1) selected++; - break; - case ALLEGRO_KEY_UP: - play(instances["Move"]); - if (selected > 0) selected--; - break; - case ALLEGRO_KEY_SPACE: - if (catchers < max_catchers) { - if (!lines.at(selected).catchers[2]) { - play(instances["Place"]); - lines.at(selected).add_catcher(); - catchers++; - } - } - break; - case ALLEGRO_KEY_M: - //Toggle music - if (ctrl) { - if (music) { - al_stop_sample_instance(instances["Theme"]); - } - else { - al_play_sample_instance(instances["Theme"]); - } - music = !music; - } - break; - case ALLEGRO_KEY_H: - if (ctrl) { - help = true; //help screen is up - - //Draw the help screen - al_clear_to_color(al_map_rgb(0, 0, 0)); +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "game_screen.h" + +#define FPS 60 + +using std::vector; +using std::string; +using std::ostringstream; +using std::map; +using std::pair; + +GameScreen::GameScreen(std::map _sprites, std::map _samples) { + sprites = _sprites; + samples = _samples; + srand(time(NULL)); //Seed random number generator with current time so sequence is unique each run +} + +//Resets game to default values, and uses new given values +void GameScreen::reset(int _lines, int _max_catchers, int _difficulty) { + lines.clear(); + for (int i = 0; i < _lines; i++) { + Line new_line(SCREEN_W / 2, SCREEN_H / _lines * i, sprites); + lines.push_back(new_line); + } + + selected = 0; + max_catchers = _max_catchers; + catchers = 0; + score = 0; + difficulty = _difficulty; + music = true; +} + +//If a sample needs to be played while it is still being played, it will be stopped first. +//Admittedly this was a hack solution to the fact that my sound effect samples are too long. +//In future projects I will use a better program for sound effect creation that does not have a large minimum length for audio export. +void play(ALLEGRO_SAMPLE_INSTANCE* x) { + if (al_get_sample_instance_playing(x)) { + al_stop_sample_instance(x); + } + al_play_sample_instance(x); +} + +void GameScreen::run(ALLEGRO_FONT* font) { + //Map that represents difficulty scale + map difficulty_map; + difficulty_map.insert(pair(1, 15)); + difficulty_map.insert(pair(2, 12)); + difficulty_map.insert(pair(3, 10)); + difficulty_map.insert(pair(4, 8)); + difficulty_map.insert(pair(5, 5)); + difficulty_map.insert(pair(6, 3)); + difficulty_map.insert(pair(7, 2)); + + ALLEGRO_EVENT_QUEUE* event_queue = NULL; + event_queue = al_create_event_queue(); + al_register_event_source(event_queue, al_get_keyboard_event_source()); + + ALLEGRO_TIMER* timer = NULL; + timer = al_create_timer(0.1); //Universally a "tick" in the context of the game is 1/10 of a second. + al_register_event_source(event_queue, al_get_timer_event_source(timer)); + + redraw(font); + al_flip_display(); + + //Create map of sample instances. This is more memory efficient than play samples over and over again individually, + //and allows the anti-overlap functionality implemented above. + map instances; + map::iterator it; + for (it = samples.begin(); it != samples.end(); it++) { + instances.insert(pair(it->first, al_create_sample_instance(it->second))); + al_attach_sample_instance_to_mixer(instances[it->first], al_get_default_mixer()); + } + + al_set_sample_instance_playmode(instances["Theme"], ALLEGRO_PLAYMODE_LOOP); + play(instances["Theme"]); + + int ticks = 0; //Counts the number of ticks, this is used to determine when to move the lines + bool exit_screen = false; + bool ctrl = false; //whether ctrl key is currently held + bool help = false; //whether help screen is currently displayed + + al_start_timer(timer); + while (!exit_screen) { + ALLEGRO_EVENT ev; + al_wait_for_event(event_queue, &ev); + if (ev.type == ALLEGRO_EVENT_TIMER) { + if (ticks == difficulty_map[difficulty]) { //If at the current difficulty level, the lines are due to move + int add_to = rand() % (lines.size() + 1); //Randomly decide which line to add to, with a 1/lines.size() chance of not adding + for (unsigned int i = 0; i < lines.size(); i++) { + if (i == add_to) { + lines.at(i).add_employee(); + } + lines.at(i).move(); //Every line will move regardless of any factors + if (lines.at(i).fall) { //If an employee has fallen, the game is over + map::iterator in_it; + for (in_it = instances.begin(); in_it != instances.end(); in_it++) { + al_stop_sample_instance(in_it->second); + } + al_play_sample_instance(instances["Fall"]); + exit_screen = true; + al_clear_to_color(al_map_rgb(0, 0, 0)); + redraw(font); + al_flip_display(); + al_rest(2.0); + } + else if (lines.at(i).caught) { //If an employee is caught, add to the score and then check if difficulty should be adjusted + play(instances["Catch"]); + catchers--; + score++; + if ((score == 10 || score == 20 || score == 30 || score == 40 || score == 50 || score == 75 || score == 100) && difficulty < 7) { + play(instances["LevelUp"]); + difficulty++; + } + } + } + //Refresh screen + al_clear_to_color(al_map_rgb(0, 0, 0)); + redraw(font); + al_flip_display(); + ticks = 0; //reset ticks + } + else { + //if not time to move, add to ticks + ticks++; + } + } + if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { + switch (ev.keyboard.keycode) { + case ALLEGRO_KEY_DOWN: + play(instances["Move"]); + if (selected < lines.size() - 1) selected++; + break; + case ALLEGRO_KEY_UP: + play(instances["Move"]); + if (selected > 0) selected--; + break; + case ALLEGRO_KEY_SPACE: + if (catchers < max_catchers) { + if (!lines.at(selected).catchers[2]) { + play(instances["Place"]); + lines.at(selected).add_catcher(); + catchers++; + } + } + break; + case ALLEGRO_KEY_M: + //Toggle music + if (ctrl) { + if (music) { + al_stop_sample_instance(instances["Theme"]); + } + else { + al_play_sample_instance(instances["Theme"]); + } + music = !music; + } + break; + case ALLEGRO_KEY_H: + if (ctrl) { + help = true; //help screen is up + + //Draw the help screen + al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 20, ALLEGRO_ALIGN_CENTER, "Place catchers at the ends of the conveyors"); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 30, ALLEGRO_ALIGN_CENTER, "You can place 3 catchers per conveyor"); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 40, ALLEGRO_ALIGN_CENTER, "Make sure to keep track of how many catchers you have left"); - al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8, SCREEN_H / 2 + 60, NULL); - al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8 + 34, SCREEN_H / 2 + 60, ALLEGRO_FLIP_VERTICAL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Up and down - Move cursor"); - - al_draw_bitmap(sprites["Spacebar"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Spacebar - "); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 15, ALLEGRO_ALIGN_LEFT, "Place Employee"); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 25, ALLEGRO_ALIGN_LEFT, "Select Menu Item"); - - al_draw_bitmap(sprites["KeyEsc"], SCREEN_W / 2 + 58, SCREEN_H / 2 + 60, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Close menu"); - - al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 2 + 110, ALLEGRO_ALIGN_LEFT, "+"); - al_draw_bitmap(sprites["KeyM"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Toggle Music"); - - al_flip_display(); - - while (help) { //While the help screen is still down - ALLEGRO_EVENT unhelp; - al_wait_for_event(event_queue, &unhelp); - - if (unhelp.type == ALLEGRO_EVENT_KEY_DOWN) { - if (unhelp.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { - help = false; //Take the help screen down - } - } - } - } - break; - case ALLEGRO_KEY_ESCAPE: - //Force quit game - back(); - exit_screen = true; - break; - case ALLEGRO_KEY_LCTRL: - case ALLEGRO_KEY_RCTRL: - ctrl = true; //ctrl is held - break; - } - if (ev.type == ALLEGRO_EVENT_KEY_UP) { - if (ev.keyboard.keycode == ALLEGRO_KEY_LCTRL || ev.keyboard.keycode == ALLEGRO_KEY_RCTRL) { - ctrl = false; //ctrl is released - } - } - //Global refresh - al_clear_to_color(al_map_rgb(0, 0, 0)); - redraw(font); - al_flip_display(); - } - } - if (next_state != Exit) { //If the game loop was exited naturally rather than forced by Esc - cont(); - } - - //Garbage collection - al_destroy_event_queue(event_queue); - al_destroy_timer(timer); - map::iterator it2; - for (it2 = instances.begin(); it2 != instances.end(); it2++) { - al_destroy_sample_instance(it2->second); - } -} - -//Redraw all elements of the screen -void GameScreen::redraw(ALLEGRO_FONT* font) { - al_draw_bitmap(sprites["Mr. Manager"], 80, 0 + (SCREEN_H / lines.size() * selected), NULL); //Draw the manager based on which line is selected - - for (unsigned int i = 0; i < lines.size(); i++) { //Draw each line - lines.at(i).draw(); - } - - //Display score, number of catchers, current difficulty level, and whether the music is on or off - ostringstream score_msg; - score_msg << "Score: " << score; - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 90, 0, ALLEGRO_ALIGN_LEFT, score_msg.str().c_str()); - - al_draw_bitmap(sprites["Mr. Man"], SCREEN_W - 90, 20, NULL); - ostringstream catcher_msg; - catcher_msg << " x " << max_catchers - catchers; - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 50, 30, ALLEGRO_ALIGN_LEFT, catcher_msg.str().c_str()); - - ostringstream difficulty_msg; - difficulty_msg << "Level: " << difficulty; - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 90, 75, ALLEGRO_ALIGN_LEFT, difficulty_msg.str().c_str()); - - string music_img = (music) ? "MusicOn" : "MusicOff"; - al_draw_bitmap(sprites[music_img], SCREEN_W - 60, 90, NULL); -} - -void GameScreen::back() { - next_state = Exit; -} - -void GameScreen::cont() { - next_state = End; -} \ No newline at end of file + al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8, SCREEN_H / 2 + 60, NULL); + al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8 + 34, SCREEN_H / 2 + 60, ALLEGRO_FLIP_VERTICAL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Up and down - Move cursor"); + + al_draw_bitmap(sprites["Spacebar"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Spacebar - "); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 15, ALLEGRO_ALIGN_LEFT, "Place Employee"); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 25, ALLEGRO_ALIGN_LEFT, "Select Menu Item"); + + al_draw_bitmap(sprites["KeyEsc"], SCREEN_W / 2 + 58, SCREEN_H / 2 + 60, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Close menu"); + + al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 2 + 110, ALLEGRO_ALIGN_LEFT, "+"); + al_draw_bitmap(sprites["KeyM"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Toggle Music"); + + al_flip_display(); + + while (help) { //While the help screen is still down + ALLEGRO_EVENT unhelp; + al_wait_for_event(event_queue, &unhelp); + + if (unhelp.type == ALLEGRO_EVENT_KEY_DOWN) { + if (unhelp.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { + help = false; //Take the help screen down + } + } + } + } + break; + case ALLEGRO_KEY_ESCAPE: + //Force quit game + back(); + exit_screen = true; + break; + case ALLEGRO_KEY_LCTRL: + case ALLEGRO_KEY_RCTRL: + ctrl = true; //ctrl is held + break; + } + if (ev.type == ALLEGRO_EVENT_KEY_UP) { + if (ev.keyboard.keycode == ALLEGRO_KEY_LCTRL || ev.keyboard.keycode == ALLEGRO_KEY_RCTRL) { + ctrl = false; //ctrl is released + } + } + //Global refresh + al_clear_to_color(al_map_rgb(0, 0, 0)); + redraw(font); + al_flip_display(); + } + } + if (next_state != Exit) { //If the game loop was exited naturally rather than forced by Esc + cont(); + } + + //Garbage collection + al_destroy_event_queue(event_queue); + al_destroy_timer(timer); + map::iterator it2; + for (it2 = instances.begin(); it2 != instances.end(); it2++) { + al_destroy_sample_instance(it2->second); + } +} + +//Redraw all elements of the screen +void GameScreen::redraw(ALLEGRO_FONT* font) { + al_draw_bitmap(sprites["Mr. Manager"], 80, 0 + (SCREEN_H / lines.size() * selected), NULL); //Draw the manager based on which line is selected + + for (unsigned int i = 0; i < lines.size(); i++) { //Draw each line + lines.at(i).draw(); + } + + //Display score, number of catchers, current difficulty level, and whether the music is on or off + ostringstream score_msg; + score_msg << "Score: " << score; + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 90, 0, ALLEGRO_ALIGN_LEFT, score_msg.str().c_str()); + + al_draw_bitmap(sprites["Mr. Man"], SCREEN_W - 90, 20, NULL); + ostringstream catcher_msg; + catcher_msg << " x " << max_catchers - catchers; + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 50, 30, ALLEGRO_ALIGN_LEFT, catcher_msg.str().c_str()); + + ostringstream difficulty_msg; + difficulty_msg << "Level: " << difficulty; + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W - 90, 75, ALLEGRO_ALIGN_LEFT, difficulty_msg.str().c_str()); + + string music_img = (music) ? "MusicOn" : "MusicOff"; + al_draw_bitmap(sprites[music_img], SCREEN_W - 60, 90, NULL); +} + +void GameScreen::back() { + next_state = Exit; +} + +void GameScreen::cont() { + next_state = End; +} diff --git a/TrustFall/TrustFall/start_screen.cpp b/TrustFall/TrustFall/start_screen.cpp index ac1b5cf..8daf9a8 100644 --- a/TrustFall/TrustFall/start_screen.cpp +++ b/TrustFall/TrustFall/start_screen.cpp @@ -1,136 +1,136 @@ -#include "start_screen.h" - -#include -#include - -using std::vector; -using std::string; - -StartScreen::StartScreen(std::map _sprites) { - vector menu_options; - menu_options.push_back("Easy"); //Select Difficulty - menu_options.push_back("Medium"); - menu_options.push_back("Hard"); - menu_options.push_back("Quit"); //Quit game - menu.activate(menu_options); - - sprites = _sprites; -} - -//Run screen -void StartScreen::run(ALLEGRO_FONT * font) { - ALLEGRO_EVENT_QUEUE* event_queue = NULL; - event_queue = al_create_event_queue(); - al_register_event_source(event_queue, al_get_keyboard_event_source()); - - redraw(font); - menu.draw(300.0, 400.0, 20.0, font); - al_flip_display(); - - bool ctrl = false; - bool exit_screen = false; - while (!exit_screen) { - ALLEGRO_EVENT ev; - al_wait_for_event(event_queue, &ev); - - if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { - switch (ev.keyboard.keycode) { - case ALLEGRO_KEY_UP: - //Move cursor up - menu.up(); - break; - case ALLEGRO_KEY_DOWN: - //Move cursor down - menu.down(); - break; - case ALLEGRO_KEY_SPACE: - if (ctrl) { //If the ctrl key is held regardless of option, they will enter EXTREME easter egg mode - next_state = EXTREME; - } - else { - cont(); //Otherwise select the menu item - } - - exit_screen = true; - break; - case ALLEGRO_KEY_ESCAPE: - //Force quit game - back(); - exit_screen = true; - break; - case ALLEGRO_KEY_LCTRL: - case ALLEGRO_KEY_RCTRL: - ctrl = true; //ctrl is held down - break; - } - redraw(font); - al_flip_display(); - } - if (ev.type == ALLEGRO_EVENT_KEY_UP) { - if (ev.keyboard.keycode == ALLEGRO_KEY_LCTRL || ev.keyboard.keycode == ALLEGRO_KEY_RCTRL) { - ctrl = false; //ctrl is released - } - } - } - - //Garbage collection - al_destroy_event_queue(event_queue); -} - -void StartScreen::redraw(ALLEGRO_FONT* font) { - al_draw_bitmap(sprites["Title"], SCREEN_W/2 - 240, 20, NULL); //logo - al_draw_bitmap(sprites["Mr. Manager"], SCREEN_W / 2 - 40, 120, NULL); //manager - for (int i = 1; i < 7; i++) { - al_draw_bitmap(sprites["Mr. Man"], SCREEN_W / 8 * i + 14, SCREEN_H / 2 - 30, NULL); //army of catchers - } - - //Instructions +#include "start_screen.h" + +#include +#include + +using std::vector; +using std::string; + +StartScreen::StartScreen(std::map _sprites) { + vector menu_options; + menu_options.push_back("Easy"); //Select Difficulty + menu_options.push_back("Medium"); + menu_options.push_back("Hard"); + menu_options.push_back("Quit"); //Quit game + menu.activate(menu_options); + + sprites = _sprites; +} + +//Run screen +void StartScreen::run(ALLEGRO_FONT * font) { + ALLEGRO_EVENT_QUEUE* event_queue = NULL; + event_queue = al_create_event_queue(); + al_register_event_source(event_queue, al_get_keyboard_event_source()); + + redraw(font); + menu.draw(300.0, 400.0, 20.0, font); + al_flip_display(); + + bool ctrl = false; + bool exit_screen = false; + while (!exit_screen) { + ALLEGRO_EVENT ev; + al_wait_for_event(event_queue, &ev); + + if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { + switch (ev.keyboard.keycode) { + case ALLEGRO_KEY_UP: + //Move cursor up + menu.up(); + break; + case ALLEGRO_KEY_DOWN: + //Move cursor down + menu.down(); + break; + case ALLEGRO_KEY_SPACE: + if (ctrl) { //If the ctrl key is held regardless of option, they will enter EXTREME easter egg mode + next_state = EXTREME; + } + else { + cont(); //Otherwise select the menu item + } + + exit_screen = true; + break; + case ALLEGRO_KEY_ESCAPE: + //Force quit game + back(); + exit_screen = true; + break; + case ALLEGRO_KEY_LCTRL: + case ALLEGRO_KEY_RCTRL: + ctrl = true; //ctrl is held down + break; + } + redraw(font); + al_flip_display(); + } + if (ev.type == ALLEGRO_EVENT_KEY_UP) { + if (ev.keyboard.keycode == ALLEGRO_KEY_LCTRL || ev.keyboard.keycode == ALLEGRO_KEY_RCTRL) { + ctrl = false; //ctrl is released + } + } + } + + //Garbage collection + al_destroy_event_queue(event_queue); +} + +void StartScreen::redraw(ALLEGRO_FONT* font) { + al_draw_bitmap(sprites["Title"], SCREEN_W/2 - 240, 20, NULL); //logo + al_draw_bitmap(sprites["Mr. Manager"], SCREEN_W / 2 - 40, 120, NULL); //manager + for (int i = 1; i < 7; i++) { + al_draw_bitmap(sprites["Mr. Man"], SCREEN_W / 8 * i + 14, SCREEN_H / 2 - 30, NULL); //army of catchers + } + + //Instructions al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 10, ALLEGRO_ALIGN_CENTER, "Mr. Manager wants to run a teambuilding exercise for his employees."); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 20, ALLEGRO_ALIGN_CENTER, "Help Mr.Manager place his 5 catchers at the end of his"); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 30, ALLEGRO_ALIGN_CENTER, "conveyor belts to make sure no other employees fall off."); al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H / 2 + 40, ALLEGRO_ALIGN_CENTER, "Try to catch enough to help the team bond!"); //Keys and their associated functions - al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8, SCREEN_H / 2 + 60, NULL); - al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8 + 34, SCREEN_H / 2 + 60, ALLEGRO_FLIP_VERTICAL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Up and down - Move cursor"); - - al_draw_bitmap(sprites["Spacebar"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Spacebar - "); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 15, ALLEGRO_ALIGN_LEFT, "Place Employee"); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 25, ALLEGRO_ALIGN_LEFT, "Select Menu Item"); - - al_draw_bitmap(sprites["KeyEsc"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 140, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 2 + 150, ALLEGRO_ALIGN_LEFT, "Exit Game"); - - al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 60, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "+"); - al_draw_bitmap(sprites["KeyH"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 60, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Help menu"); - - al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 2 + 110, ALLEGRO_ALIGN_LEFT, "+"); - al_draw_bitmap(sprites["KeyM"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 100, NULL); - al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Toggle Music"); - - al_draw_text(font, al_map_rgb(255, 255, 255), 0, SCREEN_H - 10, ALLEGRO_ALIGN_LEFT, "Copyright 2019 Braydon Kains"); -} - -void StartScreen::back() { - next_state = Exit; -} - -void StartScreen::cont() { - if (menu.get_selected() == "Easy") { - next_state = Easy; - } - else if (menu.get_selected() == "Medium") { - next_state = Medium; - } - else if (menu.get_selected() == "Hard") { - next_state = Hard; - } - else { - next_state = Exit; - } -} \ No newline at end of file + al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8, SCREEN_H / 2 + 60, NULL); + al_draw_bitmap(sprites["KeyUp"], SCREEN_W / 8 + 34, SCREEN_H / 2 + 60, ALLEGRO_FLIP_VERTICAL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Up and down - Move cursor"); + + al_draw_bitmap(sprites["Spacebar"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Spacebar - "); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 15, ALLEGRO_ALIGN_LEFT, "Place Employee"); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 150, SCREEN_H / 1.3 - 25, ALLEGRO_ALIGN_LEFT, "Select Menu Item"); + + al_draw_bitmap(sprites["KeyEsc"], SCREEN_W / 8 + 20, SCREEN_H / 2 + 140, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 8 + 68, SCREEN_H / 2 + 150, ALLEGRO_ALIGN_LEFT, "Exit Game"); + + al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 60, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "+"); + al_draw_bitmap(sprites["KeyH"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 60, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.5 - 10, ALLEGRO_ALIGN_LEFT, "Help menu"); + + al_draw_bitmap(sprites["KeyCtrl"], SCREEN_W / 2 + 40, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 70, SCREEN_H / 2 + 110, ALLEGRO_ALIGN_LEFT, "+"); + al_draw_bitmap(sprites["KeyM"], SCREEN_W / 2 + 78, SCREEN_H / 2 + 100, NULL); + al_draw_text(font, al_map_rgb(255, 255, 255), SCREEN_W / 2 + 113, SCREEN_H / 1.3 - 20, ALLEGRO_ALIGN_LEFT, "Toggle Music"); + + al_draw_text(font, al_map_rgb(255, 255, 255), 0, SCREEN_H - 10, ALLEGRO_ALIGN_LEFT, "Copyright 2019 Braydon Kains"); +} + +void StartScreen::back() { + next_state = Exit; +} + +void StartScreen::cont() { + if (menu.get_selected() == "Easy") { + next_state = Easy; + } + else if (menu.get_selected() == "Medium") { + next_state = Medium; + } + else if (menu.get_selected() == "Hard") { + next_state = Hard; + } + else { + next_state = Exit; + } +}