ProjectDDD/Assets/_DDD/_Scripts/Restaurant/Environment/RestaurantEnvironment.cs

63 lines
2.8 KiB
C#

using System;
using Spine.Unity;
using Unity.VisualScripting;
using UnityEngine;
namespace DDD.Restaurant
{
public class RestaurantEnvironment : MonoBehaviour
{
private RestaurantPropLocation restaurantPropLocation;
private Collider _collider;
private Transform _rootObject;
private Transform _visualLook;
private Renderer _renderer;
public void Initialize(RestaurantPropLocation location)
{
EnvironmentDataEntry environmentDataEntry = DataManager.Instance.GetDataSo<EnvironmentDataAsset>().GetDataById(location.Id);
_collider = GetComponent<Collider>();
_rootObject = transform.Find(CommonConstants.RootObject);
_visualLook = _rootObject.Find(CommonConstants.VisualLook);
if (environmentDataEntry == null)
{
Debug.Assert(false, "environmentDataEntry is null");
}
if (environmentDataEntry.RendererType == RendererType.Sprite)
{
var spriteRenderer = _visualLook.AddComponent<SpriteRenderer>();
_renderer = spriteRenderer;
spriteRenderer.sprite = DataManager.Instance.GetSprite(environmentDataEntry.SpriteKey);
spriteRenderer.sortingOrder = 5;
//Material material = await AssetManager.LoadAssetAsync<Material>(DataConstants.BasePropSpriteMaterial);
//spriteRenderer.material = new Material(material);
}
else if (environmentDataEntry.RendererType == RendererType.Spine)
{
var skeletonAnimation = _visualLook.AddComponent<SkeletonAnimation>();
//var skeletonDataAsset = await AssetManager.LoadAssetAsync<SkeletonDataAsset>(environmentDataEntry.SkeletonDataName);
//skeletonAnimation.skeletonDataAsset = skeletonDataAsset;
var spineController = transform.AddComponent<SpineController>();
spineController.SetSkin(environmentDataEntry.SkinName);
spineController.PlayAnimation(environmentDataEntry.DefaultAnimationName, true);
_renderer = _visualLook.GetComponent<MeshRenderer>();
}
_collider.isTrigger = environmentDataEntry.IsTrigger == 1;
transform.position = new Vector3(location.Position.x, 0f, location.Position.y);
transform.localScale = Vector3.one * environmentDataEntry.Size;
// Interaction initialize
if (environmentDataEntry.InteractionType != InteractionType.None)
{
var interactionComponent = transform.AddComponent<RestaurantInteractionComponent>();
interactionComponent.InitializeInteraction(environmentDataEntry.InteractionType);
}
}
}
}