Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6700 in orxonox.OLD for trunk/src/world_entities/world_entity.cc


Ignore:
Timestamp:
Jan 25, 2006, 3:10:17 PM (18 years ago)
Author:
bensch
Message:

trunk: Energy→Health

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/world_entities/world_entity.cc

    r6695 r6700  
    5151
    5252  this->obbTree = NULL;
    53   this->energyWidget = NULL;
    54   this->energyMax = 1.0f;
    55   this->energy = 1.0f;
     53  this->healthWidget = NULL;
     54  this->healthMax = 1.0f;
     55  this->health = 1.0f;
    5656  this->scaling = 1.0f;
    5757
     
    7777    delete this->obbTree;
    7878
    79   if (this->energyWidget != NULL)
    80     delete this->energyWidget;
     79  if (this->healthWidget != NULL)
     80    delete this->healthWidget;
    8181
    8282  // Delete the model (unregister it with the ResourceManager)
     
    105105  .defaultValues(3, NULL, 1.0f, 0);
    106106
    107   LoadParam(root, "maxEnergy", this, WorldEntity, setMaxEnergy)
    108   .describe("The Maximum energy that can be loaded onto this entity")
     107  LoadParam(root, "maxHealth", this, WorldEntity, setHealthMax)
     108  .describe("The Maximum health that can be loaded onto this entity")
    109109  .defaultValues(1, 1.0f);
    110110
    111   LoadParam(root, "energy", this, WorldEntity, setEnergy)
    112   .describe("The Energy the WorldEntity has at this moment")
     111  LoadParam(root, "health", this, WorldEntity, setHealth)
     112  .describe("The Health the WorldEntity has at this moment")
    113113  .defaultValues(1, 1.0f);
    114114}
     
    343343
    344344/**
    345  * @param energy the Energy to add.
    346  * @returns the energy left (this->energyMax - energy+this->energy)
    347  */
    348 float WorldEntity::addEnergy(float energy)
    349 {
    350   this->energy += energy;
    351   if (this->energy > this->energyMax)
    352   {
    353     float retEnergy = this->energyMax - this->energy;
    354     this->energy = this->energyMax;
    355     this->updateEnergyWidget();
    356     return retEnergy;
    357   }
    358   this->updateEnergyWidget();
     345 * @param health the Health to add.
     346 * @returns the health left (this->healthMax - health+this->health)
     347 */
     348float WorldEntity::increaseHealth(float health)
     349{
     350  this->health += health;
     351  if (this->health > this->healthMax)
     352  {
     353    float retHealth = this->healthMax - this->health;
     354    this->health = this->healthMax;
     355    this->updateHealthWidget();
     356    return retHealth;
     357  }
     358  this->updateHealthWidget();
    359359  return 0.0;
    360360}
    361361
    362362/**
    363  * @param energy the Energy to be removed
     363 * @param health the Health to be removed
    364364 * @returns 0.0 or the rest, that was not substracted (bellow 0.0)
    365365 */
    366 float WorldEntity::removeEnergy(float energy)
    367 {
    368   this->energy -= energy;
    369 
    370   if (this->energy < 0)
    371   {
    372     float retEnergy = -this->energy;
    373     this->energy = 0.0f;
    374     this->updateEnergyWidget();
    375     return retEnergy;
    376   }
    377   this->updateEnergyWidget();
     366float WorldEntity::decreaseHealth(float health)
     367{
     368  this->health -= health;
     369
     370  if (this->health < 0)
     371  {
     372    float retHealth = -this->health;
     373    this->health = 0.0f;
     374    this->updateHealthWidget();
     375    return retHealth;
     376  }
     377  this->updateHealthWidget();
    378378  return 0.0;
    379379
     
    381381
    382382/**
    383  * @param maxEnergy the maximal energy that can be loaded onto the entity.
    384  */
    385 void WorldEntity::setMaxEnergy(float maxEnergy)
    386 {
    387   this->energyMax = maxEnergy;
    388   if (this->energy > this->energyMax)
    389   {
    390     PRINTF(3)("new maxEnergy is bigger as the old energy. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName());
    391     this->energy = this->energyMax;
    392   }
    393   this->updateEnergyWidget();
    394 }
    395 
    396 /**
    397  * @brief creates the EnergyWidget
    398  *
    399  * since not all entities need an EnergyWidget, it is only created on request.
    400  */
    401 void WorldEntity::createEnergyWidget()
    402 {
    403   if (this->energyWidget == NULL)
    404   {
    405     this->energyWidget = new GLGuiBar();
    406     this->energyWidget->setSize2D(30,400);
    407     this->energyWidget->setAbsCoor2D(10,100);
    408 
    409     this->updateEnergyWidget();
     383 * @param maxHealth the maximal health that can be loaded onto the entity.
     384 */
     385void WorldEntity::setHealthMax(float healthMax)
     386{
     387  this->healthMax = healthMax;
     388  if (this->health > this->healthMax)
     389  {
     390    PRINTF(3)("new maxHealth is bigger as the old health. Did you really intend to do this for (%s::%s)\n", this->getClassName(), this->getName());
     391    this->health = this->healthMax;
     392  }
     393  this->updateHealthWidget();
     394}
     395
     396/**
     397 * @brief creates the HealthWidget
     398 *
     399 * since not all entities need an HealthWidget, it is only created on request.
     400 */
     401void WorldEntity::createHealthWidget()
     402{
     403  if (this->healthWidget == NULL)
     404  {
     405    this->healthWidget = new GLGuiBar();
     406    this->healthWidget->setSize2D(30,400);
     407    this->healthWidget->setAbsCoor2D(10,100);
     408
     409    this->updateHealthWidget();
    410410  }
    411411  else
    412     PRINTF(3)("Allready created the EnergyWidget for %s::%s\n", this->getClassName(), this->getName());
    413 }
    414 
    415 GLGuiWidget* WorldEntity::getEnergyWidget()
    416 {
    417   this->createEnergyWidget();
    418   return this->energyWidget;
    419 }
    420 
    421 /**
    422  * @param visibility shows or hides the energy-bar
     412    PRINTF(3)("Allready created the HealthWidget for %s::%s\n", this->getClassName(), this->getName());
     413}
     414
     415void WorldEntity::increaseHealthMax(float increaseHealth)
     416{
     417  this->healthMax += increaseHealth;
     418  this->updateHealthWidget();
     419}
     420
     421
     422GLGuiWidget* WorldEntity::getHealthWidget()
     423{
     424  this->createHealthWidget();
     425  return this->healthWidget;
     426}
     427
     428/**
     429 * @param visibility shows or hides the health-bar
    423430 * (creates the widget if needed)
    424431 */
    425 void WorldEntity::setEnergyWidgetVisibilit(bool visibility)
     432void WorldEntity::setHealthWidgetVisibilit(bool visibility)
    426433{
    427434    if (visibility)
    428435    {
    429       if (this->energyWidget != NULL)
    430         this->energyWidget->show();
     436      if (this->healthWidget != NULL)
     437        this->healthWidget->show();
    431438      else
    432439      {
    433         this->createEnergyWidget();
    434         this->updateEnergyWidget();
    435         this->energyWidget->show();
     440        this->createHealthWidget();
     441        this->updateHealthWidget();
     442        this->healthWidget->show();
    436443      }
    437444    }
    438     else if (this->energyWidget != NULL)
    439       this->energyWidget->hide();
    440 }
    441 
    442 /**
    443  * @brief updates the EnergyWidget
    444  */
    445 void WorldEntity::updateEnergyWidget()
    446 {
    447   if (this->energyWidget != NULL)
    448   {
    449     this->energyWidget->setMaximum(this->energyMax);
    450     this->energyWidget->setValue(this->energy);
     445    else if (this->healthWidget != NULL)
     446      this->healthWidget->hide();
     447}
     448
     449/**
     450 * @brief updates the HealthWidget
     451 */
     452void WorldEntity::updateHealthWidget()
     453{
     454  if (this->healthWidget != NULL)
     455  {
     456    this->healthWidget->setMaximum(this->healthMax);
     457    this->healthWidget->setValue(this->health);
    451458  }
    452459}
Note: See TracChangeset for help on using the changeset viewer.