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-14
- Revision:
- 12:9e6d5d0a0c82
- Parent:
- 11:4e2eb64031a0
- Child:
- 13:feadff02d3f7
File content as of revision 12:9e6d5d0a0c82:
/* 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_speed(1);
    _tankr.set_position(84-10, 0);
    _tankr.set_speed(1);
    _tankl.set_health(1);
    _tankr.set_health(1);
    _turn = 1; // change to 1
    _fire = false;
    _move = 0;
    _turn_timer = 10;
    cooldownl = -1;
    cooldownr = -1;
}
void TanksEngine::left_tank_turn(Gamepad &pad)
{
        _tankl.move_position(_move);
        _left_tank_shoots();
}
void TanksEngine::right_tank_turn(Gamepad &pad)
{
        _tankr.move_position(_move);
        _right_tank_shoots();
}
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;
        _proj.set_launch_parameters(x, y, angle, 1.3, 0.02, 0.00); // 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, 1.3, 0.02, 0.00); // 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()
{
    _proj.update_flight();
    if (_proj.check_boundaries() == true) { _change_turn(); }
  /*  if (_collision_pl(_tankl, _proj) == true) {
        _tankl.lose_health();
        _change_turn();
    } */
}
void TanksEngine::_change_turn()
{
    if (_turn == 2) { _turn = 3; }
    if (_turn == 4) { _turn = 1; }
}
void TanksEngine::read_input(Gamepad &pad)
{ 
    _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; }
    
    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;
}
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;
    }
}