engine

package
v0.0.0-...-4306f7c Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package engine is the game engien: it contains the game model and game logic.

The engine's Loop() method should be launched as a goroutine, and it can be controlled with opaque commands safely from other goroutines.

Index

Constants

View Source
const (
	// BlockEmpty is the empty, free-to-walk block
	BlockEmpty = iota
	// BlockWall designates an unpassable wall.
	BlockWall

	// BlockCount is not a valid block: just to tell how many blocks there are
	BlockCount
)
View Source
const (
	// DirRight .
	DirRight = iota
	// DirLeft .
	DirLeft
	// DirUp .
	DirUp
	// DirDown .
	DirDown

	// DirCount is not a valid direction: just to tell how many directions there are
	DirCount
)
View Source
const (
	// BlockSize is the size of the labyrinth unit in pixels.
	BlockSize = 40
)

Variables

View Source
var Difficulties = []*Difficulty{
	&Difficulty{Name: "Baby", bulldogDensity: 0},
	&Difficulty{Name: "Easy", bulldogDensity: 5},
	&Difficulty{Name: "Normal", bulldogDensity: 10, Default: true},
	&Difficulty{Name: "Hard", bulldogDensity: 20},
	&Difficulty{Name: "Brutal", bulldogDensity: 40},
}

Difficulties is a slice of all, ordered difficulties.

View Source
var DifficultyDefaultIdx int

DifficultyDefaultIdx is the index of the default difficulty in Difficulties.

View Source
var LabSizeDefaultIdx int

LabSizeDefaultIdx is the index of the default lab size in LabSizes.

View Source
var LabSizes = []*LabSize{
	&LabSize{Name: "XS", rows: 9, cols: 9},
	&LabSize{Name: "S", rows: 15, cols: 15},
	&LabSize{Name: "M", rows: 33, cols: 33, Default: true},
	&LabSize{Name: "L", rows: 51, cols: 51},
	&LabSize{Name: "XL", rows: 99, cols: 99},
}

LabSizes is a slice of all, ordered lab sizes.

View Source
var SpeedDefaultIdx int

SpeedDefaultIdx is the index of the default speed in Speeds.

View Source
var Speeds = []*Speed{
	&Speed{Name: "Slow", loopDelay: 67 * time.Millisecond},
	&Speed{Name: "Normal", loopDelay: 50 * time.Millisecond, Default: true},
	&Speed{Name: "Fast", loopDelay: 37 * time.Millisecond},
}

Speeds is a slice of all, ordered speeds.

Functions

This section is empty.

Types

type Block

type Block int

Block is a square unit of the Labyrinth

type Click

type Click struct {
	X, Y  int  // Click coordinates in the lab
	Left  bool // Tells if left button was pressed
	Right bool // Tells if right button was pressed
}

Click describes a click event.

type Difficulty

type Difficulty struct {
	Name string

	Default bool
	// contains filtered or unexported fields
}

Difficulty of the game.

func (*Difficulty) String

func (d *Difficulty) String() string

type Dir

type Dir int

Dir represents directions

func (Dir) String

func (d Dir) String() string

type Engine

type Engine struct {
	Model *Model
	// contains filtered or unexported fields
}

Engine calculates and controls the game.

func NewEngine

func NewEngine(invalidate func()) *Engine

NewEngine returns a new Engine. invalidate is a func which will be called by the engine to request a new view frame.

func (*Engine) Loop

func (e *Engine) Loop()

Loop starts calculating the game. This function returns only if the user closes the app.

func (*Engine) NewGame

func (e *Engine) NewGame(cfg GameConfig)

NewGame enqueues a new game command with the given config.

func (*Engine) SendClick

func (e *Engine) SendClick(c Click)

SendClick sends a click event from the user.

func (*Engine) SendKey

func (e *Engine) SendKey(k Key)

SendKey sends a key event from the user.

type GameConfig

type GameConfig struct {
	Difficulty *Difficulty
	LabSize    *LabSize
	Speed      *Speed
}

GameConfig holds config to start a new game.

type Key

type Key struct {
	DirKeys map[Dir]bool // Tells if keys for the directions were pressed
}

Key describes a key event.

type LabSize

type LabSize struct {
	Name string

	Default bool
	// contains filtered or unexported fields
}

LabSize difines choosable labyrinth sizes..

func (*LabSize) String

func (l *LabSize) String() string

type Model

type Model struct {
	// Mutex to protect the model from concurrent access.
	sync.RWMutex

	// Game counter. Must be increased by one when a new game is initialized.
	// Can be used to invalidate caches when its value changes.
	Counter int

	// Size of the labyrinth in blocks.
	Rows, Cols int

	// Blocks of the lab. First indexed by row, then by column.
	Lab [][]Block

	// ExitPos: the position Gopher has to reach to win the game.
	ExitPos image.Point

	// Our well-beloved hero Gopher
	Gopher *MovingObj

	// The ancient enemies of Gopher: the bloodthirsty Bulldogs.
	Bulldogs []*MovingObj

	// Dead tells if Gopher is dead.
	Dead bool

	// Won tells if we won
	Won bool

	// For Gopher we maintain multiple target positions which specify a path on which Gopher will move along
	TargetPoss []image.Point
}

Model is the model of the game.

type MovingObj

type MovingObj struct {
	// The position in the labyrinth in pixel coordinates
	Pos struct {
		X, Y float64
	}

	// Direction this object is facing to
	Dir Dir

	// Target position this object is moving to
	TargetPos image.Point
}

MovingObj describes moving objects in the labyrinth.

type Speed

type Speed struct {
	Name string

	Default bool
	// contains filtered or unexported fields
}

Speed of the game.

func (*Speed) String

func (s *Speed) String() string

Jump to

Keyboard shortcuts

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