コード例 #1
0
        public void Update(float dt)
        {
            // Update resources.
            for (int i = 0; i < resources.GetLength(0); i++)
            {
                for (int j = 0; j < resources.GetLength(1); j++)
                {
                    resources[j, i].Update(dt);

                    // Update colony productions.
                    if (resources[j, i].isAColony())
                    {
                        resources[j, i].EvaluateProduction(ships, hexes[j, i]);
                    }

                    // Validate workable tiles.
                    ValidateAdjacentTiles(new Vector2(i, j), false);

                    // Assign influence hexes.
                    if (!resources[j, i].GetHasInfluence())
                    {
                        // Influence not assigned; must assign.
                        ownedHexes.Add(new HexCell(hexes[j, i].GetPos() + new Vector3(0, (int)(lenSide * -0.01), 0), (int)(lenSide * 1.01), hexes[j, i].GetCoordinates()));
                        resources[j, i].ChangeInfluence(true);
                    }
                }
            }

            for (int i = 0; i < ships.Count; i++)
            {
                if (ships[i].active)
                {
                    ships[i].Update(dt);
                }
            }

            // Update influence hexes.
            for (int i = 0; i < ownedHexes.Count; i++)
            {
                ownedHexes[i].Update(dt, resources[(int)ownedHexes[i].GetCoordinates().Y, (int)ownedHexes[i].GetCoordinates().X].workers);
            }

            // Pulsate selection and hover hexes.
            selectionHex.Update(dt, 0.0f);
            hoverHex.Update(dt, 0.0f);

            // Update AI.
            basicAI.Update(dt);
            // See if AI can create a new ship.
            if (basicAI.newShipInterval <= 0.0f)
            {
                // Make sure spawning hex is empty.
                if (!hexes[height - 1, width - 1].hasShip)
                {
                    ScoutShip newAIShip = new ScoutShip((int)Global.PlayerOwners.Computer1, hexes[height - 1, width - 1]);
                    ships.Add(newAIShip);
                    basicAI.AddAI(ships[ships.IndexOf(newAIShip)]);   // Just to not make me dumb from thinking pointers aren't the same in other programming languages.
                }
            }
            // See if AI can give orders to any ships.
            for (int i = 0; i < basicAI.orderIntervals.Count; i++)
            {
                BaseShip AISelectedShip = basicAI.AIShips[i];
                if (basicAI.orderIntervals[i] <= 0.0f)
                {
                    // An order is up.
                    int     theOrder         = basicAI.OrderShip(i);
                    Vector2 orderDestination = AISelectedShip.hexCell.GetCoordinates();
                    bool    oddRow           = AISelectedShip.hexCell.GetCoordinates().Y % 2 > 0 ? true : false;
                    if (oddRow)
                    {
                        switch (theOrder)
                        {
                        case 0:
                            // TopRight exit
                            orderDestination.X += 1.0f;
                            orderDestination.Y += 1.0f;
                            break;

                        case 1:
                            // Right exit
                            orderDestination.X += 1.0f;
                            break;

                        case 2:
                            // BottomRight exit
                            orderDestination.X += 1.0f;
                            orderDestination.Y -= 1.0f;
                            break;

                        case 3:
                            // BottomLeft exit
                            orderDestination.Y -= 1.0f;
                            break;

                        case 4:
                            // Left exit
                            orderDestination.X -= 1.0f;
                            break;

                        case 5:
                            // TopLeft exit
                            orderDestination.Y += 1.0f;
                            break;
                        }
                    }
                    if (!oddRow)
                    {
                        switch (theOrder)
                        {
                        case 0:
                            // TopRight exit
                            orderDestination.Y += 1.0f;
                            break;

                        case 1:
                            // Right exit
                            orderDestination.X += 1.0f;
                            break;

                        case 2:
                            // BottomRight exit
                            orderDestination.Y -= 1.0f;
                            break;

                        case 3:
                            // BottomLeft exit
                            orderDestination.Y -= 1.0f;
                            orderDestination.X -= 1.0f;
                            break;

                        case 4:
                            // Left exit
                            orderDestination.X -= 1.0f;
                            break;

                        case 5:
                            // TopLeft exit
                            orderDestination.Y += 1.0f;
                            orderDestination.X -= 1.0f;
                            break;
                        }
                    }

                    // Ensure the destination is valid.
                    if ((orderDestination.X > 0 && orderDestination.X < Global.Instance.HexGridSizeX) && (orderDestination.Y > 0 && orderDestination.Y < Global.Instance.HexGridSizeY))
                    {
                        bool combatKilledMe = false;
                        // Check for combat (instant).
                        for (int j = 0; j < ships.Count; j++)
                        {
                            if (ships[j].owner == (int)Global.PlayerOwners.User)
                            {
                                if (ships[j].hexCell.GetCoordinates() == orderDestination)
                                {
                                    combatKilledMe = Combat(AISelectedShip, ships[j]);
                                }
                            }
                        }

                        // Don't continue if player defended successfully against attack.
                        if (combatKilledMe)
                        {
                        }
                        else
                        {
                            // Set course.
                            AISelectedShip.SetDestination(hexes[(int)orderDestination.Y, (int)orderDestination.X], hexes);
                        }
                    }
                    else
                    {
                        // Order is ignored and skipped as it is invalid.
                    }
                }
            }
        }