コード例 #1
0
    private void createMap()
    {
        map = new HexMap(UnityEngine.Random.Range(minSize, maxSize), UnityEngine.Random.Range(minSize, maxSize));
        for (int column = 0; column < HexMap.NumColumns; column++)
        {
            for (int row = 0; row < HexMap.NumRows; row++)
            {
                // Instantiate a Hex
                HexContinent h   = new HexContinent(column, row);
                Vector3      pos = h.hex.PositionFromCamera(
                    Camera.main.transform.position,
                    HexMap.NumRows,
                    HexMap.NumColumns
                    );


                GameObject hexGO = (GameObject)Instantiate(
                    HexPrefab,
                    pos,
                    Quaternion.identity,
                    mapHolder.transform
                    );
                HexComponent hexC = hexGO.GetComponent <HexComponent>();
                hexC.Hex = h;
                //hexC.Hex.hex.HexMap = map;

                map.AddHex(hexC.Hex);

                // MeshRenderer mr = hexGO.GetComponentInChildren<MeshRenderer>();
                // mr.material = HexMaterials[Random.Range(0, HexMaterials.Length)];
            }
        }
        JsonManager <HexMap> .saveJson(map);
    }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: alexmccloy/HexMap
        /// <summary>
        /// Mouse has been clicked on the canvas. Depending on the radio button that is active either
        ///     - Add a new hex
        ///     - Select (or unselect a hex). If shift is held select multiple
        ///     - Delete a hex
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cnv_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //TODO add some sort of undo feature here - deleting hexes and not being able to recover them may be very bad down the line

            //Unselect the hex before adding or deleting
            if (!_selectButton.IsChecked ?? true)
            {
                _selectedHexes = new List <Hex>();
            }

            // Determine which point was clicked after calculating offset
            var clickedPoint = new Point(e.GetPosition(_canvas).X - +_offset.X, e.GetPosition(_canvas).Y - _offset.Y);

            if (_addButton.IsChecked ?? false)
            {
                //Add a new hex under the mouse if one does not already exist - if one exists do nothing
                var cubicCoords = HexUtils.CartesianToCubic(Utils.PointToPoint(clickedPoint), _hexMap.Orientation, _hexMap.Width);
                try {
                    _hexMap.AddHex(cubicCoords);
                }
                catch (ArgumentException) {
                    //This means a hex already exists at that location
                    Console.WriteLine($"Not adding hex at {cubicCoords} as it already exists");
                }

                Console.WriteLine($"Adding hex at {cubicCoords}");
            }
            else if (_selectButton.IsChecked ?? false)
            {
                //Select the hex under the mouse if it exists
                var key = HexUtils.CartesianToCubic(Utils.PointToPoint(clickedPoint), _hexMap.Orientation, _hexMap.Width);

                if (_hexMap.Hexes.ContainsKey(key))
                {
                    var clickedHex = _selectedHexes.FirstOrDefault(hex => hex.Coordinate.Equals(key));
                    if (clickedHex != null)
                    {
                        if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                        {
                            //CLicked on a hex that is already selected - unselect it
                            _selectedHexes.Remove(clickedHex);
                        }
                        else
                        {
                            if (_selectedHexes.Count > 1)
                            {
                                //Multiple hexes are selected so deselect all of them except for the one clicked
                                _selectedHexes = new List <Hex>()
                                {
                                    _hexMap.Hexes[key]
                                };
                            }
                            else
                            {
                                _selectedHexes.Clear();
                            }
                        }
                    }
                    else
                    {
                        //Check if selecting multiple hexes
                        if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                        {
                            _selectedHexes.Add(_hexMap.Hexes[key]);
                        }
                        else
                        {
                            _selectedHexes = new List <Hex>()
                            {
                                _hexMap.Hexes[key]
                            };
                        }
                    }

                    RedrawHexes();
                }
            }
            else if (_deleteButton.IsChecked ?? false)
            {
                //Delete the hex that was clicked
                var key = HexUtils.CartesianToCubic(Utils.PointToPoint(clickedPoint), _hexMap.Orientation, _hexMap.Width);

                if (_hexMap.Hexes.ContainsKey(key))
                {
                    _hexMap.Hexes.Remove(key);
                    RedrawHexes();
                }
            }
            else
            {
                Console.WriteLine("Error - no radio buttons are selected?? or all of them are null somehow");
            }

            RedrawHexes();
        }