ELEC2645 (2018/19) / Mbed 2 deprecated EL17MCD

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el17mcd
Date:
Sat Apr 13 16:31:43 2019 +0000
Parent:
10:d4fb12e9e7cd
Child:
12:9e6d5d0a0c82
Commit message:
!Can take left tank's turn including movement and firing turret. This transitions into "projectile phase" which ends when the projectile goes out of bounds (off screen). Objects instantiated inside game engine to tidy main.cpp.

Changed in this revision

Projectile/Projectile.cpp Show annotated file Show diff for this revision Revisions of this file
Projectile/Projectile.h Show annotated file Show diff for this revision Revisions of this file
TankL/TankL.cpp Show annotated file Show diff for this revision Revisions of this file
TankL/TankL.h Show annotated file Show diff for this revision Revisions of this file
TanksEngine/TanksEngine.cpp Show annotated file Show diff for this revision Revisions of this file
TanksEngine/TanksEngine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Projectile/Projectile.cpp	Fri Apr 12 12:01:47 2019 +0000
+++ b/Projectile/Projectile.cpp	Sat Apr 13 16:31:43 2019 +0000
@@ -40,7 +40,7 @@
 {
     _position_x = _init_x + _init_vel * _time * cos(_lnch_ang * DEG2PI) - 0.5 * _wind_acc * _time * _time;
     _position_y = _init_y + _init_vel * _time * sin(_lnch_ang * DEG2PI) - 0.5 * _grav_acc * _time * _time;
-    _time++;
+    _time += 0.4;
 }
 
 bool Projectile::check_boundaries()
@@ -55,9 +55,9 @@
     _position_y = y;
 }
 
-void Projectile::set_launch_parameters(int t, int x, int y, float ang, float vel, float grav, float wind)
+void Projectile::set_launch_parameters(int x, int y, float ang, float vel, float grav, float wind)
 {
-    _time = t;
+    _time = 0;
     _init_x = x;
     _init_y = y;
     _lnch_ang = ang;
--- a/Projectile/Projectile.h	Fri Apr 12 12:01:47 2019 +0000
+++ b/Projectile/Projectile.h	Sat Apr 13 16:31:43 2019 +0000
@@ -15,7 +15,7 @@
     void update_flight();
     bool check_boundaries();
     void set_position(int x, int y);
-    void set_launch_parameters(int t, int x, int y, float ang, float vel, float grav, float wind);
+    void set_launch_parameters(int x, int y, float ang, float vel, float grav, float wind);
     int get_position_x();
     int get_position_y();
     int get_hitbox(int i);
@@ -25,9 +25,9 @@
     int _position_x;
     int _position_y;
     int _hitbox[5];
-    int _time;
     int _init_x;
     int _init_y;
+    float _time;
     float _lnch_ang;
     float _init_vel; //1.3
     float _grav_acc; //0.02
--- a/TankL/TankL.cpp	Fri Apr 12 12:01:47 2019 +0000
+++ b/TankL/TankL.cpp	Sat Apr 13 16:31:43 2019 +0000
@@ -4,17 +4,17 @@
 1.4.19
 */
 #include "TankL.h"
-/*
+
 TankL::TankL()
 {
-
+    
 }
 
 TankL::~TankL()
 {
 
 }
-*/
+
 int TankL::get_position_x()
 {
     return _position_x;
@@ -53,7 +53,7 @@
 
 void TankL::move_position(int d)
 {
-    int slowness = 24 / _speed;
+    int slowness = 9 - _speed;
     int i = _move_counter % slowness;
     if (d > 0) {
         if (i == 0) {_position_x++;}
@@ -95,14 +95,16 @@
     };
     lcd.drawSprite(_position_x,42 - _position_y,6,10,(int *)tank_l_spr);
 
-    if (_angle < 18) {
-        const int turret_spr1[3][7] =   {
+    if (_angle >= 72 && _angle < 100 || _angle >= -260 && _angle < -255) {
+        const int turret_spr5[5][7] =   {
             { 0,0,1,0,0,0,0 },
-            { 0,1,1,1,1,1,1 },
+            { 0,0,1,0,0,0,0 },
+            { 0,0,1,0,0,0,0 },
+            { 0,1,1,1,0,0,0 },
             { 1,1,1,1,1,1,0 },                                                              
         };
-        lcd.drawSprite(2 + _position_x,42 - _position_y,3,7,(int *)turret_spr1);
-    } 
+        lcd.drawSprite(2 + _position_x,40 - _position_y,5,7,(int *)turret_spr5);
+    }
     else if (_angle >= 18 && _angle < 36) {  
         const int turret_spr2[3][7] =   {
             { 0,0,1,1,1,1,1 },
@@ -130,13 +132,11 @@
         lcd.drawSprite(2 + _position_x,41 - _position_y,4,7,(int *)turret_spr4);
     }
     else {
-        const int turret_spr5[5][7] =   {
-            { 0,0,1,0,0,0,0 },
+       const int turret_spr1[3][7] =   {
             { 0,0,1,0,0,0,0 },
-            { 0,0,1,0,0,0,0 },
-            { 0,1,1,1,0,0,0 },
+            { 0,1,1,1,1,1,1 },
             { 1,1,1,1,1,1,0 },                                                              
         };
-        lcd.drawSprite(2 + _position_x,40 - _position_y,5,7,(int *)turret_spr5);
-    }
+        lcd.drawSprite(2 + _position_x,42 - _position_y,3,7,(int *)turret_spr1);
+    } 
 }
\ No newline at end of file
--- a/TankL/TankL.h	Fri Apr 12 12:01:47 2019 +0000
+++ b/TankL/TankL.h	Sat Apr 13 16:31:43 2019 +0000
@@ -9,6 +9,9 @@
 class TankL
 {
 public:
+    
+    TankL();
+    ~TankL();
     //Accessors
     int get_position_x();
     int get_position_y();
--- a/TanksEngine/TanksEngine.cpp	Fri Apr 12 12:01:47 2019 +0000
+++ b/TanksEngine/TanksEngine.cpp	Sat Apr 13 16:31:43 2019 +0000
@@ -5,8 +5,9 @@
 #include "TanksEngine.h"
 #include "Projectile.h"
 #include "TankL.h"
-/*
+
 TanksEngine::TanksEngine()
+
 {
 
 }
@@ -15,14 +16,85 @@
 {
 
 }
-*/
-bool TanksEngine::collision_pl(TankL tl_obj, Projectile proj_obj)
+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_obj.get_hitbox(i0) == tl_obj.get_hitbox(i1)) {return true;}               
+            if (_proj.get_hitbox(i0) == _tankl.get_hitbox(i1)) {return true;}               
         }
     }
     return false;
 }
 
+int TanksEngine::get_turn()
+{
+    return _turn;
+}
+
--- a/TanksEngine/TanksEngine.h	Fri Apr 12 12:01:47 2019 +0000
+++ b/TanksEngine/TanksEngine.h	Sat Apr 13 16:31:43 2019 +0000
@@ -10,10 +10,34 @@
 class TanksEngine
 {
 public:
+
+    TanksEngine();
+    ~TanksEngine();
     
-    bool collision_pl(TankL tl_obj, Projectile proj_obj);
+    void initgame();
+    void left_tank_turn(Gamepad &pad, N5110 &lcd);
+    void projectile_phase(N5110 &lcd);
+    void right_tank_turn(Gamepad &pad, N5110 &lcd);
+    int get_turn();
     
 private:
+
+    void _read_input(Gamepad &pad);    
+    void _left_tank_shoots();
+    bool _collision_pl(TankL _tankl, Projectile _proj);
+    void _change_turn();
     
+    int _turn;
+    int _turn_timer;
+    int _turn_alternater;
+    int _move;
+    bool _fire;
+    float _power;
+    float _angle;
+    float _mag; 
+    
+    TankL _tankl;
+    
+    Projectile _proj;
 };
 
--- a/main.cpp	Fri Apr 12 12:01:47 2019 +0000
+++ b/main.cpp	Sat Apr 13 16:31:43 2019 +0000
@@ -26,9 +26,8 @@
 
 /////////////// objects ///////////////
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-TankL tankl;
-Projectile proj;
 TanksEngine engine;
+Gamepad pad;
 
 void welcome()
 {
@@ -45,36 +44,29 @@
 int main()
 {
     lcd.init();
-
-    float a;
-    float p;
+    pad.init();
+    engine.initgame();
+    int fps = 60;
+    float frame_period_ms = 1000/fps;
     
-    int i = 0;
-    tankl.set_angle(0);
-    tankl.set_speed(4);
-    tankl.set_position(40-6, 0);
-
-
    // welcome();  // display welcome message 
 
     while(1) {  // infinite loop
-            
-        lcd.clear();
-        tankl.draw(lcd);
+    
+        lcd.clear();          
+        if (engine.get_turn() == 1) {
+            engine.left_tank_turn(pad, lcd);
+        } else if (engine.get_turn() == 2) {
+            engine.projectile_phase(lcd);
+        } else { int turn = 3; }// means nothing
         lcd.refresh();
         wait_ms(16.666);
-        
-        a = 50 + 50 * sin((double)i/50);
-        p = 1.1*cos((double)i / 60);
-        
-        tankl.set_angle(a);       
-        tankl.move_position(p);     
-  
-        i++;
+
         
     }
+    /*
 lcd.clear();
 lcd.printString("DONE",0,1);
 lcd.refresh();
-wait(10);
+wait(10);*/
 }