Started encpsulation of player logic
This commit is contained in:
89
TWANG32/Player.h
Normal file
89
TWANG32/Player.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
class Player
|
||||
{
|
||||
public:
|
||||
Player(int[3], int, int);
|
||||
void Lives(int);
|
||||
int Lives();
|
||||
void Kill();
|
||||
bool Alive();
|
||||
void moveto(int);
|
||||
void moveby(int);
|
||||
int color[3];
|
||||
int position;
|
||||
bool attacking;
|
||||
int last_attack;
|
||||
void updateState(bool tilted, bool wobbled, int time);
|
||||
void startAttack(int time);
|
||||
int _lastState; // 0-> idle; 1-> move; 2 -> attack
|
||||
private:
|
||||
int _lives;
|
||||
int _state;
|
||||
|
||||
int _attackDuration;
|
||||
};
|
||||
|
||||
Player::Player(int player_color[3], int lives, int attackDuration){
|
||||
_lives = lives;
|
||||
attacking = false;
|
||||
|
||||
color[0] = player_color[0];
|
||||
color[1] = player_color[1];
|
||||
color[2] = player_color[2];
|
||||
|
||||
_attackDuration = attackDuration;
|
||||
|
||||
_lastState = 0; // start as idle;
|
||||
}
|
||||
|
||||
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(){
|
||||
return _lives > 0;
|
||||
}
|
||||
|
||||
void Player::Kill(){
|
||||
_lives--;
|
||||
if (_lives < 0) _lives = 0;
|
||||
}
|
||||
|
||||
int Player::Lives(){
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user