Ejemplo n.º 1
0
    private bool _obstacleCanBePlacedHere(ObstacleComponent obstacle, int index)
    {
        if (obstacle == null)
        {
            return(true);
        }
        // Check to see if currentoccupants[i] can be occupied
        // Check Previous Obstacle
        int previousObs = GetNearestPreviousOccupants(index);

        if (previousObs != -1 &&
            CurrentOccupants[previousObs].GetComponent <ObstacleComponent> ().ObstacleLength > Mathf.Abs(index - previousObs))
        {
            return(false);
        }
        // Then Check Behind Obstacle
        int obsLenght = obstacle.ObstacleLength;

        for (int i = 0; i < obsLenght; i++)
        {
            if (index + i >= CurrentOccupants.Length || CurrentOccupants[index + i] != null)
            {
                return(false);
            }
        }
        return(true);
    }
Ejemplo n.º 2
0
    private void OnTriggerExit(Collider other)
    {
        UnitComponent awarenessUnit = other.gameObject.GetComponent <UnitComponent>();

        if (awarenessUnit != null && mUnitComponent.Neighbors.Contains(awarenessUnit))
        {
            mUnitComponent.Neighbors.Remove(awarenessUnit);
        }

        ObstacleComponent obstacle = other.gameObject.GetComponent <ObstacleComponent>();

        if (obstacle != null && mUnitComponent.Obstacles.Contains(obstacle))
        {
            mUnitComponent.Obstacles.Remove(obstacle);
        }
    }
Ejemplo n.º 3
0
 public void OnEncounterObstacles(ObstacleComponent obstacle)
 {
     print(name + " Encountered: " + obstacle.Type);
     Animator.SetTrigger(obstacle.Type.ToString());
     RuntimeManager.PlayOneShot("event:/" + Type.ToString() + "/" + obstacle.Type.ToString() + obstacle.Version);
 }
Ejemplo n.º 4
0
    public void OnDrop(Transform target)
    {
        int index = GetCurrentIndex(target.gameObject);

        if (index != -1)
        {
            CurrentOccupants[index] = null;
        }
        int   minIndex    = index;
        float minDistance = Mathf.Infinity;

        for (int i = 0; i < LocationTransforms.Length; i++)
        {
            Vector3 locationPos = LocationTransforms[i].position;
            float   dist        = Vector2.Distance(locationPos, target.position);
            if (dist < minDistance)
            {
                minDistance = dist;
                minIndex    = i;
            }
        }
        // Before putting it in place, check to see if it's obstacle and being blocked by other obstacles
        ObstacleComponent obs = target.GetComponent <ObstacleComponent> ();

        if (obs != null)
        {
            if (minIndex != index)
            {
                // Check Previous Obstacles to see blocking
                int lastObstacle = GetNearestPreviousOccupants(minIndex);
                if (lastObstacle != -1)
                {
                    if (CurrentOccupants[lastObstacle].GetComponent <ObstacleComponent>().ObstacleLength > Mathf.Abs(minIndex - lastObstacle))
                    {
                        minIndex = index;
                    }
                }
                // Check Behind Obstacles to see blocking
                for (int i = 0; i < obs.ObstacleLength; i++)
                {
                    if (minIndex + i >= CurrentOccupants.Length || CurrentOccupants[minIndex + i] != null)
                    {
                        minIndex = index;
                        break;
                    }
                }
            }
        }
        if (minIndex == -1 && index != -1)
        {
            minIndex = index;
        }
        target.position = LocationTransforms[minIndex].position;
        GameObject swapCache = null;

        // Swap if current position has occupants
        if (CurrentOccupants[minIndex] != null)
        {
            swapCache = CurrentOccupants[minIndex];
        }
        CurrentOccupants[minIndex] = target.gameObject;
        if (swapCache != null)
        {
            CurrentOccupants[index]      = swapCache;
            swapCache.transform.position = LocationTransforms[index].position;
        }
    }