-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
157 lines (115 loc) · 3.85 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
package main
import (
"math"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/rajveermalviya/go-webgpu/wgpu"
)
type Player struct {
Entity
state *State
p *Mat
v *Mat
mvp_buf *wgpu.Buffer
roll float32
}
func NewPlayer(state *State) (*Player, error) {
mvp_buf, err := state.device.CreateBuffer(&wgpu.BufferDescriptor{
Size: 64,
Usage: wgpu.BufferUsage_Uniform | wgpu.BufferUsage_CopyDst,
})
if err != nil {
return nil, err
}
position := [3]float32{0, 0, 0}
rotation := [2]float32{math.Pi / 2, 0}
return &Player{
Entity: *NewEntity(state, position, rotation, 0.2, 1.72*M_TO_AYLIN),
state: state,
p: NewMat().Identity(),
v: NewMat().Identity(),
mvp_buf: mvp_buf,
}, nil
}
func (player *Player) HandleInputs() {
speed := float32(1.5)
// Camera movement
input := []float32{0, 0}
if player.state.win.GetKey(glfw.KeyW) == glfw.Press || player.state.win.GetKey(glfw.KeyUp) == glfw.Press {
input[1] = -1
}
if player.state.win.GetKey(glfw.KeyS) == glfw.Press || player.state.win.GetKey(glfw.KeyDown) == glfw.Press {
input[1] = 1
}
if player.state.win.GetKey(glfw.KeyA) == glfw.Press || player.state.win.GetKey(glfw.KeyLeft) == glfw.Press {
input[0] = -1
}
if player.state.win.GetKey(glfw.KeyD) == glfw.Press || player.state.win.GetKey(glfw.KeyRight) == glfw.Press {
input[0] = 1
}
if player.state.win.GetKey(glfw.KeySpace) == glfw.Press {
player.Jump()
}
if input[1] != 0 || input[0] != 0 {
angle := player.rot[0] - math.Pi/2 + float32(math.Atan2(float64(input[1]), float64(input[0])))
player.acc[0] = float32(math.Cos(float64(angle))) * speed
player.acc[2] = float32(math.Sin(float64(angle))) * speed
}
}
func (player *Player) HandleMouse() {
sensitivity := 6
// Camera rotation
x, y := player.state.win.GetCursorPos()
width, height := player.state.win.GetSize()
player.rot[0] += float32((x-float64(width)/2)/float64(width)) * float32(sensitivity)
player.rot[1] -= float32((y-float64(height)/2)/float64(height)) * float32(sensitivity)
player.rot[1] = float32(math.Max(-math.Pi/2, math.Min(math.Pi/2, float64(player.rot[1]))))
// Lock cursor in the center of the window
player.state.win.SetCursorPos(float64(width)/2, float64(height)/2)
// Hide cursor
player.state.win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
}
func (player *Player) Release() {
player.mvp_buf.Release()
}
const M_TO_AYLIN = 1 / 1.64
var doneuk1 = false
func (player *Player) mvp(m *Mat) *Mat {
width, height := player.state.win.GetSize()
eyelevel := float32(1) // exactly one Aylin
player.p.Perspective(math.Pi/2, float32(width)/float32(height), 0.01, 50)
player.v.Identity()
player.v.Multiply(NewMat().Rotate2d((player.rot[0] - math.Pi/2), player.rot[1]))
player.v.Multiply(NewMat().Translation(-player.pos[0], -player.pos[1]-eyelevel, -player.pos[2]))
target_roll := 0.
if player.Entity.pos[1] < -1 && !player.state.apat.portal_lit {
if !doneuk1 {
displayDialogue(getDialogues(), "ukulele1", player.state)
doneuk1 = true
}
player.state.alexis_room.should_draw = false
target_roll = math.Pi
}
player.roll += (float32(target_roll) - player.roll) * player.state.dt * 1
roll_mat := NewMat().Rotate(player.roll, 0, 0, 1)
aylin_conversion_mat := NewMat().Scale(M_TO_AYLIN, M_TO_AYLIN, M_TO_AYLIN)
if player.state.obama_room.should_draw {
player.pos[0] = -2
player.pos[1] = 0
player.pos[2] = 0
}
mvp := NewMat().Multiply(player.p).Multiply(roll_mat).Multiply(player.v).Multiply(m).Multiply(aylin_conversion_mat)
player.state.queue.WriteBuffer(player.mvp_buf, 0, wgpu.ToBytes(mvp.Data[:]))
return mvp
}
func (player *Player) Update() {
player.HandleInputs()
player.HandleMouse()
player.Entity.Update([]*Model{
player.state.alexis_room.room,
player.state.apat.landscape,
})
if player.state.win.GetKey(glfw.KeyEscape) == glfw.Press {
println("Escape pressed -> Close window")
player.state.win.SetShouldClose(true)
}
}