-
Notifications
You must be signed in to change notification settings - Fork 12
/
stage.go
277 lines (234 loc) · 7.07 KB
/
stage.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package vimman
import (
"fmt"
"github.com/nsf/termbox-go"
"time"
)
var levelConstructors = []func(*Game) *Level{
NewLevelBasicMovement,
NewLevelExitingVim,
NewLevelTextEditing,
NewLevelBomberman,
}
// holds the current level meta data, canvas and entities
type Stage struct {
Game *Game
Level int
LevelInstance *Level
Fps float64
CanvasEntities []Renderer // entities to be rendered in canvas
ScreenEntities []Renderer // entities to be rendered outside of the canvas
TypedEntities []Renderer
BgCell *termbox.Cell
Canvas Canvas
Width int
Height int
pixelMode bool
offsetx int
offsety int
ModeLabel *Word
ColonLine *Word
}
func NewStage(g *Game, level int, fps float64, bgCell *termbox.Cell) *Stage {
return &Stage{
Game: g,
Level: level,
Fps: fps,
CanvasEntities: nil,
ScreenEntities: nil,
TypedEntities: nil,
BgCell: bgCell,
Canvas: nil,
Width: 0,
Height: 0,
pixelMode: false,
offsetx: 0,
offsety: 0,
}
}
func (s *Stage) AddCanvasEntity(e ...Renderer) {
s.CanvasEntities = append(s.CanvasEntities, e...)
}
func (s *Stage) AddScreenEntity(e ...Renderer) {
s.ScreenEntities = append(s.ScreenEntities, e...)
}
func (s *Stage) AddTypedEntity(e ...Renderer) {
s.TypedEntities = append(s.TypedEntities, e...)
}
func (s *Stage) ClearCanvasEntities() {
s.CanvasEntities = nil
s.ScreenEntities = nil
s.TypedEntities = nil
}
func (s *Stage) SetGame(game *Game) {
s.Game = game
}
// this function handles all the rendering
// sets and renders in order:
// tilemap cells: the background cells
// canvas entities: canvas entities, they have update methods that gets called on stage.update method
// screen cells: cells outside of the canvas such as level label and instructions
func (s *Stage) Render() {
s.SetCanvasBackgroundCells()
for i, _ := range s.CanvasEntities {
e := s.CanvasEntities[i]
// sets the cells for the entity, so that it overwrites the background cell(s)
e.SetCells(s)
}
s.TermboxSetScreenCells()
s.TermboxSetCanvasCells()
s.TermboxSetTypedCells()
s.TermboxSetCursorCell()
termbox.Flush()
}
func (s *Stage) update(ev termbox.Event, delta time.Duration) {
for _, e := range s.CanvasEntities {
e.Update(s, ev, delta)
}
s.updateModeLabel()
s.updateColonLine()
}
func (s *Stage) updateModeLabel() {
s.ModeLabel.Content = fmt.Sprintf("-- %s MODE --", s.LevelInstance.VimMode)
s.ModeLabel.Entity.Cells = ConvertStringToCells(s.ModeLabel.Content, s.ModeLabel.Fg, s.ModeLabel.Bg)
}
func (s *Stage) updateColonLine() {
s.ColonLine.Entity.Cells = ConvertStringToCells(s.ColonLine.Content, s.ColonLine.Fg, s.ColonLine.Bg)
}
func (s *Stage) Init() {
level := s.Level - 1
if level < 0 || level > len(levelConstructors) {
level = 0
}
s.SetLevel(levelConstructors[level](s.Game))
}
func (s *Stage) SetLevel(levelInstance *Level) {
s.Reset()
s.LevelInstance = levelInstance
s.LevelInstance.Init()
s.LevelInstance.LoadTileMap()
s.Resize(s.LevelInstance.GetTileMapDimensions())
for _, e := range s.LevelInstance.Entities {
s.AddCanvasEntity(e)
}
modeLabelOptions := WordOptions{InitCallback: nil, Fg: levelTitleFg, Bg: levelTitleBg, CenterHorizontally: false, Tags: []Tag{{"ModeLabel"}}}
s.ModeLabel = NewWord(s, 0, s.Game.getScreenSizeY()-2, fmt.Sprintf("-- %s MODE --", levelInstance.VimMode), modeLabelOptions)
colonLineOptions := WordOptions{InitCallback: nil, Fg: levelTitleFg, Bg: levelTitleBg, CenterHorizontally: false, Tags: []Tag{{"ColonLine"}}}
s.ColonLine = NewWord(s, 0, s.Game.getScreenSizeY()-1, "", colonLineOptions)
s.AddScreenEntity(s.ModeLabel, s.ColonLine)
}
func (s *Stage) Reset() {
s.ClearCanvasEntities()
s.Canvas = NewCanvas(10, 10)
}
func (s *Stage) Resize(w, h int) {
s.Width = w
s.Height = h
if s.pixelMode {
s.Height *= 2
}
if s.LevelInstance.Height != 0 && s.LevelInstance.Width != 0 {
s.Width = s.LevelInstance.Width
s.Height = s.LevelInstance.Height
}
c := NewCanvas(s.Width, s.Height)
// Copy old data that fits
for i := 0; i < MinInt(s.Height, len(s.Canvas)); i++ {
for j := 0; j < MinInt(s.Width, len(s.Canvas)); j++ {
c[i][j] = s.Canvas[i][j]
}
}
s.Canvas = c
}
func (s *Stage) GetDefaultBgCell() *TermBoxCell {
return &TermBoxCell{s.BgCell, false, TileMapCellData{}}
}
func (s *Stage) GetRendererEntityByTag(wantedTag Tag) Renderer {
for _, ce := range s.CanvasEntities {
for _, tag := range ce.GetTags() {
if tag.Name == wantedTag.Name {
return ce
}
}
}
return nil
}
// sets the background cells to be rendered, this gets rendered first in the render method
// so that other cells can be overwritten into the same location
func (s *Stage) SetCanvasBackgroundCells() {
for i, row := range s.Canvas {
for j, _ := range row {
if s.LevelInstance.TileMap[i][j].Cell != nil {
// insert tile map cell
s.Canvas.SetCellAt(i, j, s.LevelInstance.TileMap[i][j])
} else {
//insert default bg cell
s.Canvas.SetCellAt(i, j, s.GetDefaultBgCell())
}
}
}
}
// calls termbox.setCell, sets the coordinates and the cell attributes
// this does the actual rendering of the characters, thanks to termbox-go <3
func (s *Stage) TermboxSetCell(x, y int, cell *TermBoxCell, offset bool) {
if offset {
offsetX, offsetY := s.LevelInstance.GetScreenOffset()
x += offsetX
y += offsetY
}
termbox.SetCell(x, y, cell.Ch,
termbox.Attribute(cell.Fg),
termbox.Attribute(cell.Bg))
}
// sets the cells inside the canvas, offset is being applied in order to keep the canvas in center
func (s *Stage) TermboxSetCanvasCells() {
for i, row := range s.Canvas {
for j, _ := range row {
cell := row[j]
// intentionally use j,i in reverse order
s.TermboxSetCell(j, i, cell, true)
}
}
}
// sets the cells outside of the canvas, no offset is being applied
func (s *Stage) TermboxSetScreenCells() {
for _, e := range s.ScreenEntities {
for j, _ := range e.GetCells() {
cell := e.GetCells()[j]
x := e.GetPositionX()
offsetX := 0
if e.ShouldCenterHorizontally() {
offsetX, _ = e.GetScreenOffset()
}
x = x + j + offsetX
s.TermboxSetCell(x, e.GetPositionY(), cell, false)
}
}
}
func (s *Stage) TermboxSetTypedCells() {
for _, e := range s.TypedEntities {
for j, _ := range e.GetCells() {
cell := e.GetCells()[j]
s.TermboxSetCell(e.GetPositionX(), e.GetPositionY(), cell, true)
}
}
}
func (s *Stage) TermboxSetCursorCell() {
cursorEntity := s.GetRendererEntityByTag(Tag{"Cursor"})
for j, _ := range cursorEntity.GetCells() {
cell := cursorEntity.GetCells()[j]
s.TermboxSetCell(cursorEntity.GetPositionX(), cursorEntity.GetPositionY(), cell, true)
}
}
func (s *Stage) CheckCollision(x, y int) bool {
return s.Canvas.CheckCollision(x, y)
}
// clear Canvas cell and TileMap cell at the given positions
func (s *Stage) ClearTileMapCellsAt(positions [][2]int) {
for _, pos := range positions {
options := DefaultWordOptions()
emptyChar := NewEmptyCharacter(s, pos[0], pos[1], options)
s.AddTypedEntity(emptyChar)
s.LevelInstance.TileMap[pos[1]][pos[0]].collidesPhysically = false
}
}