/
Watch-Game.ps1
78 lines (70 loc) · 2.35 KB
/
Watch-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
function Watch-Game
{
<#
.Synopsis
Watches the Game Loop
.Description
While the game is running, this function will:
* Watch for keyboard input
* Call any key handlers
* Call any OnTick events found on the Game, CurrentLevel, or loaded sprites
* Sleep until the next expected $game.Clock event
.Notes
Unless you are building a game, you won't run this command directly.
This command will be run within Initialize-Game, which in turn is called
.Example
Initialize-Game -GamePath .\ROM\Nibbles2020 | Add-Member NoteProperty IsRunning $true -Force -PassThru | Watch-Game
.Link
Start-Game
.Link
Initialize-Game
#>
param(
# The Game Object
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName,ParameterSetName='Game')]
[Parameter(Position=1,ParameterSetName='LevelPath')]
[PSObject]
$Game
)
begin {
$NotifyTick = {
if ($game.OnTick.Invoke) {
$game.OnTick()
}
if ($game.CurrentLevel.OnTick.Invoke) {
$game.CurrentLevel.OnTick()
}
foreach ($gameSprite in @($game.CurrentLevel.Sprites)) {
if ($gameSprite.OnTick.Invoke) {
$gameSprite.OnTick()
}
}
}
}
process {
$lastTick = $null
$game | Add-Member NoteProperty LoopCounter 0 -Force
:GameLoop while ($game.IsRunning) {
$game.LoopCounter++
& $NotifyTick
$keyInput = @(
$keySplat = @{}
if ($Game.KeyHandlers) {
$keySplat['OnKey'] = $Game.KeyHandlers
}
. Watch-Keyboard @keySplat
)
if ($keyInput -and $keyInput.Key.VirtualKeyCode -ne 10 -and $keyInput.Key.VirtualKeyCode -ne 13) {
$null = $null
}
$gameClock = $game.Clock -as [Timespan]
if (-not $gameClock) { $gameClock = [Timespan]'00:00:00.02' }
do {
[Threading.Thread]::Sleep(1)
# Start-Sleep -Milliseconds 1
} while ($lastTick -and ([DateTime]::Now -lt $nextTick))
$lastTick = [DateTime]::Now
$nextTick = $lastTick + $gameClock
}
}
}