-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.go
110 lines (97 loc) · 2.06 KB
/
enemy.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"
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
)
const curveAcc = 0.05
type EnemyType int64
const (
Linear EnemyType = iota
CurveL
CurveR
)
type Enemy struct {
X, Y float64
VX, VY float64
Width, Height float64
Hit bool
Alive bool
Type EnemyType
Score int
}
func NewEnemy(X, Y, VX, VY float64) *Enemy {
return &Enemy{X: X, Y: Y, VX: VX, VY: VY, Width: EnemyW, Height: EnemyH, Hit: false, Alive: true, Type: Linear, Score: 20}
}
func NewRandomEnemy(VY float64) *Enemy {
x := float64(rand.Int31n(SCREENWIDTH - EnemyW))
var eType EnemyType
var vx float64
var score int
if v := rand.Int63n(6); v >= 4 {
if x+EnemyH/2 < SCREENWIDTH/2 {
eType = CurveL
} else {
eType = CurveR
}
vx = 0
score = 50
} else {
eType = Linear
vx = rand.Float64() - 0.5
score = 20
}
return &Enemy{
X: x,
Y: -EnemyH - 5,
VX: vx,
VY: VY,
Width: EnemyW,
Height: EnemyH,
Hit: false,
Alive: true,
Type: eType,
Score: score,
}
}
func (e *Enemy) Move(speedMult float64) {
maxVX := e.VY * 1.2
switch e.Type {
case CurveL:
if e.VX <= maxVX {
e.VX += curveAcc
} else {
e.VX = maxVX
}
case CurveR:
if e.VX >= -maxVX {
e.VX -= curveAcc
} else {
e.VX = -maxVX
}
}
e.X += e.VX * speedMult
e.Y += e.VY * speedMult
}
func (e *Enemy) GetSprite() *ebiten.Image {
maxVX := e.VY * 1.2
switch e.Type {
case CurveL:
if e.VX < maxVX*0.3 {
return CurveLSheet.SubImage(image.Rect(0, 0, 50, 50)).(*ebiten.Image)
} else if e.VX < maxVX*0.6 {
return CurveLSheet.SubImage(image.Rect(50, 0, 100, 50)).(*ebiten.Image)
} else {
return CurveLSheet.SubImage(image.Rect(100, 0, 150, 50)).(*ebiten.Image)
}
case CurveR:
if e.VX > -maxVX*0.3 {
return CurveRSheet.SubImage(image.Rect(0, 0, 50, 50)).(*ebiten.Image)
} else if e.VX > -maxVX*0.6 {
return CurveRSheet.SubImage(image.Rect(50, 0, 100, 50)).(*ebiten.Image)
} else {
return CurveRSheet.SubImage(image.Rect(100, 0, 150, 50)).(*ebiten.Image)
}
}
return LinearImage
}