32 lines
1.2 KiB
C#
32 lines
1.2 KiB
C#
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<Sprite2D>(0);
|
|
walkingSound = GetChildren().First(node => node.Name == "WalkingSound") as AudioStreamPlayer2D;
|
|
walkingSound.Stream = GD.Load<AudioStream>("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();
|
|
}
|
|
} |