You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.1 KiB
C++
74 lines
2.1 KiB
C++
6 years ago
|
#include "game.h"
|
||
|
|
||
|
using std::string;
|
||
|
using std::map;
|
||
|
using std::pair;
|
||
|
|
||
|
Game::Game() {
|
||
|
}
|
||
|
|
||
|
//Load all global data for the game: sprites, audio, and font.
|
||
|
void Game::init() {
|
||
|
score = 0;
|
||
|
state = Start;
|
||
6 years ago
|
sprites.insert(pair<string, ALLEGRO_BITMAP*>("Title", al_load_bitmap("spaceforce.bmp")));
|
||
6 years ago
|
sprites.insert(pair<string, ALLEGRO_BITMAP*>("Ship", al_load_bitmap("placeholder.bmp")));
|
||
6 years ago
|
sprites.insert(pair<string, ALLEGRO_BITMAP*>("Enemy", al_load_bitmap("enemy.bmp")));
|
||
6 years ago
|
sprites.insert(pair<string, ALLEGRO_BITMAP*>("MusicOn", al_load_bitmap("music_on.bmp")));
|
||
|
sprites.insert(pair<string, ALLEGRO_BITMAP*>("MusicOff", al_load_bitmap("music_off.bmp")));
|
||
6 years ago
|
|
||
6 years ago
|
al_reserve_samples(4);
|
||
|
samples.insert(pair<string, ALLEGRO_SAMPLE*>("Theme", al_load_sample("cheesetheme.wav")));
|
||
6 years ago
|
samples.insert(pair<string, ALLEGRO_SAMPLE*>("Fire", al_load_sample("fire.wav")));
|
||
|
samples.insert(pair<string, ALLEGRO_SAMPLE*>("Hit", al_load_sample("hit.wav")));
|
||
|
samples.insert(pair<string, ALLEGRO_SAMPLE*>("Die", al_load_sample("death.wav")));
|
||
|
samples.insert(pair<string, ALLEGRO_SAMPLE*>("Win", al_load_sample("victory.wav")));
|
||
6 years ago
|
|
||
6 years ago
|
font = al_create_builtin_font();
|
||
|
}
|
||
|
|
||
|
//Was not used in this project
|
||
|
void Game::reset() {
|
||
|
}
|
||
|
|
||
|
//Run the game state machine
|
||
|
void Game::run() {
|
||
|
//Load screens
|
||
6 years ago
|
StartScreen start_screen(sprites);
|
||
|
GameScreen game_screen(sprites, samples);
|
||
6 years ago
|
ResultScreen result_screen;
|
||
6 years ago
|
|
||
|
while (state != Exit) {
|
||
|
switch (state) {
|
||
|
case Start:
|
||
6 years ago
|
//start_screen.reset();
|
||
6 years ago
|
start_screen.run(font);
|
||
|
state = start_screen.next_state;
|
||
|
break;
|
||
|
case Gameplay:
|
||
|
game_screen.reset();
|
||
|
game_screen.run(font);
|
||
6 years ago
|
state = game_screen.next_state;
|
||
6 years ago
|
break;
|
||
6 years ago
|
case Win:
|
||
|
case Lose:
|
||
|
result_screen.win = (state == Win) ? true : false;
|
||
|
result_screen.score = game_screen.score;
|
||
|
result_screen.run(font);
|
||
|
state = result_screen.next_state;
|
||
|
break;
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
//Garbage collection
|
||
|
map<string, ALLEGRO_BITMAP*>::iterator it;
|
||
|
for (it = sprites.begin(); it != sprites.end(); it++) {
|
||
|
al_destroy_bitmap(it->second);
|
||
|
}
|
||
|
map<string, ALLEGRO_SAMPLE*>::iterator it2;
|
||
|
for (it2 = samples.begin(); it2 != samples.end(); it2++) {
|
||
|
al_destroy_sample(it2->second);
|
||
|
}
|
||
6 years ago
|
al_destroy_font(font);
|
||
6 years ago
|
}
|