Сцена игрока + начальная реализация перемещения
This commit is contained in:
commit
42db203fca
95 changed files with 1816 additions and 0 deletions
45
scripts/Player.cs
Normal file
45
scripts/Player.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class Player : CharacterBody2D
|
||||
{
|
||||
[Export] public const float Speed = 300.0f;
|
||||
|
||||
// Get the gravity from the project settings to be synced with RigidBody nodes.
|
||||
public float Gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
|
||||
|
||||
protected AnimatedSprite2D Sprite;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Vector2 velocity = Velocity;
|
||||
|
||||
// Get the input direction and handle the movement/deceleration.
|
||||
// As good practice, you should replace UI actions with custom gameplay actions.
|
||||
Vector2 direction = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
||||
if (direction != Vector2.Zero)
|
||||
{
|
||||
velocity.X = direction.X * Speed;
|
||||
Sprite.Play("walk");
|
||||
Sprite.FlipH = direction.X switch
|
||||
{
|
||||
> 0 => false,
|
||||
< 0 => true,
|
||||
_ => Sprite.FlipH
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
|
||||
Sprite.Play("default");
|
||||
}
|
||||
|
||||
Velocity = velocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue