// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2018-2-29
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
using System.Collections.Generic;
using F.Entity;
using UnityEngine;
namespace G
{
public enum EntityType
{
None,
Drop,
Prop,
Pet,
Mount,
//Enemy,
Minion,
Elite,
Boss,
Npc,
General,
Player,
MainPlayer,
}
///
///
///
public class Entity : MonoBehaviour, IEntity
{
#region Property
///
/// 唯一标识
///
public int id
{
get;
protected set;
}
///
/// 配置ID
///
public int itemId
{
get;
protected set;
}
///
/// 实体类型
///
public virtual EntityType entityType
{
get { return EntityType.None; }
}
///
/// 实体表现类
///
public EntityView entityView
{
get;
private set;
}
///
///
///
public Vector3 position
{
get { return this.transform.position; }
set { this.transform.position = value; }
}
///
///
///
public Quaternion rotation
{
get { return this.transform.localRotation; }
set { this.transform.localRotation = value; }
}
///
///
///
public Vector3 eulerAngles
{
get { return this.transform.localEulerAngles; }
set { this.transform.localEulerAngles = value; }
}
///
///
///
public Vector3 scale
{
get { return this.transform.localScale; }
set { this.transform.localScale = value; }
}
///
/// 设置统一缩放
///
public float unityScale
{
get { return this.transform.localScale.x; }
set { this.transform.localScale = Vector3.one * value; }
}
///
///
///
public Vector3 forward
{
get { return this.transform.forward; }
}
///
///
///
public Vector3 up
{
get { return this.transform.up; }
}
///
///
///
public Vector3 right
{
get { return this.transform.right; }
}
///
///
///
public Entity parent
{
get { return EntityManager.Instance.GetParent(this.id); }
set
{
if (value != null)
{
EntityManager.Instance.SetParent(this.id, value.id);
}
}
}
///
///
///
///
public void GetChildren(List children)
{
if (children != null)
EntityManager.Instance.GetChildren(this.id, children);
}
#endregion
#region Method
///
///
///
public Entity()
{
this.entityView = new EntityView(this);
}
///
///
///
public void InitView()
{
this.entityView.Init(this.OnViewInitCompleted);
}
///
///
///
protected virtual void OnViewInitCompleted()
{
//Debug.Log($"{this.entityType} OnViewInitCompleted");
}
///
/// 判断物体是否在自己前方
///
///
///
public bool IsInFront2D(Entity target)
{
if (target == null)
{
return false;
}
var toward = target.position - this.position;
toward.y = 0f;
var forward = this.forward;
forward.y = 0f;
return Vector3.Dot(toward, forward) > 0;
}
///
/// 物体之间的距离
///
///
///
public float Distance2D(Entity target)
{
if (target == null)
{
return float.MaxValue;
}
var toward = target.position - this.position;
toward.y = 0f;
return toward.magnitude;
}
#endregion
#region Interface
///
/// 回收
///
public void OnRecycle()
{
}
///
/// 销毁
///
public virtual void Dispose()
{
this.entityView.Dispose();
if (gameObject)
Destroy(gameObject);
}
///
/// 显示
///
///
public void OnShow(object userData)
{
}
///
/// 隐藏
///
///
public void OnHide(object userData)
{
}
protected virtual void UpdateInternal(float delta)
{
}
public virtual void UpdatePerSecond()
{
}
object IEntity.handle
{
get { return this.entityView; }
}
void IEntity.OnUpdate(float deltaTime)
{
this.UpdateInternal(deltaTime);
this.entityView.OnUpdate(deltaTime);
}
void IEntity.OnUpdatePerSecond()
{
this.UpdatePerSecond();
}
#endregion
}
}