agent::chenergy(): Implement

This commit is contained in:
Petr Baudis 2011-11-27 04:01:58 +01:00
parent d120fcc938
commit 0398856635
3 changed files with 13 additions and 6 deletions

View file

@ -22,7 +22,7 @@ agent::move_dir(int dir_x, int dir_y)
if (dead) if (dead)
return false; return false;
energy -= world::move_cost; chenergy(world::move_cost);
class tile *t2 = &tile->tile_in_dir(dir_x, dir_y); class tile *t2 = &tile->tile_in_dir(dir_x, dir_y);
if (t2->agent) { if (t2->agent) {
@ -30,7 +30,7 @@ agent::move_dir(int dir_x, int dir_y)
class agent *a = t2->agent; class agent *a = t2->agent;
t2->on_agent_leave(*a); t2->on_agent_leave(*a);
/* Nom. */ /* Nom. */
energy += a->energy; chenergy(a->energy);
a->energy = 0; a->energy = 0;
} else { } else {
return false; return false;
@ -45,6 +45,14 @@ agent::move_dir(int dir_x, int dir_y)
return true; return true;
} }
void
agent::chenergy(int delta)
{
energy += delta;
if (energy <= 0)
die();
}
void void
agent::die(void) agent::die(void)
{ {
@ -66,9 +74,7 @@ void
agent::on_tick(void) agent::on_tick(void)
{ {
if (!dead) { if (!dead) {
energy += world::sun_energy; chenergy(world::sun_energy);
if (energy <= 0)
die();
} else { } else {
energy += world::dead_decay; energy += world::dead_decay;

View file

@ -26,6 +26,7 @@ public:
bool move_dir(int dir_x, int dir_y); bool move_dir(int dir_x, int dir_y);
void chenergy(int delta);
void die(void); void die(void);
void on_action_takes(void); void on_action_takes(void);

View file

@ -3,7 +3,7 @@
struct world { struct world {
const static int newborn_energy = 500; const static int newborn_energy = 500;
const static int move_cost = 10; const static int move_cost = -10;
const static int sun_energy = 1; const static int sun_energy = 1;
const static int dead_body_energy = 500; const static int dead_body_energy = 500;