-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorb.go
More file actions
123 lines (109 loc) · 2.88 KB
/
orb.go
File metadata and controls
123 lines (109 loc) · 2.88 KB
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
package main
import (
"math"
"github.com/creativeprojects/cavern/lib"
"github.com/hajimehoshi/ebiten/v2"
)
type Orb struct {
*Collide
blowImages []*ebiten.Image
trapImages [2][]*ebiten.Image
popSounds [][]byte
direction float64
active bool
floating bool
timer int
blownFrames int
trappedEnemyType RobotType
}
func NewOrb(level *Level) *Orb {
return &Orb{
Collide: NewCollide(level, lib.NewSprite(lib.XCentre, lib.YBottom)),
blowImages: []*ebiten.Image{images["orb0"], images["orb1"], images["orb2"],
images["orb3"], images["orb4"], images["orb5"], images["orb6"]},
trapImages: [2][]*ebiten.Image{
{images["trap00"], images["trap01"], images["trap02"], images["trap03"], images["trap04"], images["trap05"], images["trap06"], images["trap07"]},
{images["trap10"], images["trap11"], images["trap12"], images["trap13"], images["trap14"], images["trap15"], images["trap16"], images["trap17"]},
},
popSounds: [][]byte{sounds["pop0"], sounds["pop1"], sounds["pop2"], sounds["pop3"]},
}
}
func (o *Orb) Reset() *Orb {
o.active = true
o.timer = 0
return o
}
func (o *Orb) Start(x, y, direction float64) *Orb {
o.active = true
o.floating = false
o.trappedEnemyType = RobotNone
o.direction = direction
o.blownFrames = 6
o.MoveTo(x, y)
o.SetSequenceFunc(imageSequence).Animate(o.blowImages, nil, 3, true)
return o
}
func (o *Orb) Blow() {
o.blownFrames += 4
}
func (o *Orb) IsActive() bool {
return o.active
}
func (o *Orb) TrapEnemy(robotType RobotType) {
o.trappedEnemyType = robotType
o.floating = true
o.SetSequenceFunc(nil).Animate(o.trapImages[robotType-1], nil, 4, true)
}
func (o *Orb) EnemyTrapped() bool {
return o.trappedEnemyType > RobotNone
}
// Hit tests if the coordinates collide with us and returns yes if it does
func (o *Orb) Hit(x, y float64) bool {
collided := o.CollidePoint(x, y)
if collided {
o.timer = OrbMaxTimer - 1
}
return collided
}
func (o *Orb) Update(game *Game) {
if !o.IsActive() {
return
}
o.timer++
if o.floating {
o.CollideMove(0, -1, float64(randomInt(1, 2)))
} else {
ok := o.CollideMove(o.direction, 0, 4)
if !ok {
// can't go further (because of a wall)
o.floating = true
}
}
if o.timer == o.blownFrames {
o.floating = true
}
if o.timer > OrbMaxTimer || o.Y(lib.YBottom) <= -40 {
o.active = false
game.StartPop(PopOrb, o.X(lib.XCentre), o.Y(lib.YBottom))
// create an extra fruit if an enemy was trapped in it
if o.trappedEnemyType > RobotNone {
fruit := game.CreateFruit(true)
fruit.MoveTo(o.X(lib.XCentre), math.Ceil(o.Y(lib.YBottom)))
}
game.RandomSoundEffect(o.popSounds)
return
}
o.Sprite.Update()
}
func (o *Orb) Draw(screen *ebiten.Image) {
if !o.IsActive() {
return
}
o.Sprite.Draw(screen)
}
func imageSequence(timer int) int {
if timer < 9 {
return timer / 3
}
return 3 + int(math.Mod(math.Floor((float64(timer)-9)/8), 4))
}