Input via Actions

pull/11/head
Sascha 2023-10-16 13:29:24 +07:00
parent 0e4da5e4fa
commit 311bdf8dba
2 changed files with 66 additions and 32 deletions

@ -3,14 +3,19 @@ using Godot;
namespace Scripts;
public partial class PlayerShip : Node3D {
[Export] private float moveVelocity = 10f;
[Export] private float rotVelocity = 15f;
[Export] private Node3D jet;
[Export] private RigidBody3D playerRb;
private const float jetTimerMax = 0.5f;
private float jetTimer = jetTimerMax;
private Vector3 moveDir;
private Vector3 rotDir = new(0,180,0);
private Vector3 rotDir;
private const string PLAYER_MOVE_FORWARD = "Player_Move_Forward";
private const string PLAYER_MOVE_BACKWARDS = "Player_Move_Backwards";
private const string PLAYER_MOVE_LEFT = "Player_Move_Left";
private const string PLAYER_MOVE_RIGHT = "Player_Move_Right";
private bool jetActive;
private bool JetActive {
@ -34,41 +39,39 @@ public partial class PlayerShip : Node3D {
if (jetTimer < 0) JetActive = false;
}
public override void _UnhandledKeyInput(InputEvent @event) {
if (@event is InputEventKey inputEventKey) {
JetActive = true;
switch (inputEventKey.Keycode) {
case Key.A:
moveDir.X -= moveVelocity;
rotDir.Z = -30f;
break;
case Key.D:
moveDir.X += moveVelocity;
rotDir.Z = 30f;
break;
case Key.W:
moveDir.Z -= moveVelocity;
rotDir.X = -15f;
break;
case Key.S:
moveDir.Z += moveVelocity;
rotDir.X = 15f;
break;
}
}
else {
moveDir = Vector3.Zero;
rotDir = new(0,180,0);
}
public override void _UnhandledInput(InputEvent @event) {
moveDir = Vector3.Zero;
rotDir = Vector3.Zero;
if (Input.IsAnythingPressed()) JetActive = true;
// Vector2 inputVector = Input.GetVector(PLAYER_MOVE_FORWARD, PLAYER_MOVE_BACKWARDS, PLAYER_MOVE_LEFT, PLAYER_MOVE_RIGHT);
// moveDir = new Vector3(inputVector.X, 0, inputVector.Y) * moveVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_FORWARD)) moveDir.Z -= moveVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_BACKWARDS)) moveDir.Z += moveVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_LEFT)) moveDir.X -= moveVelocity;
if (Input.IsActionPressed(PLAYER_MOVE_RIGHT)) moveDir.X += moveVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_FORWARD)) rotDir.X = -rotVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_BACKWARDS)) rotDir.X = +rotVelocity;
if (Input.IsActionJustPressed(PLAYER_MOVE_LEFT)) rotDir.Z = -rotVelocity * 2;
if (Input.IsActionJustPressed(PLAYER_MOVE_RIGHT)) rotDir.Z = +rotVelocity * 2;
if (Input.IsActionJustReleased(PLAYER_MOVE_LEFT) || Input.IsActionJustReleased(PLAYER_MOVE_RIGHT)) rotDir = Vector3.Zero;
}
private void HandleMovement(double delta) {
moveDir *= (float)delta;
KinematicCollision3D collision = playerRb.MoveAndCollide(moveDir);
if (collision?.GetCollider() is Node3D collider) GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
if (moveDir != Vector3.Zero) {
KinematicCollision3D collision = playerRb.MoveAndCollide(moveDir * (float)delta);
if (collision?.GetCollider() is Node3D collider) GD.Print($"{Name} collides with {collider.GetParentNode3D().Name}");
}
// Position += moveDir;
RotationDegrees = rotDir * (float)delta;
playerRb.RotationDegrees = rotDir;
// playerRb.Rotate(rotDir.Normalized(), rotVelocity * (float)delta);
// RotationDegrees = rotDir * (float)delta;
CheckBoundaries();
}

@ -25,3 +25,34 @@ window/size/viewport_height=1280
[dotnet]
project/assembly_name="Godot-space Shooter"
[input]
Player_Move_Forward={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":11,"pressure":0.0,"pressed":true,"script":null)
]
}
Player_Move_Backwards={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":12,"pressure":0.0,"pressed":true,"script":null)
]
}
Player_Move_Left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":13,"pressure":0.0,"pressed":true,"script":null)
]
}
Player_Move_Right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":14,"pressure":0.0,"pressed":true,"script":null)
]
}