Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: Engine/Engine.cpp
- Revision:
- 9:fff2009f826e
- Parent:
- 8:5327418f823a
- Child:
- 10:8bf3713d9e9c
diff -r 5327418f823a -r fff2009f826e Engine/Engine.cpp --- a/Engine/Engine.cpp Sat Mar 23 15:36:56 2019 +0000 +++ b/Engine/Engine.cpp Sun Mar 24 19:22:50 2019 +0000 @@ -1,109 +1,107 @@ #include "Engine.h" -Engine::Engine(){} +Engine::Engine() {} -Engine::~Engine(){} +Engine::~Engine() {} void Engine::check_reset() { - if(_skater.get_reset_flag()) { - _moving_counter = 0; - _jump_counter = 0; - _direction = Left; - _level = 0; - _x = 40; - _y = 40; + if (_skater.get_reset_flag()) { + reset_skater(); _map.init(); _skater.set_reset_flag(false); wait(1); - } - } + } +} + +void Engine::reset_skater() { + _moving_counter = 0; + _jump_counter = 0; + _direction = Left; + _level = 0; + _x = 40; + _y = 40; +} void Engine::init() { - _moving_counter = 0; - _jump_counter = 0; - _direction = Left; - _level = 0; - _x = 40; - _y = 40; - - _map.init(); - srand(time(NULL)); - } + reset_skater(); + _map.init(); + srand(time(NULL)); +} void Engine::read_input(Gamepad &gamepad) { - _input.coord = gamepad.get_mapped_coord(); - _input.A_flag = gamepad.check_event(Gamepad::A_PRESSED); - } + _input.coord = gamepad.get_mapped_coord(); + _input.A_flag = gamepad.check_event(Gamepad::A_PRESSED); +} void Engine::process_y() { - if ( ( (_line_1.x_end < _x) && (_x < (_line_2.x_start - 6) ) ) && _y == 23 ) { - _fall_flag = true; - } else if ( ( (_line_2.x_end < _x) && (_x < (_line_3.x_start - 6) ) ) && _y == 23 ) { - _fall_flag = true; - } else if ( ( (_line_3.x_end < _x) && (_x < (_line_1.x_start - 6) ) ) && _y == 23 ) { - _fall_flag = true; - } + set_fall_flag(); + if (_fall_flag) { + _skater.fall(_fall_flag); + } else { + _skater.set_y_position( _input.A_flag, _jump_counter, _level ); + } + _fall_flag = _skater.get_fall_flag(); + _y = _skater.get_y_position(); + _jump_counter = _skater.get_jump_counter(); +} - if(_fall_flag) { - _skater.fall(_fall_flag); - } else { - _skater.set_y_position( _input.A_flag, _jump_counter, _level ); - } - - _fall_flag = _skater.get_fall_flag(); - _y = _skater.get_y_position(); - _jump_counter = _skater.get_jump_counter(); - } +void Engine::set_fall_flag() { + if (((_line_1.x_end < _x) && (_x < (_line_2.x_start - 6))) && _y == 23) { + _fall_flag = true; + } else if (((_line_2.x_end < _x) && (_x < (_line_3.x_start - 6))) && _y == 23) { + _fall_flag = true; + } else if (((_line_3.x_end < _x) && (_x < (_line_1.x_start - 6))) && _y == 23) { + _fall_flag = true; + } +} void Engine::process_x(int t) { - if ( _x < -10 || _x > 84 ) { + if ( _x < -10 || _x > 84 ) { _skater.set_reset_flag(true); - } else { - _skater.set_x_position( _input.coord.x, _moving_counter, _direction, _input.coord.y ); + } else { + _skater.set_x_position_and_sprite(_input.coord.x, + _moving_counter, + _direction, + _input.coord.y); _x = _skater.get_x_position(); _moving_counter = _skater.get_moving_counter(); - - if( (t % 6 == 0 || t % 8 == 0) && (_input.coord.x > -0.1) ) { - _moving_counter--; + if ((t % 6 == 0 || t % 8 == 0) && (_input.coord.x > -0.1)) { + _moving_counter--; } - } - } + } +} void Engine::process_sprite() { - _sprite = _skater.get_sprite_value(); - _direction = _skater.get_direction(); - } + _sprite = _skater.get_sprite_value(); + _direction = _skater.get_direction(); +} void Engine::find_level() { - if((_x >= 1 && _x <= 30 && _y < 15) || (_x >= 45 && _x <= 80 && _y < 15)){ + if ((_x >= 1 && _x <= 30 && _y < 15) || (_x >= 45 && _x <= 80 && _y < 15)) { _level = 1; - } else { + } else { _level = 0; - } - } + } +} void Engine::generate_map() { - _length_1 = (rand() %20)+10; - _length_2 = (rand() %20)+10; - _length_3 = (rand() %20)+10; - - _map.generate_line_1(_length_1); - _line_1 = _map.get_line_1(); - - _map.generate_line_2(_length_2); - _line_2 = _map.get_line_2(); - - _map.generate_line_3(_length_3); - _line_3 = _map.get_line_3(); - - } + _length_1 = (rand() %20) + 10; + _map.generate_line_1(_length_1); + _line_1 = _map.get_line_1(); + _length_2 = (rand() %20) + 10; + _map.generate_line_2(_length_2); + _line_2 = _map.get_line_2(); + _length_3 = (rand() %20) + 10; + _map.generate_line_3(_length_3); + _line_3 = _map.get_line_3(); +} void Engine::update_lcd(N5110 &lcd){ - _skate_sprite = _skater.get_sprite(_sprite); - lcd.drawSprite(_x,_y,17,10,(int *)_skate_sprite); - lcd.drawLine(_line_2.x_start,_line_2.y,_line_2.x_end,_line_2.y,FILL_BLACK); - lcd.drawLine(_line_1.x_start,_line_1.y,_line_1.x_end,_line_1.y,FILL_BLACK); - lcd.drawLine(_line_3.x_start,_line_3.y,_line_3.x_end,_line_3.y,FILL_BLACK); - } \ No newline at end of file + _skate_sprite = _skater.get_sprite(_sprite); + lcd.drawSprite(_x,_y,17,10,(int *)_skate_sprite); + lcd.drawLine(_line_2.x_start,_line_2.y,_line_2.x_end,_line_2.y,FILL_BLACK); + lcd.drawLine(_line_1.x_start,_line_1.y,_line_1.x_end,_line_1.y,FILL_BLACK); + lcd.drawLine(_line_3.x_start,_line_3.y,_line_3.x_end,_line_3.y,FILL_BLACK); +} \ No newline at end of file