338 lines
11 KiB
C#
338 lines
11 KiB
C#
using Thousandto.Core.Asset;
|
|
using UnityEngine;
|
|
using Thousandto.Cfg.Data;
|
|
using Thousandto.Code.Center;
|
|
using Thousandto.Code.Global;
|
|
using Thousandto.Plugins.Common.UniScene;
|
|
|
|
namespace Thousandto.Code.Logic
|
|
{
|
|
//技能对象
|
|
public class DropItem : Character
|
|
{
|
|
#region//私有变量
|
|
//是否已经发送了删除消息
|
|
private bool _postDelete = false;
|
|
//动画播放器
|
|
private DropItemAnimPlayer _animPlayer = null;
|
|
//生存时间
|
|
private float _lifeTimer = 0f;
|
|
//最大生存时间
|
|
private const float MaxLifeTime = 10f;
|
|
private int _cfgID = 0;
|
|
private Vector2 _targetPos = Vector2.zero;
|
|
private ulong _ownerID = 0;
|
|
private int _quality = QualityCode.White;
|
|
private int _iconId = 0;
|
|
#endregion
|
|
|
|
#region//属性
|
|
public int Quality
|
|
{
|
|
get
|
|
{
|
|
return _quality;
|
|
}
|
|
}
|
|
public int Icon
|
|
{
|
|
get
|
|
{
|
|
return _iconId;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//初始化,加载Skin,卸载处理
|
|
protected override FSkinBase OnSetupSkin()
|
|
{
|
|
FSkinModel skin = GameCenter.LuaSystem.Adaptor.CreateFSkinModel(FSkinTypeCode.Custom);
|
|
skin.SetLayer(LayerUtils.SummonObj);
|
|
return skin;
|
|
}
|
|
|
|
protected override bool OnInitializeAfter(EntityInitInfo baseInfo)
|
|
{
|
|
base.OnInitializeAfter(baseInfo);
|
|
var info = baseInfo as DropItemInitInfo;
|
|
SyncInfo(info);
|
|
ShowHUD();
|
|
return true;
|
|
}
|
|
|
|
protected override void OnUninitializeBefore()
|
|
{
|
|
base.OnUninitializeBefore();
|
|
|
|
HideHUD();
|
|
|
|
if(_animPlayer != null)
|
|
{
|
|
_animPlayer.Destory();
|
|
}
|
|
ItemDropEffectSystem.Remove(_ownerID);
|
|
}
|
|
|
|
public void SyncInfo(DropItemInitInfo info)
|
|
{
|
|
_propMoudle = new BaseProperty();
|
|
_propMoudle.ID = info.ID;
|
|
_propMoudle.Owner = this;
|
|
_cfgID = (int)info.CfgID;
|
|
_ownerID = info.OwnerID;
|
|
_targetPos = info.TargetPos;
|
|
_lifeTimer = 0f;
|
|
BodyScale = 0.8f;
|
|
|
|
_quality = QualityCode.White;
|
|
DeclareItem item = DeclareItem.Get(_cfgID);
|
|
if (item != null)
|
|
{
|
|
_quality = item.Color;
|
|
_propMoudle.Name = item.Name;
|
|
_iconId = item.Icon;
|
|
}
|
|
else
|
|
{
|
|
DeclareEquip equip = DeclareEquip.Get(_cfgID);
|
|
if (equip != null)
|
|
{
|
|
_quality = equip.Quality;
|
|
_propMoudle.Name = equip.Name;
|
|
_iconId = equip.Icon;
|
|
}
|
|
}
|
|
|
|
int bodyVfx = 0;
|
|
switch (_quality)
|
|
{
|
|
case QualityCode.Violet: //紫色
|
|
bodyVfx = 406;
|
|
break;
|
|
case QualityCode.Orange: //金色
|
|
bodyVfx = 406;
|
|
break;
|
|
case QualityCode.Golden: //金色
|
|
bodyVfx = 401;
|
|
break;
|
|
case QualityCode.Red:// 红色
|
|
bodyVfx = 402;
|
|
break;
|
|
case QualityCode.Pink:// 粉色
|
|
bodyVfx = 403;
|
|
break;
|
|
case QualityCode.DarkGolden:// 暗金
|
|
bodyVfx = 404;
|
|
break;
|
|
case QualityCode.Colorful:// 七彩
|
|
bodyVfx = 405;
|
|
break;
|
|
}
|
|
//开始播放动画
|
|
_animPlayer = new DropItemAnimPlayer(this, _targetPos, bodyVfx);
|
|
var owner = GameCenter.GameSceneSystem.FindEntity<Character>(_ownerID);
|
|
if (owner != null)
|
|
{
|
|
SetDirection2d(Position2d - _targetPos, false, false);
|
|
}
|
|
}
|
|
|
|
private void PostDelete()
|
|
{
|
|
if (!_postDelete)
|
|
{
|
|
var baseScene = Scene as BaseScene;
|
|
if (baseScene != null)
|
|
{
|
|
baseScene.DeleteEntity(ID);
|
|
}
|
|
_postDelete = true;
|
|
}
|
|
}
|
|
|
|
protected override void OnUpdate(float dt)
|
|
{
|
|
if (_animPlayer != null)
|
|
{
|
|
_animPlayer.Update();
|
|
if (_animPlayer.IsFinish)
|
|
{
|
|
PostDelete();
|
|
}
|
|
}
|
|
|
|
//更新生存时间
|
|
_lifeTimer += dt;
|
|
if (_lifeTimer >= MaxLifeTime)
|
|
{
|
|
PostDelete();
|
|
}
|
|
base.OnUpdate(dt);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region//私有类
|
|
//掉落物品的动画播发器
|
|
private class DropItemAnimPlayer
|
|
{
|
|
#region//常量
|
|
private const float DropTime = 0.3f;
|
|
private const float WaitTime = 3f;
|
|
private const float EndFlyTime = 0.75f;
|
|
#endregion
|
|
|
|
#region//私有变量
|
|
private DropItem _host = null;
|
|
private Vector2 _targetPos = Vector2.zero;
|
|
private DropAnimState _curState = DropAnimState.DropDown;
|
|
private Vector3[] _animCurve = null;
|
|
private int _bodySfx = 0;
|
|
private FGameObjectVFX _waitVfx = null;
|
|
private float _animTimer = 0f;
|
|
private Vector3 _flyStartPos = Vector3.zero;
|
|
#endregion
|
|
|
|
#region//属性
|
|
public bool IsFinish
|
|
{
|
|
get
|
|
{
|
|
return _curState == DropAnimState.Finish;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//构造函数
|
|
//初始并且开始播放动画
|
|
public DropItemAnimPlayer(DropItem host, Vector2 targetPos, int bodySfx)
|
|
{
|
|
_host = host;
|
|
_targetPos = targetPos;
|
|
_bodySfx = bodySfx;
|
|
ChangeState(DropAnimState.DropDown);
|
|
}
|
|
#endregion
|
|
|
|
#region//公有函数
|
|
//更新
|
|
public void Update()
|
|
{
|
|
switch (_curState)
|
|
{
|
|
case DropAnimState.DropDown:
|
|
{
|
|
_animTimer += Time.deltaTime;
|
|
if (_animTimer <= DropTime)
|
|
{
|
|
_host.SetPosition(iTween.Interp(_animCurve, _animTimer / DropTime));
|
|
}
|
|
else
|
|
{
|
|
_host.RayCastToGroundXOZ(_targetPos);
|
|
ChangeState(DropAnimState.Wait);
|
|
}
|
|
}
|
|
break;
|
|
case DropAnimState.Wait:
|
|
{
|
|
_animTimer += Time.deltaTime;
|
|
if (_animTimer > WaitTime)
|
|
{
|
|
ChangeState(DropAnimState.FlyToPlayer);
|
|
}
|
|
}
|
|
break;
|
|
case DropAnimState.FlyToPlayer:
|
|
{
|
|
var owner = GameCenter.GameSceneSystem.FindEntity<Character>(_host._ownerID);
|
|
if (owner == null)
|
|
{
|
|
ChangeState(DropAnimState.Finish);
|
|
break;
|
|
}
|
|
_animTimer += Time.deltaTime;
|
|
if(_animTimer > EndFlyTime)
|
|
{
|
|
ChangeState(DropAnimState.Finish);
|
|
}
|
|
_host.RayCastToGround(Vector3.Lerp(_flyStartPos, owner.Position, _animTimer / EndFlyTime));
|
|
}
|
|
break;
|
|
case DropAnimState.Finish:
|
|
break;
|
|
}
|
|
}
|
|
|
|
//释放
|
|
public void Destory()
|
|
{
|
|
if(_waitVfx != null)
|
|
{
|
|
_waitVfx.Destroy();
|
|
_waitVfx = null;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//私有函数
|
|
private void ChangeState(DropAnimState state)
|
|
{
|
|
_curState = state;
|
|
switch (_curState)
|
|
{
|
|
case DropAnimState.DropDown:
|
|
{
|
|
_animTimer = 0f;
|
|
var pots = new Vector3[3];
|
|
pots[0] = _host.Position;
|
|
pots[2] = _host.GetTerrainPosition(_targetPos.x, _targetPos.y);
|
|
pots[1] = Vector3.Lerp(pots[0], pots[2], 0.5f);
|
|
pots[1].y += 2f;
|
|
_animCurve = iTween.PathControlPointGenerator(pots);
|
|
}
|
|
break;
|
|
case DropAnimState.Wait:
|
|
{
|
|
_animTimer = 0f;
|
|
if (_bodySfx > 0)
|
|
{
|
|
_waitVfx = new FGameObjectVFX(ModelTypeCode.OtherVFX, _bodySfx);
|
|
_waitVfx.SetParent(_host.ModelTransform);
|
|
_waitVfx.SetPosition(_host.Position);
|
|
_waitVfx.SetLayer(_host.Layer);
|
|
_waitVfx.Play(1f);
|
|
}
|
|
}
|
|
break;
|
|
case DropAnimState.FlyToPlayer:
|
|
{
|
|
_animTimer = 0f;
|
|
_flyStartPos = _host.Position;
|
|
}
|
|
break;
|
|
case DropAnimState.Finish:
|
|
if (_waitVfx != null)
|
|
{
|
|
_waitVfx.Destroy();
|
|
_waitVfx = null;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//私有枚举
|
|
private enum DropAnimState
|
|
{
|
|
DropDown,
|
|
Wait,
|
|
FlyToPlayer,
|
|
Finish,
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
}
|