Skip to content

Commit d552bd9

Browse files
authored
Merge pull request #1 from orsinium-labs/geometry
Geometry
2 parents 8b74145 + 4d31099 commit d552bd9

File tree

4 files changed

+114
-16
lines changed

4 files changed

+114
-16
lines changed

_examples/snake/snake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func main() {
3939

4040
// Update the snake direction based on the buttons pressed on the gamepad.
4141
func input() {
42-
g := w4.Gamepads[0]
42+
g := w4.Gamepad
4343
if g.Up() {
4444
snake.Up()
4545
}

w4/api.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package w4
22

3-
type u8 = uint8
4-
5-
// A point on the plane.
6-
type Point struct {
7-
X u8
8-
Y u8
9-
}
10-
11-
// Size of a 2D shape.
12-
type Size struct {
13-
Width u8
14-
Height u8
15-
}
16-
173
// Flags used with [Blit] and [BlitSub].
184
type BlitFlags u8
195

w4/geom.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package w4
2+
3+
type u8 = uint8
4+
5+
// A point on the plane.
6+
type Point struct {
7+
X u8
8+
Y u8
9+
}
10+
11+
// A point with equal x value and y set to 0.
12+
func (p Point) XAxis() Point {
13+
p.Y = 0
14+
return p
15+
}
16+
17+
// A point with equal y value and x set to 0.
18+
func (p Point) YAxis() Point {
19+
p.X = 0
20+
return p
21+
}
22+
23+
// The componentwise minimum of two Points.
24+
func (p Point) ComponentMin(other Point) Point {
25+
if other.X < p.X {
26+
p.X = other.X
27+
}
28+
if other.Y < p.Y {
29+
p.Y = other.Y
30+
}
31+
return p
32+
}
33+
34+
// The componentwise maximum of two Points.
35+
func (p Point) ComponentMax(other Point) Point {
36+
if other.X > p.X {
37+
p.X = other.X
38+
}
39+
if other.Y > p.Y {
40+
p.Y = other.Y
41+
}
42+
return p
43+
}
44+
45+
// Convert the Point to a Size with width equal to x and height equal to y.
46+
func (p Point) AsSize() Size {
47+
return Size{Width: p.X, Height: p.Y}
48+
}
49+
50+
// Add size width to the point x and size height to the point y.
51+
func (p Point) AddSize(s Size) Point {
52+
p.X += s.Width
53+
p.Y += s.Height
54+
return p
55+
}
56+
57+
// If the point is outside of the screen, wrap it around to fit on the screen.
58+
func (p Point) Wrap() Point {
59+
p.X %= 160
60+
p.Y %= 160
61+
return p
62+
}
63+
64+
// Size of a 2D shape.
65+
type Size struct {
66+
Width u8
67+
Height u8
68+
}
69+
70+
// Add two sizes together componentwise.
71+
func (s Size) AddSize(other Size) Size {
72+
s.Width += other.Width
73+
s.Height += other.Height
74+
return s
75+
}
76+
77+
// Convert Size to a Point with x set to width and y set to height.
78+
func (s Size) AsPoint() Point {
79+
return Point{X: s.Width, Y: s.Height}
80+
}
81+
82+
// The componentwise minimum of two Sizes.
83+
func (s Size) ComponentMin(other Size) Size {
84+
if other.Width < s.Width {
85+
s.Width = other.Width
86+
}
87+
if other.Height < s.Height {
88+
s.Height = other.Height
89+
}
90+
return s
91+
}
92+
93+
// The componentwise maximum of two Sizes.
94+
func (s Size) ComponentMax(other Size) Size {
95+
if other.Width > s.Width {
96+
s.Width = other.Width
97+
}
98+
if other.Height > s.Height {
99+
s.Height = other.Height
100+
}
101+
return s
102+
}

w4/memory.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ func (drawColors) SetSecondary(c DrawColor) {
122122

123123
type gamepad uint
124124

125+
// The gamepad of the local player.
126+
//
127+
// The same as Gamepads[0].
128+
var Gamepad gamepad = 0x12
129+
125130
// Check if X button is currently pressed on the gamepad.
126131
func (g gamepad) X() bool { return memory[g]&1 != 0 }
127132

@@ -146,7 +151,7 @@ func (g gamepad) Any() bool { return memory[g] != 0 }
146151
// 4 gamepads, with each gamepad represented by a single byte.
147152
type gamepads []gamepad
148153

149-
// And array of 4 gamepads.
154+
// An array of 4 gamepads.
150155
//
151156
// - The first one is always available and is the local player.
152157
// - The second one can be either a local hotseat player or a remote one.
@@ -155,6 +160,11 @@ type gamepads []gamepad
155160
// https://wasm4.org/docs/guides/user-input#gamepad
156161
var Gamepads = gamepads{0x12, 0x13, 0x14, 0x15}
157162

163+
// Get a gamepad by index (from 0 to 3).
164+
func (gamepads) Get(idx u8) gamepad {
165+
return Gamepads[idx]
166+
}
167+
158168
type mouse struct{}
159169

160170
// The mouse position and mouse buttons (left, right, and middle) state.

0 commit comments

Comments
 (0)