/
compound.go
213 lines (188 loc) · 5.26 KB
/
compound.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
package render
import (
"errors"
"image"
"image/draw"
"sync"
"github.com/oakmound/oak/physics"
)
// The Compound type is intended for use to easily swap between multiple
// renderables that are drawn at the same position on the same layer.
// A common use case for this would be a character entitiy who switches
// their animation based on how they are moving or what they are doing.
//
// The Compound type removes the need to repeatedly draw and undraw elements
// of a character, which has a tendency to leave nothing drawn for a draw frame.
type Compound struct {
LayeredPoint
subRenderables map[string]Modifiable
curRenderable string
lock sync.RWMutex
}
// NewCompound creates a new compound from a map of names to modifiables
func NewCompound(start string, m map[string]Modifiable) *Compound {
return &Compound{
LayeredPoint: NewLayeredPoint(0, 0, 0),
subRenderables: m,
curRenderable: start,
lock: sync.RWMutex{},
}
}
// Add makes a new entry in the Compounds map
func (c *Compound) Add(k string, v Modifiable) {
c.lock.Lock()
c.subRenderables[k] = v
c.lock.Unlock()
}
// Set sets the current renderable to the one specified
func (c *Compound) Set(k string) error {
c.lock.RLock()
if _, ok := c.subRenderables[k]; !ok {
return errors.New("Unknown renderable for string " + k + " on compound")
}
c.lock.RUnlock()
c.curRenderable = k
return nil
}
// GetSub returns a keyed Modifiable from this compound's map
func (c *Compound) GetSub(s string) Modifiable {
c.lock.RLock()
m := c.subRenderables[s]
c.lock.RUnlock()
return m
}
// Get returns the Compound's current key
func (c *Compound) Get() string {
return c.curRenderable
}
// SetOffsets sets the logical offset for the specified key
func (c *Compound) SetOffsets(k string, offsets physics.Vector) {
c.lock.RLock()
if r, ok := c.subRenderables[k]; ok {
r.SetPos(offsets.X(), offsets.Y())
}
c.lock.RUnlock()
}
// Copy creates a copy of the Compound
func (c *Compound) Copy() Modifiable {
newC := new(Compound)
newC.LayeredPoint = c.LayeredPoint.Copy()
newSubRenderables := make(map[string]Modifiable)
c.lock.RLock()
for k, v := range c.subRenderables {
newSubRenderables[k] = v.Copy()
}
c.lock.RUnlock()
newC.subRenderables = newSubRenderables
newC.curRenderable = c.curRenderable
newC.lock = sync.RWMutex{}
return newC
}
//GetRGBA returns the current renderables rgba
func (c *Compound) GetRGBA() *image.RGBA {
c.lock.RLock()
rgba := c.subRenderables[c.curRenderable].GetRGBA()
c.lock.RUnlock()
return rgba
}
// Modify performs a series of modifications on the Compound
func (c *Compound) Modify(ms ...Modification) Modifiable {
c.lock.RLock()
for _, r := range c.subRenderables {
r.Modify(ms...)
}
c.lock.RUnlock()
return c
}
//DrawOffset draws the Compound at an offset from its logical location
func (c *Compound) DrawOffset(buff draw.Image, xOff float64, yOff float64) {
c.lock.RLock()
c.subRenderables[c.curRenderable].DrawOffset(buff, c.X()+xOff, c.Y()+yOff)
c.lock.RUnlock()
}
//Draw draws the Compound at its logical location
func (c *Compound) Draw(buff draw.Image) {
c.lock.RLock()
c.subRenderables[c.curRenderable].DrawOffset(buff, c.X(), c.Y())
c.lock.RUnlock()
}
// ShiftPos shifts the Compounds logical position
func (c *Compound) ShiftPos(x, y float64) {
c.SetPos(c.X()+x, c.Y()+y)
}
// ShiftY shifts the Compounds logical y position
func (c *Compound) ShiftY(y float64) {
c.SetPos(c.X(), c.Y()+y)
}
// ShiftX shifts the Compounds logical x position
func (c *Compound) ShiftX(x float64) {
c.SetPos(c.X()+x, c.Y())
}
// SetPos sets the Compound's logical position
func (c *Compound) SetPos(x, y float64) {
c.LayeredPoint.SetPos(x, y)
}
// GetDims gets the current Renderables dimensions
func (c *Compound) GetDims() (int, int) {
c.lock.RLock()
w, h := c.subRenderables[c.curRenderable].GetDims()
c.lock.RUnlock()
return w, h
}
// Pause stops the current Renderable if possible
func (c *Compound) Pause() {
c.lock.RLock()
if cp, ok := c.subRenderables[c.curRenderable].(CanPause); ok {
cp.Pause()
}
c.lock.RUnlock()
}
// Unpause tries to unpause the current Renderable if possible
func (c *Compound) Unpause() {
c.lock.RLock()
if cp, ok := c.subRenderables[c.curRenderable].(CanPause); ok {
cp.Unpause()
}
c.lock.RUnlock()
}
// IsInterruptable returns whether the current renderable is interruptable
func (c *Compound) IsInterruptable() bool {
c.lock.RLock()
defer c.lock.RUnlock()
if i, ok := c.subRenderables[c.curRenderable].(NonInterruptable); ok {
return i.IsInterruptable()
}
return true
}
// IsStatic returns whether the current renderable is static
func (c *Compound) IsStatic() bool {
c.lock.RLock()
defer c.lock.RUnlock()
if s, ok := c.subRenderables[c.curRenderable].(NonStatic); ok {
return s.IsStatic()
}
return true
}
// Revert will revert all parts of this compound that can be reverted
func (c *Compound) Revert(mod int) {
c.lock.RLock()
for _, v := range c.subRenderables {
switch t := v.(type) {
case *Reverting:
t.Revert(mod)
}
}
c.lock.RUnlock()
}
// RevertAll will revert all parts of this compound that can be reverted, back
// to their original state.
func (c *Compound) RevertAll() {
c.lock.RLock()
for _, v := range c.subRenderables {
switch t := v.(type) {
case *Reverting:
t.RevertAll()
}
}
c.lock.RUnlock()
}