-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (70 loc) · 1.47 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
package main
import (
"log"
"os"
"test/symbols"
"github.com/hajimehoshi/ebiten/v2"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
const (
screenWidth = 640
screenHeight = 480
)
// Game structure
type Game struct {
interpreter *interp.Interpreter
}
// Update method
func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyR) {
reloadScripts()
}
return nil
}
// Draw method
func (g *Game) Draw(screen *ebiten.Image) {
drawDebugString(screen)
}
// Layout method
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
var drawDebugString func(*ebiten.Image)
func reloadScripts() {
i := interp.New(interp.Options{})
i.Use(stdlib.Symbols)
i.Use(symbols.Symbols)
bytes, err := os.ReadFile("src/debug.go")
if err != nil {
log.Fatal(err)
return
}
src := string(bytes)
_, err = i.Eval(src)
if err != nil {
log.Fatal(err)
return
}
// Retrieve the DrawDebugString function from the script
v, err := i.Eval("src.DrawDebugString")
if err != nil {
log.Fatal(err)
}
// Assert the function type and call it
drawDebugString = v.Interface().(func(*ebiten.Image))
}
func main() {
// Create the game instance
game := &Game{
interpreter: interp.New(interp.Options{}),
}
reloadScripts()
// Set window size
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Ebiten with Yaegi")
// Run the game
if err := ebiten.RunGame(game); err != nil {
log.Fatal(err)
}
}