playground

package
v1.0.0-rc-2014 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2014 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

OUTPUT STANDARD 'ST_1'

1. Dots

1.1. Output pattern of dot is `X&Y` where 'X' and 'Y' are dot coordinates:

1&2
2&1
31&10

1.2. Dot set is printing as `dot1;dot2;dot3` where dotN is packed dot:

12&12;12&13;12&14;14&15

2. Directions

2.1. Direction packs as exclamation mark and letter of a direction:

!n
!s
!e
!w

2.2 Used directions

n - north s - sounth e - east w - west

3. Objects

3.1 Output pattern of objects is `'TYPE'ID[OBJECT_DATA]` where 'TYPE' is a type of object (in single quotes), 'ID' is object identifier on playground, 'OBJECT_DATA' is a dataset which object contains (string returned Pack method of object). For example:

'apple'2[22&22]
'apple'3[1&7]
'corpse'5[1&7;6&6;5&7;9&8]
'snake'10[key%3%1&2;21&2;21&3]

'OBJECT_DATA' cannot contain chars '[', ']', ','

3.3 In a objectsets objects are separated by comma:

'apple'3[1&7],'snake'10[3%1&2;21&2;21&3]

Objects of different types cannot have equal type name markers.

4. Cached objects

4.1. If object state was not updated it are created in cache list. In cache list objects are represented like numbers according with it's identifiers on playground separated by comma:

1,2,3,4,5,6,7

4.2. Identifiers of cached objects are printed in objectset:

1,2,'apple'3[1&7],4,5,6,7,8,9,'snake'10[3%1&2;21&2;21&3]

4.3 Objects cannot be created in object set twice: as identifier (like cached object) and as updated packed object

This package provides simple small engine for flat games. It contains minimum set of functions, objects and interfaces to develop online 2d arcade games such as tanks or snake etc.

Author: Pushkin Ivan <pushkin13@bk.ru>

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPGNotContainsDot = errors.New("Playground doesn't contain dot")
	ErrNilPlayground    = errors.New("Playground is nil")
	ErrInvalid_W_or_H   = errors.New("Invalid width or height")
	ErrPassedNilObject  = errors.New("Passed nil object")
)
View Source
var ErrEmptyDotList = errors.New("Empty dot list")
View Source
var ErrInvalidDirection = errors.New("Invalid direction")

Functions

func CalculatePath

func CalculatePath(from, to *Dot) uint16

CalculatePath calculates path between two dots

func ValidDirection

func ValidDirection(dir Direction) bool

ValidDirection returns true if passed direction is valid

Types

type Direction

type Direction uint8

Direction indicates movement direction of a object

const (
	DIR_NORTH Direction = iota
	DIR_EAST
	DIR_SOUTH
	DIR_WEST
)

func CalculateDirection

func CalculateDirection(from, to *Dot) Direction

CalculateDirection calculates direction by two passed dots

func RandomDirection

func RandomDirection() Direction

RandomDirection returns random direction

func ReverseDirection

func ReverseDirection(dir Direction) Direction

ReverseDirection reverses passed direction dir

func (Direction) Pack

func (dir Direction) Pack() string

Pack packs direction

type Dot

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

Dot are used to understand object position

func NewDefaultDot

func NewDefaultDot() *Dot

func NewDot

func NewDot(x, y uint8) *Dot

NewDot creates new dot object

func NewRandomDotOnSquare

func NewRandomDotOnSquare(x, y, w, h uint8) *Dot

NewRandomDotOnSquare generates random dot on square with coordinates x and y and width w and height h

func (*Dot) Equals

func (d1 *Dot) Equals(d2 *Dot) bool

Equals compares two dots

func (*Dot) Pack

func (d *Dot) Pack() string

Pack packs dot to string in accordance with standard ST_1

func (*Dot) Position

func (d *Dot) Position() (uint8, uint8)

Position returns coordinates of dot

type DotList

type DotList []*Dot

DotList represents list of dots and provides list packing

func (DotList) Contains

func (dl DotList) Contains(dot *Dot) bool

Contains returns true if dot list contains passed dot

func (DotList) Delete

func (dl DotList) Delete(dot *Dot)

Delete deletes dot from dot list

func (DotList) Pack

func (dl DotList) Pack() (output string)

Pack packs dot list to string in accordance with standard ST_1

func (DotList) Reverse

func (dl DotList) Reverse() (rdl DotList)

type Object

type Object interface {
	DotCount() uint16  // DotCount must return dot count
	Dot(i uint16) *Dot // Dot returns dot by index
	Pack() string      // Pack converts object data to string
}

Each object must implements Object interface

type Playground

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

Playground object contains all objects on map

func NewPlayground

func NewPlayground(width, height uint8) (*Playground, error)

NewPlayground returns new empty playground

func (*Playground) Contains

func (pg *Playground) Contains(dot *Dot) bool

Contains return true if playground contains passed dot

func (*Playground) Delete

func (pg *Playground) Delete(object Object) error

Delete deletes passed object from playground and returns error if there is a problem

func (*Playground) GetArea

func (pg *Playground) GetArea() uint16

GetArea returns playground area

func (*Playground) GetEmptyDot

func (pg *Playground) GetEmptyDot() (*Dot, error)

GetEmptyDot finds empty random dot

func (*Playground) GetEmptyField

func (pg *Playground) GetEmptyField(w, h uint8) (DotList, error)

GetEmptyField finds empty field with passed width and height

func (*Playground) GetObjectByDot

func (pg *Playground) GetObjectByDot(dot *Dot) Object

GetObjectByDot returns object which contains passed dot

func (*Playground) GetSize

func (pg *Playground) GetSize() (uint8, uint8)

GetSize returns width and height of playground

func (*Playground) Locate

func (pg *Playground) Locate(object Object) error

Locate tries to create object to playground

func (*Playground) Located

func (pg *Playground) Located(object Object) bool

Located returns true if passed object is located on playground

func (*Playground) Navigate

func (pg *Playground) Navigate(dot *Dot, dir Direction, dis int16,
) (*Dot, error)

Navigate calculates and returns dot placed on distance dis dots from passed dot in direction dir

func (*Playground) Occupied

func (pg *Playground) Occupied(dot *Dot) bool

Occupied returns true if passed dot already used by any object located on playground

func (*Playground) Pack

func (pg *Playground) Pack() (output string)

Pack packs playground in accordance with standard ST_1

func (*Playground) PackObjects

func (pg *Playground) PackObjects(ids []int) (output string)

PackObjects packs objects to string in accordance with standard ST_1 by passed object identifiers

func (*Playground) RandomDot

func (pg *Playground) RandomDot() *Dot

RandomDot generates random dot located on playground

func (*Playground) Updated

func (pg *Playground) Updated() bool

Updated returns true if any object on playground was updated or just added or deleted

type Shifting

type Shifting interface {
	PackChanges() string // PackChanges packs last changes
	Updated() time.Time  // Updated returns last updating time
}

Moving and shifting objects must implement Shifting interface. For objects which implements Object and Shifting interfaces method Pack returns all object data and method PackChanges returns only last updates.

Jump to

Keyboard shortcuts

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