#include "Player.h"
#include <Arduino.h>
#include <EEPROM.h>

Player::Player(void)
  : m_health(PLAYER_TOTLIVES), m_highScore(0), m_currentScore(0) {
  loadHighScore();
  reset();
}


int Player::loadHighScore(void) {
  m_highScore = EEPROM.get(PLAYER_HIGHSCORE_EEPROM_ADDR, m_highScore);
}

void Player::saveHighScore(void) {
  EEPROM.put(PLAYER_HIGHSCORE_EEPROM_ADDR, m_highScore);
}


void Player::reset(bool hard) {
  if (hard) {
    m_highScore = 0;
    saveHighScore();
  }

  m_health = PLAYER_TOTLIVES;
  m_currentScore = 0;
}

void Player::addScore(int points) {
  m_currentScore += points;
  
  if(m_currentScore > m_highScore){
    m_highScore = m_currentScore;
  }
}

void Player::takeDamage(int amount) {
  if (m_health == 0) {
    return;
  }

  m_health -= abs(amount);
  if (m_health <= 0) {
    saveHighScore();
    m_health = 0;
  }
}