/
player.go
123 lines (101 loc) · 3.16 KB
/
player.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
package game
import (
_ "image/png"
"math"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
shootCooldown = time.Millisecond * 250
rotationPerSecond = math.Pi
tankSpeed = 100.0
bulletSpawnOffset = 40.0
bulletSpeed = 10.0
)
type Player struct {
game *Game
tank *Tank
rotation float64
position Vector
bulletSprite *ebiten.Image
bullets []*Bullet
shootCooldown *Timer
}
func NewPlayer(game *Game) *Player {
bodySprite := game.assets.GetRandomTankBody()
barrelSprite := game.assets.GetRandomTankBarrell()
bulletSprite := game.assets.GetSprite("bulletRed2.png")
return &Player{
game: game,
rotation: 0,
tank: NewTank(game, bodySprite, barrelSprite),
position: Vector{X: screenWidth / 2, Y: screenHeight / 2},
shootCooldown: NewTimer(shootCooldown),
bulletSprite: bulletSprite,
}
}
func (p *Player) Update() {
rotationSpeed := rotationPerSecond / float64(ebiten.TPS())
movementSpeed := tankSpeed / float64(ebiten.TPS())
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
p.rotation -= rotationSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyRight) {
p.rotation += rotationSpeed
}
p.shootCooldown.Update()
if p.shootCooldown.IsReady() && ebiten.IsKeyPressed(ebiten.KeySpace) {
p.shootCooldown.Reset()
bounds := p.tank.BodySprite.Bounds()
bulletBounds := p.bulletSprite.Bounds()
halfWBullet := bulletBounds.Dx() / 2
halfHBullet := bulletBounds.Dy() / 2
halfW := float64(bounds.Dx()) / 2
halfH := float64(bounds.Dy()) / 2
spawnPos := Vector{
p.position.X + halfW - float64(halfWBullet) + math.Sin(p.rotation)*bulletSpawnOffset,
p.position.Y + halfH - float64(halfHBullet) + math.Cos(p.rotation)*-bulletSpawnOffset,
}
p.bullets = append(p.bullets, NewBullet(p.bulletSprite, spawnPos, p.rotation, bulletSpeed))
}
// move towards facing
if ebiten.IsKeyPressed(ebiten.KeyUp) {
//first get the direction the entity is pointed
dx := math.Sin(p.rotation)
dy := math.Cos(p.rotation)
// if direction.length() > 0 {
// direction = direction.normalise()
// }
p.position.X += dx * movementSpeed
p.position.Y -= dy * movementSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyDown) {
//first get the direction the entity is pointed
dx := math.Sin(p.rotation)
dy := math.Cos(p.rotation)
// if direction.length() > 0 {
// direction = direction.normalise()
// }
p.position.X -= dx * movementSpeed
p.position.Y += dy * movementSpeed
}
if ebiten.IsKeyPressed(ebiten.KeyT) && inpututil.IsKeyJustPressed(ebiten.KeyT) {
p.tank = NewTank(p.game, p.game.assets.GetRandomTankBody(), p.game.assets.GetRandomTankBarrell())
}
var visibleBullets []*Bullet
for _, bullet := range p.bullets {
bullet.Update()
if bullet.position.X > 0 && bullet.position.X < float64(p.game.width) && bullet.position.Y > 0 && bullet.position.Y < float64(p.game.height) {
visibleBullets = append(visibleBullets, bullet)
}
}
p.bullets = visibleBullets
}
func (p *Player) Draw(screen *ebiten.Image) {
p.tank.Draw(screen, p.position, p.rotation)
for _, bullet := range p.bullets {
bullet.Draw(screen)
}
// fmt.Println("Bullets", len(p.bullets))
}