Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
TanksEngine/TanksEngine.cpp
- Committer:
- el17mcd
- Date:
- 2019-04-17
- Revision:
- 13:feadff02d3f7
- Parent:
- 12:9e6d5d0a0c82
- Child:
- 14:fe2e16cdf219
File content as of revision 13:feadff02d3f7:
/* TanksEngine.cpp
Game Engine
9.4.19
*/
#include "TanksEngine.h"
#include "Projectile.h"
#include "Tank.h"
TanksEngine::TanksEngine()
{
}
TanksEngine::~TanksEngine()
{
}
void TanksEngine::initgame()
{
_tankl.set_position(0, 0);
_tankl.set_movement_limits(0, 21);
_tankl.set_speed(1);
_tankr.set_position(84-10, 0);
_tankr.set_movement_limits(54, 74);
_tankr.set_speed(1);
_tankl.set_health(6);
_tankr.set_health(6);
_turn = 1;
_grav = 0.03;
_fire = false;
_move = 0;
_turn_timer = 10;
cooldownl = -1;
cooldownr = -1;
}
void TanksEngine::left_tank_turn(Graphics &graphics, Gamepad &pad)
{
_tankl.move_position(_move);
if (_angle >= 355 && _angle <= 360 || _angle >= 0 && _angle <= 90) {// turret must be at acceptable angle
_left_tank_shoots();
}
graphics.show_health(_tankl.get_health(), 6, pad);
}
void TanksEngine::right_tank_turn(Graphics &graphics, Gamepad &pad)
{
_tankr.move_position(_move);
if (_angle >= 270 && _angle <= 360 || _angle >= 0 && _angle <= 5) {// turret must be at acceptable angle
_right_tank_shoots();
}
graphics.show_health(_tankr.get_health(), 6, pad);
}
void TanksEngine::render(Graphics graphics, N5110 &lcd) // Draw graphics for all objects, turrets rotate
{ // depending on current turn
graphics.draw_tank_l(_tankl.get_position_x(), _tankl.get_position_y(), lcd);
if (_turn == 1) {
graphics.draw_turret_l(_tankl.get_position_x(), _tankl.get_position_y(), _angle, lcd);
}
if (_turn == 2 || _turn == 4) {
graphics.draw_projectile(_proj.get_position_x(), _proj.get_position_y(), lcd);
}
graphics.draw_tank_r(_tankr.get_position_x(), _tankr.get_position_y(), lcd);
if (_turn == 3) {
graphics.draw_turret_r(_tankr.get_position_x(), _tankr.get_position_y(), _angle, lcd);
}
}
void TanksEngine::_left_tank_shoots()
{
if (_fire == true) {
int angle = _angle; // change to an int
angle = (-angle + 90) % 360; // gamepad's convention is N = 0 clockwise, changed into E = 0 anticlockwise
int x = _tankl.get_position_x() + 5;
int y = _tankl.get_position_y() + 5; //0.005
_proj.set_launch_parameters(x, y, angle, _vel, _grav, _wind); // set launch parameters for projectile based on tank/game parameters
_tankl.generate_hitbox(); // generate hitboxes ready to determine collision during projectile phase
_tankr.generate_hitbox();
_turn = 2; // change to projectile phase
}
}
void TanksEngine::_right_tank_shoots()
{
if (_fire == true) {
int angle = _angle; // change to an int
angle = (-angle + 90) % 360; // gamepad's convention is N = 0 clockwise changed into E = 0 anticlockwise
int x = _tankr.get_position_x() + 5;
int y = _tankr.get_position_y() + 5;
_proj.set_launch_parameters(x, y, angle, _vel, _grav, _wind); // set launch parameters for projectile
// based on tank/game parameters
_tankl.generate_hitbox(); // generate hitboxes ready to determine collision during projectile phase
_tankr.generate_hitbox();
_turn = 4; // change to projectile phase
}
}
void TanksEngine::projectile_phase(N5110 &lcd)
{
_proj.update_flight();
_proj.generate_hitbox();
if (_proj.check_boundaries() == true) { _change_turn(); } // change turn if projectile falls off left,
_object_hit(lcd);
}
void TanksEngine::_change_turn()
{
if (_turn == 2) { _turn = 3; }
if (_turn == 4) { _turn = 1; }
_wind = 0.005;
}
void TanksEngine::read_input(Gamepad &pad)
{
_vel = 1.15 + pad.read_pot();
_angle = pad.get_angle();
if (pad.check_event(Gamepad::L_PRESSED) == true && cooldownl < 0) {
_move = -1 * !_move; // toggle movement to the left
cooldownl = 10;
}
else if (pad.check_event(Gamepad::R_PRESSED) == true && cooldownr < 0) {
_move = 1 * !_move; // toggle movement to the right
cooldownr = 10;
}
else if (_turn == 2 || _turn == 4) { _move = 0; } // movement cannot be triggered during projectile phase
if (pad.check_event(Gamepad::A_PRESSED) == true) { _fire = true; }
else { _fire = false; }
_decrement_cooldowns();
}
bool TanksEngine::_collision_pl(Tank _tankl, Projectile _proj) // detects projectile hitting LEFT tank
{
for (int i0 = 0; i0 < 4; i0++) {
for (int i1 = 0; i1 < 40; i1++) {
if (_proj.get_hitbox(i0) == _tankl.get_hitbox(i1)) { return true; }
}
}
return false;
}
bool TanksEngine::_collision_pr(Tank _tankr, Projectile _proj) // detects projectile hitting RIGHT tank
{
for (int i0 = 0; i0 < 4; i0++) {
for (int i1 = 0; i1 < 40; i1++) {
if (_proj.get_hitbox(i0) == _tankr.get_hitbox(i1)) { return true; }
}
}
return false;
}
bool TanksEngine::_collision_pm(N5110 &lcd, Projectile _proj) // detects projectile hitting the MAP
{
if (_proj.get_position_x() >= 31 && _proj.get_position_x() <= 54) {
if (lcd.getPixel(_proj.get_position_x(),48 - _proj.get_position_y()) == 1) {
return true;
} else if (lcd.getPixel(_proj.get_position_x() + 1,48 - _proj.get_position_y()) == 1) {
return true;
} else { return false ;}
}
else {return false ; }
}
void TanksEngine::_object_hit(N5110 &lcd)
{
if (_collision_pl(_tankl, _proj) == true) {
_tankl.lose_health();
_change_turn();
}
else if (_collision_pr(_tankr, _proj) == true) {
_tankr.lose_health();
_change_turn();
}
else if (_collision_pm(lcd, _proj) == true) {
_change_turn();
}
}
int TanksEngine::get_turn()
{
return _turn;
}
void TanksEngine::_decrement_cooldowns() // Prevents buttons being pressed multiple times in quick succession
{
cooldownl--;
cooldownr--;
if (cooldownl < -100) { cooldownl = -1; }
if (cooldownr < -100) { cooldownr = -1; }
}
void TanksEngine::end()
{
if (_tankl.get_health() == 0) {
_tankr.set_position(84/2 - 5, 20);
_tankl.set_position(-20, 0);
_turn = 5;
}
else if (_tankr.get_health() == 0) {
_tankl.set_position(84/2 - 5, 20);
_tankr.set_position(-20, 0);
_turn = 5;
}
}