diff --git a/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj b/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj index 070cef7..6ac3c69 100644 --- a/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj +++ b/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj @@ -91,6 +91,19 @@ true true + + true + true + true + true + true + true + true + true + true + true + true + Level3 diff --git a/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj.filters b/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj.filters index 573489a..8e6ce7c 100644 --- a/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj.filters +++ b/Assignment3Project/Assignment3Project/Assignment3Project.vcxproj.filters @@ -101,12 +101,12 @@ Source Files\ScreensImplement - - Source Files - Source Files\ScreensImplement + + Source Files\Assignment3Implement + diff --git a/Assignment3Project/Assignment3Project/bullet.cpp b/Assignment3Project/Assignment3Project/bullet.cpp index f1693de..9d29b60 100644 --- a/Assignment3Project/Assignment3Project/bullet.cpp +++ b/Assignment3Project/Assignment3Project/bullet.cpp @@ -2,6 +2,7 @@ Bullet::Bullet(Behavior _behavior) { behavior = _behavior; + //Build properties based on behavior switch (behavior) { case Enemy: speed = 1.0; @@ -38,6 +39,7 @@ void Bullet::move(Direction dir) { y_pos += 6; break; } + //Only player bullets are used currently so only player bullet bound is checked if (y_pos < 0 - height) { oob = true; } diff --git a/Assignment3Project/Assignment3Project/bullet.h b/Assignment3Project/Assignment3Project/bullet.h index c616fc8..ce7b8f5 100644 --- a/Assignment3Project/Assignment3Project/bullet.h +++ b/Assignment3Project/Assignment3Project/bullet.h @@ -3,6 +3,7 @@ #include "game_element.h" +//Bullet fired by ships class Bullet : public GameElement { public: Bullet(Behavior _behavior); diff --git a/Assignment3Project/Assignment3Project/enums.h b/Assignment3Project/Assignment3Project/enums.h index 9523b53..832a68f 100644 --- a/Assignment3Project/Assignment3Project/enums.h +++ b/Assignment3Project/Assignment3Project/enums.h @@ -7,6 +7,7 @@ #define TILE_SIZE 40 #define LEVEL_LEN 120 +//State machine states enum State { Start, Gameplay, @@ -15,6 +16,7 @@ enum State { Exit }; +//Movement directions enum Direction { U, D, @@ -26,17 +28,20 @@ enum Direction { DL }; +//Game object behaviors enum Behavior { Player, Enemy }; +//Define current background layer for scrolling enum BgLayer { Front, Middle, Back }; +//Game object hitbox for collision detection struct Hitbox { int x; int y; @@ -44,12 +49,14 @@ struct Hitbox { int width; }; +//Struct for enemy queue struct NewEnemy { int x; Behavior e_type; int when; }; +//When inputs must be delayed by a number of frames (no longer used, kept for possible future use struct InputDelay { bool input_hit; int delay_sec; diff --git a/Assignment3Project/Assignment3Project/game_element.h b/Assignment3Project/Assignment3Project/game_element.h index 636fa58..c33bbc5 100644 --- a/Assignment3Project/Assignment3Project/game_element.h +++ b/Assignment3Project/Assignment3Project/game_element.h @@ -3,6 +3,7 @@ #include "enums.h" +//Game Element base class, with position, height, and screen bounding properties along with its behavior class GameElement { public: float x_pos; @@ -16,9 +17,13 @@ public: float width; bool oob; Behavior behavior; - + + //Set position of object virtual void reset_pos(float x, float y) = 0; + //Move object virtual void move(Direction dir) = 0; + //Draw object to screen virtual void draw() = 0; + //Build hitbox struct for the object at position in current frame virtual Hitbox get_hitbox() = 0; }; \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/game_screen.cpp b/Assignment3Project/Assignment3Project/game_screen.cpp index 7008edb..4655a03 100644 --- a/Assignment3Project/Assignment3Project/game_screen.cpp +++ b/Assignment3Project/Assignment3Project/game_screen.cpp @@ -10,7 +10,7 @@ #include #include -//#include "mappy_A5.h" +//#include "mappy_A5.h" I couldn't get this to work #include "game_screen.h" #include "help_screen.h" @@ -26,10 +26,12 @@ using std::map; using std::pair; using std::rand; +//Keys that will be listener for in events enum KEYS { KEYUP, KEYDOWN, KEYLEFT, KEYRIGHT, KEYSPACE, KEYCTRL, KEYH, KEYM, KEYESC }; +//Actions to manage enum ACTIONS { FIRE, MUSIC, HELP }; @@ -63,6 +65,7 @@ void GameScreen::play(ALLEGRO_SAMPLE_INSTANCE* x) { al_play_sample_instance(x); } +//Builds enemy queue out of enemies txt file, possibly used later void GameScreen::build_enemy_queue() { string line; ifstream enemies_file("enemies.txt"); @@ -102,6 +105,7 @@ void GameScreen::run(ALLEGRO_FONT* font) { NewEnemy next_enemy = enemy_q.back(); enemy_q.pop_back(); + //Sets input delay structs InputDelay inputs[3]; inputs[FIRE].input_hit = false; inputs[FIRE].delay_sec = 0; @@ -115,6 +119,7 @@ void GameScreen::run(ALLEGRO_FONT* font) { inputs[HELP].delay_sec = 0; inputs[HELP].max_delay = 80; + //Sets tilemap size and background data int max_map = TILE_SIZE * LEVEL_LEN; map_y = max_map; objects.set_background(); @@ -122,6 +127,7 @@ void GameScreen::run(ALLEGRO_FONT* font) { redraw(font); al_flip_display(); + //key tracking array bool keys[ALLEGRO_KEY_MAX]; for (int i = 0; i < ALLEGRO_KEY_MAX; i++) keys[i] = false; diff --git a/Assignment3Project/Assignment3Project/help_screen.h b/Assignment3Project/Assignment3Project/help_screen.h index d45dcfa..35d630a 100644 --- a/Assignment3Project/Assignment3Project/help_screen.h +++ b/Assignment3Project/Assignment3Project/help_screen.h @@ -3,10 +3,9 @@ #include "cursor.h" #include -//Screen to display results upon losing +//Screen to display help menu class HelpScreen : public Screen { public: - std::map sprites; Cursor menu; HelpScreen(); diff --git a/Assignment3Project/Assignment3Project/main.cpp b/Assignment3Project/Assignment3Project/main.cpp index ec31aa5..17795b1 100644 --- a/Assignment3Project/Assignment3Project/main.cpp +++ b/Assignment3Project/Assignment3Project/main.cpp @@ -9,6 +9,7 @@ #include "game.h" int main() { + //Initialize components and addons if (!al_init()) { fprintf(stderr, "Could not initialize Allegro!"); } @@ -31,13 +32,16 @@ int main() { fprintf(stderr, "Could not initialize primitives addon!"); } + //Create display ALLEGRO_DISPLAY* display = NULL; display = al_create_display(SCREEN_W, SCREEN_H); + //Run game Game main_game; main_game.init(); main_game.run(); + //Garbage collection al_uninstall_keyboard(); al_uninstall_audio(); diff --git a/Assignment3Project/Assignment3Project/object_manager.cpp b/Assignment3Project/Assignment3Project/object_manager.cpp index 2c711f3..add1a15 100644 --- a/Assignment3Project/Assignment3Project/object_manager.cpp +++ b/Assignment3Project/Assignment3Project/object_manager.cpp @@ -120,9 +120,11 @@ void ObjectManager::move_enemies() { } } +//Initialize background stars void ObjectManager::set_background() { for (int i = 0; i < 20; i++) { - Star new_star; + //Pick random layer and position + Star new_star; int l = rand() % 2; BgLayer layer; switch (l) { @@ -171,10 +173,13 @@ void ObjectManager::move_background() { } } +//Check if the hitboxes are overlapping bool ObjectManager::col_eval(Hitbox h1, Hitbox h2) { + //Check if one hitbox is within the other's width if (h1.x > (h2.x + h2.width) || h2.x > (h1.x + h1.width)) return false; + //Check if one hitbox is within the other's height if (h1.y > (h2.y + h2.height) || h2.y > (h1.y + h1.height)) return false; diff --git a/Assignment3Project/Assignment3Project/object_manager.h b/Assignment3Project/Assignment3Project/object_manager.h index 3b924a9..ce84ffa 100644 --- a/Assignment3Project/Assignment3Project/object_manager.h +++ b/Assignment3Project/Assignment3Project/object_manager.h @@ -6,25 +6,32 @@ #include "ship.h" #include "star.h" +//Manages all onscreen object movements, drawing, and hitbox detection class ObjectManager { public: Ship player; std::vector enemies; std::vector player_bullets; - std::vector enemy_bullets; + std::vector enemy_bullets; //saved for future use std::vector background; ObjectManager(); void init_player(Ship _player); + //Collision detection methods bool chk_player_col(); int chk_bullet_col(); + //Draw objects to screen void draw_objects(); + //Originally for garbage collection, once moved away fromp pointers it became kind of useless void destroy_objects(); + //Moves all enemies on screen and checks bounds void move_enemies(); + //Set and move the background, originally supposed to be tilemaps now Star objects void set_background(); void move_background(); private: + //Check if two hitboxes are colliding bool col_eval(Hitbox h1, Hitbox h2); }; \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/ship.cpp b/Assignment3Project/Assignment3Project/ship.cpp index 397ac39..9bd2713 100644 --- a/Assignment3Project/Assignment3Project/ship.cpp +++ b/Assignment3Project/Assignment3Project/ship.cpp @@ -12,6 +12,7 @@ void Ship::set_props(ALLEGRO_BITMAP* _sprite, Behavior _behavior) { sprite = _sprite; behavior = _behavior; oob = false; + //Build properties based on behavior switch (behavior) { case Enemy: speed = 1.1; @@ -74,6 +75,8 @@ void Ship::move(Direction dir) { break; } + //Check screen boundaries + //The player must be kept within the boundaries, but other objects must be set as oob so they can be unloaded if (x_pos <= l_bound) { if (behavior == Player) { x_pos = l_bound; diff --git a/Assignment3Project/Assignment3Project/ship.h b/Assignment3Project/Assignment3Project/ship.h index 20d9ce9..ad08ab3 100644 --- a/Assignment3Project/Assignment3Project/ship.h +++ b/Assignment3Project/Assignment3Project/ship.h @@ -4,11 +4,12 @@ #include "game_element.h" #include "bullet.h" +//Ship sprite displayed over background class Ship : public GameElement { public: - ALLEGRO_BITMAP* sprite; + ALLEGRO_BITMAP* sprite; //loads a sprite rather than a primitive - bool fired; + bool fired; //no longer used Ship(); @@ -18,7 +19,6 @@ public: void move(Direction dir); Hitbox get_hitbox(); - //unique to object - void set_props(ALLEGRO_BITMAP* _sprite, Behavior _behavior); - Bullet fire(); + void set_props(ALLEGRO_BITMAP* _sprite, Behavior _behavior); //Sets ship properties + Bullet fire(); //Creates a new bullet }; \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/star.cpp b/Assignment3Project/Assignment3Project/star.cpp index d5b36ae..37e3ddb 100644 --- a/Assignment3Project/Assignment3Project/star.cpp +++ b/Assignment3Project/Assignment3Project/star.cpp @@ -11,7 +11,8 @@ void Star::reset_pos(float x, float y) { void Star::set_layer(BgLayer _layer) { layer = _layer; - + //Set properties based on layer + //Scrolls at a different speed based on layer switch (layer) { case Front: speed = 1.2;