ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Engine/Engine.cpp

Committer:
lewisgw
Date:
2019-03-24
Revision:
9:fff2009f826e
Parent:
8:5327418f823a
Child:
10:8bf3713d9e9c

File content as of revision 9:fff2009f826e:

#include "Engine.h"

Engine::Engine() {} 

Engine::~Engine() {}

void Engine::check_reset() {
  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() {
  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);
} 

void Engine::process_y() {
  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();
}
    
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 ) {
    _skater.set_reset_flag(true);
  } 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--;
    }
  }
}
    
void Engine::process_sprite() {
  _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)) {
    _level = 1;    
  } else {
    _level = 0;
  }
}
    
void Engine::generate_map() {
  _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);
}