-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher.go
153 lines (141 loc) · 3.19 KB
/
gopher.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
package main
import (
"math"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
type Gopher struct {
window *glfw.Window
plan Plan
texture uint32
coords Rect
height float32
width float32
r2l bool // moving direction: true when right to left, false when left to right
onFloor bool
floor Rect
}
func NewGopher(window *glfw.Window, plan Plan) Entity {
texture, bounds := NewTexture("assets/gopher.png")
h, w := bounds.Right.Y, bounds.Right.X
return &Gopher{
window: window,
plan: plan,
texture: texture,
height: h,
width: w,
coords: Rect{
Left: Point{-w, h},
Right: Point{w, -h},
},
}
}
const (
delta float32 = 0.001
toUpDy float32 = 1.02
toBottomDy float32 = 0.08
maxDy float32 = 1.04
)
var (
toUp bool
dy float32
)
func (g *Gopher) Update() {
isButtonPress := func(b glfw.Key) bool {
return g.window.GetKey(b) == glfw.Press
}
isLeft := isButtonPress(glfw.KeyLeft)
isRight := isButtonPress(glfw.KeyRight)
isTop := isButtonPress(glfw.KeyUp)
checkBoundaries := func() (bool, Rect) {
return CheckBoundaries(g.coords, g.plan.GetBoundaries()...)
}
if isLeft {
g.coords.Left.X -= 0.1
if in, bound := checkBoundaries(); in {
g.coords.Left.X = bound.Right.X + delta
}
g.coords.Right.X = g.coords.Left.X + 2*g.width
g.r2l = true
}
if isRight {
g.coords.Right.X += 0.1
if in, bound := checkBoundaries(); in {
g.coords.Right.X = bound.Left.X - delta
}
g.coords.Left.X = g.coords.Right.X - 2*g.width
g.r2l = false
}
if g.onFloor {
if (g.coords.Left.X < g.floor.Left.X && g.coords.Right.X < g.floor.Left.X) ||
(g.coords.Right.X > g.floor.Right.X && g.coords.Left.X > g.floor.Right.X) {
g.onFloor = false
g.floor = Rect{}
}
}
if isTop && g.onFloor {
dy = toUpDy
sinY := float32(math.Sin(float64(dy)))
g.coords.Left.Y += (1.5*sinY - sinY)
if in, bound := checkBoundaries(); in {
g.coords.Left.Y = bound.Right.Y - delta
}
g.coords.Right.Y = g.coords.Left.Y - 2*g.height
g.onFloor = false
toUp = true
}
if toUp {
dy += delta
if dy < maxDy {
sinY := float32(math.Sin(float64(dy)))
g.coords.Left.Y += (1.5*sinY - sinY)
if in, bound := checkBoundaries(); in {
g.coords.Left.Y = bound.Right.Y - delta
toUp = false
dy = toBottomDy
}
g.coords.Right.Y = g.coords.Left.Y - 2*g.height
} else {
toUp = false
dy = toBottomDy
}
}
if !g.onFloor && !toUp {
dy += delta
sinY := float32(math.Sin(float64(dy)))
g.coords.Right.Y -= sinY
if in, bound := checkBoundaries(); in {
g.coords.Right.Y = bound.Left.Y + delta
g.onFloor = true
g.floor = bound
}
g.coords.Left.Y = g.coords.Right.Y + 2*g.height
}
}
func (g *Gopher) GetCoords() Rect {
return g.coords
}
func (g *Gopher) Render() {
// TODO extract
gl.PushMatrix()
{
gl.Translatef(g.coords.Left.X+g.width, g.coords.Right.Y+g.height, 0)
var rect Rect
if g.r2l {
rect = Rect{
Left: Point{g.width, -g.height},
Right: Point{-g.width, g.height},
}
} else {
rect = Rect{
Left: Point{-g.width, -g.height},
Right: Point{g.width, g.height},
}
}
DrawTexture(g.texture, rect)
}
gl.PopMatrix()
}
func (g *Gopher) Unload() {
gl.DeleteTextures(1, &g.texture)
}