예제 #1
0
        private bool rotated; // Rotated = Vertical

        public ShipBox(Ship.Type type)
        {
            this.BackColor             = System.Drawing.Color.Transparent;
            this.BackgroundImageLayout = ImageLayout.Center;
            this.Size     = new System.Drawing.Size(120, 45);
            this.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.rotated  = false;
            this.type     = type;

            switch (type)
            {
            case Ship.Type.AircraftCarrier:
                this.BackgroundImage = Properties.Resources.aircraftCarrier;
                break;

            case Ship.Type.Battleship:
                this.BackgroundImage = Properties.Resources.battleship;
                break;

            case Ship.Type.Cruiser:
                this.BackgroundImage = Properties.Resources.cruiser;
                break;

            case Ship.Type.Destroyer:
                this.BackgroundImage = Properties.Resources.destroyer;
                break;

            case Ship.Type.Submarine:
                this.BackgroundImage = Properties.Resources.submarine;
                break;
            }
        }
예제 #2
0
    /// <summary>
    /// Places the ship.
    /// выбрать режим установки корабля на поле
    /// </summary>
    /// <param name="itype">Itype.</param>
    public void PlaceShip(int itype)
    {
        BattleshipsPlacing placing = uiPlacing.GetComponent <BattleshipsPlacing>();

        //если какой то корабль был выбран, убирается с поля
        if (mPlaceShip != null)
        {
            mSelectionLength = 0;
            mShips.Remove(mPlaceShip);
            Destroy(mPlaceShip);

            placing.RefreshButtonsTexts();
        }

        // заполняются параметры - какой корабль нужно поставить
        // и создается объект на поле
        Ship.Type type = (Ship.Type)itype;

        GameObject ship;

        switch (type)
        {
        case Ship.Type.Destroyer:
            ship = (GameObject)Instantiate(DestroyerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.Cruiser:
            ship = (GameObject)Instantiate(CruiserPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.Battleship:
            ship = (GameObject)Instantiate(BattleshipPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.AircraftCarrier:
            ship = (GameObject)Instantiate(AircraftCarrierPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        default:
            return;
        }

        ship.AddComponent <Ship>();
        Ship subship = ship.GetComponent <Ship>();

        subship.Init(mFieldStart, new Vector2(mFieldWidth, mFieldHeight), type, true, -1, -1);
        mSelectionLength     = subship.GetShipLength();
        mSelectionHorizontal = true;
        mPlaceShip           = ship;

        mShips.Add(ship);

        placing.RefreshButtonsTexts();
        RefreshRedPlanes();
        RefreshBusyCells();
    }
예제 #3
0
        private void AddShipsToPlayerBoard()
        {
            ConsoleGrapher.WriteTextOnLine("Use the arrow keys to position the ship and hit ENTER.", 0);
            ConsoleGrapher.WriteTextOnLine("Hit 'R' to rotate the ship. Hit 'P' to place the ship automatically.\nHit 'F' to finish automatically.", 1);
            Queue <Ship.Type> fleet   = Board.Options.GetFleet();
            bool  finishAutomatically = false;
            Point initialPoint        = default;

            while (fleet.Count != 0 && !finishAutomatically)
            {
                Ship.Type shipType = fleet.Peek();
                bool      userDone = false;
                Ship      ship;
                do
                {
                    ConsoleGrapher.DrawBoardWithSelectedArea(3, player.Board, true, initialPoint, shipType.Size);
                    switch (Console.ReadKey().Key)
                    {
                    case ConsoleKey.Enter: userDone = true; break;

                    case ConsoleKey.LeftArrow: initialPoint.X--; break;

                    case ConsoleKey.UpArrow: initialPoint.Y--; break;

                    case ConsoleKey.RightArrow: initialPoint.X++; break;

                    case ConsoleKey.DownArrow: initialPoint.Y++; break;

                    case ConsoleKey.F: finishAutomatically = userDone = true; break;

                    case ConsoleKey.P: initialPoint = player.GetRandomPossibleInitialPoint(shipType); break;

                    case ConsoleKey.R: shipType.FlipSize(); break;
                    }
                    ship         = new Ship(shipType, initialPoint);
                    initialPoint = MoveShipBackToBoardIfOutside(ship);
                } while (!userDone);
                if (!finishAutomatically && player.Board.IsShipPositionPossible(ship))
                {
                    player.Board.AddShipToBoard(ship);
                    fleet.Dequeue();
                }
                else
                {
                    WriteMessage(new InvalidShipPositionException(new Ship(shipType, initialPoint)).Message);
                }
            }
            player.AutomaticallyAddFleetToBoard(fleet);
        }
예제 #4
0
    /// <summary>
    /// Gets the type of the ships count by.
    /// посчитать количество кораблей типа type на поле
    /// </summary>
    /// <returns>The ships count by type.</returns>
    /// <param name="type">Type.</param>
    public int GetShipsCountByType(Ship.Type type)
    {
        int res = 0;

        foreach (var ship in mShips)
        {
            Ship subship = ship.GetComponent <Ship>();

            if (subship.GetShipType() == type)
            {
                ++res;
            }
        }

        return(res);
    }
예제 #5
0
        private void PlaceShip(Ship.Type type, int length)
        {
            Square[] squares = new Square[length];
            int      indexI, indexJ;
            int      rotate; // 0 = Horizontal, 1 = Vertical
            bool     isPossible;

            do
            {
                rotate = r.Next(2);

                if (rotate == 0) // Horizontal
                {
                    indexI     = r.Next(0, (GameSettings.Default.BoardSize - length));
                    indexJ     = r.Next(0, (GameSettings.Default.BoardSize));
                    isPossible = ((indexI == 0) || (this.board[(indexI - 1), indexJ].GetShip() == null)) && (((indexI + length) == GameSettings.Default.BoardSize) || this.board[(indexI + length), indexJ].GetShip() == null);

                    for (int i = 0; isPossible && i < length; i++)
                    {
                        squares[i] = this.board[indexI + i, indexJ];
                        if ((this.board[indexI + i, indexJ].GetShip() != null) || ((indexJ != 0) && (this.board[indexI + i, (indexJ - 1)].GetShip() != null)) || (((indexJ + 1) != GameSettings.Default.BoardSize) && (this.board[indexI + i, (indexJ + 1)].GetShip() != null)))
                        {
                            isPossible = false;
                        }
                    }
                }

                else // Vertical
                {
                    indexI     = r.Next(0, (GameSettings.Default.BoardSize - 1));
                    indexJ     = r.Next(0, (GameSettings.Default.BoardSize - length - 1));
                    isPossible = ((indexJ == 0) || (this.board[indexI, (indexJ - 1)].GetShip() == null)) && (((indexJ + length) == GameSettings.Default.BoardSize) || (this.board[indexI, (indexJ + length)].GetShip() == null));

                    for (int i = 0; isPossible && i < length; i++)
                    {
                        squares[i] = this.board[indexI, indexJ + i];
                        if ((this.board[indexI, indexJ + i].GetShip() != null) || ((indexI != 0) && (this.board[(indexI - 1), indexJ + i].GetShip() != null)) || (((indexI + 1) != GameSettings.Default.BoardSize) && (this.board[(indexI + 1), indexJ + i].GetShip() != null)))
                        {
                            isPossible = false;
                        }
                    }
                }
            } while (!isPossible);

            AddShip(new Ship(squares, type));
        }
예제 #6
0
        public static string ToPL(this Ship.Type type)
        {
            switch (type)
            {
            case Ship.Type.Battleship:
                return("Pancernik");

            case Ship.Type.Destroyer:
                return("Niszczyciel");

            case Ship.Type.Submarine:
                return("Okręt podwodny");

            case Ship.Type.Cruiser:
                return("Krążownik");
            }
            throw new NotImplementedException();
        }
예제 #7
0
 /// <summary>
 /// Finds a space for a specific ship size randomly on the board by picking
 /// random initial points until the whole ship fits, or throws an exception.
 /// </summary>
 public Point GetRandomPossibleInitialPoint(Ship.Type shipType)
 {
     System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
     // Avoid looking for initial point for too long. Not the best solution...
     while (watch.ElapsedMilliseconds < Board.Size.Height * Board.Size.Width * 100)
     {
         // Flip the ship to make sure the ship fits on the board OR pick a random ship orientation.
         if (shipType.Size.Height > Board.Size.Height || shipType.Size.Height > Board.Size.Width || random.Next() % 2 == 0)
         {
             shipType.FlipSize();
         }
         Point initialPoint;
         initialPoint.X = random.Next(Board.Size.Width - shipType.Size.Width + 1);
         initialPoint.Y = random.Next(Board.Size.Height - shipType.Size.Height + 1);
         if (Board.IsShipPositionPossible(new Ship(shipType, initialPoint)))
         {
             return(initialPoint);
         }
     }
     throw new BoardIsFullException(shipType.Size, Board);
 }
예제 #8
0
    /// <summary>
    /// Places the ship.
    /// поставить кораблья на определенной клетке, в определенном положении
    /// </summary>
    /// <param name="itype">Itype.</param>
    /// <param name="horizontal">If set to <c>true</c> horizontal.</param>
    /// <param name="x">The x coordinate.</param>
    /// <param name="y">The y coordinate.</param>
    /// <param name="visible">If set to <c>true</c> visible.</param>
    public void PlaceShip(int itype, bool horizontal, int x, int y, bool visible)
    {
        BattleshipsPlacing placing = uiPlacing.GetComponent <BattleshipsPlacing>();

        Ship.Type type = (Ship.Type)itype;

        GameObject ship;

        switch (type)
        {
        case Ship.Type.Destroyer:
            ship = (GameObject)Instantiate(DestroyerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.Cruiser:
            ship = (GameObject)Instantiate(CruiserPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.Battleship:
            ship = (GameObject)Instantiate(BattleshipPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        case Ship.Type.AircraftCarrier:
            ship = (GameObject)Instantiate(AircraftCarrierPrefab, new Vector3(0, 0, 0), Quaternion.identity);
            break;

        default:
            return;
        }

        ship.AddComponent <Ship>();
        Ship subship = ship.GetComponent <Ship>();

        subship.Init(mFieldStart, new Vector2(mFieldWidth, mFieldHeight), type, horizontal, x, y);
        ship.SetActive(visible);

        mShips.Add(ship);
        RefreshBusyCells();
    }
예제 #9
0
        public Particle(Vector2 pos, Vector2 dir, Ship.Type shipColor)
        {
            // Class attributes
            Bounds.Width  = 10;
            Bounds.Height = 4;
            TopSpeed      = 1200;

            Position.X = pos.X;
            Position.Y = pos.Y;

            Velocity = dir;
            if (Velocity.LengthSquared() > 0)
            {
                Velocity = Vector2.Multiply(Velocity, TopSpeed);
            }

            SetBounds();

            Rotation  = dir.GetAngle();
            ShipColor = shipColor;

            TimeToLive = LifeLength;
        }
예제 #10
0
    }                                                           // The current Faction of the ship

    public virtual void SetupShip(Ship.Type npcType, Ship.Faction npcFaction, int identifier, float npcHealth, float npcSpeed, float npcFireRate, float npcBurstRate, float npcFireDamage, float npcFireRange, Sprite npcBullet, bool isPlayer)
    {
        SetShipType(npcType);
        SetShipFaction(npcFaction);
        SetMaxHealth(npcHealth);
        SetSpeed(npcSpeed);
        IncreaseLevel(1);
        HealthToMaxHealth();
        shipBullet     = npcBullet;
        fireRate       = npcFireRate;
        fireBurstCount = npcBurstRate;
        fireDamage     = npcFireDamage;
        fireRange      = npcFireRange;
        Identifier     = identifier;

        gameObject.tag = shipFaction.ToString();

        if (isPlayer == true)
        {
            return;
        }
        InvokeRepeating("UpdateNearestEnemy", Random.Range(1.25f, 6.75f), Random.Range(1.25f, 6.75f));
    }
예제 #11
0
 public void SpawnRandomShip(Vector2 pos, bool isPlayer = false)
 {
     Ship.Type    npcType    = GetRandomShipType();
     Ship.Faction npcFaction = GetRandomShipFaction();
     SpawnManager.current.SpawnShip(npcType, npcFaction, pos, isPlayer);
 }
예제 #12
0
    public void SpawnShip(Ship.Type shipType, Ship.Faction shipFaction, Vector2 position, bool isPlayer = false)
    {
        Sprite shipSprite;
        Sprite bulletSprite;
        int    minHealth;
        int    maxHealth;
        float  engineOffset    = 0f;
        float  healthBarOffset = 0f;
        float  shipSpeed       = 0f;
        float  fireRate        = 0f;
        float  fireDamage      = 0f;
        float  fireRange       = 0f;
        float  fireBurstCount  = 1f;

        switch (shipType)
        {
        case Ship.Type.TraderShip:
            minHealth       = 20;
            maxHealth       = 50;
            engineOffset    = 0f;
            healthBarOffset = 1.25f;
            shipSpeed       = 3.25f;
            fireRate        = 3f;
            fireDamage      = 5f;
            fireBurstCount  = 4f;
            fireRange       = 20f;

            bulletSprite = ObjectController.current.traderShipBullet;
            shipSprite   = ObjectController.current.traderShip;
            break;

        case Ship.Type.PrisonShip:
            minHealth       = 50;
            maxHealth       = 150;
            engineOffset    = 0.25f;
            healthBarOffset = 1.625f;
            shipSpeed       = 2.5f;
            fireRate        = 3f;
            fireDamage      = 7.5f;
            fireBurstCount  = 5f;
            fireRange       = 22f;

            bulletSprite = ObjectController.current.prisonShipBullet;
            shipSprite   = ObjectController.current.prisonShip;
            break;

        case Ship.Type.FighterShip:
            minHealth       = 50;
            maxHealth       = 100;
            engineOffset    = -0.125f;
            healthBarOffset = 1f;
            shipSpeed       = 3.5f;
            fireRate        = 2f;
            fireDamage      = 5f;
            fireBurstCount  = 9f;
            fireRange       = 25f;

            bulletSprite = ObjectController.current.fighterShipBullet;
            shipSprite   = ObjectController.current.fighterShip;
            break;

        case Ship.Type.AdvancedFighterShip:
            minHealth       = 100;
            maxHealth       = 350;
            engineOffset    = 0.175f;
            healthBarOffset = 1.275f;
            shipSpeed       = 4.75f;
            fireRate        = 2.5f;
            fireDamage      = 15f;
            fireBurstCount  = 4f;
            fireRange       = 30f;

            bulletSprite = ObjectController.current.advancedFighterShipBullet;
            shipSprite   = ObjectController.current.advancedFighterShip;
            break;

        case Ship.Type.HeavyFighterShip:
            minHealth       = 250;
            maxHealth       = 500;
            engineOffset    = 0.300f;
            healthBarOffset = 1.75f;
            shipSpeed       = 2.325f;
            fireRate        = 4.5f;
            fireDamage      = 25f;
            fireBurstCount  = 3f;
            fireRange       = 36f;

            bulletSprite = ObjectController.current.heavyFighterShipBullet;
            shipSprite   = ObjectController.current.heavyFighterShip;
            break;

        case Ship.Type.DestroyerShip:
            minHealth       = 500;
            maxHealth       = 1000;
            engineOffset    = 0.500f;
            healthBarOffset = 2.00f;
            shipSpeed       = 2.125f;
            fireRate        = 4f;
            fireDamage      = 10f;
            fireBurstCount  = 8f;
            fireRange       = 50f;

            bulletSprite = ObjectController.current.destroyerShipBullet;
            shipSprite   = ObjectController.current.destroyerShip;
            break;

        case Ship.Type.DroneShip:
            minHealth       = 25;
            maxHealth       = 65;
            engineOffset    = -0.5f;
            healthBarOffset = 0.75f;
            shipSpeed       = 5f;
            fireRate        = 2f;
            fireDamage      = 2.5f;
            fireBurstCount  = 12f;
            fireRange       = 28f;

            bulletSprite = ObjectController.current.droneShipBullet;
            shipSprite   = ObjectController.current.droneShip;
            break;

        default:
            minHealth  = 50;
            maxHealth  = 125;
            shipSpeed  = 2.5f;
            fireDamage = 5f;
            fireRate   = 2f;
            fireRange  = 20f;

            bulletSprite = ObjectController.current.fighterShipBullet;
            shipSprite   = ObjectController.current.fighterShip;
            break;
        }

        // Create the body and attach sprites
        var npc_body = Object.Instantiate(ObjectController.current.shipPrefab, GameController.current.canvas.transform);

        Character npc;

        if (isPlayer)
        {
            npc = npc_body.AddComponent <Player> ();
        }
        else
        {
            npc = npc_body.AddComponent <NPC> ();
        }

        var npc_sprite = npc_body.GetComponent <SpriteRenderer> ();

        npc_sprite.sprite = shipSprite;

        var npc_thrusters = Object.Instantiate(ObjectController.current.enginePrefab, npc_body.transform);

        npc_thrusters.transform.position = new Vector3(npc_body.transform.position.x, npc_body.transform.position.y - engineOffset, npc_body.transform.position.z);

        var npc_healthBar = Object.Instantiate(ObjectController.current.healthBarPrefab, npc_body.transform);

        npc_healthBar.transform.position = new Vector3(npc_body.transform.position.x, npc_body.transform.position.y + healthBarOffset, npc_body.transform.position.z);

        // Set up RigidBody2D and Collider
        var npc_rb = npc_body.AddComponent <Rigidbody2D> ();

        npc_rb.gravityScale           = 0f;
        npc_rb.isKinematic            = false;
        npc_rb.collisionDetectionMode = CollisionDetectionMode2D.Discrete;

        var npc_col = npc_body.AddComponent <PolygonCollider2D> ();

        int identifier = Random.Range(100, 999);

        npc.gameObject.name = shipType + "" + identifier + " [" + shipFaction + "]";

        // Initial Setup
        npc.SetupShip(shipType, shipFaction, identifier, Random.Range(minHealth, maxHealth), shipSpeed, fireRate, fireBurstCount, fireDamage, fireRange, bulletSprite, isPlayer);
        npc_body.transform.position = position;

        // Final Setup
        npc_rb.mass = npc.MaxHealth;

        //Debug.Log ("Spawned Ship of type \'<b>" + shipType + "</b>\' and of faction \'<b>" + shipFaction + "</b>\'.");
    }
예제 #13
0
 public Ship CreateRandomShip(Ship.Type type)
 {
     return(CreateRandomShip((int)type));
 }
        public void AddShip(Ship.Type type, int startX, int startY, Ship.Position position)
        {
            if (startX >= ocean.Size)
            {
                throw new ArgumentException(string.Empty, "X argument over board size!.");
            }
            if (startY >= ocean.Size)
            {
                throw new ArgumentException(string.Empty, "Y argument over board size!.");
            }

            List <Square> newShipSquares = new List <Square>();
            int           nextX          = startX;
            int           nextY          = startY;

            int maxX = 0;
            int maxY = 0;
            int minX = startX;
            int minY = startY;

            for (int i = 0; i < (int)type; i++)
            {
                Square nextSquare;
                try
                {
                    nextSquare = ocean.Board.ElementAt(nextX).ElementAt(nextY);
                }
                catch (ArgumentOutOfRangeException)
                {
                    throw new ArgumentException(string.Empty, "Ship size and position would place if over board");
                }

                if (null != nextSquare.Ship)
                {
                    throw new ArgumentException(string.Empty, "Ship alredy exists on selected squares");
                }
                if (null != nextSquare.Ship)
                {
                    throw new ArgumentException(string.Empty, "Ship alredy exists on selected squares");
                }
                newShipSquares.Append(nextSquare);

                if (maxX < nextX)
                {
                    maxX = nextX;
                }
                if (maxY < nextY)
                {
                    maxY = nextY;
                }

                switch (position)
                {
                case Ship.Position.DOWN:
                    startY++;

                    break;

                case Ship.Position.RIGHT:
                    startX++;

                    break;
                }
            }

            for (int i = minX; i < maxX; i++)
            {
                for (int j = minY; j < maxY; j++)
                {
                    if (null != ocean.Board.ElementAt(i).ElementAt(j).Ship)
                    {
                        throw new ArgumentException(string.Empty, "Ship position collides with existing ship");
                    }
                }
            }

            Ship newShip = new Ship(type, newShipSquares);

            ships.Append(newShip);
            foreach (Square square in newShipSquares)
            {
                square.Ship = newShip;
            }
        }
예제 #15
0
        public void AutomaticallyAddShipToBoard(Ship.Type shipType)
        {
            Point point = GetRandomPossibleInitialPoint(shipType);

            Board.AddShipToBoard(new Ship(shipType, point));
        }
예제 #16
0
 public void SetShipType(Ship.Type newType)
 {
     shipType = newType;
 }