-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlevel.go
More file actions
157 lines (127 loc) · 3.45 KB
/
Copy pathlevel.go
File metadata and controls
157 lines (127 loc) · 3.45 KB
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
package vimman
import (
"github.com/nsf/termbox-go"
"reflect"
"time"
)
type VimMode int
const (
normalMode VimMode = iota
insertMode
colonMode
)
func (m VimMode) String() string {
return [...]string{"NORMAL", "INSERT", "NORMAL"}[m]
}
const (
levelTitleCoordX int = 0
levelTitleCoordY int = 1
levelTitleFg termbox.Attribute = termbox.ColorGreen
levelTitleBg termbox.Attribute = termbox.ColorBlack
levelExplanationCoordX int = 0
levelExplanationCoordY int = 2
levelHintCoordX int = 0
levelHintCoordY int = 3
typedCharacterFg termbox.Attribute = termbox.ColorWhite
typedCharacterBg termbox.Attribute = termbox.ColorBlack
)
type Level struct {
Game *Game
VimMode VimMode
TileMapString string
TileMap [][]*TermBoxCell
TileData TileMapCellDataMap
Entities []Renderer
InputRunes []rune
BlockedKeys []termbox.Key
InputBlocked bool
TextShiftingDisabled bool
BgCell *termbox.Cell
Width int
Height int
Init func()
ColonLineCallbacks map[string]func(game *Game)
}
func (l *Level) Update(s *Stage, t time.Duration) {
}
func (l *Level) SetCells(s *Stage) {
}
func (l *Level) GetSize() (int, int) {
index, length := 0, 0
for i, line := range l.TileMap {
if len(line) > length {
index, length = i, len(line)
}
}
return len(l.TileMap[index]), len(l.TileMap)
}
func (l *Level) GetScreenOffset() (int, int) {
offsetX, offsetY := 0, 0
screenWidth, screenHeight := l.Game.getScreenSize()
levelWidth, levelHeight := l.GetSize()
if screenWidth > levelWidth {
offsetX = (screenWidth - levelWidth) / 2
}
if screenHeight > levelHeight {
offsetY = (screenHeight - levelHeight) / 2
}
return offsetX, offsetY
}
func (l *Level) LoadTileMapCells(parsedRunes [][]rune) [][]*TermBoxCell {
var cells [][]*TermBoxCell
for i, line := range parsedRunes {
rowCells := make([]*TermBoxCell, len(line))
var data TileMapCellData
for j, char := range line {
if _, ok := l.TileData[char]; !ok {
if _, ok := CommonTileMapCellData[char]; !ok {
data = NewTileMapCell(char, func() {}, i)
} else {
data = CommonTileMapCellData[char]
data.LineNumber = i
}
} else {
data = l.TileData[char]
}
if reflect.DeepEqual(data, TileMapCellData{}) {
data = CommonTileMapCellData[char]
}
cell := &TermBoxCell{
&termbox.Cell{data.Ch, data.FgColor, data.BgColor},
data.CollidesPhysically,
data,
}
rowCells[j] = cell
}
cells = append(cells, rowCells)
}
l.TileMap = cells
return l.TileMap
}
func (l *Level) LoadTileMap() {
parsed := ParseTileMapString(l.TileMapString)
l.LoadTileMapCells(parsed)
}
// row, length
func (l *Level) GetTileMapDimensions() (int, int) {
parsed := ParseTileMapString(l.TileMapString)
rowLength := len(parsed[0])
columnLength := len(parsed)
return rowLength, columnLength
}
func (l *Level) InitDefaults() {
// set default quit functions
exitTerms := []string{"q", "quit", "exit"}
if l.ColonLineCallbacks == nil {
l.ColonLineCallbacks = make(map[string]func(*Game))
}
for _, term := range exitTerms {
if _, ok := l.ColonLineCallbacks[term]; !ok {
l.ColonLineCallbacks[term] = func(g *Game) {
go func() {
g.VimManEvents <- VimManEvent{Content: "exit"}
}()
}
}
}
}