-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (74 loc) · 1.5 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
package main
import (
"fmt"
"github.com/MichaelDiBernardo/srl/lib/client"
"github.com/MichaelDiBernardo/srl/lib/client/console"
"github.com/MichaelDiBernardo/srl/lib/game"
"io"
"log"
"os"
)
// A single running game. Once we get to serverland, srl will handle multiple
// of these simultaneously.
type Session struct {
client client.Client
game *game.Game
}
func NewSession() *Session {
g := game.NewGame()
g.Start()
return &Session{
client: console.New(),
game: g,
}
}
func (s *Session) Loop() {
err := s.client.Init()
if err != nil {
panic(err)
}
defer s.client.Close()
for {
// Take potentially multiple inputs from the user until they do
// something that generates a command.
var command game.Command
for {
s.client.Render(s.game)
com, err := s.client.Poll()
if err == nil {
command = com
break
}
}
// Handle the command.
_, quit := command.(game.QuitCommand)
if quit {
return
}
s.game.Handle(command)
// TODO: HACK fix the render calls, we shouldn't need one for every single
// event handled.
for !s.game.Events.Empty() {
ev := s.game.Events.Next()
s.client.HandleEvent(ev)
s.client.Render(s.game)
}
}
}
var logfile *os.File
func setup() {
f, err := os.OpenFile("srl.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("error opening logfile: %v", err))
}
log.SetOutput(io.Writer(f))
}
func teardown() {
logfile.Close()
}
func main() {
setup()
defer teardown()
s := NewSession()
s.Loop()
}