using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoardMyGenerator : MonoBehaviour
{
[SerializeField] GameObject blockTemplate;
[SerializeField] GameObject blockRoot;
Vector3 blockSize = new Vector3(0.7f, 0.7f, 0);
public int totalRows ;
public int totalColumns ;
public float blockOverlapAmount ;
public float blockSpace ;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GenerateBoard();
}
}
public void GenerateBoard()
{
float startPointY = GetStartPointY();
// Will keep updating with iterations.
float currentPositionY = startPointY;
for (int row = 0; row < totalRows; row++)
{
int thisRowBlockCount = GetBlockCountOnRow(row);
float startPointX = GetStartPointX(thisRowBlockCount);
float currentPositionX = startPointX;
for (int column = 0; column < thisRowBlockCount; column++)
{
// Spawn a block instance and prepares it.
GameObject blockElement = GetBlockInsideGrid(row, column);
blockElement.transform.localPosition = new Vector3(currentPositionX, currentPositionY, 0);
currentPositionX += (blockSize.x + blockSpace);
currentPositionX -= blockOverlapAmount;
//blockElement.sizeDelta = blockSize;
blockElement.name = "block-" + row + "" + column;
Sets blocks logical position inside grid and its default sprite.
//Block block = blockElement.GetComponent<Block>();
//block.gameObject.SetActive(true);
//block.SetBlockLocation(row, column);
//blockRow.Add(block);
//blockElement.GetComponent<Image>().sprite = blockBGSprite;
//block.assignedSpriteTag = defaultSpriteTag;
}
//currentPositionX = startPointX;
//currentPositionY -= (blockSize.y + blockSpace);
currentPositionY -= (blockSize.y + (blockSpace));
//GamePlayUI.Instance.gamePlay.allRows.Add(blockRow);
}
}
public GameObject GetBlockInsideGrid(int rowId, int columnId)
{
GameObject block = (GameObject)(Instantiate(blockTemplate)) as GameObject;
block.transform.SetParent(blockRoot.transform);
block.transform.localScale = Vector3.one;
if ((rowId < (totalRows / 2) && (columnId % 2 == 0)) || (rowId >= (totalRows / 2) && (columnId % 2 != 0)))
{
block.transform.localEulerAngles = Vector3.zero;
}
else
{
block.transform.localEulerAngles = new Vector3(0, 0, 180);
}
return block;
}
/// <summary>
/// Vertical position from where block grid will start.
/// </summary>
private float GetStartPointY()
{
float totalHeight = (blockSize.y * totalRows) + (totalRows - 1) * blockSpace;
return ((totalHeight / 2) - (blockSize.y / 2));
}
private int GetBlockCountOnRow(int rowId)
{
if (rowId < (totalRows / 2))
{
return totalRows + (rowId * 2) + 1;
}
else
{
return totalRows + ((totalRows - rowId) * 2) - 1;
}
}
/// <summary>
/// Horizontal position from where block grid will start.
/// </summary>
private float GetStartPointX(int columnSize)
{
float totalWidth = ((blockSize.x - blockOverlapAmount) * columnSize) + ((columnSize - 1) * blockSpace);
return -((totalWidth / 2) - ((blockSize.x - blockOverlapAmount) / 2));
}
}
生成三角形棋盘格 triangle
最新推荐文章于 2024-10-09 09:14:36 发布