Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
GameEngine.cpp@16:4a1d916d97c5, 2021-04-27 (annotated)
- Committer:
- ozy
- Date:
- Tue Apr 27 22:46:33 2021 +0000
- Revision:
- 16:4a1d916d97c5
- Parent:
- 15:7fd2d34f3be5
- Child:
- 17:928edcdd1058
Enemy collision success
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ozy | 6:a1a7dc264fed | 1 | #include "GameEngine.h" |
ozy | 6:a1a7dc264fed | 2 | |
ozy | 6:a1a7dc264fed | 3 | GameEngine::GameEngine() {} |
ozy | 6:a1a7dc264fed | 4 | |
ozy | 7:737fb0c3dbef | 5 | void GameEngine::init(){ |
ozy | 7:737fb0c3dbef | 6 | _fighter.init(); |
ozy | 14:7f23841685ad | 7 | _enemy.init(); |
ozy | 6:a1a7dc264fed | 8 | } |
ozy | 6:a1a7dc264fed | 9 | |
ozy | 7:737fb0c3dbef | 10 | void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { |
ozy | 15:7fd2d34f3be5 | 11 | // initializing and drawing enemy AI agent |
ozy | 12:b4477a312158 | 12 | enemy_AI(lcd); |
ozy | 7:737fb0c3dbef | 13 | // function that draws fighter and allows user to control it |
ozy | 7:737fb0c3dbef | 14 | _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h); |
ozy | 16:4a1d916d97c5 | 15 | check_collision(lcd, buttonC); |
ozy | 7:737fb0c3dbef | 16 | lcd.refresh(); |
ozy | 10:e83899f11e8a | 17 | |
ozy | 7:737fb0c3dbef | 18 | } |
ozy | 7:737fb0c3dbef | 19 | |
ozy | 8:e2e2eb4ea0ca | 20 | void GameEngine::enemy_AI(N5110 &lcd) { |
ozy | 8:e2e2eb4ea0ca | 21 | int fighter_pos = _fighter.get_x(); |
ozy | 8:e2e2eb4ea0ca | 22 | printf("f pos = %i \n", fighter_pos); |
ozy | 8:e2e2eb4ea0ca | 23 | int enemy_pos = _enemy.get_x(); |
ozy | 8:e2e2eb4ea0ca | 24 | printf("e pos = %i \n", enemy_pos); |
ozy | 10:e83899f11e8a | 25 | |
ozy | 8:e2e2eb4ea0ca | 26 | int input = 0; |
ozy | 8:e2e2eb4ea0ca | 27 | int diff = fighter_pos - enemy_pos; |
ozy | 8:e2e2eb4ea0ca | 28 | printf("diff = %i \n", diff); |
ozy | 12:b4477a312158 | 29 | // if difference is positive, fighter is to the right so enemy must look right (if input is 1, enemy looks right) and vice versa |
ozy | 8:e2e2eb4ea0ca | 30 | if (diff > 0) { |
ozy | 8:e2e2eb4ea0ca | 31 | input = 1; |
ozy | 8:e2e2eb4ea0ca | 32 | } |
ozy | 8:e2e2eb4ea0ca | 33 | else {input = 0;} |
ozy | 16:4a1d916d97c5 | 34 | if (diff >= -10 && diff <= 10){ // randomize enemy fight moves only in fighting range |
ozy | 14:7f23841685ad | 35 | _enemy.draw(lcd, input); |
ozy | 12:b4477a312158 | 36 | _enemy.randomize_moves(lcd, input); |
ozy | 13:eaf070d5f599 | 37 | // check for collision (without guard ON) |
ozy | 13:eaf070d5f599 | 38 | // function to reduce enemy health or fighter health |
ozy | 16:4a1d916d97c5 | 39 | // check_end_of_game |
ozy | 12:b4477a312158 | 40 | } |
ozy | 14:7f23841685ad | 41 | // code for enemy to move to fighter if he is to the right |
ozy | 16:4a1d916d97c5 | 42 | else if ((enemy_pos < fighter_pos) && (diff > 10) && (rand()%3 < 1)) { |
ozy | 14:7f23841685ad | 43 | _enemy.add_x(5); |
ozy | 14:7f23841685ad | 44 | _enemy.move_right(lcd); |
ozy | 14:7f23841685ad | 45 | } |
ozy | 14:7f23841685ad | 46 | // code for enemy to move to fighter if he is to the left |
ozy | 16:4a1d916d97c5 | 47 | else if ((enemy_pos > fighter_pos) && (diff < 10) && (rand()%3 < 1)) { |
ozy | 14:7f23841685ad | 48 | _enemy.add_x(-5); |
ozy | 13:eaf070d5f599 | 49 | _enemy.move_left(lcd); |
ozy | 13:eaf070d5f599 | 50 | } |
ozy | 14:7f23841685ad | 51 | else { |
ozy | 14:7f23841685ad | 52 | _enemy.draw(lcd, input); |
ozy | 13:eaf070d5f599 | 53 | } |
ozy | 8:e2e2eb4ea0ca | 54 | } |
ozy | 15:7fd2d34f3be5 | 55 | |
ozy | 16:4a1d916d97c5 | 56 | void GameEngine::check_collision(N5110 &lcd, DigitalIn &buttonC) { |
ozy | 16:4a1d916d97c5 | 57 | int fighter_pos = _fighter.get_x(); |
ozy | 16:4a1d916d97c5 | 58 | int enemy_pos = _enemy.get_x(); |
ozy | 16:4a1d916d97c5 | 59 | int diff = fighter_pos - enemy_pos; |
ozy | 16:4a1d916d97c5 | 60 | int input; |
ozy | 16:4a1d916d97c5 | 61 | if (diff > 0) { |
ozy | 16:4a1d916d97c5 | 62 | input = 1; |
ozy | 16:4a1d916d97c5 | 63 | } |
ozy | 16:4a1d916d97c5 | 64 | else {input = 0;} |
ozy | 16:4a1d916d97c5 | 65 | int fighter_y = _fighter.get_y(); // getting fighter y pos to avoid enemy hitting fighter while fighter is above him |
ozy | 16:4a1d916d97c5 | 66 | // code to check enemy collision |
ozy | 16:4a1d916d97c5 | 67 | // check in fight range check enemy fight move check fighter is not guarding check fighter is on gound |
ozy | 16:4a1d916d97c5 | 68 | if ((diff >= -10 && diff <= 10) && (_enemy.randomize_moves(lcd, input)) && (buttonC.read() != 1) && (fighter_y == 34)) { |
ozy | 16:4a1d916d97c5 | 69 | printf("enemy collision detected!"); |
ozy | 16:4a1d916d97c5 | 70 | printf("random move = %i", _enemy.randomize_moves(lcd, input)); |
ozy | 16:4a1d916d97c5 | 71 | } |
ozy | 16:4a1d916d97c5 | 72 | // code to check fight collision |
ozy | 13:eaf070d5f599 | 73 | } |
ozy | 8:e2e2eb4ea0ca | 74 |