257 lines
7.2 KiB
C#
257 lines
7.2 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created : 2018-2-29
|
|
// Description : 掉落物品
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "EntityDrop" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class EntityDrop : Entity
|
|
{
|
|
#region Field
|
|
|
|
public TextMeshPro nameRoot;
|
|
public SpriteRenderer spriteRenderer;
|
|
|
|
public float baseSpeed = 2f;
|
|
|
|
private int _itemIndex;
|
|
private int _itemLevel;
|
|
|
|
private float _maxY = 1.5f;
|
|
private float _disTime;
|
|
private bool _drop;
|
|
float _speed = 0f;
|
|
|
|
private Transform _qualityRoot;
|
|
private Collider _myCollider;
|
|
|
|
#endregion
|
|
|
|
#region Property
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public override EntityType entityType
|
|
{
|
|
get { return EntityType.Drop; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
public bool Init(EntityInfo info)
|
|
{
|
|
if (info != null)
|
|
{
|
|
this.id = info.guid;
|
|
this.position = info.position;
|
|
_itemIndex = info.itemId;
|
|
_itemLevel = info.level;
|
|
|
|
SetDropPoint(info.position);
|
|
}
|
|
this.InitView();
|
|
return true;
|
|
}
|
|
|
|
protected override void OnViewInitCompleted()
|
|
{
|
|
_qualityRoot = this.transform.Find("Quality");
|
|
SetProp(_itemIndex);
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
F.ObjectPool.SpawnPool.Despawn(this.gameObject);
|
|
}
|
|
|
|
private void SetProp(int itemId)
|
|
{
|
|
var prop = ItemProxy.Instance.GetStaticItem<ItemProp>(itemId);
|
|
if (prop != null)
|
|
{
|
|
IconProxy.Instance.SetSprite(spriteRenderer, prop.icon);
|
|
|
|
nameRoot.text = prop.name;
|
|
nameRoot.color = Item.GetQualityColor(prop.quality);
|
|
|
|
if (itemId == 557)
|
|
{
|
|
_qualityRoot.gameObject.SetActive(true);
|
|
for (int i = 0; i < _qualityRoot.childCount; i++)
|
|
{
|
|
_qualityRoot.GetChild(i).gameObject.SetActive(i == 4);
|
|
}
|
|
}
|
|
else if (itemId == 140)
|
|
{
|
|
_qualityRoot.gameObject.SetActive(true);
|
|
for (int i = 0; i < _qualityRoot.childCount; i++)
|
|
{
|
|
_qualityRoot.GetChild(i).gameObject.SetActive(i == 5);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
int qIndex = prop.quality - 3;
|
|
if (qIndex >= 0)
|
|
{
|
|
_qualityRoot.gameObject.SetActive(true);
|
|
|
|
for (int i = 0; i < _qualityRoot.childCount; i++)
|
|
{
|
|
_qualityRoot.GetChild(i).gameObject.SetActive(i == qIndex);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_qualityRoot.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
_speed = 10f;
|
|
}
|
|
|
|
public void Disappear()
|
|
{
|
|
_myCollider.enabled = false;
|
|
transform.position = Vector3.one * 5f;
|
|
gameObject.SetActive(false);
|
|
|
|
_maxY = 1.5f;
|
|
_disTime = 0f;
|
|
_drop = false;
|
|
|
|
EntityManager.Instance.RemoveEntity(this);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Unity
|
|
|
|
private void Awake()
|
|
{
|
|
_myCollider = GetComponent<Collider>();
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.name == "cha1")// other.transform != cha1 && other.transform.IsChildOf(cha1))
|
|
{
|
|
EntityMainPlayer.Instance.GetItem(_itemIndex, _itemLevel);
|
|
Disappear();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_disTime += Time.deltaTime;
|
|
if (_disTime > 10f)
|
|
{
|
|
Disappear();
|
|
}
|
|
else if (_disTime > 3f)
|
|
{
|
|
var pos = this.transform.position;
|
|
var vec = EntityMainPlayer.Instance.position - pos;
|
|
this.transform.position = pos + vec * _speed * Time.deltaTime;
|
|
_speed += 15f * Time.deltaTime;
|
|
}
|
|
|
|
if (!_drop)
|
|
{
|
|
Vector3 position = transform.position;
|
|
if (!_jump && position.y >= 0.05f)
|
|
{
|
|
_maxY -= 4.5f * Time.deltaTime;
|
|
transform.position += Vector3.up * _maxY * Time.deltaTime;
|
|
return;
|
|
}
|
|
_jump = true;
|
|
if (_curTime < _jumpTime)
|
|
{
|
|
_curTime += Time.deltaTime;
|
|
_tempPos = _homePos + (_a * Mathf.Pow(_curTime, 2) + _b * _curTime) * Vector3.up;
|
|
this.transform.position = _tempPos;
|
|
return;
|
|
}
|
|
else if (_jumpCount > 0)
|
|
{
|
|
_jumpCount -= 1;
|
|
_jumpHeight *= 0.5f;
|
|
_jumpTime *= 0.5f;
|
|
_curTime = 0f;
|
|
CalculateAAndB();
|
|
return;
|
|
}
|
|
|
|
if (position.y >= 0.05f)
|
|
{
|
|
//transform.position -= Vector3.up * 0.1f * Time.deltaTime;
|
|
//return;
|
|
}
|
|
|
|
_drop = true;
|
|
EntityMainPlayer.Instance.FindItem(transform);
|
|
|
|
_myCollider.enabled = true;
|
|
|
|
Vector3 a = Vector3.right * position.x;
|
|
transform.position = a + Vector3.forward * position.z;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Jump
|
|
|
|
public float jumpDuration = 0.5f; //弹起的时间
|
|
public float height = 0.1f; //弹起的高度
|
|
public int count = 1; //弹起的次数
|
|
|
|
private Vector3 _homePos = Vector3.zero;
|
|
private Vector3 _tempPos = Vector3.zero;
|
|
private float _a = 0f;
|
|
private float _b = 0f;
|
|
private float _curTime = 0f;
|
|
private int _jumpCount;
|
|
private float _jumpHeight;
|
|
private float _jumpTime;
|
|
private bool _jump;
|
|
|
|
void SetDropPoint(Vector3 point)
|
|
{
|
|
_jumpCount = count;
|
|
_jumpHeight = height;
|
|
_jumpTime = jumpDuration;
|
|
|
|
_jump = false;
|
|
_homePos = point;
|
|
_homePos.y = 0f;
|
|
CalculateAAndB();
|
|
}
|
|
|
|
private void CalculateAAndB()
|
|
{
|
|
_a = -4 * _jumpHeight / Mathf.Pow(_jumpTime, 2);
|
|
_b = _a * (-1) * _jumpTime;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|