-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.go
108 lines (95 loc) · 2.71 KB
/
menu.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
rl "github.com/gen2brain/raylib-go/raylib"
)
type Menu struct {
play *Button
auth *Button
scoreboard []ScoreboardEntry
}
type ScoreboardEntry struct {
Id string `json:"id"`
Pass string `json:"pass"`
Name string `json:"name"`
Points int `json:"points"`
}
func (m *Menu) draw() {
m.play.draw()
m.auth.draw()
drawCenteredText("Score: "+strconv.Itoa(score), int32(screenWidt)/2, 10, 20, rl.White)
drawCenteredText("Highscore: "+strconv.Itoa(highScore), screenWidt/2, 40, 30, rl.White)
if gameVersion != currentVersion {
drawCenteredText("Outdated version", screenWidt/2, screenHeight-20, 30, rl.Blue)
}
rl.DrawText(currentVersion, 4, screenHeight-24, 20, rl.White)
for k, v := range m.scoreboard {
if k > 10 {
return
}
drawCenteredText(fmt.Sprintf("%s <---------> %d", v.Name, v.Points), screenWidt/2, int32(180+k*30), 26, rl.Red)
}
rl.DrawText("Session:", 20, screenHeight-120, 20, rl.White)
rl.DrawText(fmt.Sprintf("Deaths: %d", deaths), 30, screenHeight-100, 20, rl.White)
rl.DrawText(fmt.Sprintf("lastGametime: %dmin %dsec", time.Unix(lastGametime, 0).Minute(), time.Unix(lastGametime, 0).Second()), 30, screenHeight-80, 20, rl.White)
rl.DrawText(fmt.Sprintf("Opened: %s", time.Unix(gameOpened, 0).Format("2006-01-02, 15:04:05")), 30, screenHeight-60, 20, rl.White)
}
func (m *Menu) getScoreboardData() {
res, err := http.Get("https://go-snake-backend.fly.dev/get")
if err != nil {
fmt.Printf("Error fetching scoreboard: %s\n", err.Error())
return
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("Error fetching scoreboard: %s\n", err.Error())
return
}
scoreboard := make([]ScoreboardEntry, 0)
err = json.Unmarshal(data, &scoreboard)
if err != nil {
fmt.Printf("Error fetching scoreboard: %s\n", err.Error())
return
}
m.scoreboard = scoreboard
}
func (m *Menu) init() {
go m.getScoreboardData()
m.play = &Button{
x: screenWidt / 2,
y: 100,
text: "PLAY",
color: rl.White,
defaultColor: rl.White,
activeColor: rl.Beige,
fontSize: 30,
grownFontSize: 40,
normalFontSize: 30,
callback: func() {
gamescreen.init()
state = 1
score = 0
},
}
m.auth = &Button{
x: screenWidt / 2,
y: 150,
text: "AUTH",
color: rl.White,
defaultColor: rl.White,
activeColor: rl.Beige,
fontSize: 30,
grownFontSize: 40,
normalFontSize: 30,
callback: func() {
authscreen.init()
state = 3
},
}
}