/
scratch.go
244 lines (205 loc) · 6.11 KB
/
scratch.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package scratch
import (
"fmt"
"image"
"log"
"math"
"math/rand"
"sync"
"github.com/GaryBrownEEngr/scratch/game"
"github.com/GaryBrownEEngr/scratch/models"
"github.com/GaryBrownEEngr/scratch/sprite"
"github.com/GaryBrownEEngr/scratch/tools"
)
type scratchState struct {
width int
height int
g *game.EbitenGame
cmdChan chan any
justPressedBroker *tools.Broker[*models.UserInput]
posBroker *tools.PositionBroker
idToSpriteMapMutex sync.RWMutex
idToSpriteMap map[int]models.Sprite
nameToSpriteMap map[string]models.Sprite
}
var _ models.Scratch = &scratchState{} // Force the linter to tell us if the interface is implemented
type ScratchParams struct {
Width int // Window Width in pixels
Height int // Window Height in pixels
ShowFPS bool // Show Frame-Rate and Update-Rate information in top left corner of window
}
// The drawFunc will be started as a go routine.
func Start(params ScratchParams, simStartFunc func(models.Scratch)) {
log.SetFlags(log.Ltime | log.Lmicroseconds | log.Lshortfile)
ret := &scratchState{
width: params.Width,
height: params.Height,
justPressedBroker: tools.NewBroker[*models.UserInput](),
posBroker: tools.NewPositionBroker(),
idToSpriteMap: make(map[int]models.Sprite),
nameToSpriteMap: make(map[string]models.Sprite),
}
gameInit := game.GameInitStruct{
Width: params.Width,
Height: params.Height,
ShowFPS: params.ShowFPS,
JustPressedBroker: ret.justPressedBroker,
}
ret.g = game.NewGame(gameInit)
ret.cmdChan = ret.g.GetSpriteCmdChannel()
go simStartFunc(ret)
ret.g.RunGame()
}
func (s *scratchState) Exit() {
s.g.TellGameToExit()
}
func (s *scratchState) AddSprite(uniqueName string) models.Sprite {
spriteID := s.g.GetNextSpriteID()
if uniqueName == "" {
uniqueName = fmt.Sprintf("rand%X%X", rand.Uint64(), rand.Uint64())
}
update := models.CmdAddNewSprite{
SpriteID: spriteID,
}
s.cmdChan <- update
s.posBroker.AddSprite(spriteID)
ret := sprite.NewSprite(s, uniqueName, spriteID)
s.idToSpriteMapMutex.Lock()
s.idToSpriteMap[spriteID] = ret
s.nameToSpriteMap[uniqueName] = ret
s.idToSpriteMapMutex.Unlock()
s.posBroker.UpdateSpriteInfo(spriteID, ret.GetState())
return ret
}
func (s *scratchState) DeleteSprite(in models.Sprite) {
spriteID := in.GetSpriteID()
s.posBroker.RemoveSprite(spriteID)
update := models.CmdSpriteDelete{
SpriteID: spriteID,
}
s.cmdChan <- update
s.idToSpriteMapMutex.Lock()
delete(s.idToSpriteMap, spriteID)
delete(s.nameToSpriteMap, in.GetUniqueName())
s.idToSpriteMapMutex.Unlock()
}
func (s *scratchState) DeleteAllSprites() {
update := models.CmdSpritesDeleteAll{}
s.cmdChan <- update
s.posBroker = tools.NewPositionBroker()
s.idToSpriteMapMutex.Lock()
s.idToSpriteMap = make(map[int]models.Sprite)
s.nameToSpriteMap = make(map[string]models.Sprite)
s.idToSpriteMapMutex.Unlock()
}
func (s *scratchState) SpriteUpdatePosAngle(in models.Sprite) {
status := in.GetState()
s.posBroker.UpdateSpriteInfo(status.SpriteID, status)
cmd := models.CmdSpriteUpdateMin{
SpriteID: status.SpriteID,
X: status.X,
Y: status.Y,
AngleRad: status.AngleDegrees * (math.Pi / 180.0),
}
s.cmdChan <- cmd
}
func (s *scratchState) SpriteUpdateFull(in models.Sprite) {
status := in.GetState()
s.posBroker.UpdateSpriteInfo(status.SpriteID, status)
cmd := models.CmdSpriteUpdateFull{
SpriteID: status.SpriteID,
CostumeName: status.CostumeName,
X: status.X,
Y: status.Y,
Z: status.Z,
Angle: status.AngleDegrees * (math.Pi / 180.0),
Visible: status.Visible,
XScale: status.ScaleX,
YScale: status.ScaleY,
Opacity: status.Opacity,
}
s.cmdChan <- cmd
}
func (s *scratchState) GetSpriteID(uniqueName string) int {
s.idToSpriteMapMutex.RLock()
sprite, ok := s.nameToSpriteMap[uniqueName]
s.idToSpriteMapMutex.RUnlock()
if !ok {
log.Printf("%s, doesn't exist.\n", uniqueName)
return -1
}
return sprite.GetSpriteID()
}
func (s *scratchState) GetSpriteInfo(uniqueName string) models.SpriteState {
return s.posBroker.GetSpriteInfo(s.GetSpriteID(uniqueName))
}
func (s *scratchState) GetSpriteInfoByID(id int) models.SpriteState {
return s.posBroker.GetSpriteInfo(id)
}
func (s *scratchState) GetWidth() int {
return s.width
}
func (s *scratchState) GetHeight() int {
return s.height
}
func (s *scratchState) PressedUserInput() *models.UserInput {
ret := s.g.PressedUserInput()
return ret
}
func (s *scratchState) SubscribeToJustPressedUserInput() chan *models.UserInput {
return s.justPressedBroker.Subscribe()
}
func (s *scratchState) UnSubscribeToJustPressedUserInput(in chan *models.UserInput) {
s.justPressedBroker.Unsubscribe(in)
}
func (sim *scratchState) AddCostume(img image.Image, name string) {
update := models.CmdAddCostume{
Img: img,
CostumeName: name,
}
sim.cmdChan <- update
}
func (sim *scratchState) AddSound(path, name string) {
cmd := models.CmdAddSound{
Path: path,
SoundName: name,
}
sim.cmdChan <- cmd
}
func (sim *scratchState) PlaySound(name string, volume float64) {
cmd := models.CmdPlaySound{
SoundName: name,
Volume: volume,
}
sim.cmdChan <- cmd
}
func (sim *scratchState) WhoIsNearMe(x, y, distance float64) []models.NearMeInfo {
return sim.posBroker.GetSpritesNearMe(x, y, distance)
}
func (sim *scratchState) SendMsg(toSpriteID int, msg any) {
sim.idToSpriteMapMutex.RLock()
toSprite, ok := sim.idToSpriteMap[toSpriteID]
sim.idToSpriteMapMutex.RUnlock()
if !ok {
log.Printf("Could not send msg to %d, doesn't exist.\n", toSpriteID)
return
}
toSprite.AddMsg(msg)
}
// This returns nil if there is no new data.
// This will throw away all but the newest set of data available. So this should be called faster that the game update rate (60Hz),
// otherwise sim.PressedUserInput() should be used instead.
func GetNewestJustPressedFromChan(justPressedChan chan *models.UserInput) *models.UserInput {
var ret *models.UserInput
ChanExtractionLoop:
for {
select {
case i := <-justPressedChan:
ret = i
default:
// receiving from chan would block
break ChanExtractionLoop
}
}
return ret
}