/
main.go
480 lines (403 loc) · 12.1 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package main
import (
"fmt"
"github.com/nsf/termbox-go"
"math"
"math/rand"
"os"
"strconv"
"time"
"github.com/kirbywarp/rogue/engine"
"github.com/kirbywarp/rogue/base"
)
/*
Draw draws a bit of tileart to the given x,y coordinates on the terminal.
*/
func Draw(x, y int, symbol rune, fg, bg base.Color) {
// Convert colors into termbox attributes.
// To get a nice scale, round each channel.
fga := termbox.Attribute(uint16(fg.R*5+.5)*36 + uint16(fg.G*5+.5)*6 + uint16(fg.B*5+.5) + 1)
bga := termbox.Attribute(uint16(bg.R*5+.5)*36 + uint16(bg.G*5+.5)*6 + uint16(bg.B*5+.5) + 1)
termbox.SetCell(x, y, symbol, fga, bga)
}
/*
DrawString prints text to the termbox on row y starting at column x
*/
func DrawString(x, y int, text string, fg, bg base.Color) {
for _, char := range text {
Draw(x, y, char, fg, bg)
x++
}
}
/*
DrawPaddedString prints text to termbox with size cells filled up. Any remaining cells not covered by text
will be drawn empty. If text is longer than size, it will be truncated.
*/
func DrawPaddedString(x, y int, text string, fg, bg base.Color, size int) {
var i int
var char rune
for _, char = range text {
if i >= size {
break
}
Draw(x+i, y, char, fg, bg)
i++
}
for i < size {
Draw(x+i, y, '.', bg, bg)
i++
}
}
/*
DrawTextBox creates a textbox of length size at coord x, y on the termbox and blocks while the user
inputs a string. It stops blocking when the enter key is pressed
*/
func DrawTextBox(x, y int, fg, bg base.Color, size int) string {
//Draw the box
x1 := x
for i := 0; i < size+2; i++ {
Draw(x1, y, '-', fg, bg)
Draw(x1, y-2, '-', fg, bg)
x1++
}
Draw(x, y-1, '|', fg, bg)
Draw(x+size+1, y-1, '|', fg, bg)
termbox.Flush()
//check input
buffer := make([]rune, 0)
done := false
for !done {
event := termbox.PollEvent()
switch event.Ch {
default:
buffer = append(buffer, event.Ch)
DrawPaddedString(x+1, y-1, string(buffer), fg, bg, size)
termbox.Flush()
case 0:
switch event.Key {
case termbox.KeyEnter : done = true
case termbox.KeyCtrlQ:
termbox.Close()
os.Exit(0)
case termbox.KeyBackspace :
if len(buffer) > 0{
buffer = buffer[:len(buffer)-1]
DrawPaddedString(x+1, y-1, string(buffer), fg, bg, size)
termbox.Flush()
}
}
}
}
DrawPaddedString(x+1, y-1, "", fg, bg, size)
return string(buffer)
}
/*
MAP GENERATION
*/
type StoneFieldGenerator struct {
stone, grass engine.Entity
fill float64
}
func NewStoneFieldGenerator(db *engine.EntityDB, fill float64) *StoneFieldGenerator {
grass := db.New(); db.Set(grass, "art", base.NewArt('.', 0, 1, 0, 0, 0, 0))
stone := db.New(); db.Set(stone, "art", base.NewArt('#', .7, .7 , .7, 0, 0, 0))
return &StoneFieldGenerator{stone: stone, grass: grass, fill: fill}
}
func (g *StoneFieldGenerator) GenerateChunk(emap *base.EntityMap, x, y, z int64) {
chunk := emap.CreateChunk(x, y, z)
for x := int64(0); x < 16; x++ {
for y := int64(0); y < 16; y++ {
if rand.Float64() < g.fill {
chunk.Set(x, y, 0, g.stone)
} else {
chunk.Set(x, y, 0, g.grass)
}
}
}
}
/*
CreateMap creates a new map region entity and returns it
*/
func CreateMap(db *engine.EntityDB) engine.Entity {
retval := db.New("map")
// Register a chunk generator on the map
emap := db.Get(retval, "map").(*base.EntityMap)
emap.RegisterChunkGenerator(NewStoneFieldGenerator(db, .05))
return retval
}
/*
RenderMapAt draws a portion of the map centered at the given entity
*/
func RenderMapAt(db *engine.EntityDB, eid engine.Entity) {
width, height := termbox.Size()
pos := db.Get(eid, "position").(*base.Position)
emap := db.Get(pos.R, "map").(*base.EntityMap)
for y := 0; y < height; y++ {
py := pos.Y+int64(y-height/2)
for x := 0; x < width; x++ {
px := pos.X+int64(x-width/2)
// Search for the highest entity on the map in the same general layer
// as the passed entity that can be drawn. This will need to be more
// formal in the future (not hardcoded knowing the player is on z level
// 1, tiles are on z level 0, and the bat at z level 2)
topArt := &base.Art{}
for i := int64(1); i >= -1; i-- {
entity := emap.Get(px, py, pos.Z+i)
if db.Has(entity, "art") {
topArt = db.Get(entity, "art").(*base.Art)
break
}
}
// And draw the found art, which will be an empty black square
// if nothing was found
Draw(x, height-1-y, topArt.Symbol, topArt.Fg, topArt.Bg)
}
}
termbox.Flush()
}
/*
FollowAI makes an entity doggedly follow a target
*/
type FollowAI struct {
Target engine.Entity
}
func NewFollowAI(target engine.Entity) *FollowAI {
return &FollowAI{Target: target}
}
func (ai *FollowAI) Clone() base.AIController {
return &FollowAI{Target: ai.Target}
}
func (ai *FollowAI) Act(db *engine.EntityDB, eid engine.Entity) {
epos := db.Get(eid, "position").(*base.Position)
tpos := db.Get(ai.Target, "position").(*base.Position)
dx, dy := int64(0), int64(0)
if epos.X > tpos.X+1 || epos.X < tpos.X-1 {
dx = int64(math.Copysign(1, float64(tpos.X-epos.X)))
}
if epos.Y > tpos.Y+1 || epos.Y < tpos.Y-1 {
dy = int64(math.Copysign(1, float64(tpos.Y-epos.Y)))
}
// Since the bat is situated at z-level 2, it will never find
// a wall with art symbol '#' at one level below, so this move
// function still allows the bat to fly over stones! ^_^
// Hence the "+1" for the z; so the bat stays one level above
// the player.
base.HelperMove(db, eid, dx, dy, tpos.Z-epos.Z+1)
}
var (
done = false
)
/*
PlayerAI makes an entity respond to player controls.
*/
type PlayerAI struct {}
func NewPlayerAI() *PlayerAI {
return &PlayerAI{}
}
func (ai *PlayerAI) Clone() base.AIController {
return &PlayerAI{}
}
func (ai *PlayerAI) Act(db *engine.EntityDB, eid engine.Entity) {
// RENDERING
RenderMapAt(db, eid)
// INPUT HANDLING
event := termbox.PollEvent()
var dx, dy, dz int64
switch event.Ch {
case 'h': dx = -1
case 'j': dy = -1
case 'k': dy = 1
case 'l': dx = 1
case 'y': dx = -1; dy = 1
case 'u': dx = 1; dy = 1
case 'b': dx = -1; dy = -1
case 'n': dx = 1; dy = -1
case '>': dz = -4
case '<': dz = 4
case 0:
switch event.Key {
case termbox.KeyCtrlQ:
done = true
}
}
base.HelperMove(db, eid, dx, dy, dz)
}
/************************
** State Machine Code **
************************/
/*
States are UI states that respond to time ticks and input, and can
transition to other states in various ways.
*/
type State interface {
Enter(*UI) // Called when this state is entered from a different state
Exit(*UI) // Called when the machine transfers away from this state
Update(*UI, float64) // Called every frame of animation
}
/*
UI holds state information and provides convenience methods for
things like dialogue boxes and input areas.
*/
type UI struct {
States map[string]State
StateStack []string
Properties map[string]interface{}
}
func NewUI() *UI {
return &UI{States: make(map[string]State), StateStack: make([]string, 0), Properties: make(map[string]interface{})}
}
func (ui *UI) RegisterState(name string, state State) {
ui.States[name] = state
}
func (ui *UI) Transition(name string) {
l := len(ui.StateStack)
if l > 0 {
ui.StateStack[l-1] = name
}
}
func (ui *UI) Push(name string) {
ui.StateStack = append(ui.StateStack, name)
}
func (ui *UI) Pop() {
l := len(ui.StateStack)
if l > 0 {
ui.StateStack = ui.StateStack[:l-1]
}
}
func (ui *UI) Peek() string {
l := len(ui.StateStack)
if l > 0 {
return ui.StateStack[l-1]
}
return ""
}
func (ui *UI) Run() {
name := ui.Peek()
state := ui.States[name]
state.Enter(ui)
for {
state.Update(ui, 1)
next := ui.Peek()
if next == "" {
return
} else if next != name {
state.Exit(ui)
name = next
state = ui.States[name]
state.Enter(ui)
}
}
}
/************************
* State implementation *
************************/
/*
TitleState
*/
type TitleState struct {}
func NewTitleState() *TitleState {
return &TitleState{}
}
func (title *TitleState) Enter(ui *UI) {}
func (title *TitleState) Exit(ui *UI) {}
func (title *TitleState) Update(ui *UI, dt float64) {
width, height := termbox.Size()
text := "Press any key to play. Press 'y' to face an bat at your own risk!"
DrawString(width/8, height/4, text, base.RGB(0, 0, 1), base.RGB(0, 0, 0))
termbox.Flush()
event := termbox.PollEvent()
for event.Type != termbox.EventKey {
event = termbox.PollEvent()
}
switch event.Ch {
case 'y':
ui.Transition("batmenu")
return
case 0:
switch event.Key {
case termbox.KeyCtrlQ:
ui.Pop()
return
}
}
// Create a game without bats
ui.RegisterState("game", NewGameState(0))
ui.Transition("game")
}
/*
BatMenuState
*/
type BatMenuState struct {}
func NewBatMenuState() *BatMenuState {
return &BatMenuState{}
}
func (menu *BatMenuState) Enter(ui *UI) {}
func (menu *BatMenuState) Exit(ui *UI) {}
func (menu *BatMenuState) Update(ui *UI, dt float64) {
width, height := termbox.Size()
label := "How many bats?"
tryAgain := "Please enter an integer"
DrawString(width/2, height/2-4, label, base.RGB(0, 0, 1), base.RGB(0, 0, 0))
numBatstr := DrawTextBox(width/2, height/2, base.RGB(0, 0, 1), base.RGB(0, 0, 0), 4)
numBats, err := strconv.ParseInt(numBatstr, 10, 64)
for err != nil {
DrawString(width/2, height/2-3, tryAgain, base.RGB(0, 0, 1), base.RGB(0, 0, 0))
numBatstr := DrawTextBox(width/2, height/2, base.RGB(0, 0, 1), base.RGB(0, 0, 0), 4)
numBats, err = strconv.ParseInt(numBatstr, 10, 64)
}
ui.RegisterState("game", NewGameState(numBats))
ui.Transition("game")
}
/*
GameState
*/
type GameState struct {
DB *engine.EntityDB
}
func NewGameState(numbats int64) *GameState {
// Game Data Initialization
db := engine.NewEntityDB()
base.RegisterTypes(db)
tilemap := CreateMap(db)
player := db.New("movement")
db.Set(player, "ai", base.NewAI(NewPlayerAI()))
db.Set(player, "art", base.NewArt('@', 1, 0, 0, 0, 0, 0))
base.HelperPlace(db, player, tilemap, 0, 0, 1)
bat := db.New("movement")
db.Set(bat, "ai", base.NewAI(NewFollowAI(player)))
db.Set(bat, "art", base.NewArt('b', 0, 0, 1, 0, 0, 0))
// Create bats from the template entity
for i := int64(0); i < numbats; i++ {
newBat := db.Instance(bat)
base.HelperPlace(db, newBat, tilemap, rand.Int63n(numbats)-numbats/2, rand.Int63n(numbats)-numbats/2, 2)
}
return &GameState{DB: db}
}
func (game *GameState) Enter(ui *UI) {}
func (game *GameState) Exit(ui *UI) {}
func (game *GameState) Update(ui *UI, dt float64) {
base.SystemAct(game.DB)
base.SystemMove(game.DB)
if done {
ui.Pop()
}
}
func main() {
// Seed the random number generator!
rand.Seed(time.Now().UTC().UnixNano())
// GUI and input initialization
err := termbox.Init()
if err != nil {
fmt.Println(err)
return
}
defer termbox.Close()
termbox.SetOutputMode(termbox.Output216)
termbox.Clear(0, 0)
// Initial states
ui := NewUI()
ui.RegisterState("title", NewTitleState())
ui.RegisterState("batmenu", NewBatMenuState())
ui.Push("title")
ui.Run()
}