-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
90 lines (75 loc) · 1.81 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
package main
import (
glfw "github.com/go-gl/glfw3"
"math"
)
type Player struct {
Thing
score int
window *glfw.Window
}
const speed = 10
const maxSpeed float64 = 40.0
const brake = .95
const initialSize = 32
func NewPlayer(location Vector2, window *glfw.Window) *Player {
return &Player{Thing: Thing{location: location, targetSize: initialSize, size: 16, color: Color4f{0, 0, 1, 1}}, score: 0, window: window}
}
func (this *Player) update(elapsed float64) {
this.Thing.update(elapsed)
if keyDown(this.window, glfw.KeyW) {
this.up(elapsed)
}
if keyDown(this.window, glfw.KeyS) {
this.down(elapsed)
}
if keyDown(this.window, glfw.KeyA) {
this.left(elapsed)
}
if keyDown(this.window, glfw.KeyD) {
this.right(elapsed)
}
this.direction = this.direction.scale(brake)
}
func (this *Player) collideWith(e Element) {
switch t := e.(type) {
default:
debugf("unexpected type %T", t)
case *Thing:
this.absorb(e)
case *Shrinker:
this.burst()
case *Pickup:
this.score += 10
}
e.die()
}
func (this *Player) absorb(e Element) {
if this.isDead() {
panic("Dead things can't absorb!")
}
this.targetSize += e.Size()
}
func (this *Player) burst() {
if this.targetSize > initialSize {
this.targetSize -= math.Min(this.size-initialSize, this.targetSize/2)
}
}
func (this *Player) up(elapsed float64) {
this.thrust(Vector2{0, speed * elapsed})
}
func (this *Player) down(elapsed float64) {
this.thrust(Vector2{0, -speed * elapsed})
}
func (this *Player) left(elapsed float64) {
this.thrust(Vector2{-speed * elapsed, 0})
}
func (this *Player) right(elapsed float64) {
this.thrust(Vector2{speed * elapsed, 0})
}
func (this *Player) thrust(v Vector2) {
this.direction = this.direction.Add(v).clampedTo(maxSpeed)
}
func keyDown(window *glfw.Window, key glfw.Key) bool {
return window.GetKey(key) == glfw.Press
}