diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/game.cpp | 210 | ||||
-rw-r--r-- | src/game/game.hpp | 48 | ||||
-rw-r--r-- | src/game/units.cpp | 181 |
3 files changed, 230 insertions, 209 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp index 385a940..e58b8d5 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -2,215 +2,7 @@ namespace game { -static size_t selection_cookie = 1; - -enum { - ET_UNIT -}; - -class unit_t : public world::entity_t { -protected: - world::world_t *world; - - world::cmodel_t make_cmodel(v2f_t at) - { - world::cmodel_t cmodel; - - cmodel.bounds[0] = at + size[0]; - cmodel.bounds[1] = at + size[1]; - cmodel.cflags = cflags; - - return cmodel; - } - - void compute_bounds() - { - render_bounds[0] = x + render_size[0]; - render_bounds[1] = x + render_size[1]; - } - -public: - v2f_t x; - rectf_t size, render_size; - world::cflags_t cflags; - size_t selected = 0; - - unit_t() : entity_t(ET_UNIT) - { - } - - struct { - bool moving = false; - v2f_t dst; - float angle = 0.0f; - - std::list<v2f_t> path; - - bool blocked; - size_t attempts_left; - float next_attempt; - } move; - - const wchar_t *say_text; - double say_time = -INFINITY; - - void say(const wchar_t *wstr, double now) - { - say_text = wstr; - say_time = now; - std::cout << (void*)this << ": " << wstr << "\n"; - } - - void place(world::world_t *world_, v2f_t x_) - { - world = world_; - x = x_; - move.moving = false; - cflags = 1; - - unlink(); - cmodel = make_cmodel(x); - compute_bounds(); - link(world); - } - - void keep_moving(double now, double dt) - { - float time; - - if (!move.moving) - return; - - if (move.blocked && now < move.next_attempt) - return; - - time = dt * 10; - - while (time > 0.0f) { - v2f_t delta, next, x_new; - world::cmodel_t cmodel_next; - - if (!move.path.size()) { - move.moving = false; - break; - } - - next = *move.path.begin(); - delta = next - x; - move.angle = delta.angle(); - - if (delta.len() >= time) { - x_new = x + delta * time / delta.len(); - time -= delta.len(); - } else { - x_new = next; - time -= delta.len(); - move.path.pop_front(); - } - - cmodel_next = make_cmodel(x_new); - if (!world->test_rect(&cmodel_next, this)) { - x = x_new; - cmodel = cmodel_next; - continue; - } - - if (move.attempts_left) { - move.blocked = true; - move.attempts_left--; - move.next_attempt = now + 0.2f; - } else { - if ((x - move.dst).len() > 1.5f) - say(text::unit_blocked, now); - move.moving = false; - } - break; - } - - - unlink(); - compute_bounds(); - link(world); - } - - bool start_moving(v2f_t dst_, double now) - { - if (!world) { - printf("unit_t::start_moving: entity is not linked\n"); - return false; - } - - move.dst = dst_; - move.path.clear(); - - if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) { - say(text::unit_no_path, now); - move.moving = false; - return false; - } - - move.moving = true; - - move.blocked = false; - move.attempts_left = 10; - move.next_attempt = -INFINITY; - - return true; - } -}; - -class human_t : public unit_t { -public: - human_t() - { - size[0] = v2f_t(-0.4f, -0.4f); - size[1] = v2f_t(+0.4f, +0.4f); - render_size[0] = v2f_t(-0.5f, -1.0f); - render_size[1] = v2f_t(+0.5f, +0.5f); - } - - void render_to(render::state_t *render) - { - bool moving; - - if (selected == selection_cookie) - render->render_hlrect(render_bounds, sf::Color::Blue); - - moving = move.moving && !move.blocked; - - render->render((moving ? &assets::human.legs_walking : - &assets::human.legs_idle), render_bounds, move.angle); - render->render(&assets::human.body_idle, render_bounds, move.angle); - render->render(&assets::human.head_idle, render_bounds, move.angle); - - { - v2f_t x1; - static float t = 0; - world::trace_t trace; - - t += 0.02f; - - x1[0] = x[0] + cos(t) * 5; - x1[1] = x[1] + sin(t) * 5; - trace = world->trace(x, x1, 1); - render->render_arrow(x, trace.end, sf::Color::Green); - } - - if (move.moving && debug_draw_paths) - render->debug_path(&move.path); - - if (say_time + 5.0 > render->now) { - v2f_t text_pos; - float height; - - text_pos = render_bounds[0] + v2f_t(render_bounds.dim(0) / 2, -render_bounds.dim(1) * 0.1); - height = size.dim_min() * 0.20f; - render->render_text(text_pos, height, say_text, - render::ALIGN_CENTER_BOTTOM, - sf::Color::White); - } - } -}; +size_t selection_cookie = 1; void state_t::start(void) { diff --git a/src/game/game.hpp b/src/game/game.hpp index 5da6370..fa75da1 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -2,11 +2,17 @@ namespace game { enum { + ET_UNIT + }; + + enum { TILE_NONE, TILE_DIRT, TILE_WALL }; + extern size_t selection_cookie; + void worldgen(world::tile_t *tile, world::tile_index_t x, procgen::perlin_noise_t *perlin); @@ -22,4 +28,46 @@ namespace game { void load(void); } + + class unit_t : public world::entity_t { + protected: + world::world_t *world; + + world::cmodel_t make_cmodel(v2f_t at); + void compute_bounds(); + + public: + v2f_t x; + rectf_t size, render_size; + world::cflags_t cflags; + size_t selected = 0; + + unit_t(); + + struct { + bool moving = false; + v2f_t dst; + float angle = 0.0f; + + std::list<v2f_t> path; + + bool blocked; + size_t attempts_left; + float next_attempt; + } move; + + const wchar_t *say_text; + double say_time = -INFINITY; + + void say(const wchar_t *wstr, double now); + void place(world::world_t *world_, v2f_t x_); + void keep_moving(double now, double dt); + bool start_moving(v2f_t dst_, double now); + }; + + class human_t : public unit_t { + public: + human_t(); + void render_to(render::state_t *render); + }; }; diff --git a/src/game/units.cpp b/src/game/units.cpp new file mode 100644 index 0000000..268425d --- /dev/null +++ b/src/game/units.cpp @@ -0,0 +1,181 @@ +#include "game.hpp" + +namespace game { + +world::cmodel_t unit_t::make_cmodel(v2f_t at) +{ + world::cmodel_t cmodel; + + cmodel.bounds[0] = at + size[0]; + cmodel.bounds[1] = at + size[1]; + cmodel.cflags = cflags; + + return cmodel; +} + +void unit_t::compute_bounds() +{ + render_bounds[0] = x + render_size[0]; + render_bounds[1] = x + render_size[1]; +} + + +unit_t::unit_t() : entity_t(ET_UNIT) +{ +} + +void unit_t::say(const wchar_t *wstr, double now) +{ + say_text = wstr; + say_time = now; + std::cout << (void*)this << ": " << wstr << "\n"; +} + +void unit_t::place(world::world_t *world_, v2f_t x_) +{ + world = world_; + x = x_; + move.moving = false; + cflags = 1; + + unlink(); + cmodel = make_cmodel(x); + compute_bounds(); + link(world); +} + +void unit_t::keep_moving(double now, double dt) +{ + float time; + + if (!move.moving) + return; + + if (move.blocked && now < move.next_attempt) + return; + + time = dt * 10; + + while (time > 0.0f) { + v2f_t delta, next, x_new; + world::cmodel_t cmodel_next; + + if (!move.path.size()) { + move.moving = false; + break; + } + + next = *move.path.begin(); + delta = next - x; + move.angle = delta.angle(); + + if (delta.len() >= time) { + x_new = x + delta * time / delta.len(); + time -= delta.len(); + } else { + x_new = next; + time -= delta.len(); + move.path.pop_front(); + } + + cmodel_next = make_cmodel(x_new); + if (!world->test_rect(&cmodel_next, this)) { + x = x_new; + cmodel = cmodel_next; + continue; + } + + if (move.attempts_left) { + move.blocked = true; + move.attempts_left--; + move.next_attempt = now + 0.2f; + } else { + if ((x - move.dst).len() > 1.5f) + say(text::unit_blocked, now); + move.moving = false; + } + break; + } + + + unlink(); + compute_bounds(); + link(world); +} + +bool unit_t::start_moving(v2f_t dst_, double now) +{ + if (!world) { + printf("unit_t::start_moving: entity is not linked\n"); + return false; + } + + move.dst = dst_; + move.path.clear(); + + if (!world->find_path(x, move.dst, &cmodel, this, &move.path)) { + say(text::unit_no_path, now); + move.moving = false; + return false; + } + + move.moving = true; + + move.blocked = false; + move.attempts_left = 10; + move.next_attempt = -INFINITY; + + return true; +} + +human_t::human_t() +{ + size[0] = v2f_t(-0.4f, -0.4f); + size[1] = v2f_t(+0.4f, +0.4f); + render_size[0] = v2f_t(-0.5f, -1.0f); + render_size[1] = v2f_t(+0.5f, +0.5f); +} + +void human_t::render_to(render::state_t *render) +{ + bool moving; + + if (selected == selection_cookie) + render->render_hlrect(render_bounds, sf::Color::Blue); + + moving = move.moving && !move.blocked; + + render->render((moving ? &assets::human.legs_walking : + &assets::human.legs_idle), render_bounds, move.angle); + render->render(&assets::human.body_idle, render_bounds, move.angle); + render->render(&assets::human.head_idle, render_bounds, move.angle); + + { + v2f_t x1; + static float t = 0; + world::trace_t trace; + + t += 0.02f; + + x1[0] = x[0] + cos(t) * 5; + x1[1] = x[1] + sin(t) * 5; + trace = world->trace(x, x1, 1); + render->render_arrow(x, trace.end, sf::Color::Green); + } + + if (move.moving && debug_draw_paths) + render->debug_path(&move.path); + + if (say_time + 5.0 > render->now) { + v2f_t text_pos; + float height; + + text_pos = render_bounds[0] + v2f_t(render_bounds.dim(0) / 2, -render_bounds.dim(1) * 0.1); + height = size.dim_min() * 0.20f; + render->render_text(text_pos, height, say_text, + render::ALIGN_CENTER_BOTTOM, + sf::Color::White); + } +} + +} // namespace game |