- updated hp regeneration method to look for negative hp regen values and if health ticks below 0 the unit dies

This commit is contained in:
Mark Vejvoda 2010-12-28 02:17:44 +00:00
parent 8d8afe78b5
commit 335749a15e
3 changed files with 34 additions and 17 deletions

View File

@ -25,6 +25,7 @@
#include "renderer.h" #include "renderer.h"
#include "game.h" #include "game.h"
#include "socket.h" #include "socket.h"
#include "sound_renderer.h"
#include "leak_dumper.h" #include "leak_dumper.h"
@ -1112,23 +1113,39 @@ void Unit::tick() {
} }
//regenerate hp //regenerate hp
hp+= type->getHpRegeneration(); if(type->getHpRegeneration() >= 0) {
if(hp>type->getTotalMaxHp(&totalUpgrade)){ hp+= type->getHpRegeneration();
hp= type->getTotalMaxHp(&totalUpgrade); if(hp>type->getTotalMaxHp(&totalUpgrade)){
hp= type->getTotalMaxHp(&totalUpgrade);
}
} }
// If we have negative regeneration then check if the unit should die
else {
bool decHpResult = decHp(-type->getHpRegeneration());
if(decHpResult) {
Unit::game->getWorld()->getStats()->die(getFactionIndex());
game->getScriptManager()->onUnitDied(this);
}
StaticSound *sound= this->getType()->getFirstStOfClass(scDie)->getSound();
if(sound != NULL && this->getFactionIndex() == Unit::game->getWorld()->getThisFactionIndex()) {
SoundRenderer::getInstance().playFx(sound);
}
}
//stop DamageParticles //stop DamageParticles
if(hp>type->getTotalMaxHp(&totalUpgrade)/2 ){ if(hp > type->getTotalMaxHp(&totalUpgrade) / 2) {
stopDamageParticles(); stopDamageParticles();
} }
//regenerate ep //regenerate ep
ep+= type->getEpRegeneration(); ep += type->getEpRegeneration();
if(ep>type->getTotalMaxEp(&totalUpgrade)){ if(ep>type->getTotalMaxEp(&totalUpgrade)){
ep= type->getTotalMaxEp(&totalUpgrade); ep= type->getTotalMaxEp(&totalUpgrade);
} }
} }
} }
int Unit::update2(){ int Unit::update2() {
progress2++; progress2++;
return progress2; return progress2;
} }
@ -1185,12 +1202,12 @@ bool Unit::repair(){
} }
//decrements HP and returns if dead //decrements HP and returns if dead
bool Unit::decHp(int i){ bool Unit::decHp(int i) {
if(hp==0){ if(hp == 0) {
return false; return false;
} }
hp-=i; hp -= i;
if(type == NULL) { if(type == NULL) {
char szBuf[4096]=""; char szBuf[4096]="";
@ -1199,12 +1216,12 @@ bool Unit::decHp(int i){
} }
//startDamageParticles //startDamageParticles
if(hp<type->getMaxHp()/2 ){ if(hp < type->getMaxHp() / 2 ) {
startDamageParticles(); startDamageParticles();
} }
//stop DamageParticles on death //stop DamageParticles on death
if(hp<=0){ if(hp <= 0) {
alive= false; alive= false;
hp=0; hp=0;
stopDamageParticles(); stopDamageParticles();
@ -1213,7 +1230,7 @@ bool Unit::decHp(int i){
return false; return false;
} }
string Unit::getDesc() const{ string Unit::getDesc() const {
Lang &lang= Lang::getInstance(); Lang &lang= Lang::getInstance();

View File

@ -747,7 +747,7 @@ Vec2i Map::findBestBuildApproach(const Unit *unit, Vec2i originalBuildPos,const
Vec2i end = pos + Vec2i(ut->getSize()); Vec2i end = pos + Vec2i(ut->getSize());
for(int i = start.x; i <= end.x; ++i) { for(int i = start.x; i <= end.x; ++i) {
for(int j = start.y; j <= end.y; ++j){ for(int j = start.y; j <= end.y; ++j) {
Vec2i testPos(i,j); Vec2i testPos(i,j);
if(isInUnitTypeCells(ut, originalBuildPos,testPos) == false) { if(isInUnitTypeCells(ut, originalBuildPos,testPos) == false) {
float distance = unitBuilderPos.dist(testPos); float distance = unitBuilderPos.dist(testPos);