(mut commands: Commands, asset_server: Res<AssetServer>)
| 20 | const JUMP_VELOCITY: f32 = 500.; |
| 21 | |
| 22 | fn init(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 23 | commands.spawn(Camera2d); |
| 24 | |
| 25 | commands.spawn(( |
| 26 | // From `leafwing-input-manager` |
| 27 | InputMap::default() |
| 28 | .with_axis(Action::Move, VirtualAxis::horizontal_arrow_keys()) |
| 29 | .with_axis( |
| 30 | Action::Move, |
| 31 | GamepadControlAxis::new(GamepadAxis::LeftStickX), |
| 32 | ) |
| 33 | .with(Action::Jump, KeyCode::Space) |
| 34 | .with(Action::Jump, GamepadButton::South), |
| 35 | // This state machine achieves a very rigid movement system. Consider a state machine for |
| 36 | // whatever parts of your player controller that involve discrete states. Like the movement |
| 37 | // in Castlevania and Celeste, and the attacks in a fighting game. |
| 38 | Grounded::Idle, |
| 39 | StateMachine::default() |
| 40 | // Whenever the player presses jump, jump |
| 41 | .trans::<Grounded, _>( |
| 42 | just_pressed(Action::Jump), |
| 43 | Falling { |
| 44 | velocity: JUMP_VELOCITY, |
| 45 | }, |
| 46 | ) |
| 47 | // When the player hits the ground, idle |
| 48 | .trans::<Falling, _>(grounded, Grounded::Idle) |
| 49 | // When the player is grounded, set their movement direction |
| 50 | .trans_builder( |
| 51 | value_unbounded(Action::Move), |
| 52 | |trans: Trans<Grounded, _>| { |
| 53 | let value = trans.out; |
| 54 | |
| 55 | match value { |
| 56 | value if value > 0.5 => Grounded::Right, |
| 57 | value if value < -0.5 => Grounded::Left, |
| 58 | _ => Grounded::Idle, |
| 59 | } |
| 60 | }, |
| 61 | ), |
| 62 | Sprite::from_image(asset_server.load("player.png")), |
| 63 | Transform::from_xyz(500., 0., 0.), |
| 64 | )); |
| 65 | } |
| 66 | |
| 67 | #[derive(Actionlike, Clone, Eq, Hash, PartialEq, Reflect, Debug)] |
| 68 | enum Action { |
nothing calls this directly
no test coverage detected