public bool IsPushable(Vector2Int pos, GameElement.Direction dir) { Vector2Int force = Vector2Int.zero; switch (dir) { case GameElement.Direction.Up: force = Vector2Int.up; break; case GameElement.Direction.Down: force = Vector2Int.down; break; case GameElement.Direction.Left: force = Vector2Int.left; break; case GameElement.Direction.Right: force = Vector2Int.right; break; } if (GetMirror(pos) != null || GetBox(pos) != null) { if (walls.Contains(pos + force)) { return(false); } if (GetDetector(pos + force) != null) { return(false); } if (GetLaser(pos + force) != null) { return(false); } if (GetMirror(pos + force) != null || GetBox(pos + force) != null) { return(IsPushable(pos + force, dir)); } if (GetPlatform(pos + force) != null) { return(true); } if (GetFallingPlatform(pos + force) != null) { return(true); } if (tiles.Contains(pos + force)) { return(true); } return(false); } return(false); }
public GameElement[] GetPushTargets(Vector2Int pos, GameElement.Direction dir) { List <GameElement> output = new List <GameElement>(); Vector2Int force = Vector2Int.zero; switch (dir) { case GameElement.Direction.Up: force = Vector2Int.up; break; case GameElement.Direction.Down: force = Vector2Int.down; break; case GameElement.Direction.Left: force = Vector2Int.left; break; case GameElement.Direction.Right: force = Vector2Int.right; break; } Vector2Int target = pos; while (true) { Mirror mirror = GetMirror(target); Box box = GetBox(target); if (mirror != null) { output.Add(mirror); } else if (box != null) { output.Add(box); } else { break; } target += force; } return(output.ToArray()); }
private void Move(GameElement.Direction dir) { Vector2Int force = Vector2Int.zero; string animationName = ""; switch (dir) { case GameElement.Direction.Up: force = Vector2Int.up; animationName = player.WalkUp; break; case GameElement.Direction.Down: force = Vector2Int.down; animationName = player.WalkDown; break; case GameElement.Direction.Left: force = Vector2Int.left; animationName = player.WalkLeft; break; case GameElement.Direction.Right: force = Vector2Int.right; animationName = player.WalkRight; break; } Vector2Int target = new Vector2Int(player.position.x + force.x, player.position.y + force.y); if (!grid.IsWalkable(target)) { if (grid.IsPushable(target, dir)) { GameElement[] gameElements = grid.GetPushTargets(target, dir); foreach (GameElement gameElement in gameElements) { gameElement.transform.parent = player.transform; gameElement.OnStartMove(); gameElement.OnStartPush(); } StartCoroutine(player.Move(player.transform.position + (Vector3Int)force, () => { foreach (GameElement gameElement in gameElements) { gameElement.transform.parent = null; gameElement.position = gameElement.position + force; gameElement.OnMoved(); gameElement.OnPushed(); Platform platform = grid.GetPlatform(gameElement.position); if (platform != null) { gameElement.transform.parent = platform.transform; platform.gameElements.Add(gameElement); gameElement.currentPlatform = platform; } } })); player.animator.Play(animationName); } } else { StartCoroutine(player.Move(player.transform.position + (Vector3Int)force)); player.animator.Play(animationName); } }