forked from SolarLune/resolv
/
worldCompound.go
157 lines (113 loc) · 2.92 KB
/
worldCompound.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
"github.com/SolarLune/resolv/resolv"
)
type WorldCompound struct {
Space *resolv.Space
Player *resolv.Space
}
func (w *WorldCompound) Create() {
w.Space = resolv.NewSpace()
w.Space.Clear()
w.Space.Add(resolv.NewRectangle(0, 0, screenWidth, cell))
w.Space.Add(resolv.NewRectangle(0, cell, cell, screenHeight-cell))
w.Space.Add(resolv.NewRectangle(screenWidth-cell, cell, cell, screenHeight-cell))
w.Space.Add(resolv.NewRectangle(cell, screenHeight-cell, screenWidth-(cell*2), cell))
w.Space.AddTags("solid")
// Add the "solid" tag to all Shapes within the Space
for i := 0; i < 200; i++ {
square := NewSquare(w.Space)
square.Rect.AddTags("sticky")
w.Space.Add(square.Rect)
}
w.Player = resolv.NewSpace()
w.Player.Add(NewSquare(w.Space).Rect) // Gonna be lazy here and use a new Square, but just its Rect.
w.Player.AddTags("player")
w.Space.Add(w.Player)
}
func (w *WorldCompound) Update() {
player := w.Player
moveSpd := int32(1)
dx, dy := int32(0), int32(0)
if rl.IsKeyDown(rl.KeyRight) {
dx = moveSpd
}
if rl.IsKeyDown(rl.KeyLeft) {
dx = -moveSpd
}
if rl.IsKeyDown(rl.KeyUp) {
dy = -moveSpd
}
if rl.IsKeyDown(rl.KeyDown) {
dy = moveSpd
}
solids := w.Space.FilterByTags("solid")
var stickTo resolv.Shape
if res := solids.Resolve(player, dx, 0); res.Colliding() {
player.Move(res.ResolveX, 0)
if res.ShapeB.HasTags("sticky") {
stickTo = res.ShapeB
}
} else {
player.Move(dx, 0)
}
if res := solids.Resolve(player, 0, dy); res.Colliding() {
player.Move(0, res.ResolveY)
if res.ShapeB.HasTags("sticky") {
stickTo = res.ShapeB
}
} else {
player.Move(0, dy)
}
if stickTo != nil {
stickTo.AddTags("player")
player.Add(stickTo)
w.Space.Remove(stickTo)
}
if rl.IsKeyDown(rl.KeyX) {
if player.Length() > 1 {
detach := player.Get(player.Length() - 1)
detach.RemoveTags("player")
w.Space.Add(detach)
player.Remove(detach)
}
}
}
func (w *WorldCompound) DrawObject(other resolv.Shape) {
switch shape := other.(type) {
case *resolv.Rectangle:
if !shape.HasTags("square") {
rl.DrawRectangleLines(shape.X, shape.Y, shape.W, shape.H, rl.LightGray)
} else {
squareData := shape.GetData().(*Square)
color := rl.Color{0, 0, 255, 255}
if shape.HasTags("player") {
color = rl.Color{0, 255, 0, 255}
}
rl.DrawRectangleLines(squareData.Rect.X, squareData.Rect.Y, squareData.Rect.W, squareData.Rect.H, color)
}
case *resolv.Space:
for _, obj := range *shape {
w.DrawObject(obj)
}
}
}
func (w *WorldCompound) Draw() {
for _, other := range *w.Space {
w.DrawObject(other)
}
if drawHelpText {
DrawText(32, 16,
"-Compound test-",
"You're the green square. Use",
"the arrow keys to move.",
"Touch a blue square to absorb it.",
"Press the X key to detach absorbed squares.",
)
}
}
func (w *WorldCompound) Destroy() {
w.Player = nil
w.Space.Clear()
}