Esempio n. 1
0
        protected override void OnUpdate()
        {
            var randomResult = _random;

            _random.NextInt();
            var rayCaster = new MovementRayCast()
            {
                PhysicsWorld = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld
            };

            Entities.ForEach((ref Enemy enemy, ref Movable movable, in Translation translation) => {
                if (math.distance(translation.Value, enemy.PreviousCell) > 0.9f)
                {
                    enemy.PreviousCell  = math.round(translation.Value);
                    var validDirections = new NativeList <float3>(Allocator.Temp);

                    var north = new float3(0, 0, 1);
                    var south = new float3(0, 0, -1);
                    var east  = new float3(1, 0, 0);
                    var west  = new float3(-1, 0, 0);

                    if (!rayCaster.CheckRay(translation.Value, north, movable.Direction))
                    {
                        validDirections.Add(north);
                    }
                    if (!rayCaster.CheckRay(translation.Value, south, movable.Direction))
                    {
                        validDirections.Add(south);
                    }
                    if (!rayCaster.CheckRay(translation.Value, east, movable.Direction))
                    {
                        validDirections.Add(east);
                    }
                    if (!rayCaster.CheckRay(translation.Value, west, movable.Direction))
                    {
                        validDirections.Add(west);
                    }

                    if (validDirections.Length > 0)
                    {
                        movable.Direction = validDirections[randomResult.NextInt(validDirections.Length)];
                    }

                    validDirections.Dispose();
                }
            }).Schedule();
        }
    protected override void OnUpdate()
    {
        var rayCaster = new MovementRayCast()
        {
            pw = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld
        };

        rng.NextInt();
        var rngTemp = rng;

        Entities
        .WithAll <Enemy>()
        .ForEach((ref Movable mov, ref Enemy enemy, in Translation trns) => {
            if (math.distance(trns.Value, enemy.previousCell) > 0.9)
            {
                enemy.previousCell = math.round(trns.Value);

                // Perfrom raycast to see what travel direction the enemy can use
                var valiDir = new NativeList <float3>(Allocator.Temp);
                // Check if enemy can travel right
                if (!rayCaster.CheckRay(trns.Value, new float3(0, 0, -1), mov.direction))
                {
                    valiDir.Add(new float3(0, 0, -1));
                }
                // Check if enemy can travel left
                if (!rayCaster.CheckRay(trns.Value, new float3(0, 0, 1), mov.direction))
                {
                    valiDir.Add(new float3(0, 0, 1));
                }
                // Check if enemy can travel down
                if (!rayCaster.CheckRay(trns.Value, new float3(-1, 0, 0), mov.direction))
                {
                    valiDir.Add(new float3(-1, 0, 0));
                }
                // Check if enemy can travel up
                if (!rayCaster.CheckRay(trns.Value, new float3(1, 0, 0), mov.direction))
                {
                    valiDir.Add(new float3(1, 0, 0));
                }

                // From the list of possible direction randomly choose one
                mov.direction = valiDir[rngTemp.NextInt(valiDir.Length)];

                valiDir.Dispose();
            }
        }).Schedule();
    }
Esempio n. 3
0
        protected override void OnUpdate()
        {
            var raycaster = new MovementRayCast {
                pw = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld
            };

            rng.NextInt();
            var rngTemp = rng;


            Entities.ForEach((ref Movable mov, ref Enemy enemy, in Translation trans) =>
            {
                if (math.distance(trans.Value, enemy.previousCell) > .9f)
                {
                    enemy.previousCell = math.round(trans.Value);

                    var validDir = new NativeList <float3>(Allocator.Temp);

                    if (!raycaster.CheckRay(trans.Value, new float3(0, 0, -1), mov.direction))
                    {
                        validDir.Add(new float3(0, 0, -1));
                    }
                    if (!raycaster.CheckRay(trans.Value, new float3(0, 0, 1), mov.direction))
                    {
                        validDir.Add(new float3(0, 0, 1));
                    }
                    if (!raycaster.CheckRay(trans.Value, new float3(-1, 0, 0), mov.direction))
                    {
                        validDir.Add(new float3(-1, 0, 0));
                    }
                    if (!raycaster.CheckRay(trans.Value, new float3(1, 0, 0), mov.direction))
                    {
                        validDir.Add(new float3(1, 0, 0));
                    }

                    mov.direction = validDir[rngTemp.NextInt(validDir.Length)];
                    validDir.Dispose();
                }
            }).Schedule();
        }
    //public NativeArray<Unity.Mathematics.Random> RandomArray { get; private set; }

    //protected override void OnCreate()
    //{
    //	var randomArray = new Unity.Mathematics.Random[Unity.Jobs.LowLevel.Unsafe.JobsUtility.MaxJobThreadCount];
    //	var seed = new System.Random();
    //	for (int i = 0; i < Unity.Jobs.LowLevel.Unsafe.JobsUtility.MaxJobThreadCount; ++i)
    //		randomArray[i] = new Unity.Mathematics.Random((uint)seed.Next());

    //	RandomArray = new NativeArray<Unity.Mathematics.Random>(randomArray, Allocator.Persistent);

    //	Console.WriteLine("hi");
    //}
    //protected override void OnDestroy()
    //{
    //	RandomArray.Dispose();
    //}


    protected override void OnUpdate()
    {
        var raycaster = new MovementRayCast()
        {
            pw = World.GetOrCreateSystem <BuildPhysicsWorld>().PhysicsWorld
        };

        //var pw = World.GetOrCreateSystem<BuildPhysicsWorld>().PhysicsWorld;
        this.rand.NextInt();
        var rand = this.rand;
        var dt   = Time.DeltaTime;
        var et   = Time.ElapsedTime;


        ////var dirs = new Unity.Collections.FixedList32<float3>() {new float3(0,0,-1) };
        //NativeArray<Unity.Mathematics.Random> randTest = new NativeArray<Unity.Mathematics.Random>();
        //randTest[0] = new Unity.Mathematics.Random();
        //var sptr = Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetAtomicSafetyHandle(randTest);
        //var pptr =(Unity.Mathematics.Random *) Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(randTest);
        //Console.WriteLine(pptr[0].NextInt());
        //Unity.Mathematics.Random* pRand = stackalloc Unity.Mathematics.Random[10];
        //pRand[0] = new Unity.Mathematics.Random(1);

        //var randArray = RandomArray;
        //var pRandArray = (Unity.Mathematics.Random*)Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(randArray);
        //pRandArray[0].state

        //Debug.Log("hi");

        Entities.ForEach((int nativeThreadIndex, ref Movable mov, ref Enemy enemy, in Translation trans) =>
        {
            Debug.Log($"in enemy forEach!");
            //Debug.Log($"in enemy forEach! {et - enemy.lastMoveChoiceTime}");

            //var pRandTest = pRand[0].NextInt();

            //Unity.Mathematics.math.round()


            //var wasInit = enemy.isInit;
            //if (!wasInit)
            //{
            //	enemy.init();
            //}
            //if (!wasInit || math.distance(trans.Value, enemy.lastCell) > 0.9f)
            if ((math.distance(trans.Value, enemy.lastCell) > 0.9f) || (et - enemy.lastMoveChoiceTime) > 3f)            // || trans.Value.Equals(enemy.lastPos))//|| mov.direction.Equals(float3.zero))
            {
                Debug.Log("in enemy move section!");

                enemy.lastCell           = math.round(trans.Value);
                enemy.lastMoveChoiceTime = et;



                //raycast here
                var validDir = new NativeList <float3>(Allocator.Temp);
                //var validDir = new FixedList128<float3>();

                if (!raycaster.CheckRay(trans.Value, new float3(0, 0, -1), ref mov.direction))
                {
                    validDir.Add(new float3(0, 0, -1));
                }
                else
                {
                    Debug.Log("hit z-1");
                }
                if (!raycaster.CheckRay(trans.Value, new float3(0, 0, 1), ref mov.direction))
                {
                    validDir.Add(new float3(0, 0, 1));
                }
                else
                {
                    Debug.Log("hit z 1");
                }
                if (!raycaster.CheckRay(trans.Value, new float3(-1, 0, 0), ref mov.direction))
                {
                    validDir.Add(new float3(-1, 0, 0));
                }
                else
                {
                    Debug.Log("hit x-1");
                }
                if (!raycaster.CheckRay(trans.Value, new float3(1, 0, 0), ref mov.direction))
                {
                    validDir.Add(new float3(1, 0, 0));
                }
                else
                {
                    Debug.Log("hit x 1");
                }
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(0, 0, -1), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(0, 0, -1));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(0, 0, 1), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(0, 0, 1));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(-1, 0, 0), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(-1, 0, 0));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(1, 0, 0), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(1, 0, 0));
                //}


                if (validDir.Length == 0)
                {
                    Debug.Log("Empty");
                }
                else
                {
                    mov.direction = validDir[rand.NextInt(validDir.Length)];
                    Debug.Log($"FULL mov {mov.direction}");
                }

                validDir.Dispose();


                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(0, 0, -1), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(0, 0, -1));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(0, 0, 1), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(0, 0, 1));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(-1, 0, 0), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(-1, 0, 0));
                //}
                //if (raycaster.CheckRay(pos: trans.Value, targetDir: new float3(1, 0, 0), currentDir: ref mov.direction))
                //{
                //	validDir.Add(new float3(1, 0, 0));
                //}


                //raycaster.CheckRayDist(trans.Value, new float3(0, 0, -1), mov.direction);
                //raycaster.CheckRay(trans.Value, new float3(0, 0, -1), ref mov.direction);
            }



            enemy.lastPos = trans.Value;
        }).ScheduleParallel();

        this.Dependency.Complete();         //ensure completes this frame
        //Job.WithCode(() => {
        //	this.rand = rand;
        //}).Schedule();
    }