tinyrogue

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 6 Imported by: 0

README

TinyRogue

hello example screenshot

Fun package for creating roguelike games using TinyGo on Firefly Zero.

Features

Complete
walk example screenshot
  • Customizable images with cache for tiles such as walls and floors
  • Generative maps
ghost castle screenshot
  • Field of View for torch-like illumination

  • Creature behavior is configurable

  • Creatures approach Player using shortest path astar algorithm

ghost castle combat screenshot
  • Configurable action system for combat, spells, etc.
  • Popup dialogs for messages e.g. "A wild gopher has appeared!"
  • Creature spawning
  • Automatic terrain image variation for walls and floors
  • Multiple dungeons each with multiple levels.
  • Portals aka level entrances/exits
TODO
  • Game items
  • Predefined maps/levels
  • World map
  • Show/hide entrances/exits
  • PortalTypes per dungeon
  • ?

Examples

Here is a simple "Hello, Gopher" example that shows a little bit of what you can do:

package main

import (
	"github.com/deadprogram/tinyrogue"
	"github.com/firefly-zero/firefly-go/firefly"
)

func init() {
	firefly.Boot = boot
	firefly.Update = update
	firefly.Render = render
}

var game *tinyrogue.Game

func boot() {
	// create a new game
	game = tinyrogue.NewGame()

	// load the image tiles for the floor and walls
	game.LoadImage("floor")
	game.LoadImage("wall")

	// set the dimensions for the game and the tiles
	gd := tinyrogue.NewGameData(16, 10, 16, 16)
	game.SetData(gd)

	// generate a random game map
	game.SetMap(tinyrogue.NewSingleLevelGameMap())

	// create the player
	player := tinyrogue.NewPlayer("Player", "player", game.LoadImage("player"), 5)
	game.SetPlayer(player)

	// set player initial position to some open spot on the map.
	player.MoveTo(game.CurrentLevel().OpenLocation())
}

func update() {
	game.Update()
}

func render() {
	game.Render()
}

The code for this is located at Hello, Gopher

More examples
Complete Games

A complete game is "Ghost Castle" located in this repo: https://github.com/deadprogram/ghost-castle

"Ghost Castle" is in the Firefly Zero catalog here: https://catalog.fireflyzero.com/deadprogram.ghost-castle

Architecture diagram

This diagram shows the relationship between the different types that make up a TinyRogue game.

flowchart TD
    subgraph game
    Game
    Game --> GM
    Game --> Player
    Game --> Creatures
    subgraph world
    GM[GameMap] --> Dungeons
    Dungeons --> dungeon
    subgraph dungeon
    Dungeon --> Levels
    Levels --> levels
    subgraph levels
    Level --> Tiles
    Tiles --> tiles
    Level --> Rooms
    Rooms --> rooms
    subgraph tiles
    MapTile1
    MapTile2
    MapTileN
    end
    subgraph rooms
    Room1
    Room2
    RoomN
    end
    end
    end
    end
    subgraph characters
    Player
    Creatures --> Creature1
    Creatures --> Creature2
    Creatures --> CreatureN
    subgraph creatures
    Creature1
    Creature2
    CreatureN
    end
    end
    end
Game

Game is what you are making that the user can play.

Player

The Player represents the person playing the Game.

Creatures

Creatures are the non-player characters. Could be monsters, allies, or ?

GameMap

GameMap is what contains the important information about the world in which the game is played.

Dungeon

A Dungeon is a collection of Levels that the Player will explore.

Level

A Level in an individual level which contains a collection of MapTiles and a collection of Rooms.

MapTile

MapTile is an individual tile on a grid which represents the positions of the walls, floor, player, and the creatures.

Room

A Room is a rectangular area of a Level which is open to the Player or Creatures to move around in.

Useful tools

Here is a short list of some useful tools:

Convert existing images to Sweetie 16 pallette

https://tezumie.github.io/Image-to-Pixel/

Credits

Based on code originally from the following sources, but with many modifications:

https://github.com/cscazorla/roguelike

https://github.com/gogue-framework/gogue

Thank you!

Documentation

Index

Constants

View Source
const (
	BeforePlayerAction = iota
	PlayerTurn
	CreatureTurn
	GameOver
)

Variables

This section is empty.

Functions

func ConnectExits added in v0.2.0

func ConnectExits(startDungeon *Dungeon, startLevel *Level, destinationDungeon *Dungeon, destinationLevel *Level)

ConnectExits connects the exits of two levels. Should only be called after both the startLevel and destinationLevel have been generated.

func GetDiceRoll

func GetDiceRoll(num int) int

GetDiceRoll returns an integer from 1 to the number

func GetRandomBetween

func GetRandomBetween(low int, high int) int

Return a number between two numbers inclusive.

func GetRandomInt

func GetRandomInt(num int) int

GetRandomInt returns an integer from 0 to the number - 1

func InitializeWorld

func InitializeWorld(startingLevel Level)

InitializeWorld sets up the game world with the starting level.

Types

type AStar

type AStar struct{}

AStar implements the AStar Algorithm.

func (AStar) GetPath

func (as AStar) GetPath(level *Level, start Position, end Position) []Position

GetPath takes a level, the starting position and an ending position (the goal) and returns a list of Positions which is the path between the points.

type Actionable

type Actionable interface {
	Action(sender Character, receiver Character)
}

Actionable is an interface for actions that can be taken by characters.

type Character

type Character interface {
	Name() string
	Kind() string
	SetImage(img *firefly.Image)
	GetSpeed() int
	SetSpeed(speed int)
	GetPosition() Position
	Move(dx, dy int)
	MoveTo(pos Position)
	Draw()
	Update()
	IsVisible() bool
	SetVisible(visible bool)
}

Character is the interface for all characters in the game.

type Creature

type Creature struct {
	CurrentBehavior CreatureBehavior
	Visible         bool
	// contains filtered or unexported fields
}

Creature is the type for all creatures in the game.

func NewCreature

func NewCreature(name string, kind string, img *firefly.Image, speed int) *Creature

NewCreature creates a new Creature and initializes the data

func (*Creature) Approach

func (c *Creature) Approach()

Approach moves the creature towards the player.

func (*Creature) Avoid

func (c *Creature) Avoid()

func (Creature) Draw

func (c Creature) Draw()

Draw draws the character on the screen.

func (Creature) GetPosition

func (c Creature) GetPosition() Position

GetPosition returns the position of the character.

func (Creature) GetSpeed

func (c Creature) GetSpeed() int

GetSpeed returns the speed of the character. Lower is faster.

func (*Creature) IsVisible

func (c *Creature) IsVisible() bool

func (Creature) Kind

func (c Creature) Kind() string

Kind returns the kind of the character.

func (Creature) Move

func (c Creature) Move(dx, dy int)

Move moves the character by the given amount.

func (Creature) MoveTo

func (c Creature) MoveTo(pos Position)

MoveTo moves the character to the given position.

func (Creature) Name

func (c Creature) Name() string

Name returns the name of the character.

func (*Creature) SetBehavior

func (c *Creature) SetBehavior(b CreatureBehavior)

SetBehavior sets the behavior of the creature.

func (Creature) SetImage

func (c Creature) SetImage(img *firefly.Image)

SetImage sets the image for the character.

func (Creature) SetSpeed

func (c Creature) SetSpeed(speed int)

SetSpeed sets the speed of the character. Lower is faster.

func (*Creature) SetVisible

func (c *Creature) SetVisible(visible bool)

func (*Creature) Update

func (c *Creature) Update()

Update updates the creature.

type CreatureBehavior

type CreatureBehavior int
const (
	CreatureIgnore CreatureBehavior = iota
	CreatureApproach
	CreatureAvoid
)

type DebugAction

type DebugAction struct {
}

func (*DebugAction) Action

func (da *DebugAction) Action(sender Character, receiver Character)

type Dialog

type Dialog struct {
	Font              *firefly.Font
	FontColor         firefly.Color
	FillColor         firefly.Color
	Text1             string
	Text2             string
	NeedsConfirmation bool
	Confirmed         bool

	Point firefly.Point
	Size  firefly.Size
	// contains filtered or unexported fields
}

Dialog is a simple dialog box that can be displayed to the player.

func NewDialog

func NewDialog(text1 string, text2 string, font *firefly.Font, fontcolor, fillColor firefly.Color, needsConfirmation bool) *Dialog

NewDialog creates a new dialog box with the given text and font.

func (*Dialog) Draw

func (d *Dialog) Draw()

Draw draws the dialog box to the screen.

func (*Dialog) Update

func (d *Dialog) Update()

Update updates the dialog box, basically just used to dismiss it.

type Dungeon

type Dungeon struct {
	Name       string
	Levels     []*Level
	FloorTypes string
	WallTypes  string
}

Dungeon is a container for all the levels that make up a particular dungeon in the world.

func NewDungeon added in v0.2.0

func NewDungeon(name, floors, walls string) Dungeon

NewDungeon creates a new Dungeon with the given name.

func (*Dungeon) CreateLevels added in v0.2.0

func (d *Dungeon) CreateLevels(n int)

CreateLevels creates a number of empty levels in the dungeon.

func (*Dungeon) Level added in v0.2.0

func (d *Dungeon) Level(name string) *Level

Level returns a Level by name.

func (*Dungeon) NextLevel added in v0.2.0

func (d *Dungeon) NextLevel(l *Level) *Level

NextLevel returns the next level in the dungeon after the given level.

type FieldOfVision

type FieldOfVision struct {
	// contains filtered or unexported fields
}

FieldOfVision represents an area that an entity can see, defined by the torch radius. The cos and sin tables are generated once on instantiation, so we don't have to build them each time we want to calculate visible distances.

func (*FieldOfVision) InitializeFOV

func (f *FieldOfVision) InitializeFOV()

InitializeFOV generates the cos and sin tables, for 360 degrees, for use when raycasting to determine line of sight

func (*FieldOfVision) RayCast

func (f *FieldOfVision) RayCast(playerX, playerY int, m *Level)

RayCast casts out rays each degree in a 360 circle from the player. If a ray passes over a floor (does not block sight) tile, keep going, up to the maximum torch radius (view radius) of the player. If the ray intersects a wall (blocks sight), stop, as the player will not be able to see past that. Every visible tile will get the Visible and Explored properties set to true.

func (*FieldOfVision) SetAllInvisible

func (f *FieldOfVision) SetAllInvisible(m *Level)

Equal to the code that used to live in main's game:clearFOV() SetAllInvisible makes all tiles on the gamemap invisible to the player.

func (*FieldOfVision) SetTorchRadius

func (f *FieldOfVision) SetTorchRadius(radius int)

SetTorchRadius sets the radius of the FOVs torch, or how far the entity can see

type Game

type Game struct {
	Debug bool
	Map   *GameMap
	Data  GameData

	// TurnBased is a flag to determine if the game is turn based or real-time.
	TurnBased   bool
	Turn        TurnState
	TurnCounter int

	// Player and Creatures
	Player    Character
	Creatures []Character

	// Images that are cached for space efficiency. Used for tiles and creatures.
	Images map[string]firefly.Image

	// UseFOV is a flag to determine if the game should use Field of View.
	UseFOV bool

	// ActionSystem is the interface for the game to handle actions between characters.
	ActionSystem Actionable

	// DialogShowing is a flag to determine if a dialog is currently showing.
	DialogShowing bool
	// contains filtered or unexported fields
}

Game holds all data the entire game will need.

func CurrentGame

func CurrentGame() *Game

CurrentGame returns the current game.

func NewGame

func NewGame() *Game

NewGame creates a new Game Object and initializes the data

func (*Game) AddCreature

func (g *Game) AddCreature(c Character)

AddCreature adds a creature to the game.

func (*Game) CurrentDungeon added in v0.2.0

func (g *Game) CurrentDungeon() *Dungeon

CurrentDungeon returns the current Dungeon for game.

func (*Game) CurrentLevel added in v0.1.3

func (g *Game) CurrentLevel() *Level

CurrentLevel returns the current level for game.

func (*Game) GetCreatureByName

func (g *Game) GetCreatureByName(name string) Character

GetCreatureByName returns a creature by name.

func (*Game) GetCreatureForTile

func (g *Game) GetCreatureForTile(index int) Character

GetCreatureForTile returns the creature for the given tile index.

func (*Game) GetIndexFromXY

func (g *Game) GetIndexFromXY(x int, y int) int

GetIndexFromXY returns the index for the given x and y coordinates.

func (*Game) Layout

func (g *Game) Layout(w, h int) (int, int)

Layout accepts an outside size, which is a window size on desktop, and returns the game's logical screen size.

func (*Game) LoadImage

func (g *Game) LoadImage(name string) *firefly.Image

LoadImage loads a single image and caches it for later use. It returns a pointer to the image.

func (*Game) LoadImages added in v0.2.0

func (g *Game) LoadImages(names ...string)

LoadImages loads a list of image and caches them for later use.

func (*Game) NextDungeon added in v0.2.0

func (g *Game) NextDungeon() *Dungeon

NextDungeon returns the next Dungeon for game.

func (*Game) RemoveCreature

func (g *Game) RemoveCreature(c Character)

RemoveCreature removes a creature from the game.

func (*Game) Render

func (g *Game) Render()

Draw is called each on each frame loop

func (*Game) SetActionSystem

func (g *Game) SetActionSystem(a Actionable)

SetActionSystem sets the action system for the game.

func (*Game) SetData

func (g *Game) SetData(d GameData)

SetData sets the data for the game.

func (*Game) SetMap

func (g *Game) SetMap(m *GameMap)

SetMap sets the map for the game.

func (*Game) SetPlayer

func (g *Game) SetPlayer(p Character)

SetPlayer sets the player for the game.

func (*Game) ShowDialog

func (g *Game) ShowDialog(dlg *Dialog)

ShowDialog shows a dialog on the screen.

func (*Game) Update

func (g *Game) Update()

Update is called on each frame loop The default value is 1/60 [s]

type GameData

type GameData struct {
	Cols       int
	Rows       int
	TileWidth  int
	TileHeight int
	MinSize    int
	MaxSize    int
	MaxRooms   int
	FloorTypes string
	WallTypes  string
}

GameData holds the values for the size of elements within the game

func NewGameData

func NewGameData(cols, rows, tilewidth, tileheight int) GameData

NewGameData creates a fully populated GameData Struct.

func (*GameData) GameHeight

func (gd *GameData) GameHeight() int

GameHeight returns the height of the game in pixels.

func (*GameData) GameWidth

func (gd *GameData) GameWidth() int

GameWidth returns the width of the game in pixels.

type GameMap

type GameMap struct {
	Name           string
	Dungeons       []Dungeon
	CurrentDungeon string
	CurrentLevel   string
}

GameMap holds all the level and aggregate information for the entire world.

func NewGameMap

func NewGameMap(name string, dungeons []Dungeon, startDungeon string, startLevel string) *GameMap

NewGameMap creates a new set of maps for the entire game Using the predefined levels and dungeons.

func NewGeneratedGameMap added in v0.2.0

func NewGeneratedGameMap(name string, dungeonCount int, levelCount int, floors, walls string) *GameMap

NewGeneratedGameMap generated a new set of dungeons and levels for the entire game.

func NewSingleGameMapWithTerrain added in v0.2.0

func NewSingleGameMapWithTerrain(floors, walls string) *GameMap

NewSingleGameMapWithTerrain creates a single level generated game map.

func NewSingleLevelGameMap added in v0.2.0

func NewSingleLevelGameMap() *GameMap

NewSingleGameMap creates a single level generated game map.

func (*GameMap) Dungeon added in v0.2.0

func (gm *GameMap) Dungeon(name string) *Dungeon

Dungeon returns a Dungeon by name.

func (*GameMap) NextDungeon added in v0.2.0

func (gm *GameMap) NextDungeon() *Dungeon

NextDungeon returns the next Dungeon in the list.

func (*GameMap) SetCurrentLevel added in v0.2.0

func (gm *GameMap) SetCurrentLevel(d *Dungeon, l *Level)

SetCurrentLevel sets the current Dungeon and Level in the game map.

type Image

type Image struct {
}

type Level

type Level struct {
	Name       string
	Generated  bool
	Tiles      []*MapTile
	Rooms      []Rect
	FloorTypes string
	WallTypes  string
	ViewRadius int
	Entrance   *Portal
	Exit       *Portal
}

Level holds the tile information for a complete dungeon level.

func NewLevel

func NewLevel(name, floors, walls string) *Level

NewLevel creates a new game level in a dungeon.

func (*Level) Block

func (level *Level) Block(pos Position, block bool)

Block sets the blocked property of a tile at the given Position.

func (*Level) Draw

func (level *Level) Draw()

Draw the level.

func (*Level) Dump

func (level *Level) Dump()

Dump prints the level to the console.

func (*Level) Generate added in v0.2.0

func (level *Level) Generate()

Generate creates a new Dungeon Level Map.

func (*Level) GetEntrancePosition added in v0.2.0

func (level *Level) GetEntrancePosition() Position

GetEntrancePosition returns the position of the entrance for this level.

func (*Level) GetExitPosition added in v0.2.0

func (level *Level) GetExitPosition() Position

GetExitPosition returns the position of the exit for this level.

func (*Level) GetIndexFromXY

func (level *Level) GetIndexFromXY(x int, y int) int

Tiles will be stored in one slice. We will use GetIndexFromXY to determine which tile to return. GetIndexFromXY gets the index of the map array from a given X,Y TILE coordinate. This coordinate is logical tiles, not pixels.

func (*Level) GetRoom added in v0.1.3

func (level *Level) GetRoom(x, y int) int

func (*Level) InBounds

func (level *Level) InBounds(x, y int) bool

InBounds checks if the given x and y coordinates are within the level bounds.

func (*Level) IsOpaque

func (level *Level) IsOpaque(x, y int) bool

IsOpaque checks if the given x and y coordinates are within the level bounds.

func (*Level) OpenLocation added in v0.1.3

func (level *Level) OpenLocation() Position

OpenLocation returns an open location in the level. Used for "spawning".

func (*Level) OpenLocationReachableFrom added in v0.2.1

func (level *Level) OpenLocationReachableFrom(from Position) Position

OpenLocationReachableFrom returns an open location that is reachable from the given position. This ensures there is a valid path between the two positions.

func (*Level) RandomLocation

func (level *Level) RandomLocation() (Position, bool)

RandomLocation returns a random location in the level.

func (*Level) SetEntrance added in v0.2.0

func (level *Level) SetEntrance(p *Portal, pos Position)

SetEntrance sets the entrance to the level.

func (*Level) SetExit added in v0.2.0

func (level *Level) SetExit(p *Portal, pos Position)

SetExit sets the exit to the level.

type MapTile

type MapTile struct {
	PixelX   int // Upper left corner of the tile
	PixelY   int
	Blocked  bool           // tile should block the player or creatures?
	Image    *firefly.Image // image for this tile
	Visible  bool
	Explored bool
	TileType TileType
}

Each of the map tiles will be represented by one of these structures

type Player

type Player struct {
	ViewRadius int
	// contains filtered or unexported fields
}

Player represents the player character in the game.

func NewPlayer

func NewPlayer(name string, kind string, img *firefly.Image, speed int) *Player

NewPlayer creates a new Player and initializes the data.

func (Player) Draw

func (c Player) Draw()

Draw draws the character on the screen.

func (Player) GetPosition

func (c Player) GetPosition() Position

GetPosition returns the position of the character.

func (Player) GetSpeed

func (c Player) GetSpeed() int

GetSpeed returns the speed of the character. Lower is faster.

func (*Player) IsVisible

func (p *Player) IsVisible() bool

IsVisible always returns true, because the player is always visible.

func (Player) Kind

func (c Player) Kind() string

Kind returns the kind of the character.

func (Player) Move

func (c Player) Move(dx, dy int)

Move moves the character by the given amount.

func (Player) MoveTo

func (c Player) MoveTo(pos Position)

MoveTo moves the character to the given position.

func (Player) Name

func (c Player) Name() string

Name returns the name of the character.

func (Player) SetImage

func (c Player) SetImage(img *firefly.Image)

SetImage sets the image for the character.

func (Player) SetSpeed

func (c Player) SetSpeed(speed int)

SetSpeed sets the speed of the character. Lower is faster.

func (*Player) SetVisible

func (p *Player) SetVisible(visible bool)

SetVisible is just here to fulfill Character interface.

func (*Player) Update

func (p *Player) Update()

Update updates the player.

type Portal added in v0.2.0

type Portal struct {
	PortalType      string
	Visible         bool
	Image           *firefly.Image
	DungeonName     string
	DestinationName string
}

Portal is the type for all portals between levels in the game.

func NewPortal added in v0.2.0

func NewPortal(pt string, img *firefly.Image, dungeon *Dungeon, destination *Level) *Portal

NewPortal creates a new Portal and initializes the data.

func (*Portal) Destination added in v0.2.0

func (p *Portal) Destination() *Level

Destination returns the Level this portal leads to.

func (*Portal) Dungeon added in v0.2.0

func (p *Portal) Dungeon() *Dungeon

Dungeon returns the Dungeon this portal is in.

type Position

type Position struct {
	X int
	Y int
}

Position represents a position in 2D space.

func (*Position) GetManhattanDistance

func (p *Position) GetManhattanDistance(other Position) int

GetManhattanDistance returns the Manhattan distance between this position and another.

func (*Position) IsEqual

func (p *Position) IsEqual(other Position) bool

IsEqual returns true if this position is equal to another.

type Rect

type Rect struct {
	X1 int
	X2 int
	Y1 int
	Y2 int
}

Rect represents a rectangle in 2D space.

func NewRect

func NewRect(x int, y int, width int, height int) Rect

NewRect creates a new Rect given the top-left corner, width, and height.

func (*Rect) Center

func (r *Rect) Center() (int, int)

Center returns the center point of the rectangle.

func (*Rect) Contains added in v0.1.3

func (r *Rect) Contains(x, y int) bool

Contains returns true if the rectangle contains the given point.

func (*Rect) Intersect

func (r *Rect) Intersect(other Rect) bool

Intersect returns true if this rectangle intersects with another.

type TileType

type TileType int

TileType represents a type of tile in the level.

const (
	WALL TileType = iota
	FLOOR
	ENTRANCE
	EXIT
)

type TurnState

type TurnState int

TurnState represents the current state of the game turn.

func GetNextState

func GetNextState(state TurnState) TurnState

GetNextState returns the next turn state based on the current state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL