using System.Collections.Generic; using System.Linq; using Godot; namespace Tutorial1.Scripts; public partial class Character : Node { [Export] private float speed = 300; private Sprite2D sprite2D; private AudioStreamPlayer2D walkingSound; public override void _Ready() { sprite2D = GetChild(0); walkingSound = GetChildren().First(node => node.Name == "WalkingSound") as AudioStreamPlayer2D; walkingSound.Stream = GD.Load("res://Audio/SFX_footstep01_01.wav"); } public override void _UnhandledInput(InputEvent @event) { if (@event is InputEventMouseButton { Pressed: true } buttonEvent) sprite2D.Position = buttonEvent.GlobalPosition; } public override void _Process(double delta) { float moveAmount = speed * (float)delta; Vector2 moveVector = new(); if (Input.IsKeyPressed(Key.W)) moveVector.Y -= moveAmount; if (Input.IsKeyPressed(Key.S)) moveVector.Y += moveAmount; if (Input.IsKeyPressed(Key.A)) moveVector.X -= moveAmount; if (Input.IsKeyPressed(Key.D)) moveVector.X += moveAmount; sprite2D.Position += moveVector; if (moveVector != Vector2.Zero && !walkingSound.Playing) walkingSound.Play(); } }