Files

118 lines
2.2 KiB
C
Raw Permalink Normal View History

2026-01-06 23:04:15 +01:00
#include "Arduino.h"
class Player
{
public:
2026-02-11 23:20:46 +01:00
Player(int, int, int);
int Id() const;
int Lives() const;
2026-01-06 23:04:15 +01:00
void Lives(int);
2026-02-11 23:20:46 +01:00
bool Alive() const;
2026-01-06 23:04:15 +01:00
void Kill();
2026-02-11 23:20:46 +01:00
void setColor(int, int, int);
2026-01-06 23:04:15 +01:00
void moveby(int);
2026-02-11 23:20:46 +01:00
void moveto(int);
void Spawn(int);
void updateState(bool tilted, bool wobbled, int time);
void startAttack(int time);
// public variables
2026-01-06 23:04:15 +01:00
int position;
2026-02-11 23:20:46 +01:00
int speed;
2026-01-06 23:04:15 +01:00
bool attacking;
2026-02-11 23:20:46 +01:00
bool captured;
2026-01-06 23:04:15 +01:00
int last_attack;
2026-02-11 23:20:46 +01:00
int color[3];
2026-01-06 23:04:15 +01:00
private:
2026-02-11 23:20:46 +01:00
int _id;
2026-01-06 23:04:15 +01:00
int _lives;
int _state;
2026-02-11 23:20:46 +01:00
int _lastState; // 0-> idle; 1-> move; 2 -> attack
2026-01-06 23:04:15 +01:00
int _attackDuration;
};
2026-02-11 23:20:46 +01:00
Player::Player(int id, int lives, int attackDuration){
2026-01-06 23:04:15 +01:00
_lives = lives;
2026-02-11 23:20:46 +01:00
_id = id;
2026-01-06 23:04:15 +01:00
2026-02-11 23:20:46 +01:00
attacking = false;
2026-01-06 23:04:15 +01:00
_attackDuration = attackDuration;
_lastState = 0; // start as idle;
2026-02-11 23:20:46 +01:00
captured = false;
}
void Player::setColor(int r, int g, int b){
color[0] = r;
color[1] = g;
color[2] = b;
}
int Player::Id() const
{
return _id;
2026-01-06 23:04:15 +01:00
}
void Player::updateState(bool tilted, bool wobbled, int time) {
// attack timed out
if(attacking && last_attack+_attackDuration < time) attacking = false;
// no longer attacking
if (!wobbled) attacking = false;
_lastState = 0;
if (tilted) _lastState = 1; // moving
if (wobbled) _lastState = 2; // attacking
}
void Player::startAttack(int time){
// if already attacking just return
if (attacking) return;
// I got tired of attacking, I should be idle or moving for a little before attacking again.
if (_lastState == 2) return;
last_attack = time;
attacking = true;
}
void Player::Lives(int n){
_lives = n;
}
2026-02-11 23:20:46 +01:00
bool Player::Alive() const{
2026-01-06 23:04:15 +01:00
return _lives > 0;
}
void Player::Kill(){
2026-02-11 23:20:46 +01:00
captured = true;
2026-01-06 23:04:15 +01:00
_lives--;
if (_lives < 0) _lives = 0;
}
2026-02-11 23:20:46 +01:00
void Player::Spawn(int pos){
2026-03-15 22:30:39 +01:00
if (_lives <= 0) {
// Serial.printf("Player %d has no life. Not Spawning\n", _id);
captured = true;
} else {
captured = false;
moveto(pos);
}
2026-02-11 23:20:46 +01:00
}
int Player::Lives() const{
2026-01-06 23:04:15 +01:00
return _lives;
}
void Player::moveby(int amount){
position += amount;
if (position<0) position=0;
}
void Player::moveto(int amount){
position = amount;
if (position<0) position=0;
}