Player movement

pull/11/head
Sascha 2023-10-12 15:10:36 +07:00
parent bb94db3121
commit 6a9046849f
4 changed files with 66 additions and 3 deletions

@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-dev.5">
<Project Sdk="Godot.NET.Sdk/4.2.0-dev.6">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>

@ -0,0 +1,6 @@
<Project Sdk="Godot.NET.Sdk/4.2.0-dev.5">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
</Project>

@ -1,6 +1,7 @@
[gd_scene load_steps=11 format=3 uid="uid://det8556rpxhbv"]
[gd_scene load_steps=12 format=3 uid="uid://det8556rpxhbv"]
[ext_resource type="Texture2D" uid="uid://x26n7stwftii" path="res://Textures/vehicle_playerShip_orange_dff.png" id="1_7sxki"]
[ext_resource type="Script" path="res://Scripts/PlayerShip.cs" id="1_nogem"]
[ext_resource type="Texture2D" uid="uid://cg6n1hh3lj7rn" path="res://Textures/tile_nebula_green_dff.png" id="2_sggc1"]
[ext_resource type="Script" path="res://Scripts/Background.cs" id="3_vny2k"]
@ -89,7 +90,8 @@ emission = Color(0.133333, 0.423529, 0.419608, 1)
[node name="Game" type="Node3D"]
[node name="PlayerShip" type="Node3D" parent="."]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 5)
transform = Transform3D(-0.87769, 0.479228, 7.67301e-08, 0.479228, 0.87769, -4.18955e-08, -8.74228e-08, 0, -1, 0, 0, 5)
script = ExtResource("1_nogem")
[node name="RigidBody3D" type="RigidBody3D" parent="PlayerShip"]
gravity_scale = 0.0

@ -0,0 +1,55 @@
using Godot;
public partial class PlayerShip : Node3D {
[Export] private float moveVelocity = 10f;
public override void _Ready() => Position = new(0, 0, 5);
public override void _Process(double delta) {
HandleMovement(delta);
}
private void HandleMovement(double delta) {
Vector3 moveDir = new();
Vector3 rotDir = new(0,180,0);
if (Input.IsKeyPressed(Key.A)) {
moveDir.X -= moveVelocity * (float)delta;
rotDir.Z = -30f;
}
if (Input.IsKeyPressed(Key.D)) {
moveDir.X += moveVelocity * (float)delta;
rotDir.Z = 30f;
}
if (Input.IsKeyPressed(Key.W)) {
moveDir.Z -= moveVelocity * (float)delta;
rotDir.X = -15f;
}
if (Input.IsKeyPressed(Key.S)) {
moveDir.Z += moveVelocity * (float)delta;
rotDir.X = 15f;
}
Position += moveDir;
RotationDegrees = rotDir;
CheckBoundaries();
}
private void CheckBoundaries() {
Vector3 correctedPos = Position;
correctedPos.X = Position.X switch {
< -3.3f => -3.3f,
> 3.3f => 3.3f,
_ => correctedPos.X
};
correctedPos.Z = Position.Z switch {
< -5.2f => -5.2f,
> 5.2f => 5.2f,
_ => correctedPos.Z
};
Position = correctedPos;
}
}