/
Show-Game.ps1
89 lines (78 loc) · 3.02 KB
/
Show-Game.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function Show-Game
{
<#
.Synopsis
Shows the game, or shows messages in the game.
.Description
By default, Shows the game (redrawing the current level and state).
When -Message is provided, shows in-game messages.
.Example
Show-Game
.Link
Start-Game
#>
[CmdletBinding(DefaultParameterSetName='GameState')]
param(
# The Message to display
[Parameter(Position=0,ParameterSetName='Message',ValueFromPipelineByPropertyName)]
[string[]]
$Message,
# The X-coordinate of the message. If not provided, this will default to center the message on the game width.
[Parameter(ParameterSetName='Message',ValueFromPipelineByPropertyName)]
[int]
$X,
# The Y-coordinate of the message. If not provided, this will default to 1/3rd of the game height.
[Parameter(ParameterSetName='Message',ValueFromPipelineByPropertyName)]
[int]
$y,
# If provided, the message will have a border.
[Parameter(ParameterSetName='Message',ValueFromPipelineByPropertyName)]
[switch]
$Border,
# While the command shows game state by default, you can pass -GameState for clarity in code.
[Parameter(ParameterSetName='GameState',ValueFromPipelineByPropertyName)]
[switch]
$GameState
)
begin {
$allMessages = [Collections.Generic.List[PSObject]]::new()
}
process {
#region Showing a Message
if ($PSCmdlet.ParameterSetName -eq 'Message')
{
# If we're showing a message,
$PSBoundParameters.Message = # Join all message lines into a single string
$PSBoundParameters.Message -split '(?>\r\n|\n)' -join [Environment]::NewLine
$allMessages.Add([PSCustomObject]( # then create a MessageBox using the bound parameters
[Ordered]@{PSTypeName='PowerArcade.MessageBox'} + $PSBoundParameters
)) # and add it to all messages.
return
}
#endregion Showing a Message
if ($PSCmdlet.ParameterSetName -eq 'GameState') {
Clear-Host
if ($game.BackgroundColor) {
# [Console]::Write(('' + [char]0x1b + '[1049l'))
([PSCustomObject]@{
PSTypeName='PowerArcade.Box'
BackgroundColor = $game.BackgroundColor
} | Out-String -Width 1kb).Trim() | Write-Host -NoNewline
}
if ($game.CurrentLevel.Draw) {
$Game.CurrentLevel.Draw()
}
}
}
end {
if ($allMessages) { # If we have any messages to show
# Pipe them to out string and write them to the console
[Console]::Write("$(($allMessages | Out-String -Width 1kb).Trim())")
# (don't forget to hide the cursor, if we can)
try {[Console]::CursorVisible = $false} catch {$PSCmdlet.WriteVerbose("$_")}
}
if ($passThru) { # If we're passing values thru
if ($allMessages) { $allMessages.ToArray() } # return all messages
}
}
}