69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using Godot;
|
|
using Godot.Bridge;
|
|
using Godot.NativeInterop;
|
|
|
|
[ScriptPath("res://Scripts/Puzzles/CratePush.cs")]
|
|
public partial class CratePush : AnimatableBody2D, Interacteable
|
|
{
|
|
public Vector2 start;
|
|
|
|
private Coroutine pushing;
|
|
|
|
[Export(PropertyHint.None, "")]
|
|
private AudioStream pushSound;
|
|
|
|
private static readonly List<Vector2> possible = new List<Vector2>
|
|
{
|
|
Vector2.Up,
|
|
Vector2.Left,
|
|
Vector2.Down,
|
|
Vector2.Right
|
|
};
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
start = base.GlobalPosition;
|
|
}
|
|
|
|
public void Interact()
|
|
{
|
|
if (pushing == null || pushing.done)
|
|
{
|
|
pushing = Coroutine.Start(Push(Player.instance.controlling.GlobalPosition.DirectionTo(base.GlobalPosition).Snapped(Vector2.One)));
|
|
}
|
|
else
|
|
{
|
|
Player.instance.canInput = true;
|
|
}
|
|
}
|
|
|
|
private IEnumerator Push(Vector2 direction)
|
|
{
|
|
if (possible.Contains(direction))
|
|
{
|
|
Vector2 p = base.GlobalPosition;
|
|
Vector2 e = base.GlobalPosition + direction * 20f;
|
|
if (Common.DoRaycast(p, e) == null)
|
|
{
|
|
if (pushSound != null)
|
|
{
|
|
Audio.PlaySound(pushSound);
|
|
}
|
|
Player.instance.controlling.StopMoving(Entity.Animations.Idle);
|
|
Player.instance.controlling.LookAt(base.GlobalPosition);
|
|
for (float a = 0f; a < 20f; a += Main.deltaTime)
|
|
{
|
|
base.GlobalPosition = p.Lerp(e, a / 20f);
|
|
yield return null;
|
|
}
|
|
base.GlobalPosition = e.Snapped(Vector2.One * 10f);
|
|
}
|
|
}
|
|
Player.instance.canInput = true;
|
|
}
|
|
|
|
}
|