Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
GameEngine.cpp@18:22bda659c70a, 2021-04-29 (annotated)
- Committer:
- ozy
- Date:
- Thu Apr 29 22:40:08 2021 +0000
- Revision:
- 18:22bda659c70a
- Parent:
- 17:928edcdd1058
- Child:
- 19:6c63e1dd7c85
GameEngine Update
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 | 18:22bda659c70a | 5 | void GameEngine::init(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC){ |
ozy | 18:22bda659c70a | 6 | _enemy.init(); |
ozy | 7:737fb0c3dbef | 7 | _fighter.init(); |
ozy | 18:22bda659c70a | 8 | set_fighter_health(30); |
ozy | 18:22bda659c70a | 9 | set_enemy_health(30); |
ozy | 18:22bda659c70a | 10 | } |
ozy | 18:22bda659c70a | 11 | |
ozy | 18:22bda659c70a | 12 | int GameEngine::get_fighter_health() { |
ozy | 18:22bda659c70a | 13 | return _fighter_health; |
ozy | 18:22bda659c70a | 14 | } |
ozy | 18:22bda659c70a | 15 | |
ozy | 18:22bda659c70a | 16 | int GameEngine::get_enemy_health() { |
ozy | 18:22bda659c70a | 17 | return _enemy_health; |
ozy | 18:22bda659c70a | 18 | } |
ozy | 18:22bda659c70a | 19 | |
ozy | 18:22bda659c70a | 20 | void GameEngine::set_fighter_health(int fighter_health) { |
ozy | 18:22bda659c70a | 21 | _fighter_health = fighter_health; |
ozy | 18:22bda659c70a | 22 | } |
ozy | 18:22bda659c70a | 23 | |
ozy | 18:22bda659c70a | 24 | void GameEngine::set_enemy_health(int enemy_health) { |
ozy | 18:22bda659c70a | 25 | _enemy_health = enemy_health; |
ozy | 18:22bda659c70a | 26 | } |
ozy | 18:22bda659c70a | 27 | |
ozy | 18:22bda659c70a | 28 | void GameEngine::reduce_f_health(int fighter_health) { |
ozy | 18:22bda659c70a | 29 | _fighter_health -= fighter_health; |
ozy | 18:22bda659c70a | 30 | } |
ozy | 18:22bda659c70a | 31 | |
ozy | 18:22bda659c70a | 32 | void GameEngine::reduce_e_health(int enemy_health) { |
ozy | 18:22bda659c70a | 33 | _enemy_health -= enemy_health; |
ozy | 6:a1a7dc264fed | 34 | } |
ozy | 6:a1a7dc264fed | 35 | |
ozy | 7:737fb0c3dbef | 36 | void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { |
ozy | 15:7fd2d34f3be5 | 37 | // initializing and drawing enemy AI agent |
ozy | 12:b4477a312158 | 38 | enemy_AI(lcd); |
ozy | 7:737fb0c3dbef | 39 | // function that draws fighter and allows user to control it |
ozy | 7:737fb0c3dbef | 40 | _fighter.move_fighter(lcd, buttonA, buttonB, buttonC, buttonD, joy_v, joy_h); |
ozy | 18:22bda659c70a | 41 | int fighter_health = get_fighter_health(); |
ozy | 18:22bda659c70a | 42 | int enemy_health = get_enemy_health(); |
ozy | 18:22bda659c70a | 43 | int collision = check_collision(lcd, buttonA, buttonB, buttonC); |
ozy | 18:22bda659c70a | 44 | if (collision == 0) { |
ozy | 18:22bda659c70a | 45 | reduce_f_health(1); |
ozy | 18:22bda659c70a | 46 | printf("fighter health = %i\n", fighter_health); |
ozy | 18:22bda659c70a | 47 | } |
ozy | 18:22bda659c70a | 48 | else if (collision == 1) { |
ozy | 18:22bda659c70a | 49 | reduce_e_health(1); |
ozy | 18:22bda659c70a | 50 | printf("enemy health = %i\n", enemy_health); |
ozy | 18:22bda659c70a | 51 | } |
ozy | 7:737fb0c3dbef | 52 | lcd.refresh(); |
ozy | 7:737fb0c3dbef | 53 | } |
ozy | 7:737fb0c3dbef | 54 | |
ozy | 8:e2e2eb4ea0ca | 55 | void GameEngine::enemy_AI(N5110 &lcd) { |
ozy | 8:e2e2eb4ea0ca | 56 | int fighter_pos = _fighter.get_x(); |
ozy | 17:928edcdd1058 | 57 | //printf("f pos = %i \n", fighter_pos); |
ozy | 8:e2e2eb4ea0ca | 58 | int enemy_pos = _enemy.get_x(); |
ozy | 17:928edcdd1058 | 59 | //printf("e pos = %i \n", enemy_pos); |
ozy | 10:e83899f11e8a | 60 | |
ozy | 8:e2e2eb4ea0ca | 61 | int input = 0; |
ozy | 8:e2e2eb4ea0ca | 62 | int diff = fighter_pos - enemy_pos; |
ozy | 17:928edcdd1058 | 63 | // printf("diff = %i \n", diff); |
ozy | 12:b4477a312158 | 64 | // 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 | 65 | if (diff > 0) { |
ozy | 8:e2e2eb4ea0ca | 66 | input = 1; |
ozy | 8:e2e2eb4ea0ca | 67 | } |
ozy | 8:e2e2eb4ea0ca | 68 | else {input = 0;} |
ozy | 16:4a1d916d97c5 | 69 | if (diff >= -10 && diff <= 10){ // randomize enemy fight moves only in fighting range |
ozy | 14:7f23841685ad | 70 | _enemy.draw(lcd, input); |
ozy | 12:b4477a312158 | 71 | _enemy.randomize_moves(lcd, input); |
ozy | 13:eaf070d5f599 | 72 | // check for collision (without guard ON) |
ozy | 13:eaf070d5f599 | 73 | // function to reduce enemy health or fighter health |
ozy | 16:4a1d916d97c5 | 74 | // check_end_of_game |
ozy | 12:b4477a312158 | 75 | } |
ozy | 14:7f23841685ad | 76 | // code for enemy to move to fighter if he is to the right |
ozy | 17:928edcdd1058 | 77 | else if ((enemy_pos < fighter_pos) && (diff > 10) && (rand()%2 > 0)) { |
ozy | 14:7f23841685ad | 78 | _enemy.add_x(5); |
ozy | 14:7f23841685ad | 79 | _enemy.move_right(lcd); |
ozy | 14:7f23841685ad | 80 | } |
ozy | 14:7f23841685ad | 81 | // code for enemy to move to fighter if he is to the left |
ozy | 17:928edcdd1058 | 82 | else if ((enemy_pos > fighter_pos) && (diff < 10) && (rand()%2 > 0)) { |
ozy | 14:7f23841685ad | 83 | _enemy.add_x(-5); |
ozy | 13:eaf070d5f599 | 84 | _enemy.move_left(lcd); |
ozy | 13:eaf070d5f599 | 85 | } |
ozy | 14:7f23841685ad | 86 | else { |
ozy | 14:7f23841685ad | 87 | _enemy.draw(lcd, input); |
ozy | 13:eaf070d5f599 | 88 | } |
ozy | 8:e2e2eb4ea0ca | 89 | } |
ozy | 15:7fd2d34f3be5 | 90 | |
ozy | 18:22bda659c70a | 91 | int GameEngine::check_collision(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC) { |
ozy | 16:4a1d916d97c5 | 92 | int fighter_pos = _fighter.get_x(); |
ozy | 16:4a1d916d97c5 | 93 | int enemy_pos = _enemy.get_x(); |
ozy | 16:4a1d916d97c5 | 94 | int diff = fighter_pos - enemy_pos; |
ozy | 16:4a1d916d97c5 | 95 | int input; |
ozy | 18:22bda659c70a | 96 | int collision; |
ozy | 16:4a1d916d97c5 | 97 | if (diff > 0) { |
ozy | 16:4a1d916d97c5 | 98 | input = 1; |
ozy | 16:4a1d916d97c5 | 99 | } |
ozy | 16:4a1d916d97c5 | 100 | else {input = 0;} |
ozy | 16:4a1d916d97c5 | 101 | int fighter_y = _fighter.get_y(); // getting fighter y pos to avoid enemy hitting fighter while fighter is above him |
ozy | 16:4a1d916d97c5 | 102 | // code to check enemy collision |
ozy | 16:4a1d916d97c5 | 103 | // check in fight range check enemy fight move check fighter is not guarding check fighter is on gound |
ozy | 17:928edcdd1058 | 104 | if ((diff >= -10 && diff <= 10) && (_enemy.randomize_moves(lcd, input) == 1) && (buttonC.read() != 1) && (fighter_y == 34)) { |
ozy | 18:22bda659c70a | 105 | printf("enemy collision detected!\n"); |
ozy | 18:22bda659c70a | 106 | collision = 0; |
ozy | 16:4a1d916d97c5 | 107 | } |
ozy | 16:4a1d916d97c5 | 108 | // code to check fight collision |
ozy | 17:928edcdd1058 | 109 | // check in fight range check if kick or fight move is performed |
ozy | 17:928edcdd1058 | 110 | if ((diff >= -10 && diff <= 10) && ((buttonA.read() || buttonB.read()) == 1)) { |
ozy | 18:22bda659c70a | 111 | printf("fighter collision detected!\n"); |
ozy | 18:22bda659c70a | 112 | collision = 1; |
ozy | 17:928edcdd1058 | 113 | } |
ozy | 18:22bda659c70a | 114 | // printf("collision is %i\n", collision); |
ozy | 18:22bda659c70a | 115 | return collision; |
ozy | 13:eaf070d5f599 | 116 | } |
ozy | 8:e2e2eb4ea0ca | 117 | |
ozy | 18:22bda659c70a | 118 | |
ozy | 18:22bda659c70a | 119 | |
ozy | 18:22bda659c70a | 120 |