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.
123 lines
2.2 KiB
C++
123 lines
2.2 KiB
C++
6 years ago
|
#include "ship.h"
|
||
|
|
||
6 years ago
|
Ship::Ship() {
|
||
|
}
|
||
|
|
||
|
void Ship::reset_pos(float x, float y) {
|
||
|
x_pos = x;
|
||
|
y_pos = y;
|
||
|
}
|
||
|
|
||
|
void Ship::set_props(ALLEGRO_BITMAP* _sprite, Behavior _behavior) {
|
||
|
sprite = _sprite;
|
||
|
behavior = _behavior;
|
||
|
oob = false;
|
||
6 years ago
|
//Build properties based on behavior
|
||
6 years ago
|
switch (behavior) {
|
||
6 years ago
|
case Enemy:
|
||
6 years ago
|
speed = 1.1;
|
||
6 years ago
|
height = 40;
|
||
|
width = 40;
|
||
6 years ago
|
|
||
|
l_bound = SCREEN_L_B;
|
||
|
r_bound = SCREEN_R_B;
|
||
6 years ago
|
h_t_bound = 0 - height;
|
||
|
h_b_bound = SCREEN_H + height;
|
||
6 years ago
|
break;
|
||
6 years ago
|
default:
|
||
6 years ago
|
speed = 1.0;
|
||
6 years ago
|
height = 40;
|
||
|
width = 40;
|
||
|
|
||
6 years ago
|
l_bound = SCREEN_L_B;
|
||
|
r_bound = SCREEN_R_B;
|
||
|
r_bound -= width;
|
||
6 years ago
|
h_t_bound = 0;
|
||
|
h_b_bound = SCREEN_H - height;
|
||
6 years ago
|
break;
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
|
void Ship::draw() {
|
||
6 years ago
|
al_draw_bitmap(sprite, x_pos, y_pos, (behavior == Player) ? NULL : ALLEGRO_FLIP_VERTICAL);
|
||
6 years ago
|
}
|
||
|
|
||
|
void Ship::move(Direction dir) {
|
||
|
float factor = 4 * speed;
|
||
|
switch (dir) {
|
||
|
case U:
|
||
|
y_pos -= factor;
|
||
|
break;
|
||
|
case D:
|
||
|
y_pos += factor;
|
||
|
break;
|
||
|
case R:
|
||
|
x_pos += factor;
|
||
|
break;
|
||
|
case L:
|
||
|
x_pos -= factor;
|
||
|
break;
|
||
|
case UR:
|
||
|
x_pos += factor;
|
||
|
y_pos -= factor;
|
||
|
break;
|
||
|
case UL:
|
||
|
x_pos -= factor;
|
||
|
y_pos -= factor;
|
||
|
break;
|
||
|
case DR:
|
||
|
x_pos += factor;
|
||
|
y_pos += factor;
|
||
|
break;
|
||
|
case DL:
|
||
|
x_pos -= factor;
|
||
|
y_pos += factor;
|
||
|
break;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
//Check screen boundaries
|
||
|
//The player must be kept within the boundaries, but other objects must be set as oob so they can be unloaded
|
||
6 years ago
|
if (x_pos <= l_bound) {
|
||
6 years ago
|
if (behavior == Player) {
|
||
|
x_pos = l_bound;
|
||
|
}
|
||
|
else oob = true;
|
||
6 years ago
|
}
|
||
|
else if (x_pos >= r_bound) {
|
||
6 years ago
|
if (behavior == Player) {
|
||
|
x_pos = r_bound;
|
||
|
}
|
||
|
else oob = true;
|
||
6 years ago
|
}
|
||
6 years ago
|
if (y_pos <= h_t_bound) {
|
||
6 years ago
|
if (behavior == Player) {
|
||
6 years ago
|
y_pos = h_t_bound;
|
||
6 years ago
|
}
|
||
|
else oob = true;
|
||
6 years ago
|
}
|
||
6 years ago
|
else if (y_pos >= h_b_bound) {
|
||
6 years ago
|
if (behavior == Player) {
|
||
6 years ago
|
y_pos = h_b_bound;
|
||
6 years ago
|
}
|
||
|
else oob = true;
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
Hitbox Ship::get_hitbox()
|
||
|
{
|
||
|
Hitbox hitbox;
|
||
|
hitbox.x = x_pos;
|
||
|
hitbox.y = y_pos;
|
||
|
hitbox.width = width;
|
||
|
hitbox.height = height;
|
||
|
return hitbox;
|
||
|
}
|
||
|
|
||
6 years ago
|
Bullet Ship::fire() {
|
||
6 years ago
|
Bullet new_bullet(behavior);
|
||
6 years ago
|
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;
|
||
6 years ago
|
}
|