model

package
v0.0.0-...-6c50e3f Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package model defines the basic types and data structures of the game. This can be thought of as the Model part of an MVC architecture.

It is "shared", used by the view and ctrl packages.

Index

Constants

View Source
const (
	// BlockSize is the size of the labyrinth unit in pixels.
	BlockSize = 40
)
View Source
const MouseBtnRight = 2

Constant for the right Mouse button value in the Click struct. Button value for left and middle may not be the same for older browsers, but right button always has this value.

Variables

View Source
var (
	// Rows is the number of rows in the Labyrinth
	Rows int
	// Cols is the number of columns in the Labyrinth
	Cols int

	// LabWidth is the width of the labyrinth's image in pixels.
	LabWidth int

	// LabHeight is the height of the labyrinth's image in pixels.
	LabHeight int
)
View Source
var BulldogDensity float64

"Bulldog density", it tells how many Bulldogs to generate for average of 1,000 blocks. For example if this is 10.0 and rows*cols = 21*21 = 441, 10.0*441/1000 = 4.41 => 4 Bulldogs will be generated.

View Source
var BulldogImgs []*image.RGBA = make([]*image.RGBA, DirLength)

Bulldog images for each direction, each has zero Min point

View Source
var Bulldogs []*MovingObj

Slice of Bulldogs, the ancient enemy of Gophers.

View Source
var ClickCh = make(chan Click, 10)

Channel to receive mouse clicks on (view package sends, ctrl package (engine) processes them)

View Source
var Dead bool

Dead tells if Gopher died

View Source
var DeadImg *image.RGBA

Dead Gopher image.

View Source
var EmptyImg = image.NewUniform(color.RGBA{A: 0xff})

Image of the empty block

View Source
var ExitImg *image.RGBA

Image of a door, this is the exit sign

View Source
var ExitPos = image.Point{}

Exit position

View Source
var Gopher = new(MovingObj)

Gopher is our hero, the moving object the user can control.

View Source
var GopherImgs []*image.RGBA = make([]*image.RGBA, DirLength)

Gopher images for each direction, each has zero Min point

View Source
var Lab [][]Block

The model/data of the labyrinth

View Source
var LabImg *image.RGBA

Image of the labyrinth

View Source
var Mutex sync.Mutex

Mutex to be used to synchronize model modifications

View Source
var NewGameCh = make(chan int, 1)

Channel to signal new game

View Source
var TargetImg *image.RGBA

Image of the empty block

View Source
var TargetPoss = make([]image.Point, 0, 20)

For Gopher we maintain multiple target positions which define a path on which Gopher will move along

V is the moving speed of Gopher and the Buddlogs in pixel/sec.

View Source
var WallImg *image.RGBA

Image of the wall block var WallImg = image.NewUniform(WallCol)

View Source
var Won bool

Tells if we won

View Source
var WonImg *image.RGBA

Image of a congratulation

Functions

func DrawImgAt

func DrawImgAt(img image.Image, x, y int)

DrawImgAt draws the specified image at the specified position which specifies the center of the area to draw. The size of the image draw is the block size.

func InitNew

func InitNew()

InitNew initializes a new game.

Types

type Block

type Block int

Type of the unit of the labyrinth

const (
	// Empty block (free passage)
	BlockEmpty Block = iota
	// Wall block
	BlockWall
)

Block types of the labyrinth

type Click

type Click struct {
	// X, Y are the mouse coordinates in pixel, in the coordinate system of the Labyrinth
	X, Y int
	// Btn is the mouse button
	Btn int
}

Click describes a mouse click.

type Dir

type Dir int
const (
	DirRight Dir = iota
	DirLeft
	DirUp
	DirDown

	// Not a valid direction: just to tell how many directions are there
	DirLength
)

Directions of Gopher (facing directions)

func (Dir) String

func (d Dir) String() string

type MovingObj

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

	// Direction where the object is facing toward
	Direction Dir

	// Target position the object is moving to
	TargetPos image.Point

	// Images for each direction, each has zero Min point
	Imgs []*image.RGBA
}

MovingObj is a struct describing a moving object.

func (*MovingObj) DrawImg

func (m *MovingObj) DrawImg()

DrawImg draws the image of the MovingObj to the LabImg.

func (*MovingObj) DrawWithImg

func (m *MovingObj) DrawWithImg(img image.Image)

DrawWithImage draws the specified image at the position of the moving object onto the LabImg.

func (*MovingObj) EraseImg

func (m *MovingObj) EraseImg()

EraseImg erases the image of the MovingObj from the LabImg by drawing empty block to it.

Jump to

Keyboard shortcuts

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