-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathui.go
128 lines (105 loc) · 5.25 KB
/
ui.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
package main
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/go-gl/mathgl/mgl32"
)
type ui struct {
canvas *pixelgl.Canvas
miniMapSprite *pixel.Sprite
miniMapFrameCanvas *pixelgl.Canvas
miniMapCanvas *pixelgl.Canvas
miniMapScale float64
middleTextStr string
deathScreenTimer float64
uPos mgl32.Vec2
lifeCanvas *pixelgl.Canvas
playerLife float64
}
func (u *ui) create() {
u.canvas = pixelgl.NewCanvas(pixel.R(0, 0, float64(wViewMax), float64(wViewMax)))
u.canvas.Clear(pixel.RGBA{R: 0, G: 0, B: 0, A: 0})
u.miniMapCanvas = pixelgl.NewCanvas(pixel.R(0, 0, 100, 100))
u.miniMapCanvas.SetUniform("uPos", &u.uPos)
u.miniMapCanvas.SetUniform("uTime", &global.uTime)
u.miniMapCanvas.SetFragmentShader(fragmentShaderMinimap)
img, _, _, _ := loadTexture(fmt.Sprintf("%v%v", wAssetObjectsPath, "minimap.png"))
pic := pixel.PictureDataFromImage(img)
u.miniMapSprite = pixel.NewSprite(pic, pic.Bounds())
u.miniMapFrameCanvas = pixelgl.NewCanvas(pic.Bounds())
u.updatePlayerLife()
}
// Mini map
func (u *ui) updateMiniMap() {
u.miniMapScale = 0.25 / (float64(global.gWorld.width) / 1024)
pos := global.gPlayer.getPosition()
//canvas := pixelgl.NewCanvas(pixel.R(0, 0, 1, 1))
//canvas.Clear(pixel.RGBA{1.0, 0, 0, 0.5})
offsetX := float64(global.gWorld.width/2) * u.miniMapScale
offsetY := float64(global.gWorld.height/2) * u.miniMapScale
//offset_x2 := offset_x //- float64(global.gWorld.width/2)*u.miniMapScale
//offset_y2 := offset_y //- float64(global.gWorld.height/2)*u.miniMapScale
u.uPos = mgl32.Vec2{float32(offsetX / 2), float32(offsetY / 2)}
bounds := u.miniMapFrameCanvas.Bounds()
//u.miniMapSprite.Draw(u.miniMapFrameCanvas, pixel.IM.Moved(pixel.V(offset_x2, offset_y2)).ScaledXY(pixel.V(0.8, 0.8), pixel.V(0.8, 0.8)))
u.miniMapSprite.Draw(u.miniMapFrameCanvas, pixel.IM.Moved(pixel.V(bounds.Max.X/2, bounds.Max.Y/2)).ScaledXY(pixel.V(0.6, 0.6), pixel.V(0.6, 0.6)))
//global.gWorld.bgSprite.Draw(u.miniMapCanvas, pixel.IM.ScaledXY(pixel.V(u.miniMapScale, u.miniMapScale), pixel.V(u.miniMapScale/2, u.miniMapScale/2)).Moved(pixel.V(u.miniMapScale*pos.X+offset_x-float64(global.gWorld.width/2)*u.miniMapScale, u.miniMapScale*pos.Y+offset_y-float64(global.gWorld.height/2)*u.miniMapScale)))
bounds = global.gWorld.bgSprite.Frame()
global.gWorld.bgSprite.Draw(u.miniMapCanvas,
pixel.IM.ScaledXY(
pixel.V(u.miniMapScale, u.miniMapScale),
pixel.V(u.miniMapScale/2, u.miniMapScale/2),
).Moved(
pixel.V((bounds.Max.X*u.miniMapScale)/2+pos.X*u.miniMapScale, (bounds.Max.Y*u.miniMapScale)/2+pos.Y*u.miniMapScale)))
//canvas.Draw(u.canvas, pixel.IM.Moved(pixel.V(u.miniMapScale*pos.X+offset_x-float64(global.gWorld.width/2)*u.miniMapScale, u.miniMapScale*pos.Y+offset_y-float64(global.gWorld.height/2)*u.miniMapScale)))
//canvas.Draw(u.canvas, pixel.IM.Moved(pixel.V(offset_x, offset_y)))
}
func (u *ui) setMiddleText(text string) {
//u.middleTextStr = text
//u.middleText.Clear()
//u.middleText.WriteString(fmt.Sprintf("%v", text))
}
func (u *ui) updateFPS(fps int) {
//u.fps.Clear()
//u.fps.WriteString(fmt.Sprintf("FPS: %v", strconv.Itoa(fps)))
}
func (u *ui) updatePlayerLife() {
if u.playerLife != global.gPlayer.life {
u.playerLife = global.gPlayer.life
u.lifeCanvas = global.gFont.write(fmt.Sprintf("Life: %v", u.playerLife))
}
}
func (u *ui) draw(dt float64) {
// Draw death screen
color := pixel.RGBA{}
if global.gPlayer.life == 0 {
u.deathScreenTimer += dt
u.setMiddleText(wDeathScreenText)
red := u.deathScreenTimer / 5
if red > 0.5 {
red = 0.5
}
color = pixel.RGBA{R: red, G: 0, B: 0, A: u.deathScreenTimer / 5}
u.canvas.Clear(color)
u.canvas.Draw(global.gWin, pixel.IM.Moved(pixel.V(global.gCamera.pos.X+u.canvas.Bounds().Max.X/2, global.gCamera.pos.Y+u.canvas.Bounds().Max.Y/2)))
} else {
u.deathScreenTimer = 0
}
u.updatePlayerLife()
u.miniMapCanvas.Clear(pixel.RGBA{R: 0, G: 0, B: 0, A: 0})
u.miniMapFrameCanvas.Clear(pixel.RGBA{R: 0, G: 0, B: 0, A: 0})
//u.updateMiniMap()
//offsetX := (float64(global.gVariableConfig.WindowWidth) / global.gCamera.zoom) - u.lifeCanvas.Bounds().Max.X
offsetX := (u.lifeCanvas.Bounds().Max.X / global.gCamera.zoom)
offsetY := (float64(global.gVariableConfig.WindowHeight) / global.gCamera.zoom) - u.lifeCanvas.Bounds().Max.Y
lifeScale := float64(global.gVariableConfig.WindowWidth) / float64(global.gVariableConfig.WindowHeight) / 5
u.lifeCanvas.Draw(global.gWin, pixel.IM.Scaled(pixel.ZV, lifeScale).Moved(pixel.V(global.gCamera.pos.X+offsetX, global.gCamera.pos.Y+offsetY)))
//bounds := u.miniMapFrameCanvas.Bounds()
//u.miniMapFrameCanvas.Draw(global.gWin, pixel.IM.Moved(pixel.V(global.gCamera.pos.X+bounds.Max.X/2, global.gCamera.pos.Y+bounds.Max.Y/2)))
//miniMapScale := float64(global.gVariableConfig.WindowWidth) / float64(global.gVariableConfig.WindowHeight) / 5
//u.miniMapFrameCanvas.Draw(global.gWin, pixel.IM.Scaled(pixel.ZV, miniMapScale).Moved(pixel.V(global.gCamera.pos.X+offsetX, global.gCamera.pos.Y+offsetY)))
//bounds = u.miniMapCanvas.Bounds()
//u.miniMapCanvas.Draw(global.gWin, pixel.IM.Moved(pixel.V(global.gCamera.pos.X, global.gCamera.pos.Y)))
//u.miniMapCanvas.Draw(global.gWin, pixel.IM.Moved(pixel.V(global.gCamera.pos.X+wViewMax/2.0, global.gCamera.pos.Y+wViewMax/2.0)))
}