Unity 之 Pure版Entity Component System【ECS】 案例【GalacticConquest】解析【下】

简介: 书接上回:Unity 之 Pure版Entity Component System 案例【GalacticConquest】解析【上】点击下载工程我们已经完成飞船的实例化,下面就是让飞船动起来~~~创建脚本ShipMovementSyste...

书接上回:Unity 之 Pure版Entity Component System 案例【GalacticConquest】解析【上】

点击下载工程

我们已经完成飞船的实例化,下面就是让飞船动起来~~~

创建脚本ShipMovementSystem飞船的运动系统,作用:

  • 让飞船朝着目标移动
  • 达到目标是添加标签
using Data;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
using UnityEngine;
using Unity.Mathematics;

namespace Systems
{
    [UpdateAfter(typeof(ShipArrivalSystem))]
    public class ShipMovementSystem : JobComponentSystem
    {
        //所有飞船
        struct Ships
        {
            public readonly int Length;
            public ComponentDataArray<Position> Positions;
            public ComponentDataArray<Rotation> Rotations;
            public ComponentDataArray<ShipData> Data;
            public EntityArray Entities;
        }
        //所有星球
        struct Planets
        {
            public readonly int Length;
            public ComponentDataArray<PlanetData> Data;
        }
        /// <summary>
        /// 计算飞船位移 多核心运算
        /// </summary>
        [BurstCompile]
        struct CalculatePositionsJob : IJobParallelFor
        {
            public float DeltaTime;
            [ReadOnly]
            public ComponentDataArray<ShipData> Ships;
            [ReadOnly] public EntityArray Entities;
            public ComponentDataArray<Position> Positions;
            public ComponentDataArray<Rotation> Rotations;

            [ReadOnly] public ComponentDataArray<PlanetData> Planets;
            [ReadOnly] public ComponentDataFromEntity<PlanetData> TargetPlanet;

            //并发版本 因为使用的是IJobParallelFor
            public NativeQueue<Entity>.Concurrent ShipArrivedQueue;

            public void Execute(int index)
            {
                //飞船的数据
                var shipData = Ships[index];
                //对应需要攻击星球的位置
                var targetPosition = TargetPlanet[shipData.TargetEntity].Position;
                //飞船的位置
                var position = Positions[index];
                //飞船的角度
                var rotation = Rotations[index];

                //逐渐靠近需要攻击的星球
                var newPos = Vector3.MoveTowards(position.Value, targetPosition, DeltaTime * 4.0f);
                //逐一遍历所有找到的星球
                for (var planetIndex = 0; planetIndex < Planets.Length; planetIndex++)
                {
                    var planet = Planets[planetIndex];
                    //如果与星球的距离小于半径
                    if (Vector3.Distance(newPos, planet.Position) < planet.Radius)
                    {
                        //判断这个是不是需要攻击的星球
                        if (planet.Position == targetPosition)
                        {
                            //添加到飞船队列里面
                            ShipArrivedQueue.Enqueue(Entities[index]);
                        }
                        //防止飞船进入到星球内部,重新计算。很精确啊~
                        var direction = (newPos - planet.Position).normalized;
                        newPos = planet.Position + (direction * planet.Radius);
                        break;
                    }
                }
                //计算飞船朝向
                var shipCurrentDirection = math.normalize((float3)newPos - position.Value);
                rotation.Value = quaternion.LookRotation(shipCurrentDirection, math.up());
                //将计算完的结果赋值
                position.Value = newPos;
                Positions[index] = position;
                Rotations[index] = rotation;
            }
        }
        //单核心运算 IJob允许您安排与其他作业和主线程并行运行的单个作业
        struct ShipArrivedTagJob : IJob
        {
            public EntityCommandBuffer EntityCommandBuffer;
            public NativeQueue<Entity> ShipArrivedQueue;

            public void Execute()
            {
                Entity entity;
                while (ShipArrivedQueue.TryDequeue(out entity))
                {
                    //添加已经到达指定星球的标记
                    EntityCommandBuffer.AddComponent(entity, new ShipArrivedTag());
                }
            }
        }

        [Inject]
        EndFrameBarrier m_EndFrameBarrier;

        [Inject]
        Ships m_Ships;    //所有飞船
        [Inject]
        Planets m_Planets;//所有星球

        NativeQueue<Entity> m_ShipArrivedQueue;

        protected override void OnCreateManager()
        {
            m_ShipArrivedQueue = new NativeQueue<Entity>(Allocator.Persistent);
        }

        protected override void OnDestroyManager()
        {
            m_ShipArrivedQueue.Dispose();
            base.OnDestroyManager();
        }

        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (m_Ships.Length == 0)
                return inputDeps;

            //在 IJobParallelFor 中的逻辑
            var handle = new CalculatePositionsJob
            {
                Ships = m_Ships.Data,
                Planets = m_Planets.Data,
                TargetPlanet = GetComponentDataFromEntity<PlanetData>(),
                DeltaTime = Time.deltaTime,
                Entities = m_Ships.Entities,
                Positions = m_Ships.Positions,
                Rotations = m_Ships.Rotations,
                ShipArrivedQueue = m_ShipArrivedQueue.ToConcurrent()//并发
            }.Schedule(m_Ships.Length, 32, inputDeps);

            //在 IJob 中执行的逻辑
            handle = new ShipArrivedTagJob
            {
                //自动执行的队列
                EntityCommandBuffer = m_EndFrameBarrier.CreateCommandBuffer(),
                ShipArrivedQueue = m_ShipArrivedQueue
            }.Schedule(handle);

            return handle;
        }
    }
}

解析一

筛选指定的飞船和星球实体

img_8afdcdcd2f70586d16ee433dd9392e2f.png

解析二

因为是IJobParalleFor,所以队列用的事并行版本

img_a37152fe916c1140cef3032d8ffbccef.png

解析三

通过多核并发计算出飞船的位移,然后对接触到目标的飞船添加到已经到达的队列中

img_8a4e51bea085d4dfa9013cc125cc65a6.png

解析四

单核心处理需要添加到达标记的队列

img_3ac7928f355c04b2d8a323d7c06724a3.png

解析五

需要注意的事声明时不是NativeQueue<Entity>.Concurrent这种带有【Concurrent】类型需要多核心并行执行时需要在赋值时用【.ToConcurrent()】转换下。

EntityCommandBuffer我理解的是这种数据结构,适合并发且安全的不断改变数据长度的缓冲。且可有效较少因为不断的改变造成GC。

img_a1af015c5b56eb529137a25c3df2dfa8.png

创建脚本 ShipMovementSystem ,作用:

  • 处理被添加ShipArrivedTag的飞船
  • 处理星球被攻击到达一定程度时的转换颜色逻辑
using Data;
using Unity.Collections;
using Unity.Entities;
using Unity.Rendering;

namespace Systems
{
    public class ShipArrivalSystem : ComponentSystem
    {
        EntityManager _entityManager;

        //初始化
        public ShipArrivalSystem()
        {
            _entityManager = World.Active.GetOrCreateManager<EntityManager>();
        }

        //到达星球的飞船
        struct Ships
        {
            public readonly int Length;
            public ComponentDataArray<ShipData> Data;
            public EntityArray Entities;
            public ComponentDataArray<ShipArrivedTag> Tag;
        }
        [Inject]
        Ships _ships;

        protected override void OnUpdate()
        {
            if (_ships.Length == 0)
                return;

            var arrivingShipTransforms = new NativeList<Entity>(Allocator.Temp);
            var arrivingShipData = new NativeList<ShipData>(Allocator.Temp);

            for (var shipIndex = 0; shipIndex < _ships.Length; shipIndex++)
            {
                var shipData = _ships.Data[shipIndex];
                var shipEntity = _ships.Entities[shipIndex];
                arrivingShipData.Add(shipData);
                arrivingShipTransforms.Add(shipEntity);
            }

            HandleArrivedShips(arrivingShipData, arrivingShipTransforms);

            arrivingShipTransforms.Dispose();
            arrivingShipData.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arrivingShipData">到达飞船的数据集合</param>
        /// <param name="arrivingShipEntities">到达飞船的实体集合</param>
        void HandleArrivedShips(NativeList<ShipData> arrivingShipData, NativeList<Entity> arrivingShipEntities)
        {
            //逐一遍历所有飞船数据
            for (var shipIndex = 0; shipIndex < arrivingShipData.Length; shipIndex++)
            {

                var shipData = arrivingShipData[shipIndex];
                //获取对应飞船需要攻击的星球
                var planetData = _entityManager.GetComponentData<PlanetData>(shipData.TargetEntity);

                //不同队伍减少
                if (shipData.TeamOwnership != planetData.TeamOwnership)
                {
                    planetData.Occupants = planetData.Occupants - 1;
                    if (planetData.Occupants <= 0)
                    {
                        //本地飞船没有时转换队伍
                        planetData.TeamOwnership = shipData.TeamOwnership;
                        PlanetSpawner.SetColor(shipData.TargetEntity, planetData.TeamOwnership);
                    }
                }
                else//相同队伍相加
                {
                    planetData.Occupants = planetData.Occupants + 1;
                }
                //星球重新赋值
                _entityManager.SetComponentData(shipData.TargetEntity, planetData);
            }
            //删除这些已经到达的飞船
            _entityManager.DestroyEntity(arrivingShipEntities);
        }
    }
}

解析一

获取所有到达飞船的实体集合 与对应的数据集合

img_958cb497c45e6b96926c03b2a8fb4ccb.png

解析二

根据得到的数据集合判断到达的星球是否为同一队伍,相同增加飞船,不同减少飞船,星球的飞船小于0变换队伍和颜色

img_404be2e4ddeb67e21936d8ef64eb9acc.png

为了增加互动性,添加脚本UserInputSystem

  • 左键点击需要发射的星球
  • 右键点击需要攻击的星球
  • 点击空白处取消
using System.Collections.Generic;
using System.Linq;
using Data;
using Other;
using Unity.Entities;
using UnityEngine;

namespace Systems
{

    public class UserInputSystem : ComponentSystem
    {
        struct Planets
        {
            public readonly int Length;
            public ComponentDataArray<PlanetData> Data;
        }
        [Inject] Planets planets;

        //添加?是表示可空类型,可自行Google
        Dictionary<GameObject, PlanetData?> FromTargets = new Dictionary<GameObject, PlanetData?>();
        GameObject ToTarget = null;

        EntityManager _entityManager;

        /// <summary>
        /// 构造函数中初始化 EntityManager
        /// </summary>
        public UserInputSystem()
        {
            _entityManager = World.Active.GetOrCreateManager<EntityManager>();
        }

        protected override void OnUpdate()
        {
            if (Input.GetMouseButtonDown(0))
            {
                var planet = GetPlanetUnderMouse();
                if (planet == null)
                {
                    FromTargets.Clear();
                    Debug.Log("点击外部,我们清除了选择");
                }
                else
                {
                    if (FromTargets.ContainsKey(planet))
                    {
                        Debug.Log("取消选择的目标Deselecting from target " + planet.name);
                        FromTargets.Remove(planet);
                    }
                    else
                    {
                        var data = PlanetUtility.GetPlanetData(planet, _entityManager);
                        //原来的目标
                        var previousTarget = FromTargets.Values.FirstOrDefault();
                        if ((previousTarget == null || previousTarget.Value.TeamOwnership == data.TeamOwnership) && data.TeamOwnership != 0)
                        {
                            Debug.Log("选择的目标 " + planet.name);
                            FromTargets[planet] = data;
                            Debug.Log("数量:" + FromTargets.Count);
                        }
                        else
                        {
                            if (data.TeamOwnership == 0)
                            {
                                Debug.LogWarning("不能设置中性行星");
                            }
                            else
                            {
                                Debug.Log("从目标中添加行星,但是清除之前的列表,因为它是一个不同的团队");
                                FromTargets.Clear();
                                FromTargets[planet] = data;
                            }
                        }

                    }
                }

            }
            if (Input.GetMouseButtonDown(1))
            {
                var planet = GetPlanetUnderMouse();
                if (planet == null)
                {
                    Debug.Log("取消选中目标 ");
                    ToTarget = null;
                }
                else
                {
                    if (!FromTargets.Any())
                    {
                        Debug.Log("没有任何选中的发射星球");
                        return;
                    }
                    Debug.Log($"需要攻击的星球名称为{planet.name}" );
                    ToTarget = planet;
                    foreach (var p in FromTargets.Keys)
                    {
                        if (p == ToTarget)
                            continue;
                        PlanetUtility.AttackPlanet(p, ToTarget, _entityManager);

                    }
                }
            }
        }

        GameObject GetPlanetUnderMouse()
        {
            RaycastHit hit;
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << LayerMask.NameToLayer("Planet")))
            {
                return hit.collider.transform.gameObject;
            }
            return null;
        }
    }
}

为了保证逻辑能够按照指定顺序执行:添加顺序特性UpdateAfter
UserInputSystem--->ShipSpawnSystem
ShipArrivalSystem--->ShipMovementSystem

img_091879237ec539473362c7f337ddfa50.png
img_ea80e1f2a99c334aed9a976e3feb0012.png

最终效果

img_2b174266e47c573b5e6957c742eb7eb1.gif

打完收工~~~

img_569463b7983f645f063dd7b1bef93e0c.png
相关文章
|
存储 运维 数据挖掘
服务器数据恢复—EqualLogic存储硬盘出现坏道的数据恢复案例
某品牌EqualLogic PS6100存储阵列上有一组由16块硬盘组建的raid5磁盘阵列。磁盘阵列上层划分多个大小不同的卷,存放虚拟机文件。 硬盘出现故障导致存储阵列不可用,需要恢复存储阵列中的数据。
|
存储 运维 Oracle
服务器数据恢复—存储硬盘指示灯亮黄灯,RAID5阵列崩溃的数据恢复案例
服务器存储数据恢复环境: 某单位一台某品牌DS5300存储,1个机头+4个扩展柜,50块的硬盘组建了两组RAID5阵列。一组raid5阵列有27块硬盘,存放Oracle数据库文件。存储系统上层一共划分了11个卷。 服务器存储故障: 存储设备上两个硬盘指示灯亮黄色。其中一组RAID5阵列崩溃,存储不可用,设备已经过保。
|
存储 Oracle 关系型数据库
服务器数据恢复—光纤存储上oracle数据库数据恢复案例
一台光纤服务器存储上有16块FC硬盘,上层部署了Oracle数据库。服务器存储前面板2个硬盘指示灯显示异常,存储映射到linux操作系统上的卷挂载不上,业务中断。 通过storage manager查看存储状态,发现逻辑卷状态失败。再查看物理磁盘状态,发现其中一块盘报告“警告”,硬盘指示灯显示异常的2块盘报告“失败”。 将当前存储的完整日志状态备份下来,解析备份出来的存储日志并获得了关于逻辑卷结构的部分信息。
|
数据挖掘 Linux
服务器数据恢复—服务器重装系统导致分区改变的数据恢复案例
服务器上有一组由raid卡组建的raid5磁盘阵列。上层安装linux才做系统,采用XFS文件系统,划分了3个分区。 管理员将服务器的操作系统重装后,发现服务器上的分区发生了改变:一个分区消失,其他分区不可访问。
|
存储 数据挖掘
服务器数据恢复—EMC存储raid5阵列数据恢复案例
服务器存储数据恢复环境: EMC某型号存储中有一组由8块硬盘组建的raid5磁盘阵列。 服务器存储故障: raid5阵列中有2块硬盘离线,存储不可用,上层应用崩了。
|
数据挖掘
服务器数据恢复——服务器异常断电造成raid5阵列故障的数据恢复案例
某服务器上有一组由12块硬盘组建的raid5磁盘阵列。 机房供电不稳定导致机房中该服务器非正常断电,重启服务器后管理员发现服务器无法正常使用。 意外断电可能会导致服务器上的raid模块损坏。
|
Unix 应用服务中间件 索引
服务器数据恢复—LUN映射出错导致文件系统共享冲突的数据恢复案例
SUN光纤存储系统中有一组由6个硬盘组建的RAID6,划分为若干LUN,MAP到跑不同业务的服务器上,这些服务器上运行的是SOLARIS操作系统。 服务器不存在物理故障。由于公司业务变化,需要增加一台服务器跑新的应用。服务器管理员在原服务器在线的状态下,将其中一个lun映射到一台新服务器上。实际上,这个刚映射过去的卷已经map到了solaris生产系统上的某个lun上了。映射到新服务器后,服务器对这个卷进行初始化的操作,原solaris系统上的磁盘报错,重启服务器后这个卷已经无法挂载。 服务器管理员寻求sun原厂工程师的帮助。sun工程师检测后执行了fsck操作。执行完成后文件系统挂载成功。查
|
存储 数据挖掘 Linux
服务器数据恢复—重装系统导致OceanStor存储上的分区无法访问的数据恢复案例
服务器存储数据恢复环境: 华为OceanStor某型号存储+扩展盘柜,存储中的硬盘组建了raid5磁盘阵列,上层分配了1个lun。 linux操作系统,划分了两个分区,分区一通过lvm扩容,分区二为xfs文件系统。 服务器存储故障: 工作人员重装系统操作失误导致磁盘分区变化,分区二无法访问,数据丢失。
|
存储 算法 数据挖掘
服务器数据恢复—昆腾存储StorNext文件系统数据恢复案例
一台昆腾存储设备中有一组raid5磁盘阵列。阵列上有两块硬盘先后离线,raid5磁盘阵列不可用。
|
存储 数据挖掘 Windows
服务器数据恢复—RAIDZ上层ZFS文件系统数据恢复案例
一台服务器有32块硬盘,采用Windows操作系统。 服务器在正常运行的时候突然变得不可用。没有异常断电、进水、异常操作、机房不稳定等外部因素。服务器管理员重启服务器,但是服务器无法进入系统。管理员联系北亚企安数据恢复工程师要求恢复服务器数据。

热门文章

最新文章

推荐镜像

更多
  • DNS