This repository has been archived by the owner on Jan 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
picker.go
161 lines (131 loc) · 4.27 KB
/
picker.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
/*
To the extent possible under law, Ian Davis has waived all copyright
and related or neighboring rights to this Amberfell Source Code file.
This work is published from the United Kingdom.
*/
package main
import (
"fmt"
"github.com/banthar/Go-SDL/sdl"
"github.com/banthar/gl"
"math"
)
type Picker struct {
x, y, radius, actionItemRadius, selectionRadius float32
}
func NewPicker() *Picker {
var p Picker
p.radius = float32(90) * PIXEL_SCALE
p.actionItemRadius = p.radius - BLOCK_SCALE*1.5
p.selectionRadius = BLOCK_SCALE * 1.2
return &p
}
func (self *Picker) Draw(t int64) {
gl.MatrixMode(gl.MODELVIEW)
gl.PushMatrix()
gl.LoadIdentity()
gl.Disable(gl.DEPTH_TEST)
gl.Disable(gl.LIGHTING)
gl.Disable(gl.LIGHT0)
gl.Disable(gl.LIGHT1)
gl.Begin(gl.TRIANGLE_FAN)
gl.Color4ub(0, 0, 0, 128)
gl.Vertex2f(self.x, self.y)
for angle := float64(0); angle <= 2*math.Pi; angle += math.Pi / 2 / 10 {
gl.Vertex2f(self.x-float32(math.Sin(angle))*self.radius, self.y+float32(math.Cos(angle))*self.radius)
}
gl.End()
self.DrawItemHighlight(t, ThePlayer.currentAction)
self.DrawPlayerItems(t, true)
gl.PopMatrix()
}
func (self *Picker) DrawItemHighlight(t int64, position Action) {
gl.PushMatrix()
gl.LoadIdentity()
actionItemAngle := -(float64(position) - 1.5) * math.Pi / 4
gl.Begin(gl.TRIANGLE_FAN)
gl.Color4ub(64, 64, 64, 228)
gl.Vertex2f(self.x-self.actionItemRadius*float32(math.Sin(actionItemAngle)), self.y+self.actionItemRadius*float32(math.Cos(actionItemAngle)))
for angle := float64(0); angle <= 2*math.Pi; angle += math.Pi / 2 / 10 {
gl.Vertex2f(self.x-self.actionItemRadius*float32(math.Sin(actionItemAngle))-float32(math.Sin(angle))*self.selectionRadius, self.y+self.actionItemRadius*float32(math.Cos(actionItemAngle))+float32(math.Cos(angle))*self.selectionRadius)
}
gl.End()
gl.PopMatrix()
}
func (self *Picker) DrawPlayerItems(t int64, drawQuantities bool) {
gl.PushMatrix()
gl.LoadIdentity()
for i := 0; i < 5; i++ {
itemid := ThePlayer.equippedItems[i]
if itemid != ITEM_NONE {
angle := -(float64(i) + 1.5) * math.Pi / 4
gl.LoadIdentity()
x := self.x - self.actionItemRadius*float32(math.Sin(angle))
y := self.y + self.actionItemRadius*float32(math.Cos(angle))
gl.Translatef(x, y, 0)
gl.Rotated(90, 1.0, 0.0, 0.0)
gl.Rotated(30*math.Sin(float64(t)/1e9+float64(itemid)/2), 0.0, 1.0, 0.0)
gl.Scalef(BLOCK_SCALE, BLOCK_SCALE, BLOCK_SCALE)
gGuiBuffer.Reset()
if itemid < 256 {
TerrainCube(gGuiBuffer, Vectori{}, [18]BlockId{BLOCK_DIRT, BLOCK_DIRT, BLOCK_DIRT, BLOCK_DIRT, BLOCK_AIR, BLOCK_DIRT, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR, BLOCK_AIR}, NewBlockDefault(BlockId(itemid)), FACE_NONE)
} else {
RenderItemFlat(gGuiBuffer, Vectori{}, BlockId(itemid))
}
gGuiBuffer.RenderDirect(false)
if drawQuantities {
gl.LoadIdentity()
gl.Translatef(x-17*PIXEL_SCALE, y-19*PIXEL_SCALE, 20)
consoleFont.Print(fmt.Sprintf("%d", ThePlayer.inventory[itemid]))
}
}
}
gl.PopMatrix()
}
// x and y are in screen2d coords
func (self *Picker) HitTest(x, y float64) (bool, int) {
for i := 0; i < 8; i++ {
angle := -(float64(i) - 1.5) * math.Pi / 4
ix := float64(picker.x) - float64(picker.actionItemRadius)*math.Sin(angle)
iy := float64(picker.y) + float64(picker.actionItemRadius)*math.Cos(angle)
if x > ix-float64(picker.selectionRadius) && x < ix+float64(picker.selectionRadius) &&
y > iy-float64(picker.selectionRadius) && y < iy+float64(picker.selectionRadius) {
return true, i
}
}
return false, 0
}
func (self *Picker) HandleMouseButton(re *sdl.MouseButtonEvent) {
if re.Button == 1 && re.State == 1 { // LEFT, DOWN
x, y := viewport.ScreenCoordsToWorld2D(re.X, re.Y)
if hit, pos := self.HitTest(x, y); hit {
ThePlayer.SelectAction(pos)
}
}
}
func (self *Picker) HandleKeys(keys []uint8) {
if keys[sdl.K_1] != 0 {
ThePlayer.SelectAction(0)
}
if keys[sdl.K_2] != 0 {
ThePlayer.SelectAction(1)
}
if keys[sdl.K_3] != 0 {
ThePlayer.SelectAction(2)
}
if keys[sdl.K_4] != 0 {
ThePlayer.SelectAction(3)
}
if keys[sdl.K_5] != 0 {
ThePlayer.SelectAction(4)
}
if keys[sdl.K_6] != 0 {
ThePlayer.SelectAction(5)
}
if keys[sdl.K_7] != 0 {
ThePlayer.SelectAction(6)
}
if keys[sdl.K_8] != 0 {
ThePlayer.SelectAction(7)
}
}