Documentation ¶
Index ¶
- Constants
- Variables
- func NewTextbox(con *console.Console, width, height int) *textBox
- type Action
- type Connection
- type Entity
- type EntityType
- type FOV
- type Game
- type GenWorld
- type Inventory
- type Item
- type ItemSet
- type ItemType
- type Node
- type Object
- type ObjectType
- type Pathfinder
- type Placer
- type Rarity
- type Room
- type RoomConfig
- type Scene
- type SceneCharacterCreation
- func (g *SceneCharacterCreation) Close() error
- func (s *SceneCharacterCreation) Draw(con *console.Console, timeElapsed float64)
- func (s *SceneCharacterCreation) FocusOnClick() bool
- func (s *SceneCharacterCreation) NewTextbox(con *console.Console)
- func (s *SceneCharacterCreation) Update(con *console.Console, timeElapsed float64) bool
- type SceneDeath
- type SceneMap
- type SceneSuccess
- type UIif
- type ViewMode
- type World
- func (w *World) AddRoomColumns(room *Room)
- func (w *World) AddRoomFountain(room *Room)
- func (w *World) AddRoomFurnishings(room *Room)
- func (w *World) AddRoomPuddle(room *Room)
- func (w *World) CanMoveTo(x, y int) bool
- func (w *World) CarveRoom(room *Room)
- func (w *World) Fill(c rune)
- func (w *World) InBounds(x, y int) bool
- func (w *World) IsEmpty(x int, y int) bool
- func (w *World) IsSolid(x int, y int) bool
- func (w *World) NextToWall(x, y int) bool
Constants ¶
const ( ItemWeapon = iota ItemPotion ItemArmor ItemHelmet ItemTrigger ItemTypeContainer ItemTypeDocument ItemTypeDecorative ItemTypeMax )
const ( DirNorth = 0 DirEast = 1 DirSouth = 2 DirWest = 3 )
Cardinal directions.
const ( CharWall = '#' CharWater = '~' CharFloor = ' ' CharTree = 'T' CharShelf = 'S' CharColumn = 'o' )
Variables ¶
var ( EntityPlayer = &EntityType{ Tile: '@', Name: "player", BaseHealth: 10, BaseAttack: 2, BaseDefense: 10, Equipment: []*ItemType{ ItemTypeWeaponFishingRod, ItemTypeArmorPlate, ItemTypeHelmetSweatband, ItemTypePotion, ItemTypeNote, }, } EntityGoblin = &EntityType{ Tile: 'g', Name: "goblin", Description: "A small goblin.", BaseHealth: 5, BaseAttack: 1, BaseDefense: 5, Equipment: []*ItemType{ItemTypeWeaponAxe, ItemTypeArmorChain}, OptionalEquipment: []*ItemType{ ItemTypePotion, ItemTypeGoblinToe, }, } EntityOrc = &EntityType{ Tile: 'o', Name: "orc", Description: "A big orc.", BaseHealth: 10, BaseAttack: 5, BaseDefense: 14, Equipment: []*ItemType{ItemTypeWeaponSword, ItemTypeArmorLeather}, OptionalEquipment: []*ItemType{ ItemTypePotion, }, } EntityTroll = &EntityType{ Tile: 't', Name: "troll", Description: "A huge troll.", BaseHealth: 15, BaseAttack: 7, BaseDefense: 15, Equipment: []*ItemType{ItemTypeTrollPoop}, } )
var ( RarityAbundant = &Rarity{ Name: "abundant", Probability: 25, IndicateRarity: false, } RarityCommon = &Rarity{ Name: "common", Probability: 45, IndicateRarity: false, } RarityAverage = &Rarity{ Name: "average", Probability: 65, IndicateRarity: false, } RarityUncommon = &Rarity{ Name: "uncommon", Probability: 80, IndicateRarity: true, } RarityRare = &Rarity{ Name: "rare", Probability: 93, IndicateRarity: true, } RarityExotic = &Rarity{ Name: "exotic", Probability: 99, IndicateRarity: true, } RarityLegendary = &Rarity{ Name: "legendary", Probability: 100, IndicateRarity: true, } )
var ( ItemSetWeapons = &ItemSet{ Name: "weapons", Items: []*ItemType{ ItemTypeWeaponFishingRod, ItemTypeWeaponSword, ItemTypeWeaponAxe, }, } ItemSetArmor = &ItemSet{ Name: "armor", Items: []*ItemType{ ItemTypeArmorLeather, ItemTypeArmorChain, ItemTypeArmorPlate, ItemTypeHelmetSweatband, }, } ItemSetPotions = &ItemSet{ Name: "potions", Items: []*ItemType{ ItemTypePotion, ItemTypeTrollPoop, ItemTypeGoblinToe, }, } ItemSetChestLoot = &ItemSet{ Name: "chest loot", Items: []*ItemType{ ItemTypeWeaponFishingRod, ItemTypeWeaponSword, ItemTypeWeaponAxe, ItemTypePotion, ItemTypeArmorLeather, ItemTypeArmorChain, ItemTypeArmorPlate, ItemTypeHelmetSweatband, ItemTypeExit, ItemTypeTrap, ItemTypeNote, }, } ItemSetBookLoot = &ItemSet{ Name: "book loot", Items: []*ItemType{ ItemTypeNote, ItemTypeLetter, }, } ItemSetBookshelfLoot = &ItemSet{ Name: "bookshelf loot", Items: []*ItemType{ ItemTypeBook, ItemTypeNote, ItemTypeLetter, ItemTypeBook, ItemTypeBook, ItemTypeBook, }, } ItemSetStationary = &ItemSet{ Name: "stationary", Items: []*ItemType{ ItemTypeBook, ItemTypeNote, ItemTypeLetter, ItemTypeQuill, ItemTypeInk, ItemTypeParchment, ItemTypeSealingWax, }, } ItemSetBedLoot = &ItemSet{ Name: "bed loot", Items: []*ItemType{ ItemTypeBedSheet, ItemTypePillow, ItemTypeBlanket, }, } )
var ( ItemTypeWeaponFishingRod = &ItemType{ Tile: 'f', Name: "Fishing Rod", Description: "Baelin's fishing rod.", Type: ItemWeapon, Modifier: 20, Rarity: RarityLegendary, } ItemTypeWeaponSword = &ItemType{ Tile: '/', Name: "Sword", Description: "A sharp sword.", Type: ItemWeapon, Rarity: RarityUncommon, } ItemTypeWeaponAxe = &ItemType{ Tile: 'P', Name: "Axe", Description: "A sharp axe.", Type: ItemWeapon, Modifier: 1, Rarity: RarityUncommon, } ItemTypePotion = &ItemType{ Tile: 'Ö', Name: "Potion", Description: "A healing potion.", Type: ItemPotion, Rarity: RarityUncommon, } ItemTypeTrollPoop = &ItemType{ Tile: '8', Name: "Troll Poop", Description: "... with sprinkles!", Type: ItemPotion, Modifier: 10, Rarity: RarityLegendary, } ItemTypeGoblinToe = &ItemType{ Tile: 't', Name: "Goblin Toe", Description: "It's a bit smelly. A popular snack on TikTok.", Type: ItemPotion, Modifier: -1, Rarity: RarityRare, } ItemTypeArmorLeather = &ItemType{ Tile: 'L', Name: "Leather Armor", Description: "A leather armor.", Type: ItemArmor, Rarity: RarityUncommon, } ItemTypeArmorChain = &ItemType{ Tile: 'C', Name: "Chain Armor", Description: "A chain armor.", Type: ItemArmor, Modifier: 2, Rarity: RarityRare, } ItemTypeArmorPlate = &ItemType{ Tile: 'P', Name: "Plate Armor", Description: "A plate armor.", Type: ItemArmor, Modifier: 4, Rarity: RarityExotic, } ItemTypeHelmetSweatband = &ItemType{ Tile: 'S', Name: "Sweatband", Description: "A stylish sweatband.", Type: ItemHelmet, Modifier: 1, Rarity: RarityUncommon, } ItemTypeExit = &ItemType{ Tile: '>', Name: "Exit", Description: "An exit.", Type: ItemTrigger, OnTouch: func(g *Game, e *Entity, i *Item) { if e.EntityType == EntityPlayer { g.setViewMode(ViewModeSuccess) } }, } ItemTypeTrap = &ItemType{ Tile: '^', Name: "Trap", Description: "A trap.", Hidden: true, Type: ItemTrigger, OnTouch: func(g *Game, e *Entity, i *Item) { g.AddMessage(e.Name + " stepped on a trap!") e.Health -= 5 i.Hidden = false }, } ItemTypeNote = &ItemType{ Tile: 'N', Name: "Note", Description: "A note.", Type: ItemTypeDocument, Rarity: RarityRare, OnUse: func(g *Game, e *Entity, i *Item) { g.AddMessage("You read the note: \"You are a wizard, Harry!\"") }, } ItemTypeLetter = &ItemType{ Tile: 'L', Name: "Letter", Description: "A letter.", Type: ItemTypeDocument, Rarity: RarityLegendary, OnUse: func(g *Game, e *Entity, i *Item) { g.AddMessage("You read the letter: \"Dear Mr. Bigglesworth!\"") }, } ItemTypeBook = &ItemType{ Tile: 'b', Name: "Book", Description: "A book.", Type: ItemTypeDocument, Capacity: 1, Rarity: RarityCommon, PossibleLoot: ItemSetBookLoot, } ItemTypeChest = &ItemType{ Tile: 'c', Name: "Chest", Description: "A chest.", Type: ItemTypeContainer, Capacity: 4, Movable: false, PossibleLoot: ItemSetChestLoot, } ItemTypeBed = &ItemType{ Tile: 'b', Name: "Bed", Description: "A bed.", Type: ItemTypeContainer, Capacity: 2, Movable: false, PossibleLoot: ItemSetBedLoot, } ItemTypeSideTable = &ItemType{ Tile: 't', Name: "Side Table", Description: "A side table.", Type: ItemTypeContainer, Capacity: 1, Movable: false, } ItemTypeAltar = &ItemType{ Tile: 'a', Name: "Altar", Description: "An altar.", Type: ItemTypeContainer, Capacity: 1, Movable: false, } ItemTypeDesk = &ItemType{ Tile: 'd', Name: "Desk", Description: "A desk.", Type: ItemTypeContainer, Capacity: 2, Movable: false, PossibleLoot: ItemSetStationary, } ItemTypeBookshelf = &ItemType{ Tile: 'b', Name: "Book Shelf", Description: "A book shelf.", Type: ItemTypeContainer, Capacity: 4, Movable: false, Rarity: RarityCommon, PossibleLoot: ItemSetBookshelfLoot, } ItemTypeCandle = &ItemType{ Tile: 'c', Name: "Candle", Description: "A candle.", Type: ItemTypeDecorative, Movable: true, } ItemTypeQuill = &ItemType{ Tile: 'q', Name: "Quill", Description: "A quill.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Movable: true, } ItemTypeInk = &ItemType{ Tile: 'i', Name: "Ink", Description: "An ink.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Movable: true, } ItemTypeParchment = &ItemType{ Tile: 'p', Name: "Parchment", Description: "A parchment.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Movable: true, } ItemTypeSealingWax = &ItemType{ Tile: 'w', Name: "Sealing Wax", Description: "A sealing wax.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Movable: true, } ItemTypeEnvelope = &ItemType{ Tile: 'e', Name: "Envelope", Description: "An envelope.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Capacity: 1, Movable: true, PossibleLoot: ItemSetBookLoot, } ItemTypeBedSheet = &ItemType{ Tile: 's', Name: "Bed Sheet", Description: "A bed sheet.", Type: ItemTypeDecorative, Rarity: RarityCommon, Movable: true, } ItemTypePillow = &ItemType{ Tile: 'p', Name: "Pillow", Description: "A pillow.", Type: ItemTypeDecorative, Rarity: RarityUncommon, Movable: true, } ItemTypeBlanket = &ItemType{ Tile: 'b', Name: "Blanket", Description: "A blanket.", Type: ItemTypeDecorative, Rarity: RarityCommon, Movable: true, } )
var ( ObjectTypeDoor = &ObjectType{ Tile: '+', Name: "door", Description: "A door.", Actions: []*Action{ &Action{ Name: "open", Func: func(o *Object) { fmt.Println("You open the door.") }, }, &Action{ Name: "close", Func: func(o *Object) { fmt.Println("You close the door.") }, }, }, } ObjectTypeChest = &ObjectType{ Tile: 'c', Name: "chest", Description: "A chest.", Actions: []*Action{ &Action{ Name: "open", Func: func(o *Object) { fmt.Println("You open the chest.") }, }, &Action{ Name: "close", Func: func(o *Object) { fmt.Println("You close the chest.") }, }, }, } ObjectTypeBookshelf = &ObjectType{ Tile: 'b', Name: "bookshelf", Description: "A bookshelf.", Actions: []*Action{ &Action{ Name: "browse", Func: func(o *Object) { fmt.Println("You browse the bookshelf.") }, }, }, } )
var ( // RoomConfigBedroom is a bedroom. RoomConfigBedroom = &RoomConfig{ Name: "bedroom", Condition: func(room *Room, w *World) bool { if len(room.Connections) != 1 || room.W < 2 || room.H < 2 || room.W > 10 || room.H > 10 { return false } return true }, Placers: []*Placer{ placerBed, placerDesk, }, } // RoomConfigAltar is an altar room. RoomConfigAltar = &RoomConfig{ Name: "altar", HasColumns: true, Condition: func(room *Room, w *World) bool { if room.W < 10 && room.H < 10 { return false } log.Printf("Found altar room at %d,%d", room.X, room.Y) return true }, Placers: []*Placer{ placerAltar, }, } // RoomConfigLibrary is a library. RoomConfigLibrary = &RoomConfig{ Name: "library", HasColumns: true, Condition: func(room *Room, w *World) bool { if room.W < 4 || room.H < 4 { return false } log.Printf("Found library at %d,%d", room.X, room.Y) return true }, Placers: []*Placer{ placerLibrary, placerDesk, }, } )
var MonsterEntities = []*EntityType{ EntityGoblin, EntityOrc, EntityTroll, }
Functions ¶
func NewTextbox ¶
Types ¶
type Connection ¶
type Entity ¶
type Entity struct { Name string // name of entity (might be different from EntityType.Name) *EntityType // type of entity Inventory // inventory component X int // x position in the world Y int // y position in the world Health int // health points Slots [ItemTypeMax]*Item // Equipped items. }
func NewEntity ¶
func NewEntity(x, y int, e *EntityType) *Entity
NewEntity returns a new entity with the given position and tile.
func (*Entity) AttackDamage ¶
func (*Entity) DefenseValue ¶
func (*Entity) TakeDamage ¶
type EntityType ¶
type FOV ¶
type FOV struct { Seen [][]bool // keeps track of tiles that we have seen and remember Radius int // radius of the FOV *World // world to compute the FOV for }
FOV implements a field of view logic. TODO: Add raycasting or octants. See: http://journal.stuffwithstuff.com/2015/09/07/what-the-hero-sees/
func (*FOV) IsInRadius ¶
IsInRadius returns true if the given coordinates are within the FOV radius.
type Game ¶
type Game struct { Seed int64 // seed for the world Width int Height int *World // currently generated world *FOV // currently generated FOV Messages []string // messages to display // contains filtered or unexported fields }
func (*Game) AddMessage ¶
func (*Game) HandleInput ¶
type Inventory ¶
type Inventory struct {
Items []*Item
}
Inventory represents a collection of items.
func (*Inventory) RemoveItem ¶
RemoveItem removes the item with the given index from the inventory and returns it.
type Item ¶
type Item struct { *ItemType Hidden bool // indicates if the item is hidden Equipped bool // indicates if the item is equipped X int // x position in the world (if dropped) Y int // y position in the world (if dropped) Contains []*Item }
Item represents an item in the game.
func (Item) Consumable ¶
Consumable returns true if the item can be consumed.
func (Item) Equippable ¶
Equippable returns true if the item can be equipped.
type ItemType ¶
type ItemType struct { Tile byte Name string Description string Type int Modifier int Hidden bool // indicates if the item is hidden by default Movable bool // indicates if the item can be moved / picked up Rarity *Rarity Capacity int // indicates the maximum number of items this item can contain PossibleLoot *ItemSet // TODO: Allow multiple sets. // TODO: Add variants with different rarity. // Variants []*ItemType // TODO: Make this a map of event types to functions. OnTouch func(*Game, *Entity, *Item) // Trigger function called when item is used. OnUse func(*Game, *Entity, *Item) // Trigger function called when item is used. }
ItemType represents a type of item.
type Object ¶
type Object struct { *ObjectType Contains []*Item }
Object represents an object in the game.
type ObjectType ¶
type ObjectType struct { Tile byte Name string Description string Size int Capacity int Actions []*Action }
ObjectType represents the type of an object. TODO: Unify with items? A sword might have a hidden compartment... A book might contain a letter A shelf might contain various things... A bed might contain a hidden dagger.
func (*ObjectType) New ¶
func (t *ObjectType) New() *Object
NewObject returns a new object of the given type.
type Pathfinder ¶
type Pathfinder struct {
// contains filtered or unexported fields
}
Pathfinder represents a pathfinder.
func NewPathfinder ¶
func NewPathfinder(w *World, x, y, tx, ty int) *Pathfinder
NewPathfinder creates a new pathfinder.
func (*Pathfinder) FindPath ¶
func (p *Pathfinder) FindPath() []*Node
FindPath finds a path from the start to the end. NOTE: Poor man's A*. TODO: Use heuristics to prioritize the search to nodes that are closer to the end node.
type Rarity ¶
type Rarity struct { Name string // Name of this rarity Probability int // Probability of this rarity (the higher the more rare) IndicateRarity bool // Indicate rarity in item name }
Rarity represents the rarity of an item.
type Room ¶
type Room struct {
X, Y int // top left corner
W, H int // width and height
E int // elevation
RoundedCorner [4]bool // rounded corners
Connections []*Connection
}
Room represents a room in the world. TODO: Store connecting rooms
func (*Room) NextToDoor ¶
NextToDoor returns true if the given position is next to a door.
func (*Room) Overlaps ¶
Overlaps returns true if the given room overlaps with any of the rooms in the list.
func (*Room) RandAlongWall ¶
RandAlongWall returns a random position along the wall of the room.
type RoomConfig ¶
type RoomConfig struct { Name string // Name of the room type. HasColumns bool // Indicates if the room has columns. HasPuddle bool // Indicates if the room has a puddle. Condition func(*Room, *World) bool // Condition to check if the room can be placed. Placers []*Placer }
RoomConfig represents a (furnishing) generator configuration for a room type. TODO: Allow for random attributes like, columns, puddles, shape, etc.
func (*RoomConfig) Apply ¶
func (rc *RoomConfig) Apply(room *Room, w *World)
Apply applies the room config to the given room.
type SceneCharacterCreation ¶
type SceneCharacterCreation struct { *Game *console.ComponentBase // contains filtered or unexported fields }
func NewSceneCharacterCreation ¶
func NewSceneCharacterCreation(rootView *console.Console, world *Game) *SceneCharacterCreation
func (*SceneCharacterCreation) Close ¶
func (g *SceneCharacterCreation) Close() error
func (*SceneCharacterCreation) Draw ¶
func (s *SceneCharacterCreation) Draw(con *console.Console, timeElapsed float64)
func (*SceneCharacterCreation) FocusOnClick ¶
func (s *SceneCharacterCreation) FocusOnClick() bool
func (*SceneCharacterCreation) NewTextbox ¶
func (s *SceneCharacterCreation) NewTextbox(con *console.Console)
type SceneDeath ¶
type SceneDeath struct { *Game *console.ComponentBase // contains filtered or unexported fields }
func NewSceneDeath ¶
func NewSceneDeath(rootView *console.Console, world *Game) *SceneDeath
func (*SceneDeath) Close ¶
func (g *SceneDeath) Close() error
func (*SceneDeath) FocusOnClick ¶
func (s *SceneDeath) FocusOnClick() bool
type SceneMap ¶
type SceneMap struct { *Game *console.ComponentBase // contains filtered or unexported fields }
func (*SceneMap) FocusOnClick ¶
type SceneSuccess ¶
type SceneSuccess struct { *console.ComponentBase *Game // contains filtered or unexported fields }
func NewSceneSuccess ¶
func NewSceneSuccess(rootView *console.Console, world *Game) *SceneSuccess
func (*SceneSuccess) Close ¶
func (g *SceneSuccess) Close() error
func (*SceneSuccess) FocusOnClick ¶
func (s *SceneSuccess) FocusOnClick() bool
type UIif ¶
type UIif interface { Draw() Select() // Select selects the current UI element. HandleInput() // HandleInput handles input for the current UI element. }
type World ¶
type World struct { Cells [][]rune // 2D array of world cells Elevation [][]int // 2D array of elevation values Objects [][]*Object // 2D array of objects like furniture, chests, doors (TODO: Move this to cells) Width int // width of the world in cells Height int // height of the world in cells Entities []*Entity // entities in the world (creatures) Items []*Item // items in the world }
World represents a game world.
func GenWorldBigBox ¶
GenWorldBigBox generates a big box world.
func GenWorldSimpleDungeon ¶
GenWorldSimpleDungeon generates a simple random-walk-ish dungeon. - A starting room is placed in the center of the world. - Rooms are then placed in random directions neighboring a randomly selectd room. - Rooms are not placed if they would overlap with an existing room.
func (*World) AddRoomColumns ¶
AddRoomColumns adds columns to the given room.
func (*World) AddRoomFountain ¶
AddRoomFountain adds a fountain to the given room.
func (*World) AddRoomFurnishings ¶
AddRoomFurnishings adds furnishings to the given room.
func (*World) AddRoomPuddle ¶
AddRoomPuddle adds a puddle of water to the given room.
func (*World) CanMoveTo ¶
CanMoveTo checks if a tile is solid and if it is not occupied by an entity.
func (*World) IsEmpty ¶
IsEmtpy checks if a tile is empty (tile content is a space ' ' character and there is no object there).
func (*World) IsSolid ¶
IsSolid checks if a tile is solid (tile content is not a space ' ' character).
func (*World) NextToWall ¶
NextToWall returns true if the given position is next to a wall.