-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.go
62 lines (50 loc) · 1020 Bytes
/
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
package main
type Vec2 struct {
X float32
Y float32
}
const (
minPlayerPosY float32 = 40
maxPlayerPosY float32 = 340
)
var availableColors []string = []string{"red", "blue", "green", "yellow"}
type Player struct {
Id string
Name string
Pos Vec2
Vel Vec2
TargetY float32
Color string
}
func NewPlayer(socketId string, playerName string, posY float32, colorIndex int) (player *Player) {
player = &Player{
Id: socketId,
Name: playerName,
Vel: Vec2{X: 250, Y: 0},
Pos: Vec2{X: 300, Y: posY},
TargetY: 100,
Color: availableColors[colorIndex],
}
return
}
func (self *Player) Move(msg string) {
switch msg {
case "down":
self.Vel.Y = 30
case "up":
self.Vel.Y = -30
case "stop":
self.Vel.Y = 0
}
}
func (self *Player) Update(dt float32) {
self.Pos.X += self.Vel.X * dt
self.Pos.Y += self.Vel.Y
self.Vel.Y = 0
if self.Pos.Y < minPlayerPosY {
self.Pos.Y = minPlayerPosY
}
if self.Pos.Y > maxPlayerPosY {
self.Pos.Y = maxPlayerPosY
}
}