Exemple #1
0
        public Hex[] GoTo(Hex to)
        {
            if (Location == new Hex(-11, 0))
            {
            }

            if (Location == to)
            {
                return(new Hex[0]);
            }
            var possible = HexManager.WithinRange(Location, Speed);

            possible = possible.OrderBy(p => p.Distance(to)).ToArray();
            return(possible);
        }
Exemple #2
0
        public void Start()
        {
            BodyLocations = new List <Body>();

            var halfRadius = (int)Mathf.Round(MapSize / 2f);

            StartingLocations = new List <Hex> {
                new Hex(0, -halfRadius),
                new Hex(halfRadius, -halfRadius),
                new Hex(-halfRadius, 0),
                new Hex(-halfRadius, halfRadius),
                new Hex(0, halfRadius),
                new Hex(halfRadius, 0),
            };

            var playerLoc = Spawn(PlayersTemplates[PlayerID]).ToPixel();
            var pos       = new Vector3(playerLoc.x, playerLoc.y, -1);

            Camera.transform.position = pos;

            var boardTiles = new List <Hex>(HexManager.WithinRange(new Hex(0, 0), MapSize));

            for (var i = 0; i < boardTiles.Count; i++)
            {
                var hex   = boardTiles[i];
                var pixel = hex.ToPixel();
                var h     = Instantiate(HexPrefab, TilesParent);
                h.transform.position = pixel;
            }

            for (var i = 0; i < NPCCount; i++)
            {
                Spawn(PlayersTemplates[UnityEngine.Random.Range(0, PlayersTemplates.Count)]);
            }

            foreach (var p in Players)
            {
                foreach (var s in p.Ships)
                {
                    s.Color = p.Color;
                }
            }

            LoopShips(new Action <Ship>(s => {
                boardTiles.Remove(s.Location);
            }));
            boardTiles.Remove(Hex.Zero);

            for (var i = 0; i < 100; i++)
            {
                var bodyLocation = boardTiles[UnityEngine.Random.Range(0, boardTiles.Count)];
                var b            = Instantiate(BodyPrefab, BodyParent);
                b.ToHexPosition(bodyLocation);
                b.GetComponent <SpriteRenderer>().sprite = Bodies[UnityEngine.Random.Range(0, Bodies.Count)];
                BodyLocations.Add(b.GetComponent <Body>());
            }

            Hex Spawn(Player template)
            {
                var startHex = StartingLocations[UnityEngine.Random.Range(0, StartingLocations.Count)];

                StartingLocations.Remove(startHex);
                PlayersTemplates.Remove(template);

                var stationObject = Instantiate(StationPrefab, EntityParent);

                stationObject.ToHexPosition(startHex);
                template.Ships.Add(stationObject.Ship());

                var minerPositions = new List <Hex>()
                {
                    new Hex(startHex.Q, startHex.R - 1),
                    new Hex(startHex.Q, startHex.R + 1),
                    new Hex(startHex.Q - 1, startHex.R + 1),
                    new Hex(startHex.Q + 1, startHex.R - 1),
                };

                var fighterPositions = new List <Hex>()
                {
                    new Hex(startHex.Q - 1, startHex.R),
                    new Hex(startHex.Q + 1, startHex.R),
                };

                for (var i = 0; i < minerPositions.Count; i++)
                {
                    var minerObj = Instantiate(MinerPrefab, EntityParent);
                    minerObj.ToHexPosition(minerPositions[i]);
                    template.Ships.Add(minerObj.Ship());
                }

                for (var i = 0; i < fighterPositions.Count; i++)
                {
                    var fighterObj = Instantiate(FighterPrefab, EntityParent);
                    fighterObj.ToHexPosition(fighterPositions[i]);
                    template.Ships.Add(fighterObj.Ship());
                }

                foreach (var s in template.Ships)
                {
                    s.PlayerID = template.ID;
                }
                Players.Add(template);

                return(startHex);
            }
        }
        public void Update()
        {
            foreach (var p in Board.Players)
            {
                for (var i = 0; i < p.Ships.Count; i++)
                {
                    if (p.Ships[i] == null)
                    {
                        p.Ships.RemoveAt(i);
                        i--;
                    }
                }
            }

            if (HasChanged == null)
            {
                HasChanged = new List <Ship>();
            }

            if (Input.GetMouseButtonDown(0))
            {
                var mousePos    = Camera.ScreenToWorldPoint(Input.mousePosition);
                var mousePosHex = Hex.Round(HexManager.PixelToHex(mousePos));

                if (!selectionMode)
                {
                    Board.LoopShips(new Action <Ship>(s =>
                    {
                        if (s.PlayerID == Board.PlayerID &&
                            !HasChanged.Contains(s) &&
                            s.Location == mousePosHex &&
                            s.Type != "Station") // If you're selecing one of your ships that hasn't moved yet.
                        {
                            LastSelected = s;
                            Selection.SetActive(true);
                            Selection.ToHexPosition(mousePosHex);
                            selectionMode = true;

                            Highlighted = new List <Transform>();
                            var range   = HexManager.WithinRange(s.Location, s.Speed);
                            foreach (var r in range)
                            {
                                var highlight = Instantiate(Selection);
                                highlight.ToHexPosition(r);
                                var sr    = highlight.GetComponent <SpriteRenderer>();
                                sr.sprite = HighlightSprite;
                                var c     = Board.Players.Where(p => p.ID == s.PlayerID).First().Color;
                                sr.color  = new Color(c.r, c.g, c.b, 0.5f);
                                Highlighted.Add(highlight.transform);
                            }
                        }
                    }), Board.PlayerID);
                }
                else
                {
                    var didChange = false;
                    if (LastSelected.Location.Distance(mousePosHex) > LastSelected.Speed)
                    {
                        return;
                    }

                    var clickedOnShip = false;
                    Board.LoopShips(new Action <Ship>(s =>
                    {
                        if (s.Location == mousePosHex)
                        {
                            clickedOnShip = true;
                        }

                        if (s.Location == mousePosHex)
                        {
                            if (s.PlayerID == LastSelected.PlayerID && s.Type == "Carrier" && LastSelected.Type == "Fighter")
                            {
                                // Carrier Dock Code Here
                                didChange = true;
                            }
                            else if (s.PlayerID != LastSelected.PlayerID)
                            {
                                s.Health            -= LastSelected.Damage - 0.5f;
                                LastSelected.Health -= s.Damage;

                                if (s.Health <= 0)
                                {
                                    s.IsDestroyed = true;
                                }
                                if (LastSelected.Health <= 0)
                                {
                                    LastSelected.IsDestroyed = true;
                                }
                                didChange = true;
                            }
                        }
                    }));

                    if (!clickedOnShip)
                    {
                        if (mousePosHex.Distance(LastSelected.Location) <= LastSelected.Speed)
                        {
                            LastSelected.MoveTo(mousePosHex);
                            didChange = true;
                        }
                    }

                    if (didChange)
                    {
                        HasChanged.Add(LastSelected);
                        LastSelected  = null;
                        selectionMode = false;
                        Selection.SetActive(false);
                        foreach (var h in Highlighted)
                        {
                            Destroy(h.gameObject);
                        }
                        Highlighted = new List <Transform>();
                    }
                }
            }
        }