-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
176 lines (154 loc) · 4.37 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
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
package zelduh
import (
"fmt"
"image/color"
"os"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
)
type UI struct {
Window *pixelgl.Window
Text *text.Text
}
func NewUI(currLocaleMsgs LocaleMessagesMap, windowConfig WindowConfig) UI {
// Initialize text
orig := pixel.V(20, 50)
txt := text.New(orig, text.Atlas7x13)
txt.Color = colornames.Black
// Initialize window
win, err := pixelgl.NewWindow(
pixelgl.WindowConfig{
Title: currLocaleMsgs["gameTitle"],
Bounds: pixel.R(windowConfig.X, windowConfig.Y, windowConfig.Width, windowConfig.Height),
VSync: true,
},
)
if err != nil {
fmt.Println("Initializing GUI window failed:")
fmt.Println(err)
os.Exit(1)
}
return UI{
Window: win,
Text: txt,
}
}
func DrawCenterText(win *pixelgl.Window, txt *text.Text, s string, c color.RGBA) {
txt.Clear()
txt.Color = c
fmt.Fprintln(txt, s)
txt.Draw(win, pixel.IM.Moved(win.Bounds().Center().Sub(txt.Bounds().Center())))
}
func DrawMapBackground(win *pixelgl.Window, mapConfig MapConfig, color color.Color) {
s := imdraw.New(nil)
s.Color = color
s.Push(pixel.V(mapConfig.X, mapConfig.Y))
s.Push(pixel.V(mapConfig.X+mapConfig.Width, mapConfig.Y+mapConfig.Height))
s.Rectangle(0)
s.Draw(win)
}
func DrawScreenStart(win *pixelgl.Window, txt *text.Text, currLocaleMsgs LocaleMessagesMap, mapConfig MapConfig) {
win.Clear(colornames.Darkgray)
DrawMapBackground(win, mapConfig, colornames.White)
DrawCenterText(win, txt, currLocaleMsgs["gameTitle"], colornames.Black)
}
func DrawMapBackgroundImage(
win *pixelgl.Window,
spritesheet map[int]*pixel.Sprite,
allMapDrawData map[string]MapData,
name string,
modX, modY float64,
mapConfig MapConfig,
) {
d := allMapDrawData[name]
for _, spriteData := range d.Data {
if spriteData.SpriteID != 0 {
sprite := spritesheet[spriteData.SpriteID]
vec := spriteData.Rect.Min
movedVec := pixel.V(
vec.X+mapConfig.X+modX+TileSize/2,
vec.Y+mapConfig.Y+modY+TileSize/2,
)
matrix := pixel.IM.Moved(movedVec)
sprite.Draw(win, matrix)
}
}
}
func AddUICoin(systemsManager *SystemsManager, frameRate int) {
coin := BuildEntityFromConfig(GetPreset("uiCoin")(4, 14), systemsManager.NewEntityID(), frameRate)
systemsManager.AddEntity(coin)
}
// make sure only correct number of hearts exists in systems
// so, if health is reduced, need to remove a heart entity from the systems,
// the correct one... last one
func AddUIHearts(systemsManager *SystemsManager, hearts []Entity, health int) {
for i, entity := range hearts {
if i < health {
systemsManager.AddEntity(entity)
}
}
}
func DrawObstaclesPerMapTiles(
systemsManager *SystemsManager,
roomsMap Rooms,
allMapDrawData map[string]MapData,
roomID RoomID,
modX,
modY float64,
mapConfig MapConfig,
frameRate int,
) []Entity {
d := allMapDrawData[roomsMap[roomID].MapName()]
obstacles := []Entity{}
mod := 0.5
for _, spriteData := range d.Data {
if spriteData.SpriteID != 0 {
vec := spriteData.Rect.Min
movedVec := pixel.V(
vec.X+mapConfig.X+modX+TileSize/2,
vec.Y+mapConfig.Y+modY+TileSize/2,
)
if _, ok := NonObstacleSprites[spriteData.SpriteID]; !ok {
x := movedVec.X/TileSize - mod
y := movedVec.Y/TileSize - mod
id := systemsManager.NewEntityID()
obstacle := BuildEntityFromConfig(GetPreset("obstacle")(x, y), id, frameRate)
obstacles = append(obstacles, obstacle)
}
}
}
return obstacles
}
func DrawMask(win *pixelgl.Window, windowConfig WindowConfig, mapConfig MapConfig) {
// top
s := imdraw.New(nil)
s.Color = colornames.White
s.Push(pixel.V(0, mapConfig.Y+mapConfig.Height))
s.Push(pixel.V(windowConfig.Width, mapConfig.Y+mapConfig.Height+(windowConfig.Height-(mapConfig.Y+mapConfig.Height))))
s.Rectangle(0)
s.Draw(win)
// bottom
s = imdraw.New(nil)
s.Color = colornames.White
s.Push(pixel.V(0, 0))
s.Push(pixel.V(windowConfig.Width, (windowConfig.Height - (mapConfig.Y + mapConfig.Height))))
s.Rectangle(0)
s.Draw(win)
// left
s = imdraw.New(nil)
s.Color = colornames.White
s.Push(pixel.V(0, 0))
s.Push(pixel.V(0+mapConfig.X, windowConfig.Height))
s.Rectangle(0)
s.Draw(win)
// right
s = imdraw.New(nil)
s.Color = colornames.White
s.Push(pixel.V(mapConfig.X+mapConfig.Width, mapConfig.Y))
s.Push(pixel.V(windowConfig.Width, windowConfig.Height))
s.Rectangle(0)
s.Draw(win)
}