From 7002d856930148235a41e75f2241256954ddf85e Mon Sep 17 00:00:00 2001 From: Braydon Kains Date: Sun, 13 Jan 2019 16:35:02 -0500 Subject: [PATCH] Ship firing is written but holy fuck it really doesn't work --- .../Assignment3Project/bullet.cpp | 11 ++++++++-- .../Assignment3Project/game_element.h | 1 + .../Assignment3Project/game_screen.cpp | 22 +++++++++++++++++++ .../Assignment3Project/game_screen.h | 3 +++ .../Assignment3Project/ship.cpp | 6 +++-- Assignment3Project/Assignment3Project/ship.h | 3 ++- 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/Assignment3Project/Assignment3Project/bullet.cpp b/Assignment3Project/Assignment3Project/bullet.cpp index 130f189..cabbf37 100644 --- a/Assignment3Project/Assignment3Project/bullet.cpp +++ b/Assignment3Project/Assignment3Project/bullet.cpp @@ -4,6 +4,7 @@ Bullet::Bullet() { speed = 1.0; height = 20; width = 5; + oob = false; } void Bullet::reset_pos(float x, float y) { @@ -12,8 +13,14 @@ void Bullet::reset_pos(float x, float y) { } void Bullet::draw() { - al_draw_filled_rectangle(x_pos, y_pos, x_pos + width, y_pos + height, al_map_rgb(0,0,0)); + if (!oob) { + al_draw_filled_rectangle(x_pos, y_pos, x_pos + width, y_pos + height, al_map_rgb(0, 0, 0)); + } } void Bullet::move(Direction dir) { -} + y_pos -= 6; + if (y_pos < 0 - height) { + oob = true; + } +} \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/game_element.h b/Assignment3Project/Assignment3Project/game_element.h index eafe2c7..ec8fc3b 100644 --- a/Assignment3Project/Assignment3Project/game_element.h +++ b/Assignment3Project/Assignment3Project/game_element.h @@ -9,6 +9,7 @@ public: float speed; int height; int width; + bool oob; virtual void reset_pos(float x, float y) = 0; virtual void move(Direction dir) = 0; diff --git a/Assignment3Project/Assignment3Project/game_screen.cpp b/Assignment3Project/Assignment3Project/game_screen.cpp index a147057..5fb8129 100644 --- a/Assignment3Project/Assignment3Project/game_screen.cpp +++ b/Assignment3Project/Assignment3Project/game_screen.cpp @@ -33,6 +33,7 @@ GameScreen::GameScreen(std::map _sprites, std::map void GameScreen::reset() { ship.set_sprite(sprites["Ship"]); ship.reset_pos(SCREEN_W / 2, SCREEN_H / 8); + max_bullets = 3; } //If a sample needs to be played while it is still being played, it will be stopped first. @@ -66,6 +67,7 @@ void GameScreen::run(ALLEGRO_FONT* font) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_TIMER) { //Check per frame + //Ship Movement if (keys[KEYUP]) { if (keys[KEYRIGHT]) { ship.move(UR); @@ -95,6 +97,13 @@ void GameScreen::run(ALLEGRO_FONT* font) { ship.move(R); } + //Firing + if (keys[KEYSPACE]) { + if (bullets.size() < max_bullets) { + bullets.push_back(ship.fire()); + } + } + //Global refresh al_clear_to_color(al_map_rgb(0, 0, 0)); redraw(font); @@ -177,6 +186,19 @@ void GameScreen::run(ALLEGRO_FONT* font) { //Redraw all elements of the screen void GameScreen::redraw(ALLEGRO_FONT* font) { ship.draw(); + + vector unload; + for (unsigned int i = 0; i < bullets.size(); i++) { + if (bullets.at(i).oob) { + unload.push_back(i); + } + bullets.at(i).move(U); + bullets.at(i).draw(); + } + for (unsigned int i = 0; i < unload.size(); i++) { + int x = unload.at(i); + bullets.erase(bullets.begin() + x); + } } void GameScreen::back() { diff --git a/Assignment3Project/Assignment3Project/game_screen.h b/Assignment3Project/Assignment3Project/game_screen.h index 484fcd5..4410c76 100644 --- a/Assignment3Project/Assignment3Project/game_screen.h +++ b/Assignment3Project/Assignment3Project/game_screen.h @@ -4,6 +4,7 @@ #include "screen.h" #include "ship.h" +#include "bullet.h" //Main screen for gameplay class GameScreen : public Screen { @@ -15,6 +16,8 @@ public: int level; bool music; Ship ship; + unsigned int max_bullets; + std::vector bullets; GameScreen(std::map _sprites, std::map _samples); diff --git a/Assignment3Project/Assignment3Project/ship.cpp b/Assignment3Project/Assignment3Project/ship.cpp index 319cf03..0b6b92c 100644 --- a/Assignment3Project/Assignment3Project/ship.cpp +++ b/Assignment3Project/Assignment3Project/ship.cpp @@ -53,6 +53,8 @@ void Ship::move(Direction dir) { } } -void Ship::fire() { - +Bullet Ship::fire() { + Bullet new_bullet; + reset_pos((x_pos + width) / 2, y_pos + new_bullet.height); + return new_bullet; } \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/ship.h b/Assignment3Project/Assignment3Project/ship.h index 0a818b6..0001f0f 100644 --- a/Assignment3Project/Assignment3Project/ship.h +++ b/Assignment3Project/Assignment3Project/ship.h @@ -3,6 +3,7 @@ #include "enums.h" #include "game_element.h" +#include "bullet.h" class Ship : public GameElement { public: @@ -14,5 +15,5 @@ public: void set_sprite(ALLEGRO_BITMAP* _sprite); void draw(); void move(Direction dir); - void fire(); + Bullet fire(); }; \ No newline at end of file