// // Created by hoelshare on 21.05.17. // #include "Player.hpp" #define min(a, b) ((a)<(b)?(a):(b)) #define max(a, b) ((a)>(b)?(a):(b)) // TODO set constants const int Player::magazineSize[] = {10, 25, 1}; const int Player::shotTime[] = {100, 20, 300}; const int Player::magazineReloadTime[] = {800, 400, 2000}; const int Player::damage[] = {25, 10, 100}; void Player::heal(unsigned short healthPoints) { unsigned short calcHP = this->currentHP + healthPoints; this->currentHP = min(this->maxHP, calcHP); updateHealthMenuItem(); } void Player::hit(unsigned short hitPoints) { float armorPercent = (this->currentArmor / (double)this->maxArmor) * this->maxArmorPercent; unsigned short playerHitPoints = (1 - armorPercent) * hitPoints; unsigned short armorHitPoints = armorPercent * hitPoints; short calcHP = this->currentHP - playerHitPoints; short calcArmor = this->currentArmor - armorHitPoints; this->currentHP = max(short(0), calcHP); this->currentArmor = max(short(0), calcArmor); updateHealthMenuItem(); if (!this->currentHP) { this->die(); } } void Player::updateHealthMenuItem() { if (this->healthMenuItem) { char buff[5]; snprintf(buff, sizeof(buff), "%d %", this->currentHP / this->maxHP * 100); String percentage = buff; delete[] buff; this->healthMenuItem->setText(percentage); } } void Player::updateArmorMenuItem() { if (this->armorMenuItem) { char buff[5]; snprintf(buff, sizeof(buff), "%d %", this->currentArmor / this->maxArmor * 100); String percentage = buff; delete[] buff; this->armorMenuItem->setText(percentage); // this->armorMenuItem->setImagePath(String("/ammo" + String(this->weaponIndex) + ".bmp")); } } void Player::updateAmmoMenuItem() { if (this->ammoMenuItem) { char buff[20]; snprintf(buff, sizeof(buff), "%d / %d", this->currentShot[this->weaponIndex], this->shotMagazine[this->weaponIndex]); String shotStr = buff; delete[] buff; this->healthMenuItem->setText(shotStr); } } void Player::reload() { // Magazine not empty // and current not full if (this->shotMagazine[this->weaponIndex] && this->currentShot[this->weaponIndex] < Player::magazineSize[this->weaponIndex]) { // Wait some time delay(Player::magazineReloadTime[this->weaponIndex]); // refill this->currentShot[weaponIndex] = min(Player::magazineSize[this->weaponIndex], this->shotMagazine[this->weaponIndex]) - this->currentShot[this->weaponIndex]; } } void Player::shot() { if(this->currentShot[this->weaponIndex]) { this->currentShot[this->weaponIndex]--; delay(Player::shotTime[this->weaponIndex]); } else { reload(); } } void Player::die() { for (unsigned char i = 0; i < 10; i++) { pixels.setPixelColor(1, pixels.Color(255, 0, 0)); pixels.setPixelColor(2, pixels.Color(255, 0, 0)); pixels.setPixelColor(3, pixels.Color(255, 0, 0)); pixels.setPixelColor(0, pixels.Color(255, 0, 0)); badge->setVibrator(true); delay(200); badge->setVibrator(false); pixels.setPixelColor(1, pixels.Color(0, 0, 0)); pixels.setPixelColor(2, pixels.Color(0, 0, 0)); pixels.setPixelColor(3, pixels.Color(0, 0, 0)); pixels.setPixelColor(0, pixels.Color(0, 0, 0)); pixels.show(); delay(200); } rebirth(); } void Player::rebirth() { this->currentHP = this->maxHP; this->currentArmor = 0; for(short i = 0; i < 3; i++) { this->currentShot[i] = this->magazineSize[i]; // TODO refill magazine } }