-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
213 lines (182 loc) · 5.59 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"math/rand"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)
var m_pPlayers []CPlayer
type CPlayer struct {
m_nPosX int32
m_nPosY int32
m_nPosZ int32
m_nRotation int32 // 0 - up, 1 - right, 2 - down, 3 - left
m_nState int32
}
func NewCPlayer(w *CWorld) CPlayer {
return CPlayer{
m_nPosX: rand.Int31n(w.m_nWorldX),
m_nPosY: rand.Int31n(w.m_nWorldY),
m_nPosZ: 0,
m_nRotation: 0,
m_nState: 2, // standing
}
}
func (CPlayer) Close() error { return nil }
func (p *CPlayer) Tick() {
// If we're falling, move us down a cell or stop falling
if p.m_nState == 1 {
if globalWorld.TopFallingIceCube(p.m_nPosX, p.m_nPosY, p.m_nPosZ) != -1 {
p.m_nPosZ = globalWorld.TopFallingIceCube(p.m_nPosX, p.m_nPosY, p.m_nPosZ) + 1
p.m_nState = 1 // falling
} else {
p.m_nPosZ = globalWorld.TopIceCube(p.m_nPosX, p.m_nPosY) + 1
p.m_nState = 2 // standing
}
}
}
func (p *CPlayer) ProcessKey(key glfw.Key) {
var nVelX, nVelY int32
// HACK?
plusMinusOne := func(b bool) int32 {
if b {
return +1
} else {
return -1
}
}
boolToInt := func(b bool) int32 {
if b {
return +1
} else {
return 0
}
}
switch key {
case glfw.KeyUp, glfw.KeyW:
nVelX = boolToInt(p.m_nRotation == 1) - boolToInt(p.m_nRotation == 3)
nVelY = boolToInt(p.m_nRotation == 0) - boolToInt(p.m_nRotation == 2)
case glfw.KeyRight, glfw.KeyD:
nVelX = boolToInt(p.m_nRotation == 0) - boolToInt(p.m_nRotation == 2)
nVelY = boolToInt(p.m_nRotation == 3) - boolToInt(p.m_nRotation == 1)
case glfw.KeyDown, glfw.KeyS:
nVelX = boolToInt(p.m_nRotation == 3) - boolToInt(p.m_nRotation == 1)
nVelY = boolToInt(p.m_nRotation == 2) - boolToInt(p.m_nRotation == 0)
case glfw.KeyLeft, glfw.KeyA:
nVelX = boolToInt(p.m_nRotation == 2) - boolToInt(p.m_nRotation == 0)
nVelY = boolToInt(p.m_nRotation == 1) - boolToInt(p.m_nRotation == 3)
case glfw.KeyPageUp: //case glfw.KeyKP_9:
nVelX = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 1)
nVelY = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 3)
case glfw.KeyPageDown: //case glfw.KeyKP_3:
nVelX = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 3)
nVelY = plusMinusOne(p.m_nRotation == 2 || p.m_nRotation == 3)
case glfw.KeyEnd: //case glfw.KeyKP_1:
nVelX = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 1) * -1
nVelY = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 3) * -1
case glfw.KeyHome: //case glfw.KeyKP_7:
nVelX = plusMinusOne(p.m_nRotation == 0 || p.m_nRotation == 3) * -1
nVelY = plusMinusOne(p.m_nRotation == 2 || p.m_nRotation == 3) * -1
default:
}
if nVelX != 0 || nVelY != 0 {
nTIC := globalWorld.TopIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY)
nTFIC := globalWorld.TopFallingIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY, p.m_nPosZ+boolToInt(p.m_nState == 1))
if nTIC == p.m_nPosZ && globalWorld.GetIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY, nTIC+1) != 1 &&
globalWorld.GetIceCube(p.m_nPosX, p.m_nPosY, p.m_nPosZ+1) == 0 {
// nothing blocking our way, so let's climb that ice cube
p.m_nPosZ++
p.m_nPosX += nVelX
p.m_nPosY += nVelY
p.m_nState = 2 // standing
// win?
if p.m_nPosZ >= globalWorld.m_nWinHeight {
p.Win()
}
} else if nTIC < p.m_nPosZ && nTFIC == -1 &&
globalWorld.GetIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY, p.m_nPosZ) == 0 {
// no falling cubes in the way, just quickly jump down
p.m_nPosZ = nTIC + 1
p.m_nPosX += nVelX
p.m_nPosY += nVelY
p.m_nState = 2 // standing
} else if nTIC < p.m_nPosZ && nTFIC != -1 &&
(globalWorld.GetIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY, p.m_nPosZ) == 0 ||
(globalWorld.GetIceCube(p.m_nPosX+nVelX, p.m_nPosY+nVelY, p.m_nPosZ) == 1 &&
globalWorld.GetIceCube(p.m_nPosX, p.m_nPosY, p.m_nPosZ+1) == 0)) {
// ride an ice cube down
p.m_nPosZ = nTFIC + 1
p.m_nPosX += nVelX
p.m_nPosY += nVelY
p.m_nState = 1 // falling
// win?
if p.m_nPosZ >= globalWorld.m_nWinHeight {
p.Win()
}
}
}
}
func (p *CPlayer) Render() {
gl.PushMatrix()
switch p.m_nState {
case 1: // falling
gl.Translatef(float32(p.m_nPosX)+0.5, float32(p.m_nPosY)+0.5, (1.0-globalWorld.GetTickProcess())+float32(p.m_nPosZ))
case 2: // standing
gl.Translatef(float32(p.m_nPosX)+0.5, float32(p.m_nPosY)+0.5, float32(p.m_nPosZ))
default:
}
gl.Rotatef(float32(p.m_nRotation)*90.0, 0.0, 0.0, -1.0)
gl.Color3f(1.0, 0.0, 0.0)
gl.Begin(gl.LINES)
{
gl.Vertex3f(0.0, 0.0, 0.5)
gl.Vertex3f(0.0, 0.5, 0.5)
gl.Vertex3f(-0.5, 0.0, 0.5)
gl.Vertex3f(0.5, 0.0, 0.5)
gl.Vertex3f(0.0, 0.0, 0.0)
gl.Vertex3f(0.0, 0.0, 1.0)
}
gl.End()
gl.PopMatrix()
}
func (p *CPlayer) Win() {
g_bPaused = true
globalWindow.SetTitle("glPenguin v0.0 - you win!")
}
func (p *CPlayer) Die() {
g_bPaused = true
globalWindow.SetTitle("glPenguin v0.0 - you got crushed by an ice cube...")
}
func (p *CPlayer) GetCrushedAt(nPosX, nPosY, nPosZ int32) {
if nPosZ == p.m_nPosZ && nPosX == p.m_nPosX && nPosY == p.m_nPosY {
p.Die()
}
}
func CreatePlayers(nNumberPlayers int) {
// create new players
m_pPlayers = make([]CPlayer, nNumberPlayers)
for nLoop1 := range m_pPlayers {
m_pPlayers[nLoop1] = NewCPlayer(globalWorld)
}
}
func TickAtAll(nPosZ int32) {
for nLoop1 := range m_pPlayers {
if m_pPlayers[nLoop1].m_nPosZ == nPosZ {
m_pPlayers[nLoop1].Tick()
}
}
}
func ProcessKeyAll(key glfw.Key) {
for nLoop1 := range m_pPlayers {
m_pPlayers[nLoop1].ProcessKey(key)
}
}
func RenderAll() {
for nLoop1 := range m_pPlayers {
m_pPlayers[nLoop1].Render()
}
}
func GetCrushedAtAll(nPosX, nPosY, nPosZ int32) {
for nLoop1 := range m_pPlayers {
m_pPlayers[nLoop1].GetCrushedAt(nPosX, nPosY, nPosZ)
}
}