Mortal Kombat Game ELEC2645
Dependencies: mbed N5110 ShiftReg Joystick
GameEngine.cpp@12:b4477a312158, 2021-04-25 (annotated)
- Committer:
- ozy
- Date:
- Sun Apr 25 00:16:59 2021 +0000
- Revision:
- 12:b4477a312158
- Parent:
- 10:e83899f11e8a
- Child:
- 13:eaf070d5f599
Code before enemy move_around function
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 | 6:a1a7dc264fed | 7 | } |
ozy | 6:a1a7dc264fed | 8 | |
ozy | 7:737fb0c3dbef | 9 | void GameEngine::start(N5110 &lcd, DigitalIn &buttonA, DigitalIn &buttonB, DigitalIn &buttonC, DigitalIn &buttonD, AnalogIn &joy_v, AnalogIn &joy_h) { |
ozy | 12:b4477a312158 | 10 | // initializing and drawing enemy |
ozy | 12:b4477a312158 | 11 | _enemy.init(); |
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 | 7:737fb0c3dbef | 15 | lcd.refresh(); |
ozy | 10:e83899f11e8a | 16 | |
ozy | 7:737fb0c3dbef | 17 | } |
ozy | 7:737fb0c3dbef | 18 | |
ozy | 8:e2e2eb4ea0ca | 19 | void GameEngine::enemy_AI(N5110 &lcd) { |
ozy | 8:e2e2eb4ea0ca | 20 | int fighter_pos = _fighter.get_x(); |
ozy | 8:e2e2eb4ea0ca | 21 | printf("f pos = %i \n", fighter_pos); |
ozy | 8:e2e2eb4ea0ca | 22 | int enemy_pos = _enemy.get_x(); |
ozy | 8:e2e2eb4ea0ca | 23 | printf("e pos = %i \n", enemy_pos); |
ozy | 10:e83899f11e8a | 24 | |
ozy | 8:e2e2eb4ea0ca | 25 | int input = 0; |
ozy | 8:e2e2eb4ea0ca | 26 | int diff = fighter_pos - enemy_pos; |
ozy | 8:e2e2eb4ea0ca | 27 | printf("diff = %i \n", diff); |
ozy | 12:b4477a312158 | 28 | // 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 | 29 | if (diff > 0) { |
ozy | 8:e2e2eb4ea0ca | 30 | input = 1; |
ozy | 8:e2e2eb4ea0ca | 31 | } |
ozy | 8:e2e2eb4ea0ca | 32 | else {input = 0;} |
ozy | 8:e2e2eb4ea0ca | 33 | _enemy.draw(lcd, input); |
ozy | 12:b4477a312158 | 34 | if (diff >= -15 && diff <= 15){ // randomize enemy fight moves only in fighting range |
ozy | 12:b4477a312158 | 35 | _enemy.randomize_moves(lcd, input); |
ozy | 12:b4477a312158 | 36 | } |
ozy | 12:b4477a312158 | 37 | |
ozy | 8:e2e2eb4ea0ca | 38 | } |
ozy | 8:e2e2eb4ea0ca | 39 | |
ozy | 8:e2e2eb4ea0ca | 40 |