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-13
- Revision:
- 11:4e2eb64031a0
- Parent:
- 7:a3ccabdebe2e
- Child:
- 12:9e6d5d0a0c82
File content as of revision 11:4e2eb64031a0:
/* TanksEngine.cpp
Game Engine
9.4.19
*/
#include "TanksEngine.h"
#include "Projectile.h"
#include "TankL.h"
TanksEngine::TanksEngine()
{
}
TanksEngine::~TanksEngine()
{
}
void TanksEngine::initgame()
{
    _tankl.set_position(0, 0);
    _tankl.set_speed(1);
    _turn = 1;
    _fire = false;
    _turn_alternater = 0;
    _turn_timer = 10;
}
void TanksEngine::left_tank_turn(Gamepad &pad, N5110 &lcd)
{
        _read_input(pad);
        _tankl.set_angle(100 - _angle ); 
        _tankl.move_position(_move);
        _left_tank_shoots();
        _tankl.draw(lcd);
}
void TanksEngine::right_tank_turn(Gamepad &pad, N5110 &lcd)
{
        _read_input(pad);
        _tankl.draw(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() + 4;
        _proj.set_launch_parameters(x, y,angle, 1.3, 0.02, 0.00); // set launch parameters for projectile based on tank/game parameters
        _turn = 2; // change to projectile phase
    }
}
void TanksEngine::projectile_phase(N5110 &lcd)
{
    if (_proj.check_boundaries() == true) { _change_turn(); }
    _proj.update_flight();
    _tankl.draw(lcd);
    _proj.draw(lcd);
}
void TanksEngine::_change_turn()
{
    _turn_alternater = !_turn_alternater;
    if (_turn_alternater == 1) { _turn = 3; }
    else { _turn = 1; }
}
void TanksEngine::_read_input(Gamepad &pad)
{
    
    _angle = pad.get_angle();
    if (pad.check_event(Gamepad::L_PRESSED) == true) {
        _move = -1 * !_move; // toggle movement to the left 
    }
    else if (pad.check_event(Gamepad::R_PRESSED) == true) {
        _move = 1 * !_move; // toggle movement to the right 
    }
    if (pad.check_event(Gamepad::A_PRESSED) == true) { _fire = true; }
    else { _fire = false; }      
}
bool TanksEngine::_collision_pl(TankL _tankl, Projectile _proj)
{
    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;
}