Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Entity/Pickup.cs

63 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using Data;
using Item;
using Managers;
using Prefab;
using UnityEngine;
using Utils;
namespace Entity
{
public class Pickup : Entity
{
public ItemResource itemResource;
private void OnTriggerEnter2D(Collider2D other)
{
var entity = other.GetComponent<Character>();
if (entity == null) return;
if (entity.TryPickupItem(itemResource, 1) <= 0) Kill();
}
protected override void AutoBehave()
{
}
public override bool SetTarget(Vector3 pos)
{
return false;
}
public void Init(ItemDef itemDef)
{
itemResource = ItemResourceManager.Instance.GetItem(itemDef.defName);
// 预缓存枚举值(避免每次循环重复调用 Enum.GetValues
var states = Enum.GetValues(typeof(EntityState)).Cast<EntityState>().ToArray();
// 预初始化字典结构(减少内层循环的字典检查)
foreach (var state in states) bodyNodes.TryAdd(state, new Dictionary<Orientation, GameObject>());
var texture = itemResource.Icon;
if (texture.Count == 1)
{
var imageObj = Instantiate(AnimationUtils.ImagePrefab.gameObject, body.transform);
imageObj.transform.localPosition = Vector3.zero;
bodyNodes[EntityState.Idle][Orientation.Down] = imageObj;
var image = imageObj.GetComponent<ImagePrefab>();
image.SetSprite(texture[0]);
}
else if (texture.Count > 1)
{
var animatorObj = Instantiate(AnimationUtils.AnimatorPrefab.gameObject, body.transform);
animatorObj.transform.localPosition = Vector3.zero;
bodyNodes[EntityState.Idle][Orientation.Down] = animatorObj;
var animator = animatorObj.GetComponent<SpriteAnimator>();
animator.SetSprites(texture.ToArray());
}
SetBodyTexture(EntityState.Idle, Orientation.Down);
}
}
}