summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-12-14 12:43:17 +0100
committerPaweł Redman <pawel.redman@gmail.com>2017-12-14 12:43:17 +0100
commit12143c33141a9c35e8eb01609062655f560b4bc1 (patch)
treea215c1cffa153c2cb8b32214fe74bbd99e99c239
parent171e12893048f29bddccf2de183a1dec2f0a00fa (diff)
Get rid of all the time parameters in game.
-rw-r--r--src/common.hpp3
-rw-r--r--src/game/game.cpp22
-rw-r--r--src/game/game.hpp41
-rw-r--r--src/game/units.cpp75
4 files changed, 71 insertions, 70 deletions
diff --git a/src/common.hpp b/src/common.hpp
index 8a86482..2d2af4a 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -183,14 +183,13 @@ namespace game {
class unit_t;
class state_t {
- double now, dt;
-
std::unordered_set<unit_t*> units;
std::unordered_set<unit_t*> selected_units;
std::unordered_set<unit_t*> awake_units;
public:
world::world_t world;
+ double now, dt;
void start(void);
void stop(void);
diff --git a/src/game/game.cpp b/src/game/game.cpp
index 5643b3a..c2c2c19 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -11,21 +11,21 @@ void state_t::start(void)
world.generator = worldgen;
- human = new human_t;
+ human = new human_t(this);
human->place(&world, v2f_t(0.5, 0.5));
units.insert(human);
- human = new human_t;
+ human = new human_t(this);
human->place(&world, v2f_t(1.5, 0.5));
units.insert(human);
- human = new human_t;
+ human = new human_t(this);
human->place(&world, v2f_t(2.5, 0.5));
units.insert(human);
- alien = new alien_t;
+ alien = new alien_t(this);
alien->place(&world, v2f_t(15.5, -2.5));
- alien = new alien_t;
+ alien = new alien_t(this);
alien->place(&world, v2f_t(14.5, -2.5));
- alien = new alien_t;
+ alien = new alien_t(this);
alien->place(&world, v2f_t(13.5, -2.5));
}
@@ -70,7 +70,7 @@ void state_t::command(v2f_t x)
if (unit->dead)
continue;
- unit->start_moving(snap, now);
+ unit->start_moving(snap);
}
}
@@ -109,19 +109,19 @@ void state_t::tick(double now_, double dt_)
enemy->awake = true;
awake_units.insert(enemy);
- enemy->wake(now, unit);
+ enemy->wake(unit);
}
- unit->think(now, dt);
+ unit->think();
}
for (auto i = std::begin(awake_units); i != std::end(awake_units);) {
if (now - (*i)->wake_time >= 5.0) {
(*i)->awake = false;
- (*i)->sleep(now);
+ (*i)->sleep();
i = awake_units.erase(i);
} else {
- (*i)->think(now, dt);
+ (*i)->think();
i++;
}
}
diff --git a/src/game/game.hpp b/src/game/game.hpp
index f6687dc..107bfbf 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -41,6 +41,7 @@ namespace game {
class unit_t : public world::entity_t {
protected:
+ game::state_t *game;
world::world_t *world;
world::cmodel_t make_cmodel(v2f_t at);
@@ -59,9 +60,9 @@ namespace game {
type_t type;
- unit_t(type_t type_);
+ unit_t(game::state_t *game_, type_t type_);
- virtual void think(double now, double dt) = 0;
+ virtual void think(void) = 0;
struct {
bool moving = false;
@@ -76,23 +77,23 @@ namespace game {
} move;
void place(world::world_t *world_, v2f_t x_);
- bool keep_moving(double now, double dt, double speed);
- bool start_moving(v2f_t dst_, double now);
+ bool keep_moving(double speed);
+ bool start_moving(v2f_t dst);
bool awake = false;
double wake_time = -INFINITY;
- virtual void wake(double now, unit_t *by_whom) = 0;
- virtual void sleep(double now) = 0;
+ virtual void wake(unit_t *by_whom) = 0;
+ virtual void sleep(void) = 0;
bool dead = false;
double death_time = -INFINITY;
int health = 1, max_health = 1;
- void damage(double now, int points);
- virtual void die(double now) = 0;
+ void damage(int points);
+ virtual void die() = 0;
const wchar_t *say_text;
double say_time = -INFINITY;
- void say(const wchar_t *wstr, double now);
+ void say(const wchar_t *wstr);
void render_to(render::state_t *render);
@@ -100,27 +101,27 @@ namespace game {
class human_t : public unit_t {
public:
- human_t();
+ human_t(game::state_t *game);
void render_to(render::state_t *render);
- void wake(double now, unit_t *by_whom);
- void sleep(double now);
- void think(double now, double dt);
- void die(double now);
+ void wake(unit_t *by_whom);
+ void sleep(void);
+ void think(void);
+ void die(void);
};
class alien_t : public unit_t {
double next_targetting = -INFINITY;
double next_attack = -INFINITY;
- void attack(double now, unit_t *target, float range);
+ void attack(unit_t *target, float range);
public:
- alien_t();
+ alien_t(game::state_t *game);
void render_to(render::state_t *render);
- void wake(double now, unit_t *by_whom);
- void sleep(double now);
- void think(double now, double dt);
- void die(double now);
+ void wake(unit_t *by_whom);
+ void sleep(void);
+ void think(void);
+ void die(void);
};
};
diff --git a/src/game/units.cpp b/src/game/units.cpp
index b4cb333..5563124 100644
--- a/src/game/units.cpp
+++ b/src/game/units.cpp
@@ -19,9 +19,10 @@ void unit_t::compute_bounds()
render_bounds[1] = x + render_size[1];
}
-unit_t::unit_t(unit_t::type_t type_) : entity_t(ET_UNIT)
+unit_t::unit_t(game::state_t *game_, unit_t::type_t type_) : entity_t(ET_UNIT)
{
type = type_;
+ game = game_;
}
void unit_t::render_to(render::state_t *render)
@@ -59,10 +60,10 @@ void unit_t::render_to(render::state_t *render)
render->debug_path(&move.path);
}
-void unit_t::say(const wchar_t *wstr, double now)
+void unit_t::say(const wchar_t *wstr)
{
say_text = wstr;
- say_time = now;
+ say_time = game->now;
std::cout << (void*)this << ": " << wstr << "\n";
}
@@ -78,17 +79,17 @@ void unit_t::place(world::world_t *world_, v2f_t x_)
link(world);
}
-bool unit_t::keep_moving(double now, double dt, double speed)
+bool unit_t::keep_moving(double speed)
{
float time;
if (!move.moving)
return false;
- if (move.blocked && now < move.next_attempt)
+ if (move.blocked && game->now < move.next_attempt)
return false;
- time = dt * speed;
+ time = game->dt * speed;
while (time > 0.0f) {
v2f_t delta, next, x_new;
@@ -122,10 +123,10 @@ bool unit_t::keep_moving(double now, double dt, double speed)
if (move.attempts_left) {
move.blocked = true;
move.attempts_left--;
- move.next_attempt = now + 0.2f;
+ move.next_attempt = game->now + 0.2f;
} else {
if ((x - move.dst).len() > 1.5f)
- say(text::unit_blocked, now);
+ say(text::unit_blocked);
move.moving = false;
}
break;
@@ -137,18 +138,18 @@ bool unit_t::keep_moving(double now, double dt, double speed)
return true;
}
-bool unit_t::start_moving(v2f_t dst_, double now)
+bool unit_t::start_moving(v2f_t dst)
{
if (!world) {
printf("unit_t::start_moving: entity is not linked\n");
return false;
}
- move.dst = dst_;
+ move.dst = dst;
move.path.clear();
if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) {
- say(text::unit_no_path, now);
+ say(text::unit_no_path);
move.moving = false;
return false;
}
@@ -162,19 +163,19 @@ bool unit_t::start_moving(v2f_t dst_, double now)
return true;
}
-void unit_t::damage(double now, int points)
+void unit_t::damage(int points)
{
health -= points;
if (health < 0) {
printf("%p died\n", this);
dead = true;
- death_time = now;
+ death_time = game->now;
cflags = 0;
- die(now);
+ die();
}
}
-human_t::human_t() : unit_t(UNIT_HUMAN)
+human_t::human_t(game::state_t *game) : unit_t(game, UNIT_HUMAN)
{
cflags = CF_HUMAN;
health = max_health = 20;
@@ -185,20 +186,20 @@ human_t::human_t() : unit_t(UNIT_HUMAN)
render_layer = -1;
}
-void human_t::wake(double now, unit_t *by_whom)
+void human_t::wake(unit_t *by_whom)
{
}
-void human_t::sleep(double now)
+void human_t::sleep(void)
{
}
-void human_t::think(double now, double dt)
+void human_t::think(void)
{
- keep_moving(now, dt, 4.0);
+ keep_moving(4.0);
}
-void human_t::die(double now)
+void human_t::die(void)
{
render_size[0] = v2f_t(-0.75f, -0.5f);
render_size[1] = v2f_t(+0.75f, +0.5f);
@@ -221,7 +222,7 @@ void human_t::render_to(render::state_t *render)
} else
render->render(&assets::human.dead, render_bounds);
- if (say_time + 5.0 > render->now) {
+ if (say_time + 5.0 > game->now) {
v2f_t text_pos;
float height;
@@ -235,7 +236,7 @@ void human_t::render_to(render::state_t *render)
unit_t::render_to(render);
}
-alien_t::alien_t() : unit_t(UNIT_ALIEN)
+alien_t::alien_t(game::state_t *game) : unit_t(game, UNIT_ALIEN)
{
cflags = CF_SOLID;
health = max_health = 4;
@@ -245,17 +246,17 @@ alien_t::alien_t() : unit_t(UNIT_ALIEN)
render_size[1] = v2f_t(+0.3f, +0.3f);
}
-void alien_t::wake(double now, unit_t *by_whom)
+void alien_t::wake(unit_t *by_whom)
{
- start_moving(by_whom->x, now);
- next_targetting = now + 0.4;
+ start_moving(by_whom->x);
+ next_targetting = game->now + 0.4;
}
-void alien_t::sleep(double now)
+void alien_t::sleep(void)
{
}
-void alien_t::die(double now)
+void alien_t::die(void)
{
}
@@ -295,11 +296,11 @@ static unit_t *find_target(world::world_t *world, v2f_t x, float r,
return nearest;
}
-void alien_t::attack(double now, unit_t *target, float range)
+void alien_t::attack(unit_t *target, float range)
{
world::trace_t trace;
- if (now < next_attack)
+ if (game->now < next_attack)
return;
if ((x - target->x).len() > range)
@@ -307,26 +308,26 @@ void alien_t::attack(double now, unit_t *target, float range)
trace = world->trace(x, target->x, CF_SOLID);
if (!trace.hit)
- target->damage(now, rand() % 6 + rand() % 6 + 2);
+ target->damage(rand() % 6 + rand() % 6 + 2);
- next_attack = now + 0.4;
+ next_attack = game->now + 0.4;
}
-void alien_t::think(double now, double dt)
+void alien_t::think(void)
{
- if (now > next_targetting) {
+ if (game->now > next_targetting) {
unit_t *target;
target = find_target(world, x, 5.0f, UNIT_HUMAN);
if (target) {
- attack(now, target, 0.75f);
- start_moving(target->x, now);
+ attack(target, 0.75f);
+ start_moving(target->x);
}
- next_targetting = now + 0.2;
+ next_targetting = game->now + 0.2;
}
- keep_moving(now, dt, 7.0);
+ keep_moving(7.0);
}
void alien_t::render_to(render::state_t *render)