-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
162 lines (149 loc) · 3.97 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"math"
"github.com/gopxl/pixel"
"github.com/gopxl/pixel/pixelgl"
)
var (
// The player-controled role
Player *Character
// Picture of the judging point
point pixel.Picture
// Picture of Reimu
reimu pixel.Picture
)
func init() {
var err error
point, err = loadPNG(".\\pics\\point.png")
if err != nil {
panic(err)
}
reimu, err = loadPNG(".\\pics\\reimu.png")
if err != nil {
panic(err)
}
SpawnPlayer()
}
func SpawnPlayer() {
Player = NewPlayer(5, 2, 0.6, pixel.V(WindowRange.W()/2, WindowRange.H()/4.0))
}
func UpdatePlayer(win *pixelgl.Window) {
speed := Player.speedFast
// Deal with movements
if win.Pressed(pixelgl.KeyLeftShift) {
speed = Player.speedSlow
}
if win.Pressed(pixelgl.KeyA) {
speed = Player.speedSlowest
}
if win.Pressed(pixelgl.KeyUp) {
Player.Velocity.Y += 1
}
if win.Pressed(pixelgl.KeyDown) {
Player.Velocity.Y -= 1
}
if win.Pressed(pixelgl.KeyLeft) {
Player.Velocity.X -= 1
}
if win.Pressed(pixelgl.KeyRight) {
Player.Velocity.X += 1
}
if !Player.Velocity.Eq(pixel.ZV) {
Player.Velocity = Player.Velocity.Unit().Scaled(speed)
}
// TODO
if !MoveRange.Contains(Player.Position.Add(pixel.V(Player.Velocity.X, 0))) {
Player.Velocity.X = 0
}
if !MoveRange.Contains(Player.Position.Add(pixel.V(0, Player.Velocity.Y))) {
Player.Velocity.Y = 0
}
Player.Position = Player.Position.Add(Player.Velocity)
Player.Velocity = pixel.ZV
var hasHit bool = false
for _, e := range emitters {
for _, b := range e.bullets {
if b.Pattern != none && !hasHit && isHit(b.Position, Player.Position, BulletList[b.Pattern].HitBoxRadius*b.Scale, Player.hitboxRadius) {
if !b.Keepable {
b.isDeleted = true
}
HitCount++
// Avoid multi-hit in one frame
hasHit = true
}
if b.Pattern != none && !b.grazed && isHit(b.Position, Player.Position, BulletList[b.Pattern].HitBoxRadius*b.Scale+1, Player.hitboxRadius+20) {
b.grazed = true
GrazeCount++
}
}
}
win.SetTitle("Hit: " + fmt.Sprint(HitCount) + ", Graze: " + fmt.Sprint(GrazeCount))
Player.ChDraw(win)
}
type Character struct {
age int
Position pixel.Vec
speedFast float64
speedSlow float64
speedSlowest float64
Velocity pixel.Vec
hitboxRadius float64
CharacterSprite *pixel.Sprite
HitBoxSprite *pixel.Sprite
}
func (p *Character) HitBox() pixel.Circle {
return pixel.C(p.Position, p.hitboxRadius)
}
func (p *Character) HBDraw(t pixel.Target) {
p.HitBoxSprite.Draw(t, pixel.IM.Scaled(pixel.ZV, 0.05*p.hitboxRadius).Rotated(pixel.ZV, math.Pi*float64(CurrentFrame)/60).Moved(p.Position))
}
func XOR(a, b bool) bool {
if (a && !b) || (b && !a) {
return true
}
return false
}
func (p *Character) ChDraw(win *pixelgl.Window) {
p.age++
basic := pixel.R(0, 208, 32, 256)
ux := pixel.V(32, 0)
uy := pixel.V(0, -48)
if !XOR(win.Pressed(pixelgl.KeyLeft), win.Pressed(pixelgl.KeyRight)) {
p.CharacterSprite = pixel.NewSprite(p.CharacterSprite.Picture(), basic.Moved(ux.Scaled(float64((p.age/6)%8))))
}
if win.JustPressed(pixelgl.KeyLeft) || win.JustPressed(pixelgl.KeyRight) {
p.age = 0
}
if win.Pressed(pixelgl.KeyLeft) {
x := 4
if p.age < 16 {
x = (p.age / 2) % 8
} else {
x += (p.age / 7) % 4
}
p.CharacterSprite = pixel.NewSprite(p.CharacterSprite.Picture(), basic.Moved(uy.Add(ux.Scaled(float64(x)))))
}
if win.Pressed(pixelgl.KeyRight) {
x := 4
if p.age < 16 {
x = (p.age / 2) % 8
} else {
x += (p.age / 7) % 4
}
p.CharacterSprite = pixel.NewSprite(p.CharacterSprite.Picture(), basic.Moved(uy.Scaled(2).Add(ux.Scaled(float64(x)))))
}
p.CharacterSprite.Draw(win, pixel.IM.Moved(p.Position))
}
func NewPlayer(speedF float64, speedS float64, speedSest float64, position pixel.Vec) *Character {
p := Character{
Position: position,
speedFast: speedF,
speedSlow: speedS,
speedSlowest: speedSest,
hitboxRadius: 3.0,
}
p.HitBoxSprite = pixel.NewSprite(point, point.Bounds())
p.CharacterSprite = pixel.NewSprite(reimu, reimu.Bounds())
return &p
}