Mousetrap

a sokoban-style puzzle game in c++ and sdl3, no engine, with hot reloading and levels inside levels.

this tab only has code i wrote myself. the course material gave me the base for the memory arenas, the hot reloading, the undo and the input buffer, so none of that is shown here. these are the four things in mousetrap i'm proudest of: the one-way tiles, the cheese that unlocks the goal portal, pushing a block into a hole, and a level that does something in the sublevel inside it.

one-way tiles

an edge tile is a normal floor tile that you can walk onto but never back off the way you came. there are four of them, one per direction, and they all live in the same function that decides whether a tile can be walked on, which gets the direction of the move as well as the tile:

//one-way edges
//y grows downward: yDir -1 is up, xDir -1 is left
switch (tile_id) {
    case TILE_ID_EDGE_DOWN:  if (yDir == -1) { return false; } break;
    case TILE_ID_EDGE_UP:    if (yDir == 1)  { return false; } break;
    case TILE_ID_EDGE_LEFT:  if (xDir == 1)  { return false; } break;
    case TILE_ID_EDGE_RIGHT: if (xDir == -1) { return false; } break;
}

the tileset says the edge tiles are walkable, and this switch takes that back for one direction only. the function is asked about blocks as well as the mouse, so the edges stop a pushed block in the same way.

eating the cheese to open the portal

cheese is not an entity. it's a tile in a second layer of the level that sits on top of the floor, and eating it is a command that changes that tile to nothing, so it goes into the undo history like any other step and an undone step puts the cheese back:

void TryEatCheese(Entity* entity, LevelData* level, CommandBuffer* buffer, uint32_t timestamp) {
    if (HasBehaviour(entity, Behaviour::IS_PLAYER) == false) { return; }
    if (GetOverlayID(level, entity->x, entity->y) != TILE_ID_CHEESE) { return; }

    Push(buffer, OverlayCommand(entity->x, entity->y, TILE_ID_CHEESE, 0), level, timestamp);
    PlaySFX(SFX_ID::EAT);
}

the purple portal only counts once no cheese is left, and standing on it isn't enough before that. the check is in IsLevelComplete further down, and it looks through every level inside the level too, so a piece of cheese hidden in a tunnel keeps the portal shut.

pushing a block into a hole

the mouse can't walk onto a pit, but a block can be pushed onto one, so the way across is to fill it with a block. the block is moved with a normal move. then, after the move, the game checks if the block ended up on a pit, and if it did it does two things at once: it removes the block and it turns the pit tile into a filled one that you can walk on.

void TryFillPit(Entity* entity, LevelData* level, CommandBuffer* buffer, uint32_t timestamp) {
    if (HasBehaviour(entity, Behaviour::IS_PLAYER)) { return; }
    if (GetEffectiveTileID(level, entity->x, entity->y) != TILE_ID_PIT) { return; }

    int pit_x = entity->x;
    int pit_y = entity->y;

    Push(buffer, RemoveCommand(entity), level, timestamp);
    Push(buffer, OverlayCommand(pit_x, pit_y, GetOverlayID(level, pit_x, pit_y), TILE_ID_PIT_FILLED), level, timestamp);
}

both commands get the same timestamp as the push that caused them, so undoing that one step brings the block back and puts the hole back in one go.

a level that does something in the sublevel

this is the part i like most. a level has a pointer to its tunnel and one to its parent, which makes a chain of up to three levels (MAX_TUNNEL_DEPTH). a mouse hole keeps the same position in every level of its chain, so going down one in the top level puts you on the matching hole in the one inside it. and the rules look at the whole chain, not just the level you're in. a pressure plate pressed in one level opens the gates in all of them:

bool any_plate_occupied = false;
for (int i = 0; i < chain_count; i++) {
    if (UpdatePlates(chain[i])) { any_plate_occupied = true; }
}

bool any_gate_changed = false;
for (int i = 0; i < chain_count; i++) {
    if (SetGates(chain[i], any_plate_occupied)) { any_gate_changed = true; }
}

every plate in the chain is checked first, and only then are the gates in every level set from the result, so a block pushed onto a plate in a tunnel can open the way in the level above it. undo has to work through all of this too. the course taught the command pattern, and what i added is that every command remembers which level it happened in, so a step taken inside a tunnel can be undone and redone from any level of the chain. the way the holes link up and the plates share their gates are still marked as work in progress in the code, because i'd like them to work differently later.

click anywhere or press esc to close