functions/MiniGameSnake/scene/once.ps1

param($var, $func, $canvas, $sound, $object, $stop)


$GameWidth = $var.gameWidth
$GameHeight = $var.gameHeight

$_cEmpty_ = $var.difficulty.chars.Empty
$_cWallCorner_ = $var.difficulty.chars.Wall.Corner
$_cWallHorizontal_ = $var.difficulty.chars.wall.Horizontal
$_cWallVertical_ = $var.difficulty.chars.Wall.Vertical

$canvas.Clear()

$sound.Loop("snake_music")
if (-NOT $var.isWindows) {
    $canvas.DrawBottom(0, 1, 'Sound supported only on Windows')
}

<#
 
    Draw the game walls
#>

$_WallHorizontal_ = $_cWallCorner_ + ($_cWallHorizontal_ * $GameWidth) + $_cWallCorner_
$_WallVertical_ = $_cWallVertical_ + ($_cEmpty_ * $GameWidth) + $_cWallVertical_

$_GameWalls_ = @()
$_GameWalls_ += $_WallHorizontal_
foreach ($line in 1..$GameHeight) {
    $_GameWalls_ += $_WallVertical_
}
$_GameWalls_ += $_WallHorizontal_

# We start drawing at Y=3 to leave space for the game stats.
$canvas.Draw(0, 3, $_GameWalls_)

# Define a boundary for the snake for easy Out-Of-Bounds-Check.
$cageMinimum = [System.Numerics.Vector2]@{
    X = 1 # The first line is the wall.
    Y = 4 # 3 line for the stats + 1 for the wall.
}
$cageMaximum = [System.Numerics.Vector2]@{
    # -2 for the left and right wall.
    X = $_GameWalls_[0].Length - 2 
    # -2 for the upper and lower wall. + 3 for the stats.
    Y = $_GameWalls_.Count - 2 + 3
}

$canvas.Bounds.Add('cage', $cageMinimum, $cageMaximum)

# initial snake position
$var.snake = @(
    [System.Numerics.Vector2]@{
        X = [System.Math]::Floor($cageMaximum.X / 2)
        Y = [System.Math]::Floor($cageMinimum.Y)
    }
)