ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

Revision:
12:9e6d5d0a0c82
Parent:
11:4e2eb64031a0
Child:
13:feadff02d3f7
diff -r 4e2eb64031a0 -r 9e6d5d0a0c82 TanksEngine/TanksEngine.cpp
--- a/TanksEngine/TanksEngine.cpp	Sat Apr 13 16:31:43 2019 +0000
+++ b/TanksEngine/TanksEngine.cpp	Sun Apr 14 15:58:12 2019 +0000
@@ -4,7 +4,7 @@
 */
 #include "TanksEngine.h"
 #include "Projectile.h"
-#include "TankL.h"
+#include "Tank.h"
 
 TanksEngine::TanksEngine()
 
@@ -20,74 +20,115 @@
 {
     _tankl.set_position(0, 0);
     _tankl.set_speed(1);
-    _turn = 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;
-    _turn_alternater = 0;
+    _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::left_tank_turn(Gamepad &pad, N5110 &lcd)
+void TanksEngine::right_tank_turn(Gamepad &pad)
 {
-        _read_input(pad);
-        _tankl.set_angle(100 - _angle ); 
-        _tankl.move_position(_move);
-        _left_tank_shoots();
-        _tankl.draw(lcd);
+        _tankr.move_position(_move);
+        _right_tank_shoots();
 }
 
-void TanksEngine::right_tank_turn(Gamepad &pad, N5110 &lcd)
-{
-        _read_input(pad);
-        _tankl.draw(lcd);
+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
+        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
+        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::projectile_phase(N5110 &lcd)
+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(); }
-    _proj.update_flight();
-    _tankl.draw(lcd);
-    _proj.draw(lcd);
+  /*  if (_collision_pl(_tankl, _proj) == true) {
+        _tankl.lose_health();
+        _change_turn();
+    } */
 }
 
 void TanksEngine::_change_turn()
 {
-    _turn_alternater = !_turn_alternater;
-    if (_turn_alternater == 1) { _turn = 3; }
-    else { _turn = 1; }
+    if (_turn == 2) { _turn = 3; }
+    if (_turn == 4) { _turn = 1; }
 }
 
-void TanksEngine::_read_input(Gamepad &pad)
-{
-    
+void TanksEngine::read_input(Gamepad &pad)
+{ 
     _angle = pad.get_angle();
-    if (pad.check_event(Gamepad::L_PRESSED) == true) {
+    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) {
+    else if (pad.check_event(Gamepad::R_PRESSED) == true && cooldownr < 0) {
         _move = 1 * !_move; // toggle movement to the right 
-    }
-    if (pad.check_event(Gamepad::A_PRESSED) == true) { _fire = true; }
-    else { _fire = false; }      
+        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(TankL _tankl, Projectile _proj)
+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;}               
+            if (_proj.get_hitbox(i0) == _tankl.get_hitbox(i1)) { return true; }               
         }
     }
     return false;
@@ -98,3 +139,25 @@
     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;
+    }
+}
+