Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ADD health system

  • Loading branch information...
commit 6c9b84d817dc2c6cdfac09a74030cf7ecc6417a9 1 parent 3d2eb6d
@PowerKiKi authored
View
48 application/source/Actor.cpp
@@ -10,10 +10,12 @@ namespace Polukili
{
/*************************************************/
Actor::Actor(Level* level)
- : level(level), body(0), sprite(0), state(normal), powerFactor(1)
+ : level(level), body(0), sprite(0), state(normal), powerFactor(1), maxHealth(5), health(5)
{
Console::log(LOG_INFO, "Actor::Actor() - new actor");
- this->level->actors.push_back(this);
+ this->level->actors.push_back(this);
+
+ this->healthTimer.setPeriod(2000);
}
/*************************************************/
@@ -62,6 +64,17 @@ namespace Polukili
b2Vec2 pos = this->body->GetPosition();
this->sprite->setPosition(Constants::pixelsPerUnits * pos.x, Constants::pixelsPerUnits * pos.y);
this->sprite->draw();
+
+ // Draw health if needed
+ if (this->is(attacked))
+ {
+ this->sprite->drawHealth(this->health, this->maxHealth);
+
+ if (this->healthTimer.isExpired())
+ {
+ this->removeState(attacked);
+ }
+ }
}
/*************************************************/
@@ -149,12 +162,37 @@ namespace Polukili
{
return (this->state & state) == state;
}
+
+ /*************************************************/
+ void Actor::addState(ActorState state)
+ {
+ this->state = (ActorState)(this->state | state);
+ }
/*************************************************/
- void Actor::isAttackedBy(int attackPower)
+ void Actor::removeState(ActorState state)
{
- Console::log(LOG_INFO, "actor died");
- this->state = dead;
+ this->state = (ActorState)(this->state & ~state);
+ }
+
+ /*************************************************/
+ void Actor::isAttackedBy(int attackPower, Element attackElement)
+ {
+ Console::log(LOG_INFO, "actor attacked");
+ if (this->isWeak(attackElement))
+ attackPower = attackElement * 1.5;
+ if (this->isStrong(attackElement))
+ attackPower = attackElement * 0.5;
+
+ this->health -= attackPower;
+
+ this->healthTimer.reset();
+ this->addState(attacked);
+
+ if (this->health <= 0)
+ {
+ this->state = dead;
+ }
}
/*************************************************/
View
30 application/source/Actor.h
@@ -11,6 +11,7 @@
#include <ActorState.h>
#include <ActorAction.h>
#include <Timer.h>
+#include <PeriodicTimer.h>
using namespace std;
@@ -19,7 +20,7 @@ namespace Polukili
class Level;
/**
- * The highest parent for every object in games, eg: players, bullets, ennemies, levels...
+ * The highest parent for every object in games, eg: players, bullets, enemies, levels...
*/
class Actor
{
@@ -108,14 +109,24 @@ namespace Polukili
virtual int getImageHeight() const;
/**
- * Returns wether the actor is in the given state.
+ * Returns whether the actor is in the given state.
*/
bool is(ActorState state);
+
+ /**
+ * Add a state to the current state.
+ */
+ void addState(ActorState state);
+
+ /**
+ * Remove a state from the current state.
+ */
+ void removeState(ActorState state);
/**
* Remove energy to the actor and make it die eventually
*/
- void isAttackedBy(int attackPower);
+ void isAttackedBy(int attackPower, Element attackElement);
/**
* Sets the factor of power for the actor. The highest the factor, the biggest the actor (zoom on sprite) and the most powerful he is. Value 1 is default factor.
@@ -153,7 +164,17 @@ namespace Polukili
* The factor of power for the actor. The highest the factor, the biggest the actor (zoom on sprite) and the most powerful he is. Value 1 is default factor.
*/
float powerFactor;
+
+ /**
+ * The maximum health point.
+ */
+ int maxHealth;
+ /**
+ * The health point of this actor. If it reaches zero, he dies.
+ */
+ int health;
+
Timer timer;
private:
@@ -161,7 +182,8 @@ namespace Polukili
/**
* Current effect on the actor.
*/
- Effect* effect;
+ Effect* effect;
+ PeriodicTimer healthTimer;
};
}
View
1  application/source/ActorState.h
@@ -16,6 +16,7 @@ namespace Polukili
burning = 16,
collapsing = 32,
dying = 64,
+ attacked = 128,
};
}
View
5 application/source/Bullets/Bullet.cpp
@@ -13,6 +13,7 @@ namespace Polukili
Bullet::Bullet(Level* level)
: Actor(level)
{
+ this->maxHealth = 0;
this->level->bullets.push_back(this);
}
@@ -66,8 +67,8 @@ namespace Polukili
void Bullet::attacks(Actor* actor)
{
Console::log(LOG_INFO, "bullet attacks actor");
- actor->isAttackedBy(345);
- //this->state = dead;
+ actor->isAttackedBy(1, neutral);
+ this->state = dead;
}
/*************************************************/
View
9 application/source/Ennemies/Bee.cpp
@@ -78,15 +78,6 @@ namespace Polukili
}
-
- /*************************************************/
- void Bee::render()
- {
- this->sprite->setZoom(this->powerFactor);
- b2Vec2 pos = body->GetPosition();
- this->sprite->setPosition(Constants::pixelsPerUnits * pos.x, Constants::pixelsPerUnits * pos.y);
- this->sprite->draw();
- }
/*************************************************/
void Bee::nextStep()
View
5 application/source/Ennemies/Bee.h
@@ -25,11 +25,6 @@ namespace Polukili
virtual void initPhysic(const b2Vec2& position, float32 angle = 0);
-
- /**
- * Render the actor on screen according to current physic data and loaded sprite. Also render special effects if needed.
- */
- virtual void render();
/**
* Define the behavior of the bee for the next step. Apply forces/actions/modifications based on current context (wiimote inputs, state changes based on time, etc.)
View
24 application/source/Sprite.cpp
@@ -20,7 +20,7 @@ namespace Polukili
{
Console::log(LOG_INFO, "will destroy sprite");
}
-
+
/*************************************************/
void Sprite::draw()
{
@@ -28,6 +28,28 @@ namespace Polukili
}
/*************************************************/
+ void Sprite::drawHealth(int health, int maxHealth)
+ {
+ if (maxHealth <= 0)
+ return;
+
+ const f32 width = 50;
+ GRRLIB_Rectangle (this->x - this->referenceX, this->y - this->referenceY,
+ width * this->factor,
+ 5,
+ RGBA(255, 0, 0, 255),
+ 1
+ );
+
+ GRRLIB_Rectangle (this->x - this->referenceX, this->y - this->referenceY,
+ (float)health / (float)maxHealth * width * (float)this->factor,
+ 5,
+ RGBA(0, 255, 0, 255),
+ 1
+ );
+ }
+
+ /*************************************************/
void Sprite::setPosition(int x, int y)
{
this->x = x;
View
5 application/source/Sprite.h
@@ -34,6 +34,11 @@ namespace Polukili
void draw();
/**
+ * Draw the health bar on screen according to current positions.
+ */
+ void drawHealth(int health, int maxHealth);
+
+ /**
* Defines the current position
*/
void setPosition(int x, int y);
View
2  application/source/Timer.h
@@ -13,7 +13,7 @@ namespace Polukili
class Timer
{
public:
- //Timer is automaticaly started on creation
+ //Timer is automatically started on creation
Timer();
~Timer();
Please sign in to comment.
Something went wrong with that request. Please try again.