Skip to content

Commit

Permalink
thing move path pop
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 25, 2024
1 parent 139b339 commit accf91f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/game_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@ uint8_t game_mouse_down(class Game *g, int x, int y, uint32_t button)
DBG("Game mouse down");
TRACE_AND_INDENT();

if (wid_some_recent_event_occurred()) {
return false;
}

if (! g) {
DBG2("Game mouse down; no game or not started");
return false;
}

if (wid_some_recent_event_occurred()) {
DBG2("Game mouse down; some recent wid event occurred");
auto l = game_level_get(g);
if (! l) {
return false;
}

auto player = thing_player(l);
if (! player) {
return false;
}

if (thing_move_to_next(l, player)) {
return true;
}

return false;
}

Expand Down
4 changes: 1 addition & 3 deletions src/level_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ void thing_player_move_delta(Levelp l, int dx, int dy, int dz)

point3d to(t->at.x + dx, t->at.y + dy, t->at.z + dz);
if (thing_can_move_to(l, t, to)) {
thing_move(l, t, to);

level_tick_begin_requested(l, "player moved");
thing_move_to(l, t, to);
}

thing_player_move_reset(l);
Expand Down
3 changes: 2 additions & 1 deletion src/my_thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void thing_dir_tr_set(Thingp, uint8_t);
void thing_dir_up_set(Thingp, uint8_t);
void thing_fini(Levelp, Thingp);
void thing_interpolate(Levelp, Thingp, float dt);
void thing_move(Levelp, Thingp, point3d to);
void thing_move_to(Levelp, Thingp, point3d to);
void thing_player_map_center(Levelp);
void thing_player_move_accum(Levelp, bool up, bool down, bool left, bool right);
void thing_player_move_delta(Levelp, int dx, int dy, int dz);
Expand All @@ -137,6 +137,7 @@ void thing_set_dir_from_delta(Thingp, int dx, int dy);
void thing_update(Levelp, Thingp);

bool thing_can_move_to(Levelp, Thingp, point3d to);
bool thing_move_to_next(Levelp l, Thingp t);
bool thing_player_move_request(Levelp, bool up, bool down, bool left, bool right);
bool thing_is_dir_down(Thingp t);
bool thing_is_dir_tr(Thingp t);
Expand Down
57 changes: 56 additions & 1 deletion src/thing_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void thing_set_dir_from_delta(Thingp t, int dx, int dy)
}
}

void thing_move(Levelp l, Thingp t, point3d to)
void thing_move_to(Levelp l, Thingp t, point3d to)
{
if (level_is_oob(l, to)) {
return;
Expand All @@ -203,6 +203,10 @@ void thing_move(Levelp l, Thingp t, point3d to)
t->at = to;

thing_push(l, t);

if (thing_is_player(t)) {
level_tick_begin_requested(l, "player moved");
}
}

bool thing_can_move_to(Levelp l, Thingp t, point3d to)
Expand Down Expand Up @@ -321,3 +325,54 @@ void thing_pop(Levelp l, Thingp t)
}
}
}

//
// Return true if there is a move to pop.
//
static bool thing_move_path_pop(Levelp l, Thingp t, point *out)
{
TRACE_NO_INDENT();

point3d p(t->pix_at.x / TILE_WIDTH, t->pix_at.y / TILE_HEIGHT, t->at.z);

auto aip = thing_ai(l, t);
if (! aip) {
return false;
}

if (! aip->move_path.size) {
return false;
}

*out = aip->move_path.points[ 0 ];

for (int index = 0; index < aip->move_path.size - 1; index++) {
aip->move_path.points[ index ] = aip->move_path.points[ index + 1 ];
}
aip->move_path.size--;

return true;
}

//
// Move to the next path on the popped path if it exits.
//
bool thing_move_to_next(Levelp l, Thingp t)
{
TRACE_NO_INDENT();

int z = t->at.z;

point move_next;
if (! thing_move_path_pop(l, t, &move_next)) {
return false;
}

point3d move_to(move_next.x, move_next.y, z);
if (! thing_can_move_to(l, t, move_to)) {
return false;
}

thing_move_to(l, t, move_to);
return true;
}

0 comments on commit accf91f

Please sign in to comment.