clean logs and improve engine handling by removing hits and movment_delay from the loop to the engine
This commit is contained in:
@@ -10,48 +10,40 @@ bool Engine::_check_pad_ball_collision(Paddle &p) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Engine::_print_score() {
|
||||
Serial.print("P1: ");
|
||||
Serial.print(_p1.get_score());
|
||||
Serial.print(" - ");
|
||||
Serial.print("P2: ");
|
||||
Serial.print(_p2.get_score());
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void Engine::run() {
|
||||
// if (_event == P1SCORE || _event == P2SCORE) this -> _soft_reset();
|
||||
|
||||
_event= NONE;
|
||||
_ball.move();
|
||||
uint8_t bx= _ball.get_x();
|
||||
uint8_t by= _ball.get_y();
|
||||
|
||||
int8_t ball_dir= _ball.get_direction_x();
|
||||
// pad is 1 pixel far from the edge, so i need to calc this delta
|
||||
if (bx <= 1) {
|
||||
// check also if the ball is already moving away, to skip useless checks
|
||||
if (bx <= 1 && ball_dir < 0) {
|
||||
// score the point only if ball reached the edge
|
||||
if (this -> _check_pad_ball_collision(_p1) && bx == 1) {
|
||||
if (this -> _check_pad_ball_collision(_p1) && bx <= 1) {
|
||||
_ball.bounce_on_pad();
|
||||
_event= P1_COLLISION;
|
||||
_hits++;
|
||||
}
|
||||
else if (bx <= 0) {
|
||||
// p2 scores
|
||||
_p2.increase_score();
|
||||
Serial.println("Player 2 Scores");
|
||||
this -> _print_score();
|
||||
_event= P2SCORE;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (bx >= MATRIX_WIDTH-2) {
|
||||
else if (bx >= MATRIX_WIDTH-2 && ball_dir > 0) {
|
||||
// score the point only if ball reached the edge
|
||||
if (this -> _check_pad_ball_collision(_p2) && bx == MATRIX_WIDTH-2) {
|
||||
if (this -> _check_pad_ball_collision(_p2) && bx >= MATRIX_WIDTH-2) {
|
||||
_ball.bounce_on_pad();
|
||||
_event= P2_COLLISION;
|
||||
_hits++;
|
||||
}
|
||||
else if (bx >= MATRIX_WIDTH-1) {
|
||||
// p1 scores
|
||||
_p1.increase_score();
|
||||
Serial.println("Player 1 Scores");
|
||||
this -> _print_score();
|
||||
_event= P1SCORE;
|
||||
return;
|
||||
}
|
||||
@@ -61,14 +53,29 @@ void Engine::run() {
|
||||
_ball.bounce_on_sides();
|
||||
_event= WALL_COLLISION;
|
||||
}
|
||||
|
||||
if (_hits >= 6 && _ball_mv_delay >= 80) {
|
||||
_hits= 0;
|
||||
_ball_mv_delay -= 20;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t Engine::ball_movement_delay() {
|
||||
return _ball_mv_delay;
|
||||
}
|
||||
|
||||
|
||||
EngineEvents Engine::get_event() {
|
||||
return _event;
|
||||
}
|
||||
|
||||
void Engine::restart_ball() {
|
||||
_ball.reset_position();
|
||||
_ball_mv_delay= INITIAL_BALL_DELAY;
|
||||
}
|
||||
|
||||
void Engine::reset() {
|
||||
this -> restart_ball();
|
||||
_p1.reset();
|
||||
_p2.reset();
|
||||
_ball.reset_position();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user