-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbearrogue.go
628 lines (542 loc) · 19.7 KB
/
bearrogue.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package main
import (
blt "bearlibterminal"
"bearrogue/camera"
"bearrogue/ecs"
"bearrogue/examinecursor"
"bearrogue/fov"
"bearrogue/gamemap"
"bearrogue/ui"
"fmt"
"math/rand"
"strconv"
)
const (
WindowSizeX = 100
WindowSizeY = 35
ViewAreaX = 75
ViewAreaY = 30
MapWidth = 100
MapHeight = 100
Title = "BearRogue"
Font = "fonts/UbuntuMono.ttf"
FontSize = 24
PlayerTurn = iota
MobTurn = iota
MapLayer = 0
ActorLayer = 2
ItemLayer = 3
ExamineLayer = 4
)
var (
version string
buildStamp string
gitHash string
player *ecs.GameEntity
entities []*ecs.GameEntity
gameMap *gamemap.Map
gameCamera *camera.GameCamera
fieldOfView *fov.FieldOfVision
gameTurn int
messageLog ui.MessageLog
examining bool
examineCursor *examinecursor.XCursor
inMenu bool
inInventory bool
informationScreen bool
dropping bool
inventoryKeys map[int]bool
)
func init() {
blt.Open()
fmt.Printf("BearRogue -- Version %s\n", version)
fmt.Printf("Build Stamp: %s\n", buildStamp)
fmt.Printf("Git Hash: %s\n", gitHash)
// BearLibTerminal uses configuration strings to set itself up, so we need to build these strings here
// First set up the string for window properties (size and title)
size := "size=" + strconv.Itoa(WindowSizeX) + "x" + strconv.Itoa(WindowSizeY)
title := "title='" + Title + "'"
window := "window: " + size + "," + title
// Next, setup the font config string
fontSize := "size=" + strconv.Itoa(FontSize)
font := "font: " + Font + ", " + fontSize
// Now, put it all together
blt.Set(window + "; " + font)
blt.Clear()
// Create a player Entity, and add them to our slice of Entities
player = &ecs.GameEntity{}
player.SetupGameEntity()
player.AddComponent("player", ecs.PlayerComponent{})
player.AddComponent("position", ecs.PositionComponent{X: 0, Y: 0})
player.AddComponent("appearance", ecs.AppearanceComponent{Color: "white", Character: "@", Layer: 1, Name: "Player"})
player.AddComponent("movement", ecs.MovementComponent{})
player.AddComponent("controllable", ecs.ControllableComponent{})
player.AddComponent("attacker", ecs.AttackerComponent{Attack: 5, Defense: 5})
player.AddComponent("hitpoints", ecs.HitPointComponent{Hp: 20, MaxHP: 20})
player.AddComponent("block", ecs.BlockingComponent{})
player.AddComponent("killable", ecs.KillableComponent{Name: "Here lies", Character: "%", Color: "dark red"})
player.AddComponent("inventory", ecs.InventoryComponent{Capacity: 32})
entities = append(entities, player)
// Create a GameMap, and initialize it (and set the player position within it, for now)
gameMap = &gamemap.Map{Width: MapWidth, Height: MapHeight}
gameMap.InitializeMap()
playerX, playerY, mapEntities := GenerateAndPopulateCavern()
if player.HasComponent("position") {
positionComponent, _ := player.Components["position"].(ecs.PositionComponent)
positionComponent.X = playerX
positionComponent.Y = playerY
player.RemoveComponent("position")
player.AddComponent("position", positionComponent)
}
entities = append(entities, mapEntities...)
// Set the current turn to the player, so they may act first
gameTurn = PlayerTurn
// Initialize a camera object
gameCamera = &camera.GameCamera{X: 1, Y: 1, Width: ViewAreaX, Height: ViewAreaY}
// Initialize a FoV object
fieldOfView = &fov.FieldOfVision{}
fieldOfView.Initialize()
fieldOfView.SetTorchRadius(6)
// Set up the messageLog, and output a "welcome" message
messageLog = ui.MessageLog{MaxLength: 100}
messageLog.InitMessages()
// Set the default examining state to false, which means movement is normal. If this is true, only the examinecursor will
// be moved, until the player cancels examine mode
examining = false
// Set the default menu state to false. This will be true when a menu is displayed, which will indicate that control
// needs to be given to the menu, instead of the main game
inMenu = false
inInventory = false
informationScreen = false
dropping = false
inventoryKeys = map[int]bool{blt.TK_A: false,
blt.TK_B: false,
blt.TK_C: false,
blt.TK_D: false,
blt.TK_E: false,
blt.TK_F: false,
blt.TK_G: false,
blt.TK_H: false,
blt.TK_I: false,
blt.TK_J: false,
blt.TK_K: false,
blt.TK_L: false,
blt.TK_M: false,
blt.TK_N: false,
blt.TK_O: false,
blt.TK_P: false,
blt.TK_Q: false,
blt.TK_R: false,
blt.TK_S: false,
blt.TK_T: false,
blt.TK_U: false,
blt.TK_V: false,
blt.TK_W: false,
blt.TK_X: false,
blt.TK_Y: false,
blt.TK_Z: false,
}
}
func main() {
// Main game loop
renderSideBar()
messageLog.SendMessage("You find yourself in the caverns of eternal sadness...you start to feel a little more sad.")
renderMap()
ecs.SystemRender(entities, gameCamera, gameMap)
messageLog.PrintMessages(ViewAreaY, WindowSizeX, WindowSizeY)
for {
blt.Refresh()
key := blt.Read()
if key != blt.TK_CLOSE {
if gameTurn == PlayerTurn {
if player.HasComponents([]string{"movement", "controllable", "position"}) {
handleInput(key, player)
}
}
} else {
break
}
if !inMenu {
// Clear each Entity off the screen
ecs.SystemClear(entities, gameCamera)
if gameTurn == MobTurn {
var newEntities []*ecs.GameEntity
for _, e := range entities {
if e != nil {
if !e.HasComponent("player") {
ecs.SystemMovement(e, 0, 0, entities, gameMap, &messageLog)
newEntities = append(newEntities, ecs.SystemReproduce(e, entities, gameMap, &messageLog))
}
}
}
entities = append(entities, newEntities...)
gameTurn = PlayerTurn
}
renderMap()
ecs.SystemRender(entities, gameCamera, gameMap)
if examining {
examineCursor.Draw(gameCamera)
} else {
// Only print messages if the player is not examining
messageLog.PrintMessages(ViewAreaY, WindowSizeX, WindowSizeY)
}
renderSideBar()
} else {
// Render the menu or information screen
if inInventory && !informationScreen {
renderInventory("Inventory")
}
if dropping {
renderDroppingScreen()
}
}
}
blt.Close()
}
func handleInput(key int, entity *ecs.GameEntity) {
// Handle basic character movement in the four main directions, plus diagonals (and vim keys)
var (
dx, dy int
)
actionTaken := true
if !inMenu {
switch key {
case blt.TK_RIGHT, blt.TK_L:
dx, dy = 1, 0
case blt.TK_LEFT, blt.TK_H:
dx, dy = -1, 0
case blt.TK_UP, blt.TK_K:
dx, dy = 0, -1
case blt.TK_DOWN, blt.TK_J:
dx, dy = 0, 1
case blt.TK_Y:
dx, dy = -1, -1
case blt.TK_U:
dx, dy = 1, -1
case blt.TK_B:
dx, dy = -1, 1
case blt.TK_N:
dx, dy = 1, 1
case blt.TK_X:
// Look command - toggle the examining flag, and notify that this will not consume an action
actionTaken = false
examining = !examining
if player.HasComponent("position") && examining {
pos, _ := player.Components["position"].(ecs.PositionComponent)
examineCursor = &examinecursor.XCursor{X: pos.X, Y: pos.Y, Character: "_", Layer: ExamineLayer}
} else {
examineCursor.Clear(gameCamera)
}
case blt.TK_COMMA:
inventoryKeys = ecs.SystemPickupItem(player, entities, gameCamera, &messageLog, inventoryKeys)
case blt.TK_I:
inMenu = true
inInventory = true
case blt.TK_D:
inMenu = true
dropping = true
case blt.TK_ESCAPE:
// Cancel the current action and return the game state to normal
actionTaken = false
examining = false
inMenu = false
inInventory = false
informationScreen = false
dropping = false
if examineCursor != nil {
examineCursor.Clear(gameCamera)
}
ui.ClearScreen(WindowSizeX, WindowSizeX)
}
} else {
selectedEntity := ecs.FindItemWithKey(player, key)
// If we are in a menu, bound controls will be different than standard.
switch key {
case blt.TK_ESCAPE:
// Cancel the current action and return the game state to normal
actionTaken = false
examining = false
if informationScreen {
informationScreen = false
} else if inInventory {
inMenu = false
inInventory = false
} else if dropping {
inMenu = false
dropping = false
}
if examineCursor != nil {
examineCursor.Clear(gameCamera)
}
ui.ClearScreen(WindowSizeX, WindowSizeX)
}
if selectedEntity != nil {
if inInventory {
informationScreen = true
renderInformationScreen(selectedEntity)
} else if dropping {
ecs.SystemDropItem(player, selectedEntity, entities, &messageLog, inventoryKeys)
inMenu = false
dropping = false
ui.ClearScreen(WindowSizeX, WindowSizeX)
}
}
}
if examining {
// Fire off examinecursor movement
examine(dx, dy)
} else {
// Fire off the movement system
ecs.SystemMovement(entity, dx, dy, entities, gameMap, &messageLog)
}
// Switch the game turn to the Mobs turn, if an action was taken. Some commands, like examine, or checking inventory
// do not cost an action
if actionTaken && !examining && !inMenu {
gameTurn = MobTurn
}
}
func renderMap() {
// Render the game map. If a tile is blocked and blocks sight, draw a '#', if it is not blocked, and does not block
// sight, draw a '.'
// First, set the every portion of the map seen by the camera to not visible. We'll decide what is visible based on
// the torch radius. In the process, clear every camera visible Tile on the map as well
for x := 0; x < MapWidth; x++ {
for y := 0; y < MapHeight; y++ {
gameMap.Tiles[x][y].Visible = false
}
}
for x := 0; x < gameCamera.Width; x++ {
for y := 0; y < gameCamera.Height; y++ {
// Clear both our primary layers, so we don't get any strange artifacts from one layer or the other getting
// cleared.
for i := 0; i <= 2; i++ {
blt.Layer(i)
blt.Print(x, y, " ")
}
}
}
positionComponent, posOk := player.Components["position"].(ecs.PositionComponent)
if posOk {
gameCamera.MoveCamera(positionComponent.X, positionComponent.Y, MapWidth, MapHeight)
// Next figure out what is visible to the player, and what is not.
fieldOfView.RayCast(positionComponent.X, positionComponent.Y, gameMap)
}
// Now draw each tile that should appear on the screen, if its visible, or explored
blt.Layer(MapLayer)
for x := 0; x < gameCamera.Width; x++ {
for y := 0; y < gameCamera.Height; y++ {
mapX, mapY := gameCamera.X+x, gameCamera.Y+y
if gameMap.Tiles[mapX][mapY].Visible {
if gameMap.Tiles[mapX][mapY].IsWall() {
blt.Color(blt.ColorFromName("white"))
blt.Print(x, y, "#")
} else {
blt.Color(blt.ColorFromName("white"))
blt.Print(x, y, ".")
}
} else if gameMap.Tiles[mapX][mapY].Explored {
if gameMap.Tiles[mapX][mapY].IsWall() {
blt.Color(blt.ColorFromName("gray"))
blt.Print(x, y, "#")
} else {
blt.Color(blt.ColorFromName("gray"))
blt.Print(x, y, ".")
}
}
}
}
}
func renderSideBar() {
blt.Layer(0)
blt.ClearArea(ViewAreaX, 0, WindowSizeX, WindowSizeY)
if player.HasComponents([]string{"appearance", "hitpoints"}) {
playerAppearance, _ := player.Components["appearance"].(ecs.AppearanceComponent)
playerHp, _ := player.Components["hitpoints"].(ecs.HitPointComponent)
ui.PrintBasicCharacterInfo(playerAppearance.Name, ViewAreaX)
ui.PrintStats(playerHp.Hp, playerHp.MaxHP, ViewAreaX)
}
}
func renderInventory(title string) {
ui.ClearScreen(WindowSizeX, WindowSizeX)
if player.HasComponent("inventory") {
inv, _ := player.Components["inventory"].(ecs.InventoryComponent)
items := map[string]int{}
// Get the counts of each type of item in the players inventory. If there is more than one of a type, and they
// are stackable, group them together, to make the inventory more manageable
// TODO: Refactor this to use the util function counting occurences of items.
for i := 0; i < len(inv.Items); i++ {
if inv.Items[i].HasComponents([]string{"appearance", "lootable"}) {
app, _ := inv.Items[i].Components["appearance"].(ecs.AppearanceComponent)
lootable, _ := inv.Items[i].Components["lootable"].(ecs.LootableComponent)
key := string(ui.MapBltKeyCodesToRunes(lootable.Key))
name := key + " - " + "[color=" + app.Color + "]" + app.Name + "[/color]"
items[name]++
}
}
ui.DisplayInventory(title, inv.Capacity, len(inv.Items), items)
}
}
func renderInformationScreen(item *ecs.GameEntity) {
ui.ClearScreen(WindowSizeX, WindowSizeX)
if item.HasComponents([]string{"lootable", "appearance", "description"}) {
app, _ := item.Components["appearance"].(ecs.AppearanceComponent)
lootable, _ := item.Components["lootable"].(ecs.LootableComponent)
desc, _ := item.Components["description"].(ecs.DescriptionComponent)
key := string(ui.MapBltKeyCodesToRunes(lootable.Key))
title := key + " - [color=" + app.Color + "]" + app.Name + "[/color]"
occurences := ecs.CountItemInstances(player, item)
ui.DisplayInformationScreen(title, desc.ShortDesc, desc.LongDesc, occurences, WindowSizeY)
}
}
func renderDroppingScreen() {
renderInventory("Drop which Item?")
}
func examine(dx, dy int) {
// Examine command - Creates a new cursor, that moves independently of the player, takes no actions, and will list
// out any entities present at the location it is position on.
examineCursor.Clear(gameCamera)
examineCursor.Move(dx, dy, MapWidth, MapHeight, gameCamera)
examineCursor.Draw(gameCamera)
if gameMap.IsVisibleAndExplored(examineCursor.X, examineCursor.Y) {
presentEntities := ecs.GetEntityNamesPresentAtLocation(entities, examineCursor.X, examineCursor.Y)
if presentEntities != "" {
ui.PrintToMessageArea(presentEntities, ViewAreaY, WindowSizeX, WindowSizeY, examineCursor.Layer)
} else {
tile := gameMap.Tiles[examineCursor.X][examineCursor.Y]
if tile.IsWall() {
ui.PrintToMessageArea("A cavern wall, made of some kind of rock", ViewAreaY, WindowSizeX, WindowSizeY, examineCursor.Layer)
} else {
ui.PrintToMessageArea("A cavern floor, covered in dirt and stones", ViewAreaY, WindowSizeX, WindowSizeY, examineCursor.Layer)
}
}
} else {
ui.PrintToMessageArea("You cannot see here...", ViewAreaY, WindowSizeX, WindowSizeY, examineCursor.Layer)
}
}
/* Generator functions */
func GenerateAndPopulateCavern() (int, int, []*ecs.GameEntity) {
gameMap := gameMap.GenerateCavern()
pos := rand.Int() % len(gameMap)
playerX, playerY := gameMap[pos].X, gameMap[pos].Y
entities := populateCavern(gameMap)
return playerX, playerY, entities
}
func populateCavern(mainCave []*gamemap.Tile) []*ecs.GameEntity {
// Randomly sprinkle some Orcs, Trolls, and Goblins around the newly created cavern
var entities []*ecs.GameEntity
var createdEntity *ecs.GameEntity
for i := 0; i < 10; i++ {
x := 0
y := 0
locationFound := false
for j := 0; j <= 50; j++ {
// Attempt to find a clear location to create a mob (ecs for now)
pos := rand.Int() % len(mainCave)
x = mainCave[pos].X
y = mainCave[pos].Y
if ecs.GetBlockingEntitiesAtLocation(entities, x, y) == nil {
locationFound = true
break
}
}
if locationFound {
chance := rand.Intn(100)
if chance <= 5 {
// Create a Troll
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ActorLayer, Character: "T", Color: "dark green", Name: "Troll"},
"hitpoints": ecs.HitPointComponent{Hp: 20, MaxHP: 20},
"block": ecs.BlockingComponent{},
"movement": ecs.MovementComponent{},
"basic_melee_ai": ecs.BasicMeleeAIComponent{},
"attacker": ecs.AttackerComponent{Attack: 10, Defense: 7},
"killable": ecs.KillableComponent{Name: "Remains of", Color: "dark red", Character: "%"}})
} else if chance > 5 && chance <= 20 {
// Create an Orc
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ActorLayer, Character: "o", Color: "darker green", Name: "Orc"},
"hitpoints": ecs.HitPointComponent{Hp: 15, MaxHP: 15},
"block": ecs.BlockingComponent{},
"movement": ecs.MovementComponent{},
"basic_melee_ai": ecs.BasicMeleeAIComponent{},
"attacker": ecs.AttackerComponent{Attack: 7, Defense: 5},
"killable": ecs.KillableComponent{Name: "Remains of", Color: "dark red", Character: "%"}})
} else if chance > 20 && chance <= 70 {
// Create a Goblin
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ActorLayer, Character: "g", Color: "green", Name: "Goblin"},
"hitpoints": ecs.HitPointComponent{Hp: 5, MaxHP: 5},
"block": ecs.BlockingComponent{},
"movement": ecs.MovementComponent{},
"basic_melee_ai": ecs.BasicMeleeAIComponent{},
"attacker": ecs.AttackerComponent{Attack: 2, Defense: 2},
"killable": ecs.KillableComponent{Name: "Remains of", Color: "dark red", Character: "%"}})
} else if chance > 70 {
// Create a reproducing Fungus
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ActorLayer, Character: "f", Color: "yellow", Name: "Fungus"},
"hitpoints": ecs.HitPointComponent{Hp: 5, MaxHP: 5},
"block": ecs.BlockingComponent{},
"reproducer": ecs.ReproducesComponent{MaxTimes: 8, TimesRemaining: 8, PercentChance: 25},
"killable": ecs.KillableComponent{Name: "Remains of", Color: "yellow", Character: "."}})
}
entities = append(entities, createdEntity)
} else {
// No location was found after 50 tries, which means the map is quite full. Stop here and return.
break
}
}
// Next, populate some items (LootableComponent) in the dungeon in the same way. For now, just health potions will
// be created.
for i := 0; i < 15; i++ {
x := 0
y := 0
locationFound := false
for j := 0; j <= 50; j++ {
// Attempt to find a clear location to create a mob (ecs for now)
pos := rand.Int() % len(mainCave)
x = mainCave[pos].X
y = mainCave[pos].Y
if ecs.GetBlockingEntitiesAtLocation(entities, x, y) == nil {
locationFound = true
break
}
}
if locationFound {
chance := rand.Intn(100)
if chance >= 49 {
// Create a healing potion
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ItemLayer, Character: "!", Color: "dark red", Name: "Dark Red Potion"},
"lootable": ecs.LootableComponent{InInventory: false, ID: 1},
"stackable": ecs.StackableComponent{},
"description": ecs.DescriptionComponent{ShortDesc: "An unmarked, single dose, vial of a dark red liquid."}})
entities = append(entities, createdEntity)
} else {
// Create a healing potion
createdEntity = &ecs.GameEntity{}
createdEntity.SetupGameEntity()
createdEntity.AddComponents(map[string]ecs.Component{"position": ecs.PositionComponent{X: x, Y: y},
"appearance": ecs.AppearanceComponent{Layer: ItemLayer, Character: "!", Color: "light green", Name: "Bright Green Potion"},
"lootable": ecs.LootableComponent{InInventory: false, ID: 2},
"stackable": ecs.StackableComponent{},
"description": ecs.DescriptionComponent{ShortDesc: "An unmarked, single dose, vial of a bright green liquid."}})
entities = append(entities, createdEntity)
}
} else {
// No location was found after 50 tries, which means the map is quite full. Stop here and return.
break
}
}
return entities
}