Skip to main content

Power-Up Dependencies

Power-ups are great in isolation, but what if we need combinations of logic and behaviours?

Declaring a Power-Up Dependency

In the previous guide, we looked at writing a player movement system based purely on positions. One major drawback with that implementation is that if our player walks into, say, a wall, they'll pass straight through it!

Instead, let's make it so our player has realistic collision. We first need to automatically attach another power-up alongside ours - the CharacterController, and to do this, we use the Requires attribute.

@PowerUp
@Requires(CharacterController)
struct TopDownPlayerControls {
move_left: Key = Key.A
move_right: Key = Key.D
move_up: Key = Key.W
move_down: Key = Key.S
}

Let's revise our movement task to use this character controller. We add a new parameter for this power-up, but we can also get rid of our Transform power-up and Timestep state.

@Task
fn update_player_movement(
controls: &TopDownPlayerControls,
character_controller: &CharacterController,
input: &InputState,
) {
let direction = Vec2(
x = input.position_axis(controls.move_left, controls.move_right),
y = input.position_axis(controls.move_down, controls.move_up),
).normalise_or_zero()

character_controller.desired_velocity = direction * 5.0
}

Now, if we sync our project and jump into the game, we'll see that our character collides realistically with any physics objects in the scene!