136 lines
3.1 KiB
C#
136 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Fantasy;
|
|
using Fantasy.Entitas;
|
|
using UnityEngine;
|
|
|
|
namespace NBF
|
|
{
|
|
public class Player : Entity
|
|
{
|
|
/// <summary>
|
|
/// 是否本地玩家
|
|
/// </summary>
|
|
public bool IsLocalPlayer;
|
|
|
|
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[HandItemId];
|
|
|
|
#region 初始化
|
|
|
|
public void InitPlayer(MapUnitInfo unitInfo)
|
|
{
|
|
AddComponent<PlayerViewComponent>();
|
|
AddComponent<PlayerStateViewComponent>();
|
|
if (unitInfo.Id == RoleModel.Instance.Id)
|
|
{
|
|
//自己
|
|
AddComponent<PlayerInputComponent>();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 物品
|
|
|
|
public void UnUseItem()
|
|
{
|
|
if (Items.TryGetValue(HandItemId, out var item))
|
|
{
|
|
item.Dispose();
|
|
}
|
|
|
|
HandItemId = 0;
|
|
ItemChangeEvent();
|
|
}
|
|
|
|
public void UseItem(ItemBindInfo item)
|
|
{
|
|
var playerItem = Create<PlayerItem>(Scene);
|
|
playerItem.Init(item);
|
|
ItemChangeEvent();
|
|
}
|
|
|
|
private void ItemChangeEvent()
|
|
{
|
|
Scene.EventComponent.Publish(new PlayerStateChangeEvent
|
|
{
|
|
Player = this
|
|
});
|
|
}
|
|
|
|
#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
|
|
}
|
|
} |