2026-03-17 23:25:30 +01:00
|
|
|
#include "engine.h"
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
bool Engine::_check_pad_ball_collision(Paddle &p) {
|
|
|
|
|
uint8_t ppos= p.get_position();
|
|
|
|
|
for (int p= ppos; p < ppos + PADDLE_LENGTH; p++) {
|
|
|
|
|
if (_ball.get_y() == p) {
|
|
|
|
|
return true;
|
2026-03-17 20:11:21 +01:00
|
|
|
}
|
2026-03-17 23:25:30 +01:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
void Engine::_print_score() {
|
|
|
|
|
Serial.print("P1: ");
|
|
|
|
|
Serial.print(_p1.get_score());
|
|
|
|
|
Serial.print(" - ");
|
|
|
|
|
Serial.print("P2: ");
|
|
|
|
|
Serial.print(_p2.get_score());
|
|
|
|
|
Serial.println();
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-18 18:12:38 +01:00
|
|
|
void Engine::run() {
|
2026-03-17 23:25:30 +01:00
|
|
|
_event= NONE;
|
|
|
|
|
_ball.move();
|
|
|
|
|
uint8_t bx= _ball.get_x();
|
|
|
|
|
uint8_t by= _ball.get_y();
|
2026-03-18 19:12:09 +01:00
|
|
|
|
|
|
|
|
// pad is 1 pixel far from the edge, so i need to calc this delta
|
|
|
|
|
if (bx <= 1) {
|
|
|
|
|
// score the point only if ball reached the edge
|
|
|
|
|
if (this -> _check_pad_ball_collision(_p1) && bx == 1) {
|
|
|
|
|
_ball.bounce_on_pad();
|
|
|
|
|
_event= P2_COLLISION;
|
|
|
|
|
}
|
|
|
|
|
else if (bx <= 0) {
|
2026-03-17 23:25:30 +01:00
|
|
|
// p2 scores
|
|
|
|
|
_p2.increase_score();
|
|
|
|
|
Serial.println("Player 2 Scores");
|
|
|
|
|
this -> _print_score();
|
|
|
|
|
_event= P2SCORE;
|
|
|
|
|
return;
|
2026-03-17 20:11:21 +01:00
|
|
|
}
|
2026-03-18 19:12:09 +01:00
|
|
|
}
|
|
|
|
|
else if (bx >= MATRIX_WIDTH-2) {
|
|
|
|
|
// score the point only if ball reached the edge
|
|
|
|
|
if (this -> _check_pad_ball_collision(_p2) && bx == MATRIX_WIDTH-2) {
|
2026-03-17 23:25:30 +01:00
|
|
|
_ball.bounce_on_pad();
|
2026-03-18 19:12:09 +01:00
|
|
|
_event= P1_COLLISION;
|
2026-03-17 23:25:30 +01:00
|
|
|
}
|
2026-03-18 19:12:09 +01:00
|
|
|
else if (bx >= MATRIX_WIDTH-1) {
|
2026-03-17 23:25:30 +01:00
|
|
|
// p1 scores
|
|
|
|
|
_p1.increase_score();
|
|
|
|
|
Serial.println("Player 1 Scores");
|
|
|
|
|
this -> _print_score();
|
|
|
|
|
_event= P1SCORE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (by == 0 || by == MATRIX_HEIGHT-1) {
|
|
|
|
|
_ball.bounce_on_sides();
|
|
|
|
|
_event= WALL_COLLISION;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
EngineEvents Engine::get_event() {
|
|
|
|
|
return _event;
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
void Engine::reset() {
|
|
|
|
|
_p1.reset();
|
|
|
|
|
_p2.reset();
|
|
|
|
|
_ball.reset_position();
|
|
|
|
|
}
|