-
Notifications
You must be signed in to change notification settings - Fork 84
/
core.go
155 lines (139 loc) · 3.69 KB
/
core.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
package main
import (
"image/color"
oak "github.com/oakmound/oak/v2"
"github.com/oakmound/oak/v2/collision"
"github.com/oakmound/oak/v2/entities"
"github.com/oakmound/oak/v2/event"
"github.com/oakmound/oak/v2/render"
"github.com/oakmound/oak/v2/scene"
)
const (
_ = iota
RED collision.Label = iota
GREEN
BLUE
TEAL
)
func main() {
oak.Add("demo", func(string, interface{}) {
act := &AttachCollisionTest{}
act.Solid = entities.NewSolid(50, 50, 50, 50, render.NewColorBox(50, 50, color.RGBA{0, 0, 0, 255}), nil, act.Init())
collision.Attach(act.Vector, act.Space, 0, 0)
act.Bind(func(int, interface{}) int {
if act.ShouldUpdate {
act.ShouldUpdate = false
act.R.Undraw()
act.R = act.nextR
render.Draw(act.R, 0)
}
if oak.IsDown("A") {
// We could use attachement here to not have to shift both
// R and act but that is made more difficult by constantly
// changing the act's R
act.ShiftX(-3)
act.R.ShiftX(-3)
} else if oak.IsDown("D") {
act.ShiftX(3)
act.R.ShiftX(3)
}
if oak.IsDown("W") {
act.ShiftY(-3)
act.R.ShiftY(-3)
} else if oak.IsDown("S") {
act.ShiftY(3)
act.R.ShiftY(3)
}
return 0
}, "EnterFrame")
render.Draw(act.R, 0)
act.R.SetLayer(1)
collision.PhaseCollision(act.Space)
act.Bind(func(id int, label interface{}) int {
l := label.(collision.Label)
switch l {
case RED:
act.r += 125
act.UpdateR()
case GREEN:
act.g += 125
act.UpdateR()
case BLUE:
act.b += 125
act.UpdateR()
case TEAL:
act.b += 125
act.g += 125
act.UpdateR()
}
return 0
}, "CollisionStart")
act.Bind(func(id int, label interface{}) int {
l := label.(collision.Label)
switch l {
case RED:
act.r -= 125
act.UpdateR()
case GREEN:
act.g -= 125
act.UpdateR()
case BLUE:
act.b -= 125
act.UpdateR()
case TEAL:
act.b -= 125
act.g -= 125
act.UpdateR()
}
return 0
}, "CollisionStop")
upleft := entities.NewSolid(0, 0, 320, 240, render.NewColorBox(320, 240, color.RGBA{100, 0, 0, 100}), nil, 0)
upleft.Space.UpdateLabel(RED)
upleft.R.SetLayer(0)
render.Draw(upleft.R, 0)
upright := entities.NewSolid(320, 0, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 100, 0, 100}), nil, 0)
upright.Space.UpdateLabel(GREEN)
upright.R.SetLayer(0)
render.Draw(upright.R, 0)
botleft := entities.NewSolid(0, 240, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 0, 100, 100}), nil, 0)
botleft.Space.UpdateLabel(BLUE)
botleft.R.SetLayer(0)
render.Draw(botleft.R, 0)
botright := entities.NewSolid(320, 240, 320, 240, render.NewColorBox(320, 240, color.RGBA{0, 100, 100, 100}), nil, 0)
botright.Space.UpdateLabel(TEAL)
botright.R.SetLayer(0)
render.Draw(botright.R, 0)
}, func() bool {
return true
}, func() (string, *scene.Result) {
return "demo", nil
})
render.SetDrawStack(
render.NewHeap(false),
render.NewDrawFPS(),
)
oak.Init("demo")
}
type AttachCollisionTest struct {
*entities.Solid
// AttachSpace is a composable struct that allows
// spaces to be attached to vectors
collision.AttachSpace
// Phase is a composable struct that enables the call
// collision.CollisionPhase on this struct's space,
// which will start sending signals when that space
// starts and stops touching given labels
collision.Phase
r, g, b int
ShouldUpdate bool
nextR render.Renderable
}
func (act *AttachCollisionTest) Init() event.CID {
return event.NextID(act)
}
func (act *AttachCollisionTest) UpdateR() {
act.nextR = render.NewColorBox(50, 50, color.RGBA{uint8(act.r), uint8(act.g), uint8(act.b), 255})
act.nextR.SetPos(act.X(), act.Y())
act.nextR.SetLayer(1)
act.ShouldUpdate = true
}