Files
twang-mp/TWANG32/Player.h
2026-03-15 22:30:39 +01:00

118 lines
2.2 KiB
C++

#include "Arduino.h"
class Player
{
public:
Player(int, int, int);
int Id() const;
int Lives() const;
void Lives(int);
bool Alive() const;
void Kill();
void setColor(int, int, int);
void moveby(int);
void moveto(int);
void Spawn(int);
void updateState(bool tilted, bool wobbled, int time);
void startAttack(int time);
// public variables
int position;
int speed;
bool attacking;
bool captured;
int last_attack;
int color[3];
private:
int _id;
int _lives;
int _state;
int _lastState; // 0-> idle; 1-> move; 2 -> attack
int _attackDuration;
};
Player::Player(int id, int lives, int attackDuration){
_lives = lives;
_id = id;
attacking = false;
_attackDuration = attackDuration;
_lastState = 0; // start as idle;
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;
}
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;
}
bool Player::Alive() const{
return _lives > 0;
}
void Player::Kill(){
captured = true;
_lives--;
if (_lives < 0) _lives = 0;
}
void Player::Spawn(int pos){
if (_lives <= 0) {
// Serial.printf("Player %d has no life. Not Spawning\n", _id);
captured = true;
} else {
captured = false;
moveto(pos);
}
}
int Player::Lives() const{
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;
}