Untitled

 avatar
unknown
csharp
3 years ago
3.1 kB
4
Indexable
// system
using Game.Core.Authors;
using Game.Core.Entities;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;

namespace Game.Core.Systems {
    
    [BurstCompile]
    [CreateAfter(typeof(ProtectorSpawner))]
    [UpdateAfter(typeof(ProtectorSpawner))]
    public partial struct ProtectorMovementSystem: ISystem {
        private ProtectorAspect entityAspect; 
        public float CurrentOrbitSpeed;
        public float CurrentRotationSpeed;
        private struct CoreSystemTag : IComponentData { }
        private struct MovementTag : IComponentData { }

        public void OnCreate(ref SystemState state) {
            entityAspect = SystemAPI.GetAspectRW<ProtectorAspect>(SystemAPI.GetSingletonEntity<ProtectorEntity>());
            CurrentOrbitSpeed = entityAspect.GetRandomOrbitSpeed();
            CurrentRotationSpeed = entityAspect.GetRandomRotationSpeed();
            state.RequireForUpdate<ProtectorEntity>();
        }

        public void OnDestroy(ref SystemState state) {
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state) {
            var query = SystemAPI.QueryBuilder().WithAll<ProtectorTag>().Build();
            var entitiesTransform = query.ToComponentDataArray<LocalToWorldTransform>(Allocator.Temp);

            for (int i = 0; i < entitiesTransform.Length; i++) {
                var entityTransform = entitiesTransform[i];
                var transform = entityAspect.GetOribitTransform(CurrentOrbitSpeed, entityTransform.Value);
                transform.Rotation = entityAspect.GetCurrentRotaion(CurrentRotationSpeed, entityTransform.Value.Rotation);
                entitiesTransform[i] = new LocalToWorldTransform {
                    Value = transform
                };
            }

            entitiesTransform.Dispose();

        }
    }
}

// actorss methods
...

        public float GetRandomOrbitSpeed() => _controllerEntityMetaData.ValueRW.RandomObj.NextFloat(5f, 35f);
        public float GetRandomRotationSpeed() => _controllerEntityMetaData.ValueRW.RandomObj.NextFloat(5f, 50f);
        private float angleOrbit() => 1; //_controllerEntityMetaData.ValueRW.RandomObj.NextFloat(5f, 90f);

        public UniformScaleTransform GetOribitTransform(float currentOrbitSpeed,UniformScaleTransform transform) {
            var dt = SystemAPI.Time.DeltaTime;
            var distance = math.distance(math.float3(0, 0, 0), _transform.Position);
            float3 pos;
            var angle = currentOrbitSpeed * dt;
            pos.x = math.cos(angle) * distance;
            pos.y = 0f;
            pos.z = math.sin(angle) * distance;
            return new UniformScaleTransform {
                Position = pos,
                Rotation = transform.Rotation,
                Scale =  transform.Scale
            };
        }

        public Quaternion GetCurrentRotaion(float currentRotationSpeed, Quaternion currentRotation) {
            var dt = SystemAPI.Time.DeltaTime;
            return Quaternion.Euler(0, currentRotationSpeed * dt, 0) * currentRotation;
        }
...
Editor is loading...