using System.Collections.Generic;
|
using UnityEngine;
|
|
public class JiaPlayerController
|
{
|
public enum E_BehaviourType
|
{
|
MoveBeTweenNpc,
|
MaxType,
|
Fight
|
}
|
|
public struct JiaPlayerData
|
{
|
public int job;
|
public string name;
|
public int clothes;
|
public int weapon;
|
public int sedcondary;
|
public E_BehaviourType behaviourType;
|
}
|
|
private float m_Duration;
|
|
public bool IsDone;
|
private GA_JiaPlayer m_Player;
|
public E_BehaviourType BehaviourType { get; private set; }
|
public float m_WaitingTime = 0;
|
public float m_RandomIdelTime = 0;
|
private float m_DurationEscapeTime;
|
|
public void Init(JiaPlayerData data)
|
{
|
IsDone = false;
|
var _playerData = new GActorPlayerBase.PlayerInfo();
|
_playerData.name = data.name;
|
_playerData.job = (byte)data.job;
|
var _equipList = new List<GActorPlayerBase.CEquipInfo>();
|
if (ItemConfig.Has(data.clothes))
|
{
|
_equipList.Add(new GActorPlayerBase.CEquipInfo()
|
{
|
id = data.clothes,
|
place = ItemConfig.Get(data.clothes).EquipPlace
|
});
|
}
|
if (ItemConfig.Has(data.weapon))
|
{
|
_equipList.Add(new GActorPlayerBase.CEquipInfo()
|
{
|
id = data.clothes,
|
place = ItemConfig.Get(data.clothes).EquipPlace
|
});
|
}
|
if (ItemConfig.Has(data.sedcondary))
|
{
|
_equipList.Add(new GActorPlayerBase.CEquipInfo()
|
{
|
id = data.clothes,
|
place = ItemConfig.Get(data.clothes).EquipPlace
|
});
|
}
|
_playerData.itemDatas = _equipList.ToArray();
|
_equipList.Clear();
|
_equipList = null;
|
|
m_DurationEscapeTime = 0;
|
m_Duration = JiaPlayerManager.EachPlayerLastTime;
|
|
BehaviourType = data.behaviourType;
|
// 从已经刷出的NPC中随机出生位置
|
Vector3 _bornPos = GetRandomNpcPosition();
|
|
var _randomDir = new Vector3(Random.Range(-1f, -.3f), 0, Random.Range(-.9f, .9f));
|
if (Random.Range(1, 3) == 1)
|
{
|
_randomDir = new Vector3(Random.Range(.3f, 1f), 0, Random.Range(-.9f, .9f));
|
}
|
|
_bornPos = _bornPos + _randomDir;
|
GActor.TryGetValidPos(_bornPos, ref _bornPos);
|
|
m_Player = GAMgr.Instance.ReqClntPlayer<GA_JiaPlayer>(_playerData, E_ActorGroup.Player);
|
m_Player.InitBornPos((ushort)(_bornPos.x * 2), (ushort)(_bornPos.z * 2));
|
}
|
|
public void UnInit()
|
{
|
if (m_Player != null)
|
{
|
GAMgr.Instance.Release(m_Player);
|
m_Player = null;
|
}
|
}
|
|
private byte m_Step = 0;
|
|
public void Update()
|
{
|
if (BehaviourType == E_BehaviourType.MoveBeTweenNpc)
|
{
|
if (m_Player != null)
|
{
|
switch (m_Step)
|
{
|
case 0:
|
if (m_Player.IsIdle())
|
{
|
m_WaitingTime = Time.time + Random.Range(3f, 5f);
|
m_Step = 1;
|
}
|
else
|
{
|
m_Step = 2;
|
}
|
break;
|
case 1:// 等待站立的随机时间结束
|
if (Time.time > m_WaitingTime)
|
{
|
Vector3 _nextPosition = Vector3.zero;
|
bool fight = false;
|
int moveWay = Random.Range(0, 3);
|
if (moveWay == 1)
|
{
|
fight = true;
|
_nextPosition = GetFightNpcPosition();
|
}
|
else if (moveWay == 2)
|
_nextPosition = GetRandomNpcPosition();
|
else
|
_nextPosition = GetCloserNpcPosition();
|
|
if (_nextPosition == Vector3.zero)
|
{
|
fight = false;
|
_nextPosition = GetCloserNpcPosition();
|
}
|
|
if (_nextPosition == Vector3.zero)
|
{
|
m_Step = 0;
|
}
|
else
|
{
|
m_Player.MoveToPosition(_nextPosition, 1);
|
if (fight) BehaviourType = E_BehaviourType.Fight;
|
m_Step = 2;
|
}
|
}
|
break;
|
case 2:
|
if (m_Player.IsIdle())
|
{
|
m_Step = 0;
|
}
|
break;
|
}
|
}
|
}
|
else if (BehaviourType == E_BehaviourType.Fight)
|
{
|
if (m_Player != null)
|
{
|
if (m_Player.IsIdle())
|
{
|
if (!m_Player.PlaySkill())
|
BehaviourType = E_BehaviourType.MoveBeTweenNpc;
|
}
|
}
|
}
|
|
m_DurationEscapeTime += Time.deltaTime;
|
if(m_DurationEscapeTime > m_Duration || m_Player.Pos.x == 10000)
|
{
|
IsDone = true;
|
}
|
}
|
|
|
private Vector3 GetRandomNpcPosition()
|
{
|
var _list = GAMgr.Instance.GetTypeList(E_ActorClassType.NpcFunc);
|
if (_list == null || _list.Count == 0)
|
{
|
_list = GAMgr.Instance.GetTypeList(E_ActorClassType.NpcFightNorm);
|
if (_list == null || _list.Count == 0)
|
return Vector3.zero;
|
}
|
|
var _random = Random.Range(0, _list.Count);
|
return _list[_random].Pos;
|
}
|
|
private Vector3 GetFightNpcPosition()
|
{
|
var _list = GAMgr.Instance.GetTypeList(E_ActorClassType.NpcFightNorm);
|
if (_list == null || _list.Count == 0)
|
return Vector3.zero;
|
|
var _random = Random.Range(0, _list.Count);
|
return _list[_random].Pos;
|
}
|
|
private Vector3 GetCloserNpcPosition()
|
{
|
var _mapID = PlayerDatas.Instance.baseData.MapID;
|
var _mapNpcs = mapnpcConfig.GetNpcList(_mapID);
|
var _list = new List<int>(_mapNpcs.Keys);
|
/*
|
_list.Sort((n1, n2) =>
|
{
|
var _d1 = MathUtility.DistanceSqrtXZ(PlayerDatas.Instance.hero.Pos,
|
_mapNpcs[n1]);
|
var _d2 = MathUtility.DistanceSqrtXZ(PlayerDatas.Instance.hero.Pos,
|
_mapNpcs[n1]);
|
return _d2 < _d1 ? -1 : 1;
|
});
|
|
for (int i = 0; i < _list.Count; ++i)
|
{
|
if (_list[i] != currentMoveToNpc)
|
{
|
currentMoveToNpc = _list[i];
|
return _mapNpcs[_list[i]];
|
}
|
}
|
*/
|
if (_list == null || _list.Count == 0)
|
{
|
return Vector3.zero;
|
}
|
var _random = Random.Range(0, _list.Count);
|
return _mapNpcs[_list[_random]];
|
}
|
|
|
}
|