Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
俄罗斯方块课程的判断问题
sdewqazxcds发起了问答2018-11-13
1
回复
231
浏览
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;
    }

所有回复
  • 老师_Trigger 2018-11-14

    同学你好,因为我们还有一个子是锚点,不是锚点的要判断当前的有效位置并且移动,但是锚点是不进行的,所以如果不是组成块直接跳出foreach循环。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!