-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.go
58 lines (51 loc) · 1.42 KB
/
GameManager.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
/* GameManager.go © Penguin_Spy 2024
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package main
import (
"image"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"path/filepath"
)
// https://stackoverflow.com/a/49595208
func getImageFromFilePath(filePath string) (image.Image, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
image, _, err := image.Decode(f)
return image, err
}
type game struct {
id string // the internal ID of this game
name string // the user-facing title of this game
hero image.Image // the "hero" image, shown in the background of the game's page
plugin *plugin // the plugin that defined this game
}
var games []*game
// creates a game object that represents a game. does not add it to the registered games list.
func DefineGame(id string, name string, heroFile string, plugin *plugin) (*game, error) {
image, err := getImageFromFilePath(filepath.Join(getPluginDir(plugin.id), heroFile))
if err != nil {
log.Printf("[GameManager] loading image %q failed\n", heroFile)
return nil, err
}
game := &game{
id,
name,
image,
plugin,
}
return game, nil
}
// registers a game that was previously defined with DefineGame
func RegisterGame(game *game) {
games = append(games, game)
}