Exemple #1
0
        public void Update()
        {
            var mousePos     = GetComponent <Camera>().ScreenToWorldPoint(Input.mousePosition);
            var mouseGamePos = Cube.Round(HexManager.PixelToHex(mousePos).ToCube()).ToHex();

            CoordinateText.text    = string.Format("X: {0}, Y: {1}", mouseGamePos.Q, mouseGamePos.R);
            OutlineSprite.position = mouseGamePos.ToPixel();

            Ship hoveringShip = null;

            Board.LoopShips(new System.Action <Ship>(a =>
            {
                if (a.Location == mouseGamePos)
                {
                    hoveringShip = a;
                }
            }));

            if (hoveringShip == null)
            {
                OutlineSprite.gameObject.GetComponent <OutlineText>().Text = "";
            }
            else
            {
                var playerName  = Board.Players.Where(p => p.ID == hoveringShip.PlayerID).First().Name;
                var playerColor = Board.Players.Where(p => p.ID == hoveringShip.PlayerID).First().Color;
                var shipName    = hoveringShip.Type;
                OutlineSprite.gameObject.GetComponent <OutlineText>().Text            = playerName + " - " + shipName;
                OutlineSprite.gameObject.GetComponent <OutlineText>().TextImage.color = playerColor;
            }
        }
Exemple #2
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);
        }
        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>();
                    }
                }
            }
        }
 public static Hex GetHexPosition(this GameObject o)
 {
     return(Hex.Round(HexManager.PixelToHex(o.transform.position)));
 }