egriden

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 6 Imported by: 0

README

egriden is a framework for the Ebitengine game engine, perfect for creating simple grid-based puzzle or strategy games.

Current features:

  • A grid layer system
  • "Gobjects" with custom draw and update scripts
  • Animatable sprite system with easy loading from PNG files

Currently unstable! Contributions of any kind are welcome.

Quick boilerplate tutorial

package main

import (
    "github.com/greenthepear/egriden"
    "github.com/hajimehoshi/ebiten/v2"
)

type Game struct {
    egriden.EgridenAssets //Add assets needed for Egriden
    
    //Anything else you want here.
}

func (g *Game) Draw(screen *ebiten.Image) {
    g.DrawAllLayers(screen) //Draw layers according to their Z order

    // or do it your way with g.GridLayer(z).Draw()
}

func (g *Game) Update() error {
    // ... your game logic here
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
    return 320, 320 //Define screen layout
}

func main(){
    //Initialize
    g := &Game{}
	g.InitEgridenComponents()

    layer0 := g.CreateGridLayerOnTop(
        "Background", //Name
        16, //Pixel size of a tile in the grid
        10, 10, //Width and height of the grid
        egriden.Sparce, //Draw mode
        0, 0) //Draw offset from the top left corner

    //Create an image sequence from all the PNGs in a folder
    //Image sequences are made of frames, controlled by the frame index
    seq, err := egriden.CreateImageSequenceFromFolder("idle", "./Graphics/player/idle/")
    if err != nil {
        log.Fatal(err)
    }

    //Create SpritePack with the sequence. A sprite pack can have multiple sequences,
    //which can be switched using their names (keys)
    playerSprites := egriden.NewSpritePackWithSequence(seq)

    //Create Gobject (short for grid object or go object or game object or whatever
    //you like) with the ImagePack
    goPlayer := egriden.NewBaseGobject("player", playerSprites)

    //Add to layer, Build() method needed for a baseGobject, otherwise create your own
    //structure for the Gobject interface. You can define create, update, draw
    //functions and other fun stuff.
    layer0.AddGobject(goPlayer.Build(), 1, 5)

    //Run the game
    if err = ebiten.RunGame(g); err != nil {
        log.Fatal(err)
    }
}

Documentation

Index

Constants

View Source
const (
	//Used for sparcely populated grids, ranges over a map for drawing
	Sparce drawMode = iota
	//Used for thickly populated grids, ranges over a slice for drawing
	Dense
	//Used for layers that don't get updated often, creates ebiten.Image of the the entire layer.
	//Can be refreshed with GridLayer.RefreshImage().
	Static
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseGobject

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

The BaseGobject. Use it for simple Gobjects or implement your own Gobject by embedding this struct in your own.

func NewBaseGobject

func NewBaseGobject(name string, sprites SpritePack) BaseGobject

Create a new BaseGobject. Use BaseGobject.Build() to create a scriptless Gobject that can be added to a layer

func (BaseGobject) Build

func (o BaseGobject) Build() Gobject

Makes a BaseGobject respect Gobject interface by assigning it nil draw update etc. functions

For Gobjects with custom scripts you must create your own build function.

func (*BaseGobject) DrawSprite

func (o *BaseGobject) DrawSprite(on *ebiten.Image, l *GridLayer)

Default function for drawing the sprite in the grid, shouldn't be overwritten.

func (*BaseGobject) IsVisible

func (o *BaseGobject) IsVisible() bool

func (*BaseGobject) Name

func (o *BaseGobject) Name() string

func (*BaseGobject) NextFrame

func (o *BaseGobject) NextFrame()

Increments frame by one. Wraps back to 0 if out of range.

func (*BaseGobject) SetFrame

func (o *BaseGobject) SetFrame(i int)

Sets the frame to `i % len(frames)`

func (*BaseGobject) SetImageSequence

func (o *BaseGobject) SetImageSequence(name string) error

Sets Image Sequence under name, returns error if the name key is not present

func (*BaseGobject) SetSpritePack

func (o *BaseGobject) SetSpritePack(sp SpritePack)

Assigns Sprite Pack. Should not be used during game updates.

func (*BaseGobject) Sprite

func (o *BaseGobject) Sprite() *ebiten.Image

Returns the current sprite. Used for built-in drawing.

func (*BaseGobject) XY

func (o *BaseGobject) XY() (int, int)

type EgridenAssets

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

func (*EgridenAssets) CreateGridLayerOnTop

func (g *EgridenAssets) CreateGridLayerOnTop(name string, squareLength int, width, height int, drawMode drawMode, XOffset, YOffset float64) *GridLayer

Creates a grid layer at the lowest empty Z and returns a pointer to it.

See drawMode constants for which one you can use, but for small grids Sparce/Dense doesn't make much of a difference.

func (EgridenAssets) DrawAllLayers

func (g EgridenAssets) DrawAllLayers(screen *ebiten.Image)

Draw all GridLayers in their Z order. Use this in the Draw() function.

func (EgridenAssets) GridLayer

func (g EgridenAssets) GridLayer(z int) *GridLayer

Returns a GridLayer at z, panics if out of bounds

func (EgridenAssets) GridLayers

func (g EgridenAssets) GridLayers() []*GridLayer

func (*EgridenAssets) InitEgridenComponents

func (g *EgridenAssets) InitEgridenComponents()

Run this while initalizing the game, before adding any layers

func (*EgridenAssets) RunUpdateScripts

func (g *EgridenAssets) RunUpdateScripts()

Run all the OnUpdate() scripts of Gobjects that have them. Use this in the Update() function.

type Gobject

type Gobject interface {
	Name() string
	XY() (int, int)

	IsVisible() bool
	Sprite() *ebiten.Image
	SetImageSequence(string) error
	NextFrame()
	SetFrame(int)

	OnUpdate() func()                        //Runs every game.Update() call
	OnDraw() func(*ebiten.Image, *GridLayer) //Runs ever game.Draw() call
	DrawSprite(*ebiten.Image, *GridLayer)    //Default sprite drawing function
	// contains filtered or unexported methods
}

Gobject is an object that exists in a layer

type GridLayer

type GridLayer struct {
	Name          string
	Z             int
	SquareLength  int
	Width, Height int
	Visible       bool

	XOffset, YOffset float64
	NumOfGobjects    int
	// contains filtered or unexported fields
}

func (*GridLayer) AddObject

func (l *GridLayer) AddObject(o Gobject, x, y int)

Adds Gobject to the layer at x y. Will overwrite the any existing Gobject there.

func (GridLayer) Draw

func (l GridLayer) Draw(screen *ebiten.Image)

func (GridLayer) GobjectAt

func (l GridLayer) GobjectAt(x, y int) Gobject

Returns Gobject at x y, nil if empty. Panics if out of bounds.

func (GridLayer) IsOccupiedAt

func (l GridLayer) IsOccupiedAt(x, y int) bool

func (*GridLayer) RefreshImage

func (l *GridLayer) RefreshImage()

func (*GridLayer) SetVisibility

func (l *GridLayer) SetVisibility(to bool)

False visibility disables drawing both the Sprites and custom draw scripts of all Gobjects.

type ImageSequence

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

ImageSequence is a sequence of images aka frames.

func CreateImageSequenceFromFolder

func CreateImageSequenceFromFolder(name, folderPath string) (ImageSequence, error)

Searches for PNG files in the folder and creates an ImageSequence, with frame order based on the alphabetical order of the file names.

func CreateImageSequenceFromPaths

func CreateImageSequenceFromPaths(name string, paths ...string) (ImageSequence, error)

Create an ImageSequence using multiple (or just one) file paths.

type SpritePack

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

SpritePack is a collection of ImageSequences and controls things like the frame index.

func EmptySpritePack

func EmptySpritePack() SpritePack

A sprite pack that will not render anything

func NewSpritePack

func NewSpritePack() SpritePack

func NewSpritePackWithSequence

func NewSpritePackWithSequence(is ImageSequence) SpritePack

Create SpritePack and assign sequence

func NewSpritePackWithSequences

func NewSpritePackWithSequences(is ...ImageSequence) SpritePack

Create SpritePack and assign multiple sequences

func (*SpritePack) AddImageSequence

func (ip *SpritePack) AddImageSequence(is ImageSequence)

Assigns an ImageSequence to SpritePack

func (SpritePack) Sprite

func (sp SpritePack) Sprite() *ebiten.Image

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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