Ship firing and movement boundaries functional

main
BraydonKains 6 years ago
parent 7002d85693
commit a016d98fbd

@ -14,7 +14,7 @@ void Bullet::reset_pos(float x, float y) {
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(255, 255, 255));
}
}

@ -1,5 +1,8 @@
#pragma once
#define SCREEN_W 640
#define SCREEN_H 480
enum State {
Start,
Gameplay,

@ -7,8 +7,8 @@ public:
float x_pos;
float y_pos;
float speed;
int height;
int width;
float height;
float width;
bool oob;
virtual void reset_pos(float x, float y) = 0;

@ -58,6 +58,8 @@ void GameScreen::run(ALLEGRO_FONT* font) {
redraw(font);
al_flip_display();
int fired_counter = 0;
bool keys[ALLEGRO_KEY_MAX];
for (int i = 0; i < ALLEGRO_KEY_MAX; i++) keys[i] = false;
@ -100,8 +102,20 @@ void GameScreen::run(ALLEGRO_FONT* font) {
//Firing
if (keys[KEYSPACE]) {
if (bullets.size() < max_bullets) {
if (!ship.fired) {
bullets.push_back(ship.fire());
}
ship.fired = true;
}
}
if (ship.fired) {
if (fired_counter >= 20) {
ship.fired = false;
fired_counter = 0;
}
else {
fired_counter++;
}
}
//Global refresh
@ -111,15 +125,19 @@ void GameScreen::run(ALLEGRO_FONT* font) {
}
if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_S:
case ALLEGRO_KEY_DOWN:
keys[KEYDOWN] = true;
break;
case ALLEGRO_KEY_W:
case ALLEGRO_KEY_UP:
keys[KEYUP] = true;
break;
case ALLEGRO_KEY_D:
case ALLEGRO_KEY_RIGHT:
keys[KEYRIGHT] = true;
break;
case ALLEGRO_KEY_A:
case ALLEGRO_KEY_LEFT:
keys[KEYLEFT] = true;
break;
@ -143,15 +161,19 @@ void GameScreen::run(ALLEGRO_FONT* font) {
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_S:
case ALLEGRO_KEY_DOWN:
keys[KEYDOWN] = false;
break;
case ALLEGRO_KEY_W:
case ALLEGRO_KEY_UP:
keys[KEYUP] = false;
break;
case ALLEGRO_KEY_D:
case ALLEGRO_KEY_RIGHT:
keys[KEYRIGHT] = false;
break;
case ALLEGRO_KEY_A:
case ALLEGRO_KEY_LEFT:
keys[KEYLEFT] = false;
break;
@ -192,9 +214,11 @@ void GameScreen::redraw(ALLEGRO_FONT* font) {
if (bullets.at(i).oob) {
unload.push_back(i);
}
else {
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);

@ -7,9 +7,6 @@
#include "enums.h"
#define SCREEN_W 640
#define SCREEN_H 480
//Screen base class, implemented by every game screen
class Screen {
public:

@ -4,6 +4,12 @@ Ship::Ship() {
speed = 1.0;
height = 40;
width = 40;
l_bound = 50;
r_bound = 590;
r_bound -= width;
h_bound = SCREEN_H;
h_bound -= height;
}
void Ship::reset_pos(float x, float y) {
@ -51,10 +57,28 @@ void Ship::move(Direction dir) {
y_pos += factor;
break;
}
if (x_pos <= l_bound) {
x_pos = l_bound;
}
else if (x_pos >= r_bound) {
x_pos = r_bound;
}
if (y_pos <= 0.0) {
y_pos = 0.0;
}
else if (y_pos >= h_bound) {
y_pos = h_bound;
}
}
Bullet Ship::fire() {
if (!fired) {
Bullet new_bullet;
reset_pos((x_pos + width) / 2, y_pos + new_bullet.height);
float x = (x_pos) + width / 2.0;
float y = y_pos - new_bullet.height;
new_bullet.reset_pos(x, y);
fired = true;
return new_bullet;
}
}

@ -9,6 +9,11 @@ class Ship : public GameElement {
public:
ALLEGRO_BITMAP* sprite;
bool fired;
float h_bound;
float l_bound;
float r_bound;
Ship();
void reset_pos(float x, float y);

Loading…
Cancel
Save