-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgame.go
121 lines (102 loc) · 3.08 KB
/
game.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
109
110
111
112
113
114
115
116
117
118
119
120
121
package gorched
import (
"fmt"
tl "github.com/JoelOtter/termloop"
"github.com/zladovan/gorched/core"
"github.com/zladovan/gorched/debug"
"github.com/zladovan/gorched/hud"
)
// Game holds information which is kept during whole session.
type Game struct {
// players holds all players in the game
players core.Players
// engine references to termloop's game
engine *tl.Game
// options holds game options used to create new game
options GameOptions
// hud holds games HUD
hud *hud.HUD
// controls contains processing of input
controls *Controls
// round is responsible for creating and managing state of game rounds
round *Round
}
// GameOptions provide configuration needed for creating new game
type GameOptions struct {
// Width of game world in number of console pixels (cells)
Width int
// Height of game world in number of console pixels (cells)
Height int
// PlayerCount is number of players which will be added to game
PlayerCount int
// Seed is number used as random seed and if it is reused it allows to play same game with same looking rounds
Seed int64
// Fps sets screen framerate
Fps int
// AsciiOnly identifies that only ASCII characters can be used for all graphics
ASCIIOnly bool
// LowColor identifies that only 8 colors can be used for all graphics
LowColor bool
// BrowserMode identifies that game was run in browser and some controls need to be modified to do not collide with usual browser shortcuts
BrowserMode bool
// Debug turns on debug mode if set to true
Debug bool
}
// NewGame creates new game object.
// Game is not started yet. You need to call Start().
func NewGame(o GameOptions) *Game {
game := &Game{}
game.options = o
// init engine
game.engine = tl.NewGame()
game.engine.Screen().SetFps(float64(o.Fps))
// init debug
if o.Debug {
debug.Attach(game.engine)
}
// init players
game.players = make(core.Players, o.PlayerCount)
for pi := range game.players {
game.players[pi] = core.NewPlayer(fmt.Sprintf("Player %d", pi+1))
}
// init controls
game.controls = &Controls{game: game}
game.engine.Screen().AddEntity(game.controls)
// init HUD
game.hud = hud.NewHUD(game, hud.Options{
ASCIIOnly: o.ASCIIOnly,
LowColor: o.LowColor,
BrowserMode: o.BrowserMode,
})
game.engine.Screen().AddEntity(game.hud)
// init round
game.round = NewRound(game)
game.engine.Screen().AddEntity(game.round)
// show info at startup
game.hud.ShowInfo()
return game
}
// Start starts the game which means that game engine is started and first round is set up.
func (g *Game) Start() {
g.engine.Start()
}
// InitialSeed returns seed used for the first level.
func (g *Game) InitialSeed() int64 {
return g.options.Seed
}
// LastSeed returns seed used for the last (current active) level.
func (g *Game) LastSeed() int64 {
return g.options.Seed + int64(g.round.Number()-1)
}
// Hud returns games HUD
func (g *Game) Hud() *hud.HUD {
return g.hud
}
// Engine returns reference to underlying game engine
func (g *Game) Engine() *tl.Game {
return g.engine
}
// Players returns all players in the game
func (g *Game) Players() core.Players {
return g.players
}