-
Notifications
You must be signed in to change notification settings - Fork 0
/
tank.go
219 lines (203 loc) · 5.89 KB
/
tank.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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"math"
"math/rand"
"strconv"
)
const (
ShootCooled = 180
DieHitStatus = 30
)
var (
keyUpUpdates int64
keyDownUpdates int64
keyLeftUpdates int64
keyRightUpdates int64
)
type Tank struct {
*BoxSprite
typ int
game *Game
life int
maxLife int
speed float64
bulletSize float64
bulletSpeed float64
bullet *Bullet
shootCool int // 射击冷却程度
shootCoolDown int // 射击冷却速度
hitSprites [5]SpriteInfo // 爆炸动画,被击中且死亡时播放
hitStatus int // 大于0表示被击中,免疫攻击
hitProtect int // 击中后的免疫时间
}
type Hero struct {
*Tank
}
type Enemy struct {
*Tank
}
func (tk *Tank) Draw(screen *ebiten.Image) {
if tk.hitStatus > 0 {
if tk.life > 0 {
tk.BoxSprite.Draw(screen)
tk.BoxSprite.DrawBorder(screen)
} else {
options := &ebiten.DrawImageOptions{}
options.GeoM.Translate(tk.X, tk.Y)
sprite := tk.hitSprites[(tk.hitStatus*len(tk.hitSprites)-1)/DieHitStatus]
screen.DrawImage(GetSpriteImage(tk.game.spriteImages, sprite), options)
}
} else {
tk.BoxSprite.Draw(screen)
}
if tk.life > 0 {
text.Draw(screen, strconv.Itoa(tk.life), tk.game.chsFont,
int(tk.X+float64(tk.W)/2-5), int(tk.Y+float64(tk.H)/2+5),
LifeColors[(tk.life*len(LifeColors)-1)/tk.maxLife])
}
bullet := tk.bullet
for bullet != nil {
bullet.Draw(screen)
bullet = bullet.next
}
}
func (tk *Tank) CollideOthers() (minX, minY, maxX, maxY float64) {
// 与其他坦克的碰撞检测
for other := tk.game.enemy; other != nil; other = other.Next {
if tk != other.Value.Tank && other.Value.life > 0 {
if cx, cy := tk.CollideXY(other.Value.BoxSprite); cx != 0 && cy != 0 {
maxX = math.Max(cx, maxX)
minX = math.Min(cx, minX)
maxY = math.Max(cy, maxY)
minY = math.Min(cy, minY)
}
}
}
// 敌人需要判断与英雄的碰撞
if tk != tk.game.hero.Tank {
if cx, cy := tk.CollideXY(tk.game.hero.Tank.BoxSprite); cx != 0 && cy != 0 {
maxX = math.Max(cx, maxX)
minX = math.Min(cx, minX)
maxY = math.Max(cy, maxY)
minY = math.Min(cy, minY)
}
}
// 坦克与树的碰撞检测
for tree := tk.game.ground.trees; tree != nil; tree = tree.Next {
if cx, cy := tk.CollideXY(tree.Value); cx != 0 && cy != 0 {
maxX = math.Max(cx, maxX)
minX = math.Min(cx, minX)
maxY = math.Max(cy, maxY)
minY = math.Min(cy, minY)
}
}
return
}
func (h *Hero) UpdateMove() {
if h.life < 1 {
return
}
minKeyUpdates := getMinKeyUpdates()
if minKeyUpdates < math.MaxInt64 {
// 控制坦克方向
if minKeyUpdates == keyUpUpdates {
h.A = AnglePi
h.Tank.Move()
}
if minKeyUpdates == keyDownUpdates {
h.A = AngleZero
h.Tank.Move()
}
if minKeyUpdates == keyLeftUpdates {
h.A = AngleHalfPi
h.Tank.Move()
}
if minKeyUpdates == keyRightUpdates {
h.A = AngleTrebleHalfPi
h.Tank.Move()
}
}
}
func getMinKeyUpdates() int64 {
var minKeyUpdates int64 = math.MaxInt64
if ebiten.IsKeyPressed(ebiten.KeyW) || ebiten.IsKeyPressed(ebiten.KeyUp) ||
ebiten.IsStandardGamepadButtonPressed(GamepadID, ebiten.StandardGamepadButtonLeftTop) ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisLeftStickVertical) < -0.4 ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisRightStickVertical) < -0.4 {
keyUpUpdates++
minKeyUpdates = minInt64(keyUpUpdates, minKeyUpdates)
} else {
keyUpUpdates = 0
}
if ebiten.IsKeyPressed(ebiten.KeyS) || ebiten.IsKeyPressed(ebiten.KeyDown) ||
ebiten.IsStandardGamepadButtonPressed(GamepadID, ebiten.StandardGamepadButtonLeftBottom) ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisLeftStickVertical) > 0.4 ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisRightStickVertical) > 0.4 {
keyDownUpdates++
minKeyUpdates = minInt64(keyDownUpdates, minKeyUpdates)
} else {
keyDownUpdates = 0
}
if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft) ||
ebiten.IsStandardGamepadButtonPressed(GamepadID, ebiten.StandardGamepadButtonLeftLeft) ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) < -0.4 ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisRightStickHorizontal) < -0.4 {
keyLeftUpdates++
minKeyUpdates = minInt64(keyLeftUpdates, minKeyUpdates)
} else {
keyLeftUpdates = 0
}
if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight) ||
ebiten.IsStandardGamepadButtonPressed(GamepadID, ebiten.StandardGamepadButtonLeftRight) ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisLeftStickHorizontal) > 0.4 ||
ebiten.StandardGamepadAxisValue(GamepadID, ebiten.StandardGamepadAxisRightStickHorizontal) > 0.4 {
keyRightUpdates++
minKeyUpdates = minInt64(keyRightUpdates, minKeyUpdates)
} else {
keyRightUpdates = 0
}
return minKeyUpdates
}
func minInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func (e *Enemy) AutoMove() {
if e.life < 1 {
return
}
if e.game.updates%(1+rand.Intn(180)) == 0 {
e.A = TankAngles[rand.Intn(len(TankAngles))]
}
e.speed = TankSpeeds[e.typ] * (1 + float64(e.game.score)/1000)
e.Tank.Move()
}
func (tk *Tank) Move() {
if tk.A == AnglePi {
tk.Y = tk.Y - tk.speed
}
if tk.A == AngleZero {
tk.Y = tk.Y + tk.speed
}
if tk.A == AngleHalfPi {
tk.X = tk.X - tk.speed
}
if tk.A == AngleTrebleHalfPi {
tk.X = tk.X + tk.speed
}
minX, minY, maxX, maxY := tk.CollideOthers()
if tk.A == AnglePi || tk.A == AngleZero {
tk.Y = tk.Y + minY + maxY
}
if tk.A == AngleHalfPi || tk.A == AngleTrebleHalfPi {
tk.X = tk.X + minX + maxX
}
// 限制不能超出屏幕
dw, dh := tk.GetDrawWH()
tk.X = math.Max(0, math.Min(tk.X, float64(tk.game.width)-dw))
tk.Y = math.Max(0, math.Min(tk.Y, float64(tk.game.height)-dh))
}