go2d

package module
v0.0.0-...-7207b0f Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: MIT Imports: 12 Imported by: 0

README

Go2D

A Canvas based 2D Game Engine written in Go built on SDL.

Running a Go2D Game

If you are only trying to run a game that was created using Go2D, you will need to install SDL.

See Installing SDL for more information on installing SDL for your particular system.

$ cd pong
$ CGO_ENABLED=1 CC=gcc GOOS=linux GOARCH=amd64 go build -tags static -ldflags "-s -w" pong.go
$ ./pong

Demos

There are two Demos packaged with Go2D which demonstrate the use of some of it's data structures.

  • Pong: A basic example of the classic game of Pong with a mild twist where the paddles shrink in size each time the ball collides with them.

  • Shooter: A simple Shooter game where balls will spawn at the top and you can shoot them by pressing the space bar.

Documentation

Index

Constants

View Source
const (
	AspectRatioControlAxisWidth = iota
	AspectRatioControlAxisHeight
)
View Source
const (
	MOUSE_BUTTON_LEFT   = 1
	MOUSE_BUTTON_MIDDLE = 2
	MOUSE_BUTTON_RIGHT  = 3
	MOUSE_BUTTON_X1     = 4
	MOUSE_BUTTON_X2     = 5
)
View Source
const (
	LINECAP_MITER = iota
	LINECAP_BEVEL
	LINECAP_ROUND
	LINECAP_SQUARE
	LINECAP_BUTT
)
View Source
const (
	TEXT_CENTERING_NONE = iota
	TEXT_CENTERING_VERTICAL
	TEXT_CENTERING_HORIZONTAL
)
View Source
const (
	RectSideLeft = iota
	RectSideRight
	RectSideTop
	RectSideBottom
)
View Source
const TICK_DURATION = time.Duration(0)

TICK_DURATION is intended to be used as the duration for a VelocityVector that should move the given amount in one tick.

Variables

This section is empty.

Functions

func NewTimer

func NewTimer(seconds float64, trigger TimerTrigger) *timer

NewTimer creates a new timer that will trigger the given TimerTrigger every given seconds.

func SetDefaultFont

func SetDefaultFont(font string, fontSize float64, textColor string)

SetDefaultFont sets the default font, font size, and text color for all text entities created with NewTextEntitySimple.

Types

type AspectRatio

type AspectRatio struct {
	Dimensions
	ControlAxis AspectRatioControlAxis
}

AspectRatio is a simple aspect ratio implementation.

func NewAspectRatio

func NewAspectRatio(w float64, h float64, c AspectRatioControlAxis) *AspectRatio

NewAspectRatio creates a new aspect ratio with the given width, height, and control axis.

func (*AspectRatio) NewDimensions

func (this *AspectRatio) NewDimensions(v float64) Dimensions

NewDimensions returns a new Dimensions struct that is the result of applying this aspect ratio to the given value on the control axis.

type AspectRatioControlAxis

type AspectRatioControlAxis int

AspectRatioControlAxis is an enum that defines which axis of an aspect ratio should be in control when calculating new dimensions.

type Dimensions

type Dimensions struct {
	Width  float64
	Height float64
}

Dimensions is a simple struct that represents the width and height of an object.

func NewSquareDimensions

func NewSquareDimensions(size float64) Dimensions

NewSquareDimensions creates a new Dimensions struct with equal width and height.

func NewZeroDimensions

func NewZeroDimensions() Dimensions

NewZeroDimensions creates a new Dimensions struct with zero width and height.

func (*Dimensions) Plus

func (this *Dimensions) Plus(other Dimensions) Dimensions

Plus returns a new Dimensions struct that is the sum of this Dimensions struct and the given Dimensions struct.

func (*Dimensions) PlusHeight

func (this *Dimensions) PlusHeight(height float64) Dimensions

PlusHeight returns a new Dimensions struct that is the sum of this Dimensions struct and the given height.

func (*Dimensions) PlusWidth

func (this *Dimensions) PlusWidth(width float64) Dimensions

PlusWidth returns a new Dimensions struct that is the sum of this Dimensions struct and the given width.

type Engine

type Engine struct {
	// Name is the name of the game window.
	Name string
	// Dimensions is the dimensions of the game window.
	Dimensions Dimensions
	// FPSUpdateHandler is the handler for when the FPS is updated.
	FPSUpdateHandler IFPSUpdateHandler
	// TPSUpdateHandler is the handler for when the TPS is updated.
	TPSUpdateHandler ITPSUpdateHandler
	// Canvas is the canvas that is used to render the game.
	Canvas *canvas.Canvas
	// HideCursor is a flag that determines if the cursor should be hidden.
	HideCursor bool
	// MaxTPS is the maximum TPS that the game should run at.
	MaxTPS int
	// contains filtered or unexported fields
}

Engine is the main engine that handles the game loop and rendering.

func GetActiveEngine

func GetActiveEngine() *Engine

GetActiveEngine returns the active engine.

func GetActiveEngines

func GetActiveEngines() []*Engine

GetActiveEngines returns all active engines.

func NewEngine

func NewEngine(name string, dimensions Dimensions) *Engine

NewEngine creates a new engine with the given name and dimensions.

func (*Engine) Bounds

func (this *Engine) Bounds() Rect

Bounds returns the bounds of the game window.

func (*Engine) GetFPS

func (this *Engine) GetFPS() int

GetFPS returns the current FPS.

func (*Engine) GetScene

func (this *Engine) GetScene() *Scene

GetScene returns the current scene.

func (*Engine) GetTPS

func (this *Engine) GetTPS() int

GetTPS returns the current TPS.

func (*Engine) Run

func (this *Engine) Run()

Run starts the game loop.

func (*Engine) SetScene

func (this *Engine) SetScene(scene *Scene)

SetScene sets the current scene to the given scene.

type Entity

type Entity struct {
	// Visible is a flag that can be used to hide the entity.
	Visible bool
	// Bounds is the rectangle that defines the entity's position and size.
	Bounds Rect
	// Velocity is the entity's velocity.
	Velocity VelocityVector
}

Entity is a basic entity that can be used to create more complex entities.

func (*Entity) CollidesWith

func (this *Entity) CollidesWith(other *Entity) bool

CollidesWith returns true if the entity collides with the other entity. This is not using IEntityCollider and IEntityCollisionDetection, but rather directly checks the entity's bounds.

func (*Entity) MoveTo

func (this *Entity) MoveTo(pos Vector)

MoveTo moves the entity to the specified position instantly.

func (*Entity) Push

func (this *Entity) Push(distance Vector)

Push moves the entity by the specified distance instantly.

func (*Entity) Update

func (this *Entity) Update()

Update updates the entity's position based on its velocity.

type EntityGroup

type EntityGroup struct {
	Entity
	// contains filtered or unexported fields
}

EntityGroup is an entity that represents a group of entities that can be rendered in layers.

func NewEntityGroup

func NewEntityGroup() *EntityGroup

NewEntityGroup creates a new entity group.

func (*EntityGroup) AddEntity

func (this *EntityGroup) AddEntity(layer int, ent interface{}) string

AddEntity adds an entity to the group.

func (*EntityGroup) AddNamedEntity

func (this *EntityGroup) AddNamedEntity(name string, layer int, ent interface{})

AddNamedEntity adds an entity to the group with the given name.

func (*EntityGroup) ClearEntities

func (this *EntityGroup) ClearEntities()

ClearEntities clears all entities from the group.

func (*EntityGroup) GetEntity

func (this *EntityGroup) GetEntity(layer int, name string) interface{}

GetEntity gets an entity from the group.

func (*EntityGroup) GetEntityGroupEntity

func (this *EntityGroup) GetEntityGroupEntity() *Entity

GetEntityGroupEntity returns the entity group entity.

func (*EntityGroup) IterateEntities

func (this *EntityGroup) IterateEntities(cb func(interface{}))

IterateEntities iterates over all entities in the group.

func (*EntityGroup) RemoveEntity

func (this *EntityGroup) RemoveEntity(layer int, name string)

RemoveEntity removes an entity from the group.

func (*EntityGroup) Render

func (this *EntityGroup) Render(engine *Engine)

Render renders the entity group.

func (*EntityGroup) Update

func (this *EntityGroup) Update(engine *Engine)

Update updates the entity group.

type IEntity

type IEntity interface {
	GetEntity() *Entity
}

type IEntityCollider

type IEntityCollider interface {
	GetCollider() Rect
}

IEntityCollider is an interface that can be implemented by entities that want to be recognized by other entities with that implement IEntityCollisionDetection.

type IEntityCollisionDetection

type IEntityCollisionDetection interface {
	CollidedWith(other interface{})
}

IEntityCollisionDetection is an interface that can be implemented by entities that want to be notified when they collide with other entities that implement IEntityCollider.

type IEntityConstrainedHandler

type IEntityConstrainedHandler interface {
	OnConstrained(s RectSide)
}

IEntityConstrainedHandler is an interface that can be implemented by entities that want to be notified when they are constrained.

type IEntityConstraint

type IEntityConstraint interface {
	Constrain(e *Engine) []RectSide
}

IEntityConstraint is an interface that can be implemented by entities that want to constrain themselves.

type IEntityRenderer

type IEntityRenderer interface {
	Render(c *Engine)
}

IEntityRenderer is an interface that can be implemented by entities that want to render themselves.

type IEntityUpdater

type IEntityUpdater interface {
	Update(e *Engine)
}

IEntityUpdater is an interface that can be implemented by entities that want to update themselves.

type IFPSUpdateHandler

type IFPSUpdateHandler interface {
	OnFPSUpdated(*Engine, int)
}

type IKeyChar

type IKeyChar interface {
	KeyChar(rn rune)
}

type IKeyDown

type IKeyDown interface {
	KeyDown(scanCode int, rn rune, name string)
}

type IKeyUp

type IKeyUp interface {
	KeyUp(scanCode int, rn rune, name string)
}

type IMouseDown

type IMouseDown interface {
	MouseDown(button int, pos Vector)
}

type IMouseMove

type IMouseMove interface {
	MouseMove(pos Vector)
}

type IMouseUp

type IMouseUp interface {
	MouseUp(button int, pos Vector)
}

type ISceneInitializer

type ISceneInitializer interface {
	Initialize(engine *Engine, scene *Scene)
}

type IScenePreRenderer

type IScenePreRenderer interface {
	PreRender(engine *Engine, scene *Scene)
}

type ISceneRenderer

type ISceneRenderer interface {
	Render(engine *Engine, scene *Scene)
}

type ISceneUpdater

type ISceneUpdater interface {
	Update(engine *Engine, scene *Scene)
}

type ITPSUpdateHandler

type ITPSUpdateHandler interface {
	OnTPSUpdated(*Engine, int)
}

type ITexture

type ITexture interface {
	GetTexture() image.Image
}

ITexture is an interface that represents a texture.

type ImageEntity

type ImageEntity struct {
	Entity
	// contains filtered or unexported fields
}

ImageEntity is a simple image entity that can be used to draw images on the screen.

func LoadImageEntity

func LoadImageEntity(path string) (*ImageEntity, error)

LoadImageEntity loads an image entity from the given path.

func NewCircleImageEntity

func NewCircleImageEntity(color string, radius int) *ImageEntity

NewCircleImageEntity creates a new image entity depicting a circle with the given color and radius.

func NewImageEntity

func NewImageEntity(img image.Image) *ImageEntity

NewImageEntity creates a new image entity from the given image.

func NewRectImageEntity

func NewRectImageEntity(color string, dimensions Dimensions) *ImageEntity

NewRectImageEntity creates a new image entity with the given color and dimensions.

func (*ImageEntity) GetEntity

func (this *ImageEntity) GetEntity() *Entity

GetEntity returns the entity of the image entity.

func (*ImageEntity) GetImage

func (this *ImageEntity) GetImage() image.Image

GetImage returns the image of the image entity.

func (*ImageEntity) Render

func (this *ImageEntity) Render(e *Engine)

Render renders the image entity.

func (*ImageEntity) Update

func (this *ImageEntity) Update(e *Engine)

Update updates the image entity.

type LineCapStyle

type LineCapStyle uint8

LienCapStyle is a type that represents the different line cap styles

type LineEntity

type LineEntity struct {
	Entity
	// contains filtered or unexported fields
}

LineEntity is a simple line entity that can be used to draw lines on the screen.

func NewLineEntity

func NewLineEntity(from Vector, direction Vector, length float64, thickness int, color string) *LineEntity

NewLineEntity creates a new line entity with the given from vector, direction vector, length, thickness, and color.

func NewLineEntitySimple

func NewLineEntitySimple(from Vector, to Vector, thickness int, color string) *LineEntity

NewLineEntitySimple creates a new line entity with the given from and to vectors, thickness, and color.

func (*LineEntity) GetEntity

func (this *LineEntity) GetEntity() *Entity

GetEntity returns the entity of the line entity.

func (*LineEntity) Render

func (this *LineEntity) Render(e *Engine)

Render renders the line entity.

func (*LineEntity) SetCapStyle

func (this *LineEntity) SetCapStyle(capStyle LineCapStyle)

SetCapStyle sets the cap style of the line.

func (*LineEntity) Update

func (this *LineEntity) Update(e *Engine)

Update updates the line entity.

type Rect

type Rect struct {
	Vector
	Dimensions
}

Rect is a simple rectangle implementation.

func NewRect

func NewRect(x, y, w, h float64) Rect

NewRect creates a new rectangle with the given width and height at the given position.

func NewZeroRect

func NewZeroRect(w, h float64) Rect

NewZeroRect creates a new rectangle with the given width and height at the origin.

func (Rect) Center

func (this Rect) Center() Vector

Center returns the center of this rectangle.

func (*Rect) Constrain

func (this *Rect) Constrain(r Rect) []RectSide

Constrain constrains this rectangle to the given rectangle. If this rectangle is outside of the given rectangle, it will be moved to the closest point on the rectangle. The return value is a slice of the sides that were constrained.

func (Rect) Contains

func (this Rect) Contains(v Vector) bool

Contains returns true if the given vector is contained within this rectangle.

func (Rect) Equals

func (this Rect) Equals(other Rect) bool

Equals returns true if the given rectangle is equal to this rectangle.

func (Rect) IntersectsWith

func (this Rect) IntersectsWith(other Rect) bool

IntersectsWith returns true if this rectangle intersects with the given rectangle.

type RectSide

type RectSide int

RectSide is an enumeration of the sides of a rectangle.

type Scene

type Scene struct {
	*EntityGroup

	// Name is the name of the scene.
	Name string

	// Initializer is the initializer that will be called when the scene is initialized.
	Initializer ISceneInitializer
	// PreRenderer is the pre-renderer that will be called before the scene is rendered.
	PreRenderer IScenePreRenderer
	// Renderer is the renderer that will be called when the scene is rendered.
	Renderer ISceneRenderer
	// Updater is the updater that will be called when the scene is updated.
	Updater ISceneUpdater
	// contains filtered or unexported fields
}

Scene is a simple scene implementation.

func GetActiveScene

func GetActiveScene() *Scene

GetActiveScene returns the active scene. If no scene is active, nil is returned. If you are using multiple scenes, you should use GetActiveScenes() otherwise you will get a panic.

func GetActiveScenes

func GetActiveScenes() []*Scene

GetActiveScenes returns all active scenes.

func NewScene

func NewScene(engine *Engine, name string) Scene

NewScene creates a new scene with the given name.

func (*Scene) AddTimer

func (this *Scene) AddTimer(name string, t *timer)

AddTimer adds a timer to the scene.

func (*Scene) ClearResources

func (this *Scene) ClearResources()

ClearResources clears all resources.

func (*Scene) GetResource

func (this *Scene) GetResource(name string) interface{}

GetResource returns a resource by name.

func (*Scene) RemoveTimer

func (this *Scene) RemoveTimer(name string)

RemoveTimer removes a timer from the scene.

func (*Scene) RenderStats

func (this *Scene) RenderStats(font string, size float64, color string)

RenderStats will enable rendering of the FPS and TPS in the top left corner of the screen. The font, size and color can be specified. The stats will be rendered on top of everything else.

func (*Scene) SetResource

func (this *Scene) SetResource(name string, res interface{})

SetResource sets a resource by name.

func (*Scene) StopRenderingStats

func (this *Scene) StopRenderingStats()

StopRenderingStats will stop rendering the FPS and TPS.

type SpriteSheet

type SpriteSheet struct {
	// RowSize is the size of each row in the sprite sheet.
	RowSize int
	// ColumnSize is the size of each column in the sprite sheet.
	ColumnSize int
	// contains filtered or unexported fields
}

SpriteSheet is a simple sprite sheet implementation.

func NewSpriteSheet

func NewSpriteSheet(path string, columnSize int, rowSize int) (*SpriteSheet, error)

NewSpriteSheet creates a new sprite sheet from the given image path with the given column and row size.

func (*SpriteSheet) GetSprite

func (this *SpriteSheet) GetSprite(location Vector) image.Image

GetSprite returns the sprite at the given location.

type TextCentering

type TextCentering int

TextCentering is a type that represents the different ways text can be centered.

type TextEntity

type TextEntity struct {
	Entity
	// contains filtered or unexported fields
}

TextEntity is a simple text entity that can be used to display text on the screen.

func NewTextEntity

func NewTextEntity(text string, font string, fontSize float64, textColor string) *TextEntity

NewTextEntity creates a new text entity with the given text, font, font size, and text color.

func NewTextEntitySimple

func NewTextEntitySimple(text string) *TextEntity

NewTextEntitySimple creates a new text entity with the given text, using the default font, font size, and text color.

func (*TextEntity) GetEntity

func (this *TextEntity) GetEntity() *Entity

GetEntity returns the base entity of this text entity.

func (*TextEntity) Measure

func (this *TextEntity) Measure(e *Engine)

Measure updates the bounds of this text entity based on the text, font, and font size also taking into account the centering of the text.

func (*TextEntity) Render

func (this *TextEntity) Render(e *Engine)

Render renders this text entity to the given engine.

func (*TextEntity) SetCenteredIn

func (this *TextEntity) SetCenteredIn(r Rect)

SetCenteredIn sets the rectangle that this text entity should be centered in.

func (*TextEntity) SetCentering

func (this *TextEntity) SetCentering(c TextCentering)

SetCentering sets the centering of this text entity.

func (*TextEntity) SetFont

func (this *TextEntity) SetFont(font string)

SetFont sets the font of this text entity.

func (*TextEntity) SetFontSize

func (this *TextEntity) SetFontSize(fontSize float64)

SetFontSize sets the font size of this text entity.

func (*TextEntity) SetText

func (this *TextEntity) SetText(text string)

SetText sets the text of this text entity.

func (*TextEntity) SetTextColor

func (this *TextEntity) SetTextColor(color string)

SetTextColor sets the text color of this text entity.

func (*TextEntity) Update

func (this *TextEntity) Update(e *Engine)

Update updates this text entity.

type TimerTrigger

type TimerTrigger interface {
	OnTriggered(owner interface{})
}

type Vector

type Vector struct {
	X float64
	Y float64
}

Vector is a 2D vector with an X and Y component.

func DirectionDown

func DirectionDown() Vector

DirectionDown returns a Vector with a y component of 1.

func DirectionLeft

func DirectionLeft() Vector

DirectionLeft returns a Vector with an x component of -1.

func DirectionRight

func DirectionRight() Vector

DirectionRight returns a Vector with an x component of 1.

func DirectionUp

func DirectionUp() Vector

DirectionUp returns a Vector with a y component of -1.

func NewRandomVector

func NewRandomVector(max Vector) Vector

NewRandomVector creates a new Vector with random x and y components within the given maximum.

func NewRandomVectorWithin

func NewRandomVectorWithin(r Rect) Vector

NewRandomVectorWithin creates a new Vector with random x and y components within the given rectangle.

func NewVector

func NewVector(x, y float64) Vector

NewVector creates a new Vector with the given x and y components.

func NewZeroVector

func NewZeroVector() Vector

NewZeroVector creates a new Vector with x and y components of 0.

func (*Vector) AngleTo

func (this *Vector) AngleTo(other Vector) float64

AngleTo returns the angle between this vector and the given vector.

func (*Vector) ConstrainTo

func (this *Vector) ConstrainTo(r Rect)

ConstrainTo constrains this vector to the given rectangle. If the vector is outside of the rectangle, it will be moved to the closest point on the rectangle.

func (Vector) Constrained

func (this Vector) Constrained(r Rect) Vector

Constrained returns a copy of this vector constrained to the given rectangle. If the vector is outside of the rectangle, it will be moved to the closest point on the rectangle.

func (*Vector) Copy

func (this *Vector) Copy() Vector

Copy returns a copy of this vector.

func (*Vector) DirectionTo

func (this *Vector) DirectionTo(other Vector) Vector

DirectionTo returns a Vector that points from this vector to the given vector.

func (*Vector) DistanceTo

func (this *Vector) DistanceTo(other Vector) float64

DistanceTo returns the distance between this vector and the given vector.

func (Vector) Equals

func (this Vector) Equals(other Vector) bool

Equals returns true if this vector is equal to the given vector.

func (Vector) Inverted

func (this Vector) Inverted() Vector

Inverted returns a copy of this vector with its x and y components inverted.

func (Vector) InvertedX

func (this Vector) InvertedX() Vector

InvertedX returns a copy of this vector with its x component inverted.

func (Vector) InvertedY

func (this Vector) InvertedY() Vector

InvertedY returns a copy of this vector with its y component inverted.

func (Vector) IsAbove

func (this Vector) IsAbove(v Vector) bool

IsAbove returns true if this vector is above the given vector.

func (Vector) IsBelow

func (this Vector) IsBelow(v Vector) bool

IsBelow returns true if this vector is below the given vector.

func (Vector) IsInsideOf

func (this Vector) IsInsideOf(r Rect) bool

IsInsideOf returns true if this vector is inside of the given rectangle.

func (Vector) IsLeftOf

func (this Vector) IsLeftOf(v Vector) bool

IsLeftOf returns true if this vector is to the left of the given vector.

func (Vector) IsRightOf

func (this Vector) IsRightOf(v Vector) bool

IsRightOf returns true if this vector is to the right of the given vector.

func (Vector) IsZero

func (this Vector) IsZero() bool

IsZero returns true if this vector is equal to the zero vector.

type VelocityVector

type VelocityVector struct {
	Vector
	Duration time.Duration
}

VelocityVector is a vector that has a duration. The duration is the amount of time it should take for the vector to move from one point to another. If the duration is 0, then the vector will move the given amount in one tick.

func NewVelocityVector

func NewVelocityVector(x, y float64, d time.Duration) VelocityVector

NewVelocityVector creates a new VelocityVector with the given x and y components and duration. The duration is the amount of time it should take for the vector to move from one point to another. If the duration is 0, then the vector will move the given amount in one tick.

func (VelocityVector) GetNextMovement

func (this VelocityVector) GetNextMovement() Vector

GetNextMovement returns the next movement that should be applied to the vector. This is used to calculate the next movement of the vector based on the duration. If the duration is 0, then the vector will move the given amount in one tick.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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