コード例 #1
0
    public void SpawnMultiUnits()
    {
        if (gameOver)
        {
            return;
        }

        Grid    startGrid = connectionGrid.gridList[1];
        Vector3 offset    = new Vector3(0, 5, 0);

        for (int i = 0; i < agentCount; i++)
        {
            if (myAgents.Count < MAX_UNITS)
            {
                int     randomIndex   = Random.Range(0, startGrid.permanentNodes.Count);
                Vector3 spawnLocation = startGrid.permanentNodes[randomIndex].GetLocation() + offset;

                GameObject newUnit  = Instantiate(unitPrefab, spawnLocation, Quaternion.identity) as GameObject;
                GridAgent  newAgent = newUnit.GetComponent <GridAgent>();
                newAgent.SetNavigationGrid(connectionGrid);
                newAgent.Initialize(this);
                myAgents.Add(newAgent);
            }
        }
    }
コード例 #2
0
    public virtual void SpawnUnit(Controller newController)
    {
        Node centerNode = newController.controllerBaseLocation;

        if (spawnNode == null)
        {
            spawnNode = centerNode.gridParent.LookUpNode(centerNode.gridCoordinates.x - range, centerNode.gridCoordinates.z - range);
            currentX  = (int)centerNode.gridCoordinates.x - range;
            currentZ  = (int)centerNode.gridCoordinates.z - range;
        }
        Node newNode = spawnNode;

        if (newNode.available)
        {
            float   offset        = 1;
            Vector3 spawnLocation = newNode.sphereCoordinates * offset;
            Vector3 upVector      = (mainGrid.gameObject.transform.position - spawnLocation).normalized;

            GameObject newAgentPrefab = Instantiate(agentPrefab, spawnLocation, agentPrefab.transform.rotation) as GameObject;
            newAgentPrefab.transform.up = upVector;
            GridAgent newAgent = newAgentPrefab.GetComponent <GridAgent>();
            newAgent.Initialize(mainGrid, mainLevelManager, newNode);
            Unit newUnit = newAgentPrefab.GetComponent <Unit>();
            newUnit.Initialize(newController);

            newController.ManageActiveUnits(newUnit);
        }
        if (currentX == (int)centerNode.gridCoordinates.x - range && currentZ < (int)centerNode.gridCoordinates.z + range)
        {
            currentZ++;
        }
        else if (currentX < (int)centerNode.gridCoordinates.x + range && currentZ == (int)centerNode.gridCoordinates.z + range)
        {
            currentX++;
        }
        else if (currentX == (int)centerNode.gridCoordinates.x + range && currentZ > (int)centerNode.gridCoordinates.z - range)
        {
            currentZ--;
        }
        else if (currentX > (int)centerNode.gridCoordinates.x - range && currentZ == (int)centerNode.gridCoordinates.z - range)
        {
            currentX--;
        }
        spawnNode = centerNode.gridParent.LookUpNode(currentX, currentZ);
    }