Why does the lemming have a pointer to *Game
?
I would have expected the game to own all the lemmings so that there is no need for a pointer back to the game.
Instead of update_function
consider having just an enum and then switching on that enum that way you likely can use u8 to back that enum.
pub const Lemming = struct // 33 bytes now
{
list_index: u7,
action: LemmingAction, // enum(u8)
update_state: UpdateState, // enum(u8)
state: LemmingState, // 13 bytes
animation: LemmingAnimation, // 8 bytes
pos: Point, // (i32, i32) 8 bytes
delta_x: i32, // this could be a i8 too
}
I think this with MultiArrayList could mean that your update loop touches way less memory.
For flags
I would use a packed struct of booleans, but that is just for not having to deal with manual bitshifts and easier syntax reading/writing those flags.
Maybe you could use the packed struct to pack flags together with the death enum if there arenât more than 16 different ones enum(u4)
, that would spare another byte making the lemming struct 32 bytes which is always nice to have powers of 2.
const LemmingState = struct
{
const Death = enum(u4) { splat, boom, drown, ... };
const Data = packed struct {
can_climb: bool,
can_float: bool,
is_dying: bool,
is_dead: bool, // is dead could be part of the death enum as one of the states, instead there would be an alive state in the enum, that would give us 31 kinds of death
death: Death,
}; // 1 byte
born: u32,
fallen: i32 = 0,
data: Data,
excavated_pixels: u8 = 0,
explosion_timer: u8 = 0,
remaining_bricks: u8 = 0,
} // 12 bytes