using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Model : MonoBehaviour {
public const int MAX_ROWS = 20; //最大行数
public const int MAX_COLUMNS = 10; //最大列数
private Transform[,] map = new Transform[MAX_COLUMNS, MAX_ROWS];
/// <summary>
/// 判断是否在地图内
/// </summary>
/// <returns><c>true</c> if this instance is valid map position the specified t; otherwise, <c>false</c>.</returns>
/// <param name="t">T.</param>
public bool IsValidMapPosition(Transform t){
foreach (Transform child in t) {
if (child.tag != "block") continue;
Vector2 pos = child.position.Round();
if (IsInsideMap (pos) == false)
return false;
if (map [(int)pos.x, (int)pos.y] != null)//是否跟别的方块重叠
return false;
}
return true;
}
/// <summary>
/// 是否超出边界
/// </summary>
/// <returns><c>true</c> if this instance is inside map the specified pos; otherwise, <c>false</c>.</returns>
/// <param name="pos">Position.</param>
private bool IsInsideMap(Vector2 pos){
//分别为 左边界 , 右边界 , 下边界
return pos.x >= 0 && pos.x < MAX_COLUMNS && pos.y >= 0;
}
/// <summary>
/// 把落下的方块塞到map数组里
/// </summary>
/// <param name="t">T.</param>
public void PlaceShape(Transform t){
foreach(Transform child in t){ //上一个落下的方块
if (child.tag != "block")
return;
Vector2 pos = child.position.Round();
map [(int)pos.x, (int)pos.y] = child; //把地图上的该位置设置为有对象,下个对象就不会重叠
}
}
}
/// <summary>
/// 判断是否在地图内
/// </summary>
/// <returns><c>true</c> if this instance is valid map position the specified t; otherwise, <c>false</c>.</returns>
/// <param name="t">T.</param>
public bool IsValidMapPosition(Transform t){
foreach (Transform child in t) {
if (child.tag != "block") continue; 这一段看不懂,判断是否block有啥意义?
Vector2 pos = child.position.Round();
if (IsInsideMap (pos) == false)
return false;
if (map [(int)pos.x, (int)pos.y] != null)//是否跟别的方块重叠
return false;
}
return true;
}