/
New-Sprite.ps1
132 lines (120 loc) · 4.4 KB
/
New-Sprite.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function New-Sprite
{
<#
.Synopsis
Creates Sprites
.Description
Creates a new Sprite.
.Example
New-Sprite -X 10 -Y 20 -Content '!' -Color "#ff0000"
.Link
Add-Sprite
.Link
Find-Sprite
.Link
Move-Sprite
.Link
Remove-Sprite
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification="This does not change state, it creates an object")]
param(
# The type of the sprite. The sprite type is used to group sprites and handle specific collisions.
[Parameter(Position=0,ValueFromPipelineByPropertyName)]
[string]
$Type,
# The X coordinate of the sprite.
# If the X coordinate would not be visible, the sprite will not be rendered
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$X,
# The X coordinate of the sprite.
# If the Y coordinate would not be visible, the sprite will not be rendered
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$Y,
# The name of the sprite.
# Giving a sprite a name will declare it as a global variable.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Name,
# The sprite content. This can be used for sprites that are a single line.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Content,
# The sprite color.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$Color,
# The sprite background color.
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$BackgroundColor,
# The width of the sprite.
# Supplying this and -Height will make the sprite a rectangle.
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$Width,
# The height of the sprite
# Supplying this and -Width will make the sprite a rectangle.
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$Height,
# Additional properties of the sprite.
# This can contain any custom information.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Properties')]
[Collections.IDictionary]
$Property,
# Additional methods for the sprite
# These can be used to dynamically create sprite behavior.
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]
$Method)
process {
$out = [PSCustomObject]([Ordered]@{
SpriteID = [BitConverter]::ToString([GUID]::NewGuid().ToByteArray()).Replace('-','').ToLower()
} + $PSBoundParameters)
$out.pstypenames.clear()
$out.pstypenames.add("PowerArcade.Sprite")
if ($out.Width -and $out.Height) {
$out.psobject.Members.Add([PSNoteProperty]::new('Shapes',@(
$shape = [PSCustomObject]@{PSTypeName='PowerArcade.Box';Width=$out.Width;Height=$out.Height}
$shape.psobject.Members.Add([PSNoteProperty]::new('Sprite',$out))
$shape.psobject.Members.Add([PSScriptProperty]::new('X', { $this.Sprite.X}))
$shape.psobject.Members.Add([PSScriptProperty]::new('Y', { $this.Sprite.Y}))
$shape.psobject.Members.Add([PSScriptProperty]::new('BackgroundColor', { $this.Sprite.BackgroundColor}))
$shape.psobject.Members.Add([PSScriptProperty]::new('Color', { $this.Sprite.Color}))
$shape
)))
}
if ($Type) {
if ($Game.SpriteTypes.$Type) {
foreach ($member in $game.SpriteTypes.$type.psobject.Members) {
$out.psobject.Members.Add($member, $true)
}
}
if ($out.Default -is [Collections.IDictionary]) {
foreach ($kv in $out.Default.GetEnumerator()) {
if (-not $out.($kv.Key)) {
$out.psobject.members.Add([PSNoteProperty]::new($kv.Key,$kv.Value), $true)
}
}
}
$out.pstypenames.add("$Type.Sprite")
}
if ($Property) {
foreach ($kv in $Property.GetEnumerator()) {
$out.psobject.members.add([PSNoteProperty]::new($kv.Key,$kv.Value))
}
}
if ($Method) {
foreach ($kv in $Method.GetEnumerator()) {
$out.psobject.members.add([PSScriptMethod]::new($kv.Key, $kv.Value))
}
}
if ($out.Initialize.Invoke) {
$out.Initialize()
}
$out
}
}