302 lines
4.9 KiB
C#
302 lines
4.9 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created : 2018-2-29
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "Entity" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
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,
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class Entity : MonoBehaviour, IEntity
|
|
{
|
|
#region Property
|
|
|
|
/// <summary>
|
|
/// 唯一标识
|
|
/// </summary>
|
|
public int id
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 配置ID
|
|
/// </summary>
|
|
public int itemId
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实体类型
|
|
/// </summary>
|
|
public virtual EntityType entityType
|
|
{
|
|
get { return EntityType.None; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 实体表现类
|
|
/// </summary>
|
|
public EntityView entityView
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 position
|
|
{
|
|
get { return this.transform.position; }
|
|
set { this.transform.position = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Quaternion rotation
|
|
{
|
|
get { return this.transform.localRotation; }
|
|
set { this.transform.localRotation = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 eulerAngles
|
|
{
|
|
get { return this.transform.localEulerAngles; }
|
|
set { this.transform.localEulerAngles = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 scale
|
|
{
|
|
get { return this.transform.localScale; }
|
|
set { this.transform.localScale = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置统一缩放
|
|
/// </summary>
|
|
public float unityScale
|
|
{
|
|
get { return this.transform.localScale.x; }
|
|
set { this.transform.localScale = Vector3.one * value; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 forward
|
|
{
|
|
get { return this.transform.forward; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 up
|
|
{
|
|
get { return this.transform.up; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Vector3 right
|
|
{
|
|
get { return this.transform.right; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Entity parent
|
|
{
|
|
get { return EntityManager.Instance.GetParent(this.id); }
|
|
set
|
|
{
|
|
if (value != null)
|
|
{
|
|
EntityManager.Instance.SetParent(this.id, value.id);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="children"></param>
|
|
public void GetChildren(List<Entity> children)
|
|
{
|
|
if (children != null)
|
|
EntityManager.Instance.GetChildren(this.id, children);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Entity()
|
|
{
|
|
this.entityView = new EntityView(this);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public void InitView()
|
|
{
|
|
this.entityView.Init(this.OnViewInitCompleted);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected virtual void OnViewInitCompleted()
|
|
{
|
|
//Debug.Log($"{this.entityType} OnViewInitCompleted");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断物体是否在自己前方
|
|
/// </summary>
|
|
/// <param name="target"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 物体之间的距离
|
|
/// </summary>
|
|
/// <param name="target"></param>
|
|
/// <returns></returns>
|
|
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
|
|
|
|
/// <summary>
|
|
/// 回收
|
|
/// </summary>
|
|
public void OnRecycle()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 销毁
|
|
/// </summary>
|
|
public virtual void Dispose()
|
|
{
|
|
this.entityView.Dispose();
|
|
if (gameObject)
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示
|
|
/// </summary>
|
|
/// <param name="userData"></param>
|
|
public void OnShow(object userData)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 隐藏
|
|
/// </summary>
|
|
/// <param name="userData"></param>
|
|
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
|
|
}
|
|
} |