-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblinkTrail.go
110 lines (89 loc) · 2.52 KB
/
blinkTrail.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
package main
import (
"image"
"github.com/hajimehoshi/ebiten"
)
// BlinkTrail the trail when the player blinks
type BlinkTrail struct {
speed float64
speedMax float64
sections []BlinkTrailSection
}
// BlinkTrailSection is the actual animated section of the trail
type BlinkTrailSection struct {
position Vec2f
spritesheet Spritesheet
animation *Animation
timeToLive int // cya loser lol
delete bool
direction Direction
image *ebiten.Image
}
// BlinkTrail
func createBlinkTrail(speed float64) BlinkTrail {
return BlinkTrail{
speed: speed,
speedMax: speed,
sections: []BlinkTrailSection{},
}
}
func (b *BlinkTrail) render(screen *ebiten.Image) {
for _, s := range b.sections {
s.render(screen)
}
}
func (b *BlinkTrail) update() {
for i := 0; i < len(b.sections); i++ {
b.sections[i].update()
}
}
func (b *BlinkTrail) spawnUpdate(position Vec2f, direction Direction) {
// Add new section every few ticks
if b.speed >= 0 {
b.speed--
} else {
t := createBlinkTrailSection(position, direction)
b.sections = append(b.sections, t)
b.speed = b.speedMax
t.animation.startBackwards()
}
}
// BlinkTrailSection
func createBlinkTrailSection(position Vec2f, direction Direction) BlinkTrailSection {
image := iplayerSpritesheet
blinkSpritesheet := createSpritesheet(newVec2i(102, 153), newVec2i(145, 171), 3, image)
animation := createAnimation(blinkSpritesheet, image)
return BlinkTrailSection{
position: position,
spritesheet: blinkSpritesheet,
animation: &animation,
direction: direction,
timeToLive: 21,
image: image,
}
}
func (b *BlinkTrailSection) render(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(0, 0)
// If the player is facing left, then flip the sprite 180 [-x, y]
if b.direction == Left || b.direction == DownLeft || b.direction == UpLeft {
op.GeoM.Scale(-1, 1)
b.position.x += float64(b.spritesheet.sprites[0].size.x)
}
op.GeoM.Translate(b.position.x, b.position.y)
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
subImageRect := image.Rect(
b.spritesheet.sprites[b.animation.currentFrame].startPosition.x,
b.spritesheet.sprites[b.animation.currentFrame].startPosition.y,
b.spritesheet.sprites[b.animation.currentFrame].endPosition.x,
b.spritesheet.sprites[b.animation.currentFrame].endPosition.y,
)
screen.DrawImage(b.image.SubImage(subImageRect).(*ebiten.Image), op)
}
func (b *BlinkTrailSection) update() {
if b.timeToLive <= 0 {
b.delete = true // Delete the boy
}
b.timeToLive--
b.animation.update(1.5)
}