86 lines
1.8 KiB
C#
86 lines
1.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITFALL.Player.Movement;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.UX;
|
|
using kcp2k;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITFALL.UX
|
|
{
|
|
public class UXPlayerMovement : UXBase
|
|
{
|
|
[SerializeField] private VisualTreeAsset template;
|
|
[UXBindPath("movement-container")]
|
|
private VisualElement _container;
|
|
[Inject]
|
|
public IEntityMovement _movement;
|
|
private Pool<VisualElement> _pool;
|
|
private readonly Queue<VisualElement> activeElements = new();
|
|
private IPlayerEdgeClimbState _edgeClimbState;
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
_container.Clear();
|
|
_pool = new Pool<VisualElement>(Create,_Reset,8);
|
|
}
|
|
private VisualElement Create()
|
|
{
|
|
var visualElement = _container.Create(template);
|
|
visualElement.SetVisible(false);
|
|
return visualElement;
|
|
}
|
|
private static void _Reset(VisualElement obj)
|
|
{
|
|
obj.SetVisible(false);
|
|
}
|
|
protected override void OnPlayerInitialized(Entity obj)
|
|
{
|
|
base.OnPlayerInitialized(obj);
|
|
_movement.OnStateChanged += OnMovementStateChanged;
|
|
while (activeElements.TryDequeue(out var y))
|
|
{
|
|
_pool.Return(y);
|
|
}
|
|
}
|
|
|
|
private void OnMovementStateChanged(IEntityMovementState arg1, IEntityMovementState arg2)
|
|
{
|
|
if (arg2 is IPlayerEdgeClimbState x)
|
|
{
|
|
_edgeClimbState = x;
|
|
}
|
|
else
|
|
{
|
|
_edgeClimbState = null;
|
|
while (activeElements.TryDequeue(out var y))
|
|
{
|
|
_pool.Return(y);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_edgeClimbState is not null)
|
|
{
|
|
while (activeElements.TryDequeue(out var x))
|
|
{
|
|
_pool.Return(x);
|
|
}
|
|
foreach (var x in _edgeClimbState.ClosestPoint)
|
|
{
|
|
var container =_pool.Take();
|
|
activeElements.Enqueue(container);
|
|
container.SetVisible(true);
|
|
container.SetPosition(x);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|