2026-03-17 23:25:30 +01:00
|
|
|
#include "paddle.h"
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
void Paddle::move_pad_up() {
|
2026-03-19 19:38:10 +01:00
|
|
|
if (_pos_y > 0) {
|
|
|
|
|
_pos_y -= 1;
|
2026-03-17 23:25:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void Paddle::move_pad_down() {
|
2026-03-19 19:38:10 +01:00
|
|
|
if (_pos_y + _height < MATRIX_HEIGHT) {
|
|
|
|
|
_pos_y += 1;
|
2026-03-17 23:25:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-19 18:26:27 +01:00
|
|
|
void run_paddle() {
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
uint8_t Paddle::get_position() {
|
2026-03-19 19:38:10 +01:00
|
|
|
return _pos_y;
|
2026-03-17 23:25:30 +01:00
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
bool Paddle::is_human() {
|
|
|
|
|
return _human;
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
void Paddle::increase_score() {
|
|
|
|
|
if (_score <= 9) _score += 1;
|
|
|
|
|
}
|
2026-03-17 20:11:21 +01:00
|
|
|
|
2026-03-17 23:25:30 +01:00
|
|
|
uint8_t Paddle::get_score() {
|
|
|
|
|
return _score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Paddle::reset() {
|
|
|
|
|
_score= 0;
|
|
|
|
|
}
|
2026-03-18 19:00:50 +01:00
|
|
|
|
2026-03-19 18:26:27 +01:00
|
|
|
bool Paddle::check_pad_movement() {
|
|
|
|
|
// redefine me
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-18 19:00:50 +01:00
|
|
|
|
2026-03-19 18:49:58 +01:00
|
|
|
bool Paddle::check_pad_movement(Ball &ball) {
|
|
|
|
|
// redefine me
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-03-19 19:38:10 +01:00
|
|
|
uint8_t Paddle::get_skills() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2026-03-19 18:49:58 +01:00
|
|
|
|
2026-03-19 18:26:27 +01:00
|
|
|
bool HumanPaddle::check_pad_movement() {
|
|
|
|
|
bool need_refresh= false;
|
|
|
|
|
if (digitalRead(_pin_btn_top) == LOW) {
|
|
|
|
|
this -> move_pad_up();
|
2026-03-18 19:00:50 +01:00
|
|
|
need_refresh= true;
|
|
|
|
|
}
|
2026-03-19 18:26:27 +01:00
|
|
|
else if (digitalRead(_pin_btn_bottom) == LOW) {
|
|
|
|
|
this -> move_pad_down();
|
2026-03-18 19:00:50 +01:00
|
|
|
need_refresh= true;
|
|
|
|
|
}
|
|
|
|
|
return need_refresh;
|
|
|
|
|
}
|
2026-03-19 18:26:27 +01:00
|
|
|
|
2026-03-19 18:49:58 +01:00
|
|
|
bool BotPaddle::check_pad_movement(Ball &ball) {
|
2026-03-19 19:38:10 +01:00
|
|
|
uint8_t ball_y= ball.get_y();
|
|
|
|
|
int8_t ball_dir= ball.get_direction_x();
|
|
|
|
|
|
|
|
|
|
// ball is moving left and pad is on right, do not move
|
|
|
|
|
if (ball_dir < 0 && _pos_x > MATRIX_WIDTH / 2) return false;
|
|
|
|
|
// ball is moving right and pad is on left, do not move
|
|
|
|
|
else if (ball_dir > 0 && _pos_x < MATRIX_WIDTH / 2) return false;
|
|
|
|
|
|
|
|
|
|
uint8_t ball_x= ball.get_x();
|
|
|
|
|
int8_t ball_distance= ball_x - _pos_x;
|
|
|
|
|
if (ball_distance < 0) ball_distance *= -1;
|
|
|
|
|
switch (this -> get_skills()) {
|
|
|
|
|
case 1:
|
|
|
|
|
if (ball_distance > MATRIX_WIDTH / 2 - 2) return false;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
if (ball_distance > MATRIX_WIDTH / 2 - 1) return false;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
if (ball_distance > MATRIX_WIDTH / 2) return false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 18:49:58 +01:00
|
|
|
// TODO BotPaddle movement logics
|
|
|
|
|
// on higher difficult level i could also check the ball direction
|
|
|
|
|
// or at lover difficulty level i could also check the distance from the pad and move only when the ball si near
|
2026-03-19 19:38:10 +01:00
|
|
|
|
|
|
|
|
for (uint8_t py= _pos_y; py < _pos_y+PADDLE_LENGTH; py++) {
|
2026-03-19 18:49:58 +01:00
|
|
|
// don't move if ball is already centered to the pad
|
2026-03-19 19:38:10 +01:00
|
|
|
if (py == ball_y) continue;
|
|
|
|
|
else if (_pos_y - ball_y >= 0) this -> move_pad_up();
|
2026-03-19 18:49:58 +01:00
|
|
|
else this -> move_pad_down();
|
|
|
|
|
}
|
2026-03-19 18:26:27 +01:00
|
|
|
}
|
2026-03-19 19:38:10 +01:00
|
|
|
|
|
|
|
|
uint8_t BotPaddle::get_skills() {
|
|
|
|
|
return _skills;
|
|
|
|
|
}
|