52 lines
1.9 KiB
C#
52 lines
1.9 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
[Service(typeof(IBuildToolPreviewManager))]
|
|
public class BuildToolPreviewManager : GameSystem, IBuildToolPreviewManager
|
|
{
|
|
[InjectService]
|
|
private ITileSpace _tileSpace;
|
|
|
|
[InjectService]
|
|
private ISignalBus _signalBus;
|
|
|
|
public BuildToolPreviewManager(IServiceLocator serviceLocator) : base(serviceLocator)
|
|
{
|
|
}
|
|
|
|
public void PlayPlacementAnimationAndBuild(BuildToolPreview preview, BuildingDefinition definition, TileRect rect, Directions orientation)
|
|
{
|
|
_ = PlayPlacementAnimationAndBuildAsync(preview, definition, rect, orientation);
|
|
}
|
|
|
|
private async UniTask PlayPlacementAnimationAndBuildAsync(BuildToolPreview preview, BuildingDefinition definition, TileRect rect, Directions orientation)
|
|
{
|
|
_signalBus.Raise(new BuildingPlacementAnimationStartedSignal(definition, rect, orientation));
|
|
|
|
var animatedObject = Object.Instantiate(preview.PreviewObject);
|
|
|
|
preview.ClearPreviewObject();
|
|
|
|
var placementAnimation = animatedObject.GetComponent<BuildingPlacementAnimation>();
|
|
if (placementAnimation)
|
|
{
|
|
var finalPosition = _tileSpace.GetRectWorldCenter(rect);
|
|
var finalRotation = orientation.ToQuaternion();
|
|
|
|
await placementAnimation.PlayAsync(definition, animatedObject.transform.position, finalPosition, finalRotation);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"Prefab '{definition.name}' has no {nameof(BuildingPlacementAnimation)} component");
|
|
}
|
|
|
|
_signalBus.Raise(new BuildingPlacementAnimationCompletedSignal(definition, rect, orientation));
|
|
|
|
await UniTask.NextFrame();
|
|
|
|
Object.Destroy(animatedObject);
|
|
}
|
|
}
|
|
} |