This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
139 lines (112 loc) · 2.72 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"image/color"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
const (
healRate = 0.5
)
var (
Player = player{
health: 100,
maxHealth: 100,
attackDam: 15,
aoe: 15,
}
playerPics []*pixel.Sprite
playerSize = pixel.V(16, 16)
)
type player struct {
coins int
angle float64
health float64
maxHealth float64
attackDam float64
aoe float64
}
func (p *player) update(dt float64, win *pixelgl.Window) leveler {
if p.health < 0 {
pauseGame()
return &DeathScreen
}
if p.health < p.maxHealth {
p.health += healRate * dt
}
if p.health > p.maxHealth {
p.health = p.maxHealth
}
// check for coin collisions
coinCollision()
p.angle = winBounds.Center().To(win.MousePosition()).Angle()
// Attack
if basicAttack.acquired && win.JustPressed(pixelgl.MouseButton1) {
p.attack(win)
}
if win.JustPressed(pixelgl.MouseButton2) {
if atomBomb.acquired {
p.Atom(win)
} else if rocketLauncher.acquired {
p.rocket(win)
} else if gun.acquired {
p.gun(win)
}
}
return currentLvl
}
func (p *player) draw(target pixel.Target) {
// TODO animate
pos := p.pos()
playerPics[0].Draw(target, pixel.IM.Moved(pos).Rotated(pos, p.angle))
}
func (p player) collisionBox() pixel.Rect {
centre := cam.Unproject(winBounds.Center().Sub(playerSize))
return pixel.R(
centre.X,
centre.Y,
centre.X+playerSize.X,
centre.Y+playerSize.Y,
)
}
func (p *player) hurt(delta float64) {
p.health -= delta
PlaySound(hurtSound)
}
func (p player) pos() pixel.Vec {
return cam.Unproject(winBounds.Center().Sub(playerSize))
}
func (p player) attack(win *pixelgl.Window) {
PlaySound(attackSound)
clickedPos := win.MousePosition()
toClick := winBounds.Center().To(clickedPos).Unit().Scaled(p.aoe * 1.5).Add(p.pos())
aoe := pixel.C(toClick, p.aoe)
NewSwipe(toClick, winBounds.Center().To(clickedPos), p.aoe, color.RGBA{
R: 0x66,
G: 0xbb,
B: 0x66,
A: 0xbb,
})
for _, e := range Enemies {
if aoe.IntersectRect(e.collisionBox()) != pixel.ZV {
e.hurt(p.attackDam)
}
}
}
func (p player) gun(win *pixelgl.Window) {
PlaySound(gunSound)
dir := winBounds.Center().To(win.MousePosition())
NewProjectile(p.pos().Add(dir.Unit().Scaled(1.1*16)), winBounds.Center().To(win.MousePosition()), 16*8, 25, 4, colornames.Black, true)
}
func (p player) rocket(win *pixelgl.Window) {
PlaySound(rocketLauncherSound)
dir := winBounds.Center().To(win.MousePosition())
NewProjectile(p.pos().Add(dir.Unit().Scaled(1.1*16)), winBounds.Center().To(win.MousePosition()), 16*6, 50, 8, colornames.Black, true)
}
func (p player) Atom(win *pixelgl.Window) {
atom(win)
}
func addCoins(delta int) {
PlaySound(coinPickupSound)
Player.coins += delta
}