engine

package module
v0.0.0-...-fede144 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2020 License: MIT Imports: 19 Imported by: 0

README

Lakra

Simple game engine built with Golang

Preview

Game Engine Info

File by Info
  • game.go:
    • Gets the current level
    • Gets the input broadcasted
  • game_object.go:
    • Gets the current sprite of the games' state
    • Gets the current sprite frame based on ticker
    • Gets dimensions of the current game object
    • Handles position of game object
  • level.go:
    • Handles transitioning to levels after completions
  • sprite.go:
    • Handles creation of a single sprite and adding it to an image canvas
  • sprite_group.go:
    • Handles creation of sprite group and adding them to image canvas
  • window.go:
    • Creates the game window and renders image being shown

Build

The main.go file builds to a game written in the file

go build examples/main.go

Run

After bulding, you should be able to run the game:

  • Windows

Click on the exe file built

  • Unix (Linux/Mac OS)
./main

Roadmap

  • Create basic game engine
  • Create GUI for sprite creation
  • Create GUI for defining sprite control

Documentation

Index

Constants

View Source
const (
	// The values are determined via a catersian plane
	DirLeft       = -1 // x-axis
	DirRight      = 1  // x-axis
	DirStationary = 0  // x-axis
)

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 a 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 object 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 represented a sprite and its properties

func (*GameObject) ClearDynamicData

func (gameObject *GameObject) ClearDynamicData(key string)

ClearDynamicData clears a piece of specific 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 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 define value if the data does not exist

func (*GameObject) HasDynamicData

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

HasDynamicData checks whether a piece of dynamic data game object data exists

func (*GameObject) Height

func (gameObject *GameObject) Height() int

Height gets 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 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 via 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 game

type Palette

type Palette map[string]color.RGBA

Palette a type that defines stripe palletes

type Sprite

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

Sprite defines structure of single sprite

func CreateSprite

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

CreateSprite 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 sprite to 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 as 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