Mortal Kombat Game ELEC2645

Dependencies:   mbed N5110 ShiftReg Joystick

Revision:
14:7f23841685ad
Parent:
13:eaf070d5f599
Child:
15:7fd2d34f3be5
--- a/GameEngine.cpp	Sun Apr 25 15:04:06 2021 +0000
+++ b/GameEngine.cpp	Mon Apr 26 22:19:28 2021 +0000
@@ -4,11 +4,11 @@
 
 void GameEngine::init(){
     _fighter.init();
+    _enemy.init();
 }
 
 void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn  &joy_v, AnalogIn  &joy_h) {
     // initializing and drawing enemy
-    _enemy.init(1);
     enemy_AI(lcd);
     // function that draws fighter and allows user to control it 
     _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h);
@@ -30,18 +30,25 @@
         input = 1;
         }
     else {input = 0;}
-    _enemy.draw(lcd, input);
     if (diff >= -15 && diff <= 15){ // randomize enemy fight moves only in fighting range
+        _enemy.draw(lcd, input);
         _enemy.randomize_moves(lcd, input);
         // check for collision (without guard ON)
         // function to reduce enemy health or fighter health
         // check_enemy_dead and deploy next god 
         }
-    else if (diff < 0) { // negative difference so fighter is to the left so enemy must move left
+    // code for enemy to move to fighter if he is to the right 
+    else if ((enemy_pos < fighter_pos) && (diff > 15) && (rand()%2 > 0)) {
+        _enemy.add_x(5);
+        _enemy.move_right(lcd);
+    }
+    // code for enemy to move to fighter if he is to the left 
+    else if ((enemy_pos > fighter_pos) && (diff < 15) && (rand()%2 > 0)) {
+        _enemy.add_x(-5);
         _enemy.move_left(lcd);
     }
-    else { // move enemy to fighter until within fighting range
-         _enemy.draw(lcd, input);
+    else {
+        _enemy.draw(lcd, input);
     }
 }
 /*