Beispiel #1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        int curEntityCnt = m_mobQuery.CalculateEntityCount();

        if (na_rayCommands.Length != curEntityCnt)
        {
            mobMovementJH.Complete();

            na_rayCommands.Dispose();
            na_rayCommands = new NativeArray <RaycastCommand>(curEntityCnt, Allocator.Persistent);

            na_rayHits.Dispose();
            na_rayHits = new NativeArray <RaycastHit>(curEntityCnt, Allocator.Persistent);
        }

        var setupRaycastJob = new SetupRaycastJob
        {
            layerMask       = 1 << LayerMask.NameToLayer("Wall"),
                rayCommands = na_rayCommands,
                wallPos     = GameManager.instance.Target.transform.position
        };

        var setupJH   = setupRaycastJob.Schedule(this, inputDeps);
        var raycastJH = RaycastCommand.ScheduleBatch(na_rayCommands, na_rayHits, 100, setupJH);

        var mobMovementJob = new MobMovementJob
        {
            deltaTime = Time.deltaTime,
            hits      = na_rayHits
        };

        mobMovementJH = mobMovementJob.Schedule(this, raycastJH);
        mobMovementJH.Complete();

        var entities = m_mobQuery.ToEntityArray(Allocator.TempJob);

        for (int i = 0; i < entities.Length; ++i)
        {
            if (EntityManager.GetComponentData <MobStateData>(entities[i]).Value == MobState.Throw)
            {
                Vector3       curPos = EntityManager.GetComponentData <Translation>(entities[i]).Value;
                SpearBehavior spear  = PoolManager.instance.SpearPool.SpawnObject(curPos + SettingsManager.ThrowingPoint,
                                                                                  Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehavior;

                spear.Throw();
                EntityManager.SetComponentData(entities[i], new MobStateData {
                    Value = MobState.FromTarget
                });
            }
        }
        entities.Dispose();

        return(mobMovementJH);
    }
Beispiel #2
0
    void Update()
    {
        Vector3 curPos = transform.position;

        switch (m_state)
        {
        case MobState.ToTarget:
        {
            curPos = Vector3.MoveTowards(curPos, TargetPos, Time.deltaTime);

            Vector3 shootTarget = new Vector3(TargetPos.x, GameManager.instance.Target.transform.position.y, TargetPos.z);
            Vector3 dir         = shootTarget - curPos;

            if (Physics.Raycast(curPos, dir, SettingsManager.ShootingRange, 1 << LayerMask.NameToLayer("Wall")))
            {
                m_state = MobState.Throw;
            }

            if (Vector3.Distance(curPos, TargetPos) < 2.0f)
            {
                m_state = MobState.FromTarget;
            }
        } break;

        case MobState.Throw:
        {
            SpearBehavior spear = PoolManager.instance.SpearPool.SpawnObject(curPos + SettingsManager.ThrowingPoint,
                                                                             Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehavior;

            spear.Throw();
            m_state = MobState.FromTarget;
        } break;

        case MobState.FromTarget:
        {
            curPos = Vector3.MoveTowards(curPos, StartPos, Time.deltaTime);

            if (Vector3.Distance(curPos, StartPos) < 2.0f)
            {
                m_state = MobState.ToTarget;
            }
        } break;
        }
        ;

        transform.position = curPos;
    }
    private void Update()
    {
        if (!taa_mobs.isCreated || taa_mobs.length == 0 ||
            nl_startPos.Length != taa_mobs.length ||
            nl_targetPos.Length != taa_mobs.length ||
            na_mobStates.Length != taa_mobs.length)
        {
            return;
        }

        if (!na_rayCommands.IsCreated || na_rayCommands.Length != taa_mobs.length)
        {
            movementJH.Complete();

            if (na_rayCommands.IsCreated)
            {
                na_rayCommands.Dispose();
            }

            if (na_rayHits.IsCreated)
            {
                na_rayHits.Dispose();
            }

            na_rayCommands = new NativeArray <RaycastCommand>(taa_mobs.length, Allocator.Persistent);
            na_rayHits     = new NativeArray <RaycastHit>(taa_mobs.length, Allocator.Persistent);
        }

        var setupRaycastJob = new SetupRaycastJob
        {
            layerMask       = 1 << LayerMask.NameToLayer("Wall"),
                rayCommands = na_rayCommands,
                targetPos   = nl_targetPos,
                wallPos     = GameManager.instance.Target.transform.position
        };

        var setupJH   = setupRaycastJob.Schedule(taa_mobs);
        var raycastJH = RaycastCommand.ScheduleBatch(na_rayCommands, na_rayHits, 100, setupJH);

        var mobMovementJob = new MobMovementJob
        {
            deltaTime = Time.deltaTime,
            hits      = na_rayHits,
            mobStates = na_mobStates,
            startPos  = nl_startPos,
            targetPos = nl_targetPos
        };

        movementJH = mobMovementJob.Schedule(taa_mobs, raycastJH);

        movementJH.Complete();

        for (int i = 0; i < na_mobStates.Length; ++i)
        {
            if (na_mobStates[i] == MobState.Throw)
            {
                SpearBehavior spear = PoolManager.instance.SpearPool.SpawnObject(taa_mobs[i].position + SettingsManager.ThrowingPoint,
                                                                                 Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehavior;

                spear.Throw();
                na_mobStates[i] = MobState.FromTarget;
            }
        }
    }