-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatEntityVertical.go
119 lines (107 loc) · 3.44 KB
/
catEntityVertical.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
package main
import (
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type CatEntityVertical struct {
CatEntity
// Input parameter for update
CameraY float64
// Input parameter for update
Collided bool
flyImage *ebiten.Image
flyAnimationDirection float64
flyAnimationFrame float64
DebugModeEnabled bool
angle float64
collidedY float64
lastSteerDirection Direction
Direction Direction
}
func (me *CatEntityVertical) Initialize() {
me.Width = 19
me.Height = 48
me.flyImage = LoadImage(CAT_FLY_DOWN_IMAGE_BYTES)
me.flyAnimationDirection = 1
me.Direction = DIRECTION_BOTTOM
me.DebugModeEnabled = false
}
func (me *CatEntityVertical) Update(deltaTime float64) {
me.flyAnimationFrame += deltaTime * CAT_FLY_ANIMATION_FRAME_PER_SECOND * me.flyAnimationDirection
if CAT_FLY_ANIMATION_FRAME_COUNT <= me.flyAnimationFrame {
me.flyAnimationFrame = CAT_FLY_ANIMATION_FRAME_COUNT - 1
me.flyAnimationDirection = -1
}
if me.flyAnimationFrame <= 0 {
me.flyAnimationFrame = 1
me.flyAnimationDirection = 1
}
if me.Collided {
me.collidedY += deltaTime * me.getCollidedSpeedY()
me.angle = UnwindAngle(me.angle + deltaTime*me.getCollidedRotationSpeed())
} else {
me.updateSteer(deltaTime)
me.Y += me.GetSpeedY() * deltaTime
}
}
func (me *CatEntityVertical) updateSteer(deltaTime float64) {
for _, key := range me.PressedKeys {
if key == ebiten.KeyLeft {
me.X -= deltaTime * me.GetSteerSpeed()
me.lastSteerDirection = DIRECTION_LEFT
break
} else if key == ebiten.KeyRight {
me.lastSteerDirection = DIRECTION_RIGHT
me.X += deltaTime * me.GetSteerSpeed()
}
}
}
func (me *CatEntityVertical) Draw(screen *ebiten.Image) {
var drawOptions ebiten.DrawImageOptions
if me.lastSteerDirection == DIRECTION_LEFT {
ScaleCentered(&drawOptions, CAT_FLY_ANIMATION_FRAME_WIDTH, float64(me.flyImage.Bounds().Dy()), -1, 1)
}
if me.Direction == DIRECTION_TOP {
ScaleCentered(&drawOptions, CAT_FLY_ANIMATION_FRAME_WIDTH, float64(me.flyImage.Bounds().Dy()), 1, -1)
}
RotateCentered(&drawOptions, CAT_FLY_ANIMATION_FRAME_WIDTH, float64(me.flyImage.Bounds().Dy()), me.angle)
drawOptions.GeoM.Translate(me.X, me.Y-me.CameraY+me.collidedY)
var spriteShiftX = float64(int(me.flyAnimationFrame)) * CAT_FLY_ANIMATION_FRAME_WIDTH
var rectangle = GetShiftedRectangle(spriteShiftX, me.Width, me.Height)
if me.DebugModeEnabled {
var box = me.GetHitBox()
vector.DrawFilledRect(screen,
float32(box.A.X), float32(box.A.Y-me.CameraY), float32(box.GetWidth()), float32(box.GetHeight()),
color.NRGBA{R: 255, G: 255, B: 255, A: 127}, true)
}
screen.DrawImage(me.flyImage.SubImage(rectangle).(*ebiten.Image), &drawOptions)
}
// Measurement unit: pixels per second
func (me *CatEntityVertical) GetSpeedY() (result float64) {
result = 50
if me.Direction == DIRECTION_TOP {
result = -result
}
return
}
func (me *CatEntityVertical) GetSteerSpeed() float64 {
return 80
}
func (me *CatEntityVertical) GetHitBox() Rectangle {
var rect = Rectangle{
A: FloatPoint{
X: me.X,
Y: me.Y,
},
}
rect.B.X = rect.A.X + me.Width
rect.B.Y = rect.A.Y + me.Height
return rect.Shrink(1)
}
func (me *CatEntityVertical) getCollidedSpeedY() float64 {
return -50
}
func (me *CatEntityVertical) getCollidedRotationSpeed() float64 {
return 7
}