2
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.2 KiB
C#

using Entitas;
using UnityEngine;
using Game;
using System.Collections.Generic;
[Game]
public class ViewComponent : IComponent
{
public EntityPoolItem EntityPoolItem;
public GameObject GameObject;
public GameObject GameObjectLogic;
public Transform TransformLogic;
public Transform TransformView;
public Transform TransformViewRot;
public Transform TransformViewOffset;
public Transform TransformViewMain;
public Transform TransformViewOther;
public Collider Collider;
public Material Material;
public Vector3 PositionView => TransformViewMain.position;
public Vector3 PositionLogic => TransformLogic.position;
public Vector3 PositionPre;
public SpriteRenderer SpriteRenderer; //物体的主要sprite
public MetaData<Vector3> PosView = new MetaData<Vector3>();
//额外对象
public List<EffectPoolItem> EffectObject = new List<EffectPoolItem>(); //挂在实体上的特效
public ShadowPoolItem SkillPointerObject; //技能指示器
// 特殊效果
public Vector3 LocalPositionOrder = Vector3.zero; //避免z-fighting
public Vector3 LocalPositionShake = Vector3.zero; //抖动带来的额外偏移
public Vector3 ScaleDir = Vector3.zero; //缩放方向
public Color FlashColor = Color.white; //闪白颜色
public float ShakeTimeLeft; //抖动剩余时间
public float FlashTimeLeft; //闪白剩余时间
public float ScaleTimeLeft; //缩放剩余时间
public float ScaleRate; //缩放强度
}
namespace Game
{
public abstract partial class Util
{
public static void EntityShake(GameEntity entity, int shakeTimes)
{
entity.view.ShakeTimeLeft = shakeTimes * GameConst.ShakeTerm;
}
public static void EntityFlash(GameEntity entity)
{
EntityFlash(entity, Color.white);
}
public static void EntityFlash(GameEntity entity, Color flashColor)
{
entity.view.FlashTimeLeft = GameConst.FlashTerm;
entity.view.FlashColor = flashColor;
}
public static void EntityScale(GameEntity entity, Vector3 dir)
{
entity.view.ScaleTimeLeft = GameConst.ScaleTerm;
entity.view.ScaleDir = dir;
var rate = Mathf.Clamp01((dir.magnitude - GameConst.ScaleFloatingMin) /
(GameConst.ScaleFloatingMax - GameConst.ScaleFloatingMin));
var scaleRate = Mathf.Lerp(GameConst.ScaleRateMin, GameConst.ScaleRateMax, rate);
entity.view.ScaleRate = scaleRate;
}
public static void SetEntityPos(GameEntity entity, Vector3 pos)
{
entity.view.GameObject.transform.position = pos;
}
public static int GetEntityLayer(GameEntity entity)
{
return entity.view.GameObject.layer;
}
public static void SetEntityLayer(GameEntity entity, int layer)
{
entity.view.GameObject.layer = layer;
}
public static Vector3 EntityLogicPos(int entity)
{
var e = Util.GetEntity(entity);
if (e == null)
{
return Vector3.zero;
}
return EntityLogicPos(e);
}
public static Vector3 EntityLogicPos(GameEntity entity)
{
return entity.view.TransformLogic.position;
}
public static Vector3 EntityViewPos(int entity)
{
var e = Util.GetEntity(entity);
if (e == null)
{
return Vector3.zero;
}
return EntityViewPos(e);
}
public static Vector3 EntityViewPos(GameEntity entity)
{
return entity.view.TransformViewMain.position;
}
public static float EntityDistance(int entity1, int entity2)
{
var e1 = Util.GetEntity(entity1);
var e2 = Util.GetEntity(entity2);
if (e1 == null || e2 == null)
{
return 0;
}
return EntityDistance(e1, e2);
}
public static float EntityDistance(GameEntity entity1, GameEntity entity2)
{
return Vector3.Distance(EntityLogicPos(entity1), EntityLogicPos(entity2));
}
public static float EntityDistanceXZ(int entity1, int entity2)
{
var e1 = Util.GetEntity(entity1);
var e2 = Util.GetEntity(entity2);
if (e1 == null || e2 == null)
{
return 0;
}
return EntityDistanceXZ(e1, e2);
}
public static float EntityDistanceXZ(GameEntity entity1, GameEntity entity2)
{
var pos1 = EntityLogicPos(entity1);
var pos2 = EntityLogicPos(entity2);
pos1.y = 0;
pos2.y = 0;
return Vector3.Distance(pos1, pos2);
}
public static Vector3 EntityOffset(int entity1, int entity2)
{
var e1 = Util.GetEntity(entity1);
var e2 = Util.GetEntity(entity2);
if (e1 == null || e2 == null)
{
return Vector3.zero;
}
return EntityOffset(e1, e2);
}
public static Vector3 EntityOffset(GameEntity entity1, GameEntity entity2)
{
return EntityLogicPos(entity2) - EntityLogicPos(entity1);
}
public static void ClearView(GameEntity entity)
{
var view = entity.view;
//特效销毁
foreach (var item in view.EffectObject)
{
item.EffectDestroy();
}
view.EffectObject.Clear();
//删除技能指示器
if (view.SkillPointerObject != null)
{
view.SkillPointerObject.Destroy();
}
//重置shader参数
view.Material.SetColor("_FlashColor", new Color(1f, 1f, 1f, 1f));
entity.view.GameObjectLogic.SetActive(true);
view.EntityPoolItem.Destroy();
}
}
}