158 lines
3.8 KiB
C#
158 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Fantasy;
|
|
using Fantasy.Entitas;
|
|
using NBF.Utils;
|
|
using UnityEngine;
|
|
|
|
namespace NBF
|
|
{
|
|
public class Player : Entity
|
|
{
|
|
// ========== 本地状态 ==========
|
|
/// <summary>
|
|
/// 是否本地玩家
|
|
/// </summary>
|
|
public bool IsLocalPlayer;
|
|
|
|
/// <summary>
|
|
/// 是否切换物品中
|
|
/// </summary>
|
|
public bool IsChangeItemIng;
|
|
|
|
public bool IsLureRod => false;
|
|
|
|
public bool IsSelf => RoleModel.Instance.Id == Id;
|
|
|
|
// ========== 物理状态(高频同步) ==========
|
|
public Vector3 Position;
|
|
public Quaternion Rotation;
|
|
public Vector2 MoveInput;
|
|
public float Speed;
|
|
public float RotationSpeed;
|
|
public bool IsGrounded;
|
|
public bool Run;
|
|
public float EyeAngle;
|
|
|
|
/// <summary>
|
|
/// 标志量
|
|
/// </summary>
|
|
public long TagValue;
|
|
|
|
/// <summary>
|
|
/// 上一个状态
|
|
/// </summary>
|
|
public PlayerState PreviousState;
|
|
|
|
/// <summary>
|
|
/// 当前状态
|
|
/// </summary>
|
|
public PlayerState State;
|
|
|
|
/// <summary>
|
|
/// 状态参数
|
|
/// </summary>
|
|
public StateEnterParams StateParams;
|
|
|
|
|
|
/// <summary>
|
|
/// 玩家的物品
|
|
/// </summary>
|
|
public Dictionary<long, PlayerItem> Items = new Dictionary<long, PlayerItem>();
|
|
|
|
/// <summary>
|
|
/// 当前手持物品id
|
|
/// </summary>
|
|
public long HandItemId;
|
|
|
|
/// <summary>
|
|
/// 当前手持物品
|
|
/// </summary>
|
|
public PlayerItem HandItem => Items.GetValueOrDefault(HandItemId);
|
|
|
|
#region 初始化
|
|
|
|
public void InitPlayer(MapUnitInfo unitInfo)
|
|
{
|
|
AddComponent<PlayerView>();
|
|
AddComponent<PlayerStateView>();
|
|
if (unitInfo.Id == RoleModel.Instance.Id)
|
|
{
|
|
//自己
|
|
AddComponent<PlayerInput>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 物品
|
|
|
|
public void ReleaseItem(PlayerItem item)
|
|
{
|
|
if (Items.ContainsValue(item))
|
|
{
|
|
Items.Remove(item.Id);
|
|
item.Dispose();
|
|
}
|
|
}
|
|
|
|
public void UnUseItem()
|
|
{
|
|
var prevItem = HandItem;
|
|
HandItemId = 0;
|
|
ItemChangeEvent(prevItem);
|
|
}
|
|
|
|
public void UseItem(ItemInfo item)
|
|
{
|
|
var prevItem = HandItem;
|
|
var itemType = item.ConfigId.GetItemType();
|
|
if (itemType == ItemType.Rod)
|
|
{
|
|
var playerItemRod = Create<PlayerItemRod>(Scene);
|
|
playerItemRod.Init(this, item);
|
|
Items[playerItemRod.Id] = playerItemRod;
|
|
HandItemId = playerItemRod.Id;
|
|
}
|
|
|
|
ItemChangeEvent(prevItem);
|
|
}
|
|
|
|
private void ItemChangeEvent(PlayerItem prevItem)
|
|
{
|
|
Scene.EventComponent.Publish(new PlayerItemChangeEvent
|
|
{
|
|
Player = this,
|
|
Item = HandItem,
|
|
PrevItem = prevItem
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 状态切换
|
|
|
|
/// <summary>
|
|
/// 切换状态
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <param name="stateParams"></param>
|
|
public void ChangeState(PlayerState state, StateEnterParams stateParams = null)
|
|
{
|
|
if (state == State)
|
|
{
|
|
return;
|
|
}
|
|
|
|
PreviousState = State;
|
|
State = state;
|
|
StateParams = stateParams;
|
|
Scene.EventComponent.Publish(new PlayerStateChangeEvent
|
|
{
|
|
Player = this
|
|
});
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |