added player animations for walking

This commit is contained in:
2022-12-11 19:07:17 +01:00
parent d3cc88ecd6
commit 6c57a23f85
5 changed files with 108 additions and 16 deletions

View File

@@ -7,18 +7,12 @@ public partial class player : CharacterBody2D
public string playerName;
[Export]
public int speed = 400;
public Vector2 velocity;
public override void _PhysicsProcess(double delta)
{
MoveAndCollide(new Vector2
(
Input.GetActionStrength("move_right")
- Input.GetActionStrength("move_left"),
Input.GetActionStrength("move_down")
- Input.GetActionStrength("move_up")
).LimitLength(1)
* speed * (float)delta
);
velocity = Input.GetVector("move_left", "move_right", "move_up", "move_down").LimitLength(1);
MoveAndCollide(velocity * speed * (float)delta);
}
public void ChangeProcess(bool process){ if(process) ProcessMode = ProcessModeEnum.Inherit; else ProcessMode = ProcessModeEnum.Disabled; }
public override void _Process(double delta)
@@ -29,9 +23,30 @@ public partial class player : CharacterBody2D
if (Input.IsActionJustPressed("move_left")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(-raylength, 0);
if (Input.IsActionJustPressed("move_down")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, raylength);
if (Input.IsActionJustPressed("move_up")) GetNode<RayCast2D>("ray_cast_2d").TargetPosition = new Vector2(0, -raylength);
//call event in raycasted object
if (Input.IsActionJustPressed("ui_accept") && GetNode<RayCast2D>("ray_cast_2d").IsColliding())
GetNode<RayCast2D>("ray_cast_2d").GetCollider().Call("OnInteraction", playerName);
//animation system (with controller support wcih cant get normalized vector)
var animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
if (velocity.Length() != 0)
animatedSprite.Play();
else
{
animatedSprite.Frame = 0;
animatedSprite.Stop();
}
if (Input.IsActionPressed("move_right") || Input.IsActionPressed("move_left"))
{
animatedSprite.Animation = "move_side";
animatedSprite.FlipH = velocity.x < 0.5;
animatedSprite.SpeedScale = Math.Abs(velocity.x);
}
else if (Input.IsActionPressed("move_up") || Input.IsActionPressed("move_down"))
{
if (velocity.y > 0.05) animatedSprite.Animation = "move_down";
if (velocity.y < 0.05) animatedSprite.Animation = "move_up";
animatedSprite.FlipH = false;
animatedSprite.SpeedScale = Math.Abs(velocity.y);
}
}
}