Ship firing is written but holy fuck it really doesn't work

main
Braydon Kains 6 years ago
parent 31c60269ac
commit 7002d85693

@ -4,6 +4,7 @@ Bullet::Bullet() {
speed = 1.0; speed = 1.0;
height = 20; height = 20;
width = 5; width = 5;
oob = false;
} }
void Bullet::reset_pos(float x, float y) { void Bullet::reset_pos(float x, float y) {
@ -12,8 +13,14 @@ void Bullet::reset_pos(float x, float y) {
} }
void Bullet::draw() { void Bullet::draw() {
if (!oob) {
al_draw_filled_rectangle(x_pos, y_pos, x_pos + width, y_pos + height, al_map_rgb(0, 0, 0)); 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) { void Bullet::move(Direction dir) {
y_pos -= 6;
if (y_pos < 0 - height) {
oob = true;
}
} }

@ -9,6 +9,7 @@ public:
float speed; float speed;
int height; int height;
int width; int width;
bool oob;
virtual void reset_pos(float x, float y) = 0; virtual void reset_pos(float x, float y) = 0;
virtual void move(Direction dir) = 0; virtual void move(Direction dir) = 0;

@ -33,6 +33,7 @@ GameScreen::GameScreen(std::map<std::string, ALLEGRO_BITMAP*> _sprites, std::map
void GameScreen::reset() { void GameScreen::reset() {
ship.set_sprite(sprites["Ship"]); ship.set_sprite(sprites["Ship"]);
ship.reset_pos(SCREEN_W / 2, SCREEN_H / 8); 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. //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; ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev); al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER) { //Check per frame if (ev.type == ALLEGRO_EVENT_TIMER) { //Check per frame
//Ship Movement
if (keys[KEYUP]) { if (keys[KEYUP]) {
if (keys[KEYRIGHT]) { if (keys[KEYRIGHT]) {
ship.move(UR); ship.move(UR);
@ -95,6 +97,13 @@ void GameScreen::run(ALLEGRO_FONT* font) {
ship.move(R); ship.move(R);
} }
//Firing
if (keys[KEYSPACE]) {
if (bullets.size() < max_bullets) {
bullets.push_back(ship.fire());
}
}
//Global refresh //Global refresh
al_clear_to_color(al_map_rgb(0, 0, 0)); al_clear_to_color(al_map_rgb(0, 0, 0));
redraw(font); redraw(font);
@ -177,6 +186,19 @@ void GameScreen::run(ALLEGRO_FONT* font) {
//Redraw all elements of the screen //Redraw all elements of the screen
void GameScreen::redraw(ALLEGRO_FONT* font) { void GameScreen::redraw(ALLEGRO_FONT* font) {
ship.draw(); ship.draw();
vector<int> 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() { void GameScreen::back() {

@ -4,6 +4,7 @@
#include "screen.h" #include "screen.h"
#include "ship.h" #include "ship.h"
#include "bullet.h"
//Main screen for gameplay //Main screen for gameplay
class GameScreen : public Screen { class GameScreen : public Screen {
@ -15,6 +16,8 @@ public:
int level; int level;
bool music; bool music;
Ship ship; Ship ship;
unsigned int max_bullets;
std::vector<Bullet> bullets;
GameScreen(std::map<std::string, ALLEGRO_BITMAP*> _sprites, std::map<std::string, ALLEGRO_SAMPLE*> _samples); GameScreen(std::map<std::string, ALLEGRO_BITMAP*> _sprites, std::map<std::string, ALLEGRO_SAMPLE*> _samples);

@ -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;
} }

@ -3,6 +3,7 @@
#include "enums.h" #include "enums.h"
#include "game_element.h" #include "game_element.h"
#include "bullet.h"
class Ship : public GameElement { class Ship : public GameElement {
public: public:
@ -14,5 +15,5 @@ public:
void set_sprite(ALLEGRO_BITMAP* _sprite); void set_sprite(ALLEGRO_BITMAP* _sprite);
void draw(); void draw();
void move(Direction dir); void move(Direction dir);
void fire(); Bullet fire();
}; };
Loading…
Cancel
Save