-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
98 lines (77 loc) · 1.89 KB
/
main.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
package main
import (
"errors"
_ "image/jpeg"
"log"
"github.com/ably-labs/Ableye/config"
"github.com/hajimehoshi/ebiten/v2"
)
var (
state gameState
)
func init() {
state = titleScreen
}
type Game struct{}
// NewGame is a constructor for the game.
func NewGame() *Game {
return &Game{}
}
//handleClose is called when the used closes the game window.
func handleClose() {
connectionIDs := []connectionID{clientA, clientB, clientC, clientD}
for _, id := range connectionIDs {
if connections[id] != nil && connections[id].realtimeClient != nil {
connections[id].realtimeClient.Close()
}
}
}
//Update updates the logical state.
func (g *Game) Update() error {
if ebiten.IsWindowBeingClosed() {
handleClose()
// An error must be returned to trigger the window closing once closing has been handled.
return errors.New("window has been closed")
}
// Handle updates for each game state.
switch state {
case titleScreen:
updateTitleScreen()
case clientScreen:
updateClientScreen()
}
return nil
}
//Draw renders the screen.
func (g *Game) Draw(screen *ebiten.Image) {
//Draw debug elements if debug mode is on.
if config.Cfg.DebugMode {
drawDebugText(screen)
}
//Handle drawing for each game state.
switch state {
case titleScreen:
drawTitleScreen(screen)
case clientScreen:
drawRealtimeScreen(screen)
}
}
//Layout returns the logical screen size, the screen is automatically scaled.
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle(titleText)
// initialisation
initialiseTitleScreen()
initialiseRealtimeScreen()
// Create a new instance of game.
game := NewGame()
// Set window closing handled to true.
ebiten.SetWindowClosingHandled(true)
// Run the game.
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}