noobtus tetris游戏统一检查有效网格位置

时间:2018-10-11 12:35:37

标签: unity3d

谁能为我解释为什么我们必须执行此功能。我阅读了说明,但不知道为什么。网站:https://noobtuts.com/unity/2d-tetris-game/

bool isValidGridPos() {        
     foreach (Transform child in transform) {
         Vector2 v = Grid.roundVec2(child.position);

         // Not inside Border?
         if (!Grid.insideBorder(v))
             return false;

         // Block in grid cell (and not part of same group)?
         if (Grid.grid[(int)v.x, (int)v.y] != null &&
             Grid.grid[(int)v.x, (int)v.y].parent != transform)
             return false;
     }
     return true;
 }

2 个答案:

答案 0 :(得分:1)

  

该功能非常易于理解。首先,它通过使用foreach遍历每个孩子,然后将孩子的舍入位置存储在变量中。然后,它会找出该位置是否在边界内,然后找出在同一网格条目中是否已经存在一个块。

换句话说-每次移动或旋转形状时,都必须检查该位置是否有效。

您可以通过检查形状的位置是否在边界内以及该形状是否与其他任何形状相交来实现。

如果超出范围或与其他形状相交,则恢复上一个动作。

操作:exports.handler = (event, context, callback) => { console.log ("Trigger function =", event.triggerSource); // Send post authentication data to Cloudwatch logs if (event.request.userAttributes.email.endsWith('@mydomain.com')) { console.log ("Authentication successful: ", event.request); callback(null, event); } else { console.log ("Authentication failed: ", event.request); callback("can't connect to admin", event) } };

还原:transform.Rotate(0, 0, -90);

transform.Rotate(0, 0, 90);

答案 1 :(得分:0)

该功能非常简单。布尔值isValidGridPos检查一个组中的所有子级是否都有效,例如可以移动。

bool isValidGridPos() {        
    foreach (Transform child in transform) { // Loop through all childs
        Vector2 v = Grid.roundVec2(child.position); //Get the position as Int (rounded number or floored number)

        // Not inside Border?
        if (!Grid.insideBorder(v)) // Checks if the Position is in the Game
            return false;

        // Block in grid cell (and not part of same group)?
        if (Grid.grid[(int)v.x, (int)v.y] != null &&
            Grid.grid[(int)v.x, (int)v.y].parent != transform)
            return false;
    }
    return true;
}
相关问题