unity3d 如何格式化球员在六边形的位置

afdcj2ne  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(135)

我需要格式化球员在六边形位置像这样

目前我设法将它们格式化成螺旋图案,但我不知道如何将它们设置成六边形。2这是我的螺旋形成代码。

private void FormatSpiralPlacement()
{
    _enemyCount = enemyParent.childCount;
    var index = 0;

    foreach (Transform child in enemyParent)
    {
        var angle = AngleStep * index * angleFactor * Mathf.Deg2Rad;
        var radius = radiusFactor * Mathf.Sqrt(index);
        var pos = new Vector3(radius * Mathf.Cos(angle), child.position.y, radius * Mathf.Sin(angle));
        child.localPosition = pos;
        index++;
    }
}
blpfk2vs

blpfk2vs1#

我使用windows窗体获取位置。您可以根据需要修改。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        const int RADIUS = 10;
        Player[] players = new Player[54];
        public Form1()
        {
            InitializeComponent();
            Player.HexPosition(players, RADIUS);
            Player.AddToPanel(players, panel1);
            
            
        }
        public class Player
        {
            public Point location { get; set; }
            static int[] numberColumns = { 4, 5, 6, 7, 8, 7, 6, 6, 5 };

            public Player() { }
            public Player(int x, int y)
            {
                this.location = new Point(x, y);
            }

            public static void HexPosition(Player[] players,int radius)
            {
                int playerCount = 0;
                Point firstPlayer = new Point(9 * radius, 2 * radius);
                for (int row = 0; row < 9; row++)
                {
                    int y = 0;
                     y = 2 * row * radius;
                    int firstCol = 0;
                    if (row < 5)
                    {
                        firstCol = firstPlayer.X - (row * radius);
                    }
                    else
                    {
                        firstCol = firstPlayer.X - (radius * (8 - row));
                    }
                    
                    for (int col = 0; col < numberColumns[row]; col++)
                    {
                        int x = firstCol + (2 * col * radius);
                        Point point = new Point(x, y);
                        players[playerCount] = new Player(x,y);
                        Console.WriteLine("index = {0}, x = {1}, y = {2}, row = {3}, col = {4}", playerCount, x, y, row, col);
                        playerCount++;
                    }
                }
            }
            public static void AddToPanel(Player[] players,Panel panel)
            {
                panel.Size = new Size(24 * RADIUS, 22 * RADIUS);
                foreach (Player player in players)
                {
                    ShapeContainer container = new ShapeContainer();
                    container = new ShapeContainer() { Size = new Size(2 * RADIUS, 2 * RADIUS)};
                    OvalShape circle = new OvalShape(player.location.X, player.location.Y, 2 * RADIUS, 2 * RADIUS);
                    container.Shapes.AddRange(new Microsoft.VisualBasic.PowerPacks.Shape[] { circle} );
                    panel.Controls.Add(container);
                }
            }
        }

    }
}

这是结果

相关问题