spritengine

package module
v0.0.0-...-3294fc8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2018 License: GPL-3.0 Imports: 19 Imported by: 0

README

spritengine

GoDoc

spritengine is a simple game engine written in Golang that provides the tools to easily build an 80s style 2D side-scrolling platformer.

A basic example game can be found in the example/ directory.

Sprites

Sprites are 16x16 pixels in size, represented by a group of 32 32-bit integers. They can contain up to 16 different RGBA colours as defined by a colour palette.

Sprites can be combined into groups to form larger sprites, and both individual sprites and groups can be easily drawn onto an image canvas.

A helper function is included to generate ready-to-use Golang sprite files from PNGs.

Game Objects

Game objects wrap sprites and add extra properties that the game can interact with, such as mass and velocity.

Levels

Levels represent a traditional game 'level' and contain all of the game objects required for that level. Under the hood the game delegates responsibility for repainting the canvas to the level.

Game

The game object contains all of the levels and is responsible for creating a window and capturing user input.

Documentation

Index

Constants

View Source
const (
	DirLeft       = -1
	DirRight      = 1
	DirStationary = 0
)

Direction constants

View Source
const (
	EventFloorCollision = 0
	EventDropOffLevel   = 1
	EventFreefall       = 2
)

Event constants

View Source
const (
	EdgeTop         = "top"
	EdgeTopLeft     = "top_left"
	EdgeTopRight    = "top_right"
	EdgeBottom      = "bottom"
	EdgeBottomLeft  = "bottom_left"
	EdgeBottomRight = "bottom_right"
	EdgeLeft        = "left"
	EdgeRight       = "right"
	EdgeNone        = "none"
)

Collision edges

Variables

This section is empty.

Functions

func GenerateFromPNGFile

func GenerateFromPNGFile(inputFile string, outputFile string, packageName string, exportedSpriteName string, palettes ...Palette)

GenerateFromPNGFile generates a SpriteGroup package file from an image on disk

Types

type BeforePaint

type BeforePaint func(level *Level)

BeforePaint is the signature for functions that are called on levels prior to them being repainted

type Collision

type Collision struct {
	GameObject *GameObject
	Edge       string
}

Collision is a struct that represents a collision with another game object

type CollisionHandler

type CollisionHandler func(gameObject *GameObject, collision Collision)

CollisionHandler is the signature for functions that handle collision events

type DynamicData

type DynamicData map[string]interface{}

DynamicData is a type that defines a repository of arbitrary game object data

type EventHandler

type EventHandler func(eventCode int, gameObject *GameObject)

EventHandler is the signature for functions that handle game events

type FramePainter

type FramePainter func(stage *image.RGBA, level *Level, frameRate float64)

FramePainter is the signature for functions that handle frame painting

type Game

type Game struct {
	Title           string
	Width           int
	Height          int
	ScaleFactor     int
	TargetFrameRate int
	FramePainter    FramePainter
	KeyListener     KeyListener
	Levels          []*Level
	CurrentLevelID  int
	CurrentFrame    int
}

Game is a struct that defines a game and the window that contains it

func CreateGame

func CreateGame(title string, width int, height int, scaleFactor int, targetFrameRate int, framePainter FramePainter, keyListener KeyListener, levels []*Level) *Game

CreateGame sets up a game and its window

func (*Game) BroadcastInput

func (game *Game) BroadcastInput(event key.Event)

BroadcastInput sends the game input to the current level's objects if they are controllable

func (*Game) CurrentLevel

func (game *Game) CurrentLevel() *Level

CurrentLevel gets the current Level object

type GameObject

type GameObject struct {
	CurrentState     string
	States           GameObjectStates
	Position         Vector
	Mass             float64
	Velocity         Vector
	Direction        int
	IsFlipped        bool
	IsControllable   bool
	IsFloor          bool
	IsInteractive    bool
	IsHidden         bool
	Level            *Level
	DynamicData      DynamicData
	FloorY           float64
	EventHandler     EventHandler
	CollisionHandler CollisionHandler
}

GameObject represents a sprite and its properties

func (*GameObject) ClearDynamicData

func (gameObject *GameObject) ClearDynamicData(key string)

ClearDynamicData clears a piece of dynamic game object data

func (*GameObject) CurrentSprite

func (gameObject *GameObject) CurrentSprite() SpriteInterface

CurrentSprite gets the current sprite for the object's state

func (*GameObject) GetCollisionEdge

func (gameObject *GameObject) GetCollisionEdge(collidingObject *GameObject) string

GetCollisionEdge infers the edge on which an intersecting object collided with the game object

func (*GameObject) GetDynamicData

func (gameObject *GameObject) GetDynamicData(key string, fallback interface{}) interface{}

GetDynamicData gets a piece of dynamic game object data, falling back to a defined value if the data does not exist

func (*GameObject) HasDynamicData

func (gameObject *GameObject) HasDynamicData(key string) bool

HasDynamicData checks whether a piece of dynamic game object data exists

func (*GameObject) Height

func (gameObject *GameObject) Height() int

Height gets the height of the game object

func (*GameObject) IsResting

func (gameObject *GameObject) IsResting() bool

IsResting determined whether the game object is currently atop another game object

func (*GameObject) RecalculatePosition

func (gameObject *GameObject) RecalculatePosition(gravity float64)

RecalculatePosition recalculates the latest X and Y position of the game object from its properties

func (*GameObject) SetDynamicData

func (gameObject *GameObject) SetDynamicData(key string, value interface{})

SetDynamicData sets a piece of dynamic game object data

func (*GameObject) Width

func (gameObject *GameObject) Width() int

Width gets the width of the game object

type GameObjectStates

type GameObjectStates map[string]SpriteSeries

GameObjectStates is a type that defines the various states (and accompanying sprites) of game objects

type KeyListener

type KeyListener func(event key.Event, gameObject *GameObject)

KeyListener is the signature for functions that handle key events for controllable game objects

type Level

type Level struct {
	BackgroundColour color.RGBA
	Gravity          float64
	GameObjects      []*GameObject
	Game             *Game
	PaintOffset      Vector
	BeforePaint      BeforePaint
}

Level is a struct that defines a single level of a game

func (*Level) AssignFloors

func (level *Level) AssignFloors()

AssignFloors iterates through all objects in the level and defines which object beneath them (if any) should be considered their 'floor' object, setting its top edge as the lowest point that the object can fall

func (*Level) CalculateCollisions

func (level *Level) CalculateCollisions()

CalculateCollisions iterates through all objects in the level and defines which objects (if any) intersect them

func (*Level) Repaint

func (level *Level) Repaint(stage *image.RGBA)

Repaint redraws the entire level for a new frame

type Palette

type Palette map[string]color.RGBA

Palette is a type that defines sprite palettes

type Sprite

type Sprite struct {
	Palette   *Palette
	Scanlines *[]int
}

Sprite is a struct that represents sprite objects

func CreateSprite

func CreateSprite(palette *Palette, scanlines []int) (*Sprite, error)

CreateSprite creates a sprite object based on a set of hex-encoded scanlines

func (*Sprite) AddToCanvas

func (sprite *Sprite) AddToCanvas(canvas *image.RGBA, targetX int, targetY int, mirrorImage bool)

AddToCanvas draws the sprite on an existing image canvas

func (*Sprite) Height

func (sprite *Sprite) Height() int

Height gets the pixel height of the sprite

func (*Sprite) Width

func (sprite *Sprite) Width() int

Width gets the pixel width of the sprite

type SpriteGroup

type SpriteGroup struct {
	GroupWidth  int
	GroupHeight int
	Sprites     *[]*Sprite
}

SpriteGroup is a struct that represents a group of sprites that form a larger individual sprite

func CreateSpriteGroup

func CreateSpriteGroup(width int, height int, sprites *[]*Sprite) (*SpriteGroup, error)

CreateSpriteGroup creates a sprite group based on a grid size and collection of sprites

func (*SpriteGroup) AddToCanvas

func (spriteGroup *SpriteGroup) AddToCanvas(canvas *image.RGBA, targetX int, targetY int, mirrorImage bool)

AddToCanvas draws the sprite group on an existing image canvas

func (*SpriteGroup) Height

func (spriteGroup *SpriteGroup) Height() int

Height gets the pixel height of the sprite group

func (*SpriteGroup) Width

func (spriteGroup *SpriteGroup) Width() int

Width gets the pixel width of the sprite group

type SpriteInterface

type SpriteInterface interface {
	AddToCanvas(canvas *image.RGBA, targetX int, targetY int, mirrorImage bool)
	Width() int
	Height() int
}

SpriteInterface is an interface that defines objects that can be treated a single sprites

type SpriteSeries

type SpriteSeries struct {
	Sprites         []SpriteInterface
	CyclesPerSecond int
}

SpriteSeries is a type that defines a series of sprites that form an animation for a game object state

type Vector

type Vector struct {
	X float64
	Y float64
}

Vector is a struct to represent X/Y vectors

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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