Skip to content

Commit

Permalink
mouse follow works
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 26, 2024
1 parent accf91f commit 28e8bd6
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void level_fini(Levelp l)
{
TRACE_NO_INDENT();

FOR_ALL_THINGS(l, t) { thing_fini(l, t); }
FOR_ALL_THINGS_ALL_DEPTHS(l, t) { thing_fini(l, t); }
myfree(l);
}

Expand Down
4 changes: 2 additions & 2 deletions src/level_dungeon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ void level_dungeon_create_and_place(Levelp l, int z)
{
level_map_set(l, z,
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"x1.2x...........x..............x"
"x1..x...........x..............x"
"x...x=xx=x......x..xxxx==xxxx..x"
"x3.4x..x..x.....x..xxxx...xxx..x"
"x...x..x..x.....x..xxxx...xxx..x"
"x...x..xx..x....x..xxx.....=...x"
"x...=.xx....x...x...=......=...x"
"x...=..x.....x..x..xxx.....=...x"
Expand Down
62 changes: 47 additions & 15 deletions src/level_tick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "my_callstack.hpp"
#include "my_level.hpp"
#include "my_main.hpp"
#include "my_tp.hpp"

void level_tick(Levelp l)
{
Expand All @@ -17,7 +19,7 @@ void level_tick(Levelp l)
// A tick is running
//
level_tick_time_step(l);
} else if (l->_tick_begin_requested) {
} else if (l->tick_begin_requested) {
//
// Start the tick
//
Expand All @@ -38,8 +40,8 @@ void level_tick(Levelp l)
//
// End of tick?
//
if (l->_tick_end_requested) {
level_tick_end_requested(l);
if (l->tick_end_requested) {
level_tick_end(l);
}
}

Expand All @@ -60,8 +62,8 @@ void level_tick_time_step(Levelp l)
//
// Tick has surpassed its time
//
l->time_step = 1.0;
l->_tick_end_requested = true;
l->time_step = 1.0;
l->tick_end_requested = true;
}
}
}
Expand All @@ -70,10 +72,14 @@ void level_tick_body(Levelp l, float dt)
{
TRACE_NO_INDENT();

auto p = thing_player(l);
int player_speed = p ? p->speed : 100;
auto p = thing_player(l);
if (! p) {
return;
}

int player_speed = p->speed;

FOR_ALL_THINGS(l, t)
FOR_ALL_THINGS_Z_DEPTH(l, t, p->at.z)
{
// Tick 1 Tick 2
// =================== ===================
Expand All @@ -91,42 +97,68 @@ void level_tick_body(Levelp l, float dt)

thing_interpolate(l, t, t->thing_dt);

//
// If the thing tick has completed
if (t->thing_dt >= 1.0) {
// thing tick
t->thing_dt = 0.0;
thing_move_finish(l, t);
}
}
}

void level_tick_begin(Levelp l)
{
l->tick++;
l->_tick_begin_requested = false;
l->tick_begin_requested = false;
l->frame_begin = l->frame;
l->time_step = 0.0;
l->tick_in_progress = true;
l->requested_auto_scroll = true;

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

FOR_ALL_THINGS_Z_DEPTH(l, t, p->at.z)
{
if (thing_is_tickable(t)) {
thing_tick_begin(l, t);
}
}
}

void level_tick_begin_requested(Levelp l, const char *why)
{
TRACE_NO_INDENT();

l->_tick_begin_requested = true;
l->tick_begin_requested = true;

l->requested_move_up = false;
l->requested_move_left = false;
l->requested_move_keft = false;
l->requested_move_right = false;
}

void level_tick_end_requested(Levelp l)
void level_tick_end(Levelp l)
{
TRACE_NO_INDENT();

l->tick_in_progress = false;
l->_tick_end_requested = false;
l->time_step = 0;
l->tick_in_progress = false;
l->tick_end_requested = false;
l->time_step = 0;

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

FOR_ALL_THINGS_Z_DEPTH(l, t, p->at.z)
{
if (thing_is_tickable(t)) {
thing_tick_end(l, t);
}
}
}

bool level_tick_is_in_progress(Levelp l)
Expand Down
556 changes: 527 additions & 29 deletions src/my_enums.hpp

Large diffs are not rendered by default.

32 changes: 22 additions & 10 deletions src/my_level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ typedef struct Level_ {
//
// Player has moved.
//
bool _tick_begin_requested : 1;
bool _tick_end_requested : 1;
bool tick_begin_requested : 1;
bool tick_end_requested : 1;
//
// Player move request.
//
Expand Down Expand Up @@ -142,22 +142,34 @@ void level_scroll_warp_to_player(Levelp);
void level_tick_begin(Levelp);
void level_tick_begin_requested(Levelp, const char *);
void level_tick_body(Levelp, float dt);
void level_tick_end_requested(Levelp);
void level_tick_end(Levelp);
void level_tick(Levelp);
void level_tick_time_step(Levelp);

//
// Works on a copy of the level data, so things can move cells and we never
// walk anything twice.
// Walk all things on all Z depths
//
#define FOR_ALL_THINGS(_l_, _t_) \
static Level _l_copy_; \
_l_copy_ = *_l_; \
#define FOR_ALL_THINGS_ALL_DEPTHS(_l_, _t_) \
Thingp t; \
for (auto _id_ = 0; _id_ < 1 << THING_COMMON_ID_BITS; _id_++) \
if (_l_copy_.thing_body[ _id_ ].id) \
if ((_t_ = thing_find_optional(_l_, _l_copy_.thing_body[ _id_ ].id)))
if (_l_->thing_body[ _id_ ].id) \
if ((_t_ = thing_find_optional(_l_, _l_->thing_body[ _id_ ].id)))

//
// For all things at this Z depth
//
#define FOR_ALL_THINGS_Z_DEPTH(_l_, _t_, _z_) \
Thingp _t_; \
Tpp _tp_; \
for (auto _y_ = 0; _y_ < MAP_HEIGHT; _y_++) \
for (auto _x_ = 0; _x_ < MAP_WIDTH; _x_++) \
for (auto _slot_ = 0; _t_ = thing_and_tp_get(_l_, point3d(_x_, _y_, _z_), _slot_, &_tp_), _slot_ < MAP_SLOTS; \
_slot_++) \
if (_t_)

//
// For all things at a specific location
//
#define FOR_ALL_THINGS_AT(_l_, _t_, _p_) \
Thingp _t_; \
for (auto _slot_ = 0; _t_ = thing_get(_l_, _p_, _slot_), _slot_ < MAP_SLOTS; _slot_++) \
Expand Down
15 changes: 14 additions & 1 deletion src/my_thing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ typedef struct Thing_ {
//
point3d at;
//
// Old map co-ords used for interpolation when moving.
// Previous map co-ords. Does not change when the move finishes.
//
point3d old_at;
//
// Previous map co-ords used for interpolation when moving. Changes when
// the move finishes.
//
point3d moving_from;
//
// Direction
//
uint8_t dir;
Expand Down Expand Up @@ -103,6 +108,10 @@ typedef struct Thing_ {
// Keeps track of counters in the level this thing has modified.
//
uint8_t count[ THING_FLAG_MAX ];
//
// set when the player starts following the cursor path.
//
uint8_t is_following_a_path : 1;
} Thing;

Tpp thing_tp(Thingp t);
Expand All @@ -127,10 +136,14 @@ void thing_dir_up_set(Thingp, uint8_t);
void thing_fini(Levelp, Thingp);
void thing_interpolate(Levelp, Thingp, float dt);
void thing_move_to(Levelp, Thingp, point3d to);
void thing_follow_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);
void thing_player_move_reset(Levelp);
void thing_move_finish(Levelp, Thingp);
void thing_tick_begin(Levelp, Thingp);
void thing_tick_end(Levelp, Thingp);
void thing_pop(Levelp, Thingp);
void thing_push(Levelp, Thingp);
void thing_set_dir_from_delta(Thingp, int dx, int dy);
Expand Down
22 changes: 20 additions & 2 deletions src/thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ Thingp thing_init(Levelp l, Tpp tp, point3d at)
return nullptr;
}

t->at = at;
t->old_at = at;
t->at = at;
t->old_at = at;
t->moving_from = at;

t->pix_at.x = t->at.x * TILE_WIDTH;
t->pix_at.y = t->at.y * TILE_HEIGHT;
Expand Down Expand Up @@ -279,3 +280,20 @@ Thingp thing_find(Levelp l, ThingId id)

return t;
}

//
// Called at the start of each tick
//
void thing_tick_begin(Levelp l, Thingp t) { TRACE_NO_INDENT(); }

//
// Called at the end of each tick
//
void thing_tick_end(Levelp l, Thingp t)
{
TRACE_NO_INDENT();

if (t->is_following_a_path) {
thing_move_to_next(l, t);
}
}
Loading

0 comments on commit 28e8bd6

Please sign in to comment.