/
item.go
281 lines (241 loc) · 5.96 KB
/
item.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package item
import (
"fmt"
"strings"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/xackery/aseprite"
"github.com/xackery/magnets/camera"
"github.com/xackery/magnets/entity"
"github.com/xackery/magnets/global"
"github.com/xackery/magnets/library"
)
type Item struct {
hid string
entityID uint
layer *aseprite.Layer
image *ebiten.Image
spriteName string
layerName string
Data *ItemData
x float64
y float64
spawnX float64
spawnY float64
animation animation
hookX float64
hookY float64
isHooked bool
isAttracted bool
isDead bool
}
type animation struct {
tag *aseprite.Tag
delay time.Time
index int16
isPingPongToggle bool
}
func New(itemType int, x, y float64) (*Item, error) {
data, ok := itemTypes[itemType]
if !ok {
return nil, fmt.Errorf("unknown item type %d", itemType)
}
name := "base"
layer, err := library.Layer(data.SpriteName, data.LayerName)
if err != nil {
return nil, fmt.Errorf("library.Layer: %w", err)
}
if len(layer.Cells) < 1 {
return nil, fmt.Errorf("no cells found on layer %s", name)
}
n := &Item{
Data: data,
spriteName: data.SpriteName,
layerName: data.LayerName,
x: x,
y: y,
layer: layer,
entityID: entity.NextEntityID(),
image: layer.Cells[0].EbitenImage,
}
err = n.SetAnimation("walk")
if err != nil {
return nil, fmt.Errorf("SetAnimation %s: %w", "walk", err)
}
items = append(items, n)
err = entity.Register(n)
if err != nil {
return nil, fmt.Errorf("entity.Register: %w", err)
}
return n, nil
}
func (n *Item) IsHit(x, y float64) bool {
if n.IsDead() {
return false
}
if n.x-(float64(n.layer.SpriteWidth)/2) > x {
return false
}
if n.x+(float64(n.layer.SpriteWidth)/2) < x {
return false
}
if n.y-(float64(n.layer.SpriteHeight)/2) > y {
return false
}
if n.y+(float64(n.layer.SpriteHeight)/2) < y {
return false
}
return true
}
func (n *Item) Draw(screen *ebiten.Image) error {
n.animationStep()
if len(n.layer.Cells) <= int(n.animation.index) {
return fmt.Errorf("animationIndex %d is out of bounds for body cells %d", n.animation.index, len(n.layer.Cells))
}
c := n.layer.Cells[int(n.animation.index)]
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(n.layer.SpriteWidth/2), -float64(n.layer.SpriteHeight/2))
op.GeoM.Translate(float64(c.PositionX), float64(c.PositionY))
op.GeoM.Translate(n.x, n.y)
op.GeoM.Translate(camera.X, camera.Y)
op.GeoM.Scale(global.ScreenScaleX(), global.ScreenScaleY())
if n.IsDead() {
op.ColorM.Scale(1, 1, 1, 0.6)
} else {
op.ColorM.Scale(1, 1, 1, 1)
}
//duplication spell effect?
/*for j := -128; j <= 128; j += 32 {
//for i := -1; i <= 1; i++ {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(global.ScreenScaleX(), global.ScreenScaleY())
op.GeoM.Translate(float64(int(c.PositionX)+n.x+n.dragX), float64(int(c.PositionY)+n.y+n.dragY+j))
//op.GeoM.Translate(1, float64(j))
// Alpha scale should be 1.0/49.0, but accumulating 1/49 49 times doesn't reach to 1, because
// the final color is affected by the destination alpha when CompositeModeSourceOver is used.
// This composite mode is the default mode. See how this is calculated at the doc:
// https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#CompositeMode
//
// Use a higher value than 1.0/49.0. Here, 1.0/25.0 here to get a reasonable result.
op.ColorM.Scale(1, 1, 1, 0.8)
screen.DrawImage(c.EbitenImage, op)
//}
}*/
screen.DrawImage(c.EbitenImage, op)
if n.IsDead() {
return nil
}
x := n.x
y := n.y + 30
x -= float64(n.SWidth() / 2)
y += float64(n.SHeight() / 2)
return nil
}
// SetAnimation sets the animation of the item
func (n *Item) SetAnimation(name string) error {
name = strings.ToLower(name)
tag, err := library.Tag(n.spriteName, name)
if err != nil {
return fmt.Errorf("tag: %w", err)
}
n.animation.tag = tag
n.animation.index = tag.From
return nil
}
func (n *Item) animationStep() {
if n.IsDead() {
return
}
if global.IsPaused() {
return
}
if n.animation.delay.After(time.Now()) {
return
}
if n.animation.tag.AnimationDirection == 2 && n.animation.isPingPongToggle {
n.animation.index--
if n.animation.index <= n.animation.tag.From {
n.animation.isPingPongToggle = false
}
if n.animation.index < 0 {
n.animation.index = 0
}
} else {
n.animation.index++
if n.animation.index > n.animation.tag.To {
if n.animation.tag.AnimationDirection == 2 {
n.animation.index -= 2
n.animation.isPingPongToggle = true
} else {
n.animation.index = n.animation.tag.From
}
}
}
if n.layer == nil {
return
}
if len(n.layer.Cells) <= int(n.animation.index) {
return
}
c := n.layer.Cells[int(n.animation.index)]
n.animation.delay = time.Now().Add(time.Duration(c.Duration) * time.Millisecond)
}
func (n *Item) SetPosition(x, y float64) {
n.x, n.y = x, y
}
func (n *Item) Position() (float64, float64) {
return n.x, n.y
}
func (n *Item) HID() string {
return n.hid
}
func (n *Item) IsDead() bool {
return n.isDead
}
func (n *Item) EntityID() uint {
return n.entityID
}
func (n *Item) SWidth() int {
return int(n.layer.SpriteWidth * uint16(global.ScreenScaleX()))
}
func (n *Item) SHeight() int {
return int(n.layer.SpriteHeight * uint16(global.ScreenScaleY()))
}
func (n *Item) AnimationAttack() error {
return nil
}
func (n *Item) AnimationGotHit() error {
return nil
}
func (n *Item) X() float64 {
return n.x
}
func (n *Item) Y() float64 {
return n.y
}
func (n *Item) move() {
maxMove := float64(2)
if global.Distance(n.x, n.y, global.Player.X(), global.Player.Y()) < global.Player.AttractionRange() {
if !n.isHooked {
n.hookX = n.x
n.hookY = n.y
n.isHooked = true
}
dx := n.x - global.Player.X()
if dx > maxMove {
dx = maxMove
}
if dx < -maxMove {
dx = -maxMove
}
dy := n.y - global.Player.Y()
if dy > maxMove {
dy = maxMove
}
if dy < -maxMove {
dy = -maxMove
}
n.x -= dx
n.y -= dy
}
}