goaseprite

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

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

Go to latest
Published: Aug 26, 2018 License: MIT Imports: 8 Imported by: 2

README

GoAseprite

Aseprite JSON loader for Go (Golang)

Yo, 'sup! This is a JSON loader for Aseprite files written in / for Go.

How To Use

Usage is pretty straightforward. You export a sprite sheet from Aseprite (Ctrl+E), with a vertical or horizontal strip and an Output File, JSON data checked, and the values set to Hash with Frame Tags on (the default).

For use with your project, you'll call goaseprite.New() with a string argument of where to find the outputted Aseprite JSON data file. The function will return a goaseprite.AsepriteFile struct. It's from here that you control your animation. Since each instance of an AsepriteFile struct has playback specific variables and values, it's best not to share these across multiple instances (unless they are supposed to share animation playback).

After you have an AsepriteFile, you can just call its Update() function with an argument of delta time (the time between the previous frame and the current one) to get it updating, and call GetFrameXY() to get the X and Y position of the current frame on the sprite sheet. Here's a quick pseudo-example for a simple "Player" class using raylib-go:

package main

import (
	"github.com/gen2brain/raylib-go/raylib"
	"github.com/solarlune/GoAseprite"
)


type Player struct {
    Ase         goaseprite.AsepriteFile
    Texture     raylib.Texture2D
    TextureRect raylib.Rectangle
}

func NewPlayer() *Player {

    player := Player{}
    
    // goaseprite.New() returns an AsepriteFile, assuming it finds the JSON file
    player.Ase = goaseprite.New("assets/graphics/Player.json")
    
    // AsepriteFile.ImagePath will be relative to the working directory
    player.Texture = raylib.LoadTexture(player.Ase.ImagePath)
    
    // Set up the texture rectangle for drawing the sprite
    player.TextureRect = raylib.Rectangle{0, 0, player.Ase.FrameWidth, player.Ase.FrameHeight}
    
    // Queues up the "Play" animation
    player.Ase.Play("Idle")
    
    return &player
    
}

func (this *Player) Update() {
    
    // Call this every frame with the delta-time (time since the last frame)
    this.Ase.Update(raylib.GetFrameTime())
    
    // Set up the source rectangle for drawing the sprite (on the sprite sheet)
    this.TextureRect.X, this.TextureRect.Y = this.Ase.GetFrameXY()
}

func (this *Player) Draw() {
    // And draw it~!
    raylib.DrawTextureRec(
    this.Texture,
    this.TextureRect,
    raylib.Vector2{0, 0},
    raylib.White)
}

Take a look at the Wiki for more information on the API!

Additional Notes

As for dependencies, GoAseprite makes use of tidwall's nice gjson package.

Documentation

Index

Constants

View Source
const (
	// AsepritePlayForward plays animations forward
	AsepritePlayForward = "forward"
	// AsepritePlayBackward plays animations backwards
	AsepritePlayBackward = "reverse"
	// AsepritePlayPingPong plays animation forward then backward
	AsepritePlayPingPong = "pingpong"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AsepriteAnimation

type AsepriteAnimation struct {
	Name      string
	Start     int32
	End       int32
	Direction string
}

AsepriteAnimation contains the details of each tagged animation

type AsepriteFile

type AsepriteFile struct {
	ImagePath        string
	FrameWidth       int32
	FrameHeight      int32
	Frames           []AsepriteFrame
	Animations       []AsepriteAnimation
	CurrentAnimation *AsepriteAnimation

	CurrentFrame int32

	PrevFrame int32
	PlaySpeed float32
	Playing   bool
	// contains filtered or unexported fields
}

AsepriteFile contains all properties of an exported aseprite file.

func New

func New(aseJSONFilePath string) AsepriteFile

New Parses and returns an AsepriteFile. Your starting point.

func (*AsepriteFile) GetAnimation

func (asf *AsepriteFile) GetAnimation(animName string) *AsepriteAnimation

GetAnimation Returns a pointer to an AsepriteAnimation of the desired name. If it can't be found, it will return `nil`.

func (*AsepriteFile) GetFrameXY

func (asf *AsepriteFile) GetFrameXY() (float32, float32)

GetFrameXY Returns the current frame's X and Y coordinates on the source sprite sheet for drawing the sprite.

func (*AsepriteFile) HitTag

func (asf *AsepriteFile) HitTag(tagName string) bool

HitTag Returns if the AsepriteFile's playback just touched a tag by the specified name.

func (*AsepriteFile) HitTags

func (asf *AsepriteFile) HitTags() []string

HitTags Returns a list of tags the AsepriteFile just touched.

func (*AsepriteFile) IsPlaying

func (asf *AsepriteFile) IsPlaying(animName string) bool

IsPlaying Returns if the named animation is playing.

func (*AsepriteFile) LeftTag

func (asf *AsepriteFile) LeftTag(tagName string) bool

LeftTag Returns if the AsepriteFile's playback just left a tag by the specified name.

func (*AsepriteFile) LeftTags

func (asf *AsepriteFile) LeftTags() []string

LeftTags Returns a list of tags the AsepriteFile just left.

func (*AsepriteFile) Play

func (asf *AsepriteFile) Play(animName string)

Play Queues playback of the specified animation (assuming it can be found).

func (*AsepriteFile) TouchingTag

func (asf *AsepriteFile) TouchingTag(tagName string) bool

TouchingTag Returns if the AsepriteFile's playback is touching a tag (animation) by the specified name.

func (*AsepriteFile) TouchingTags

func (asf *AsepriteFile) TouchingTags() []string

TouchingTags Returns a list of tags the playback is touching.

func (*AsepriteFile) Update

func (asf *AsepriteFile) Update(deltaTime float32)

Update Steps the file forward in time, updating the currently playing animation (and also handling looping).

type AsepriteFrame

type AsepriteFrame struct {
	X        int32
	Y        int32
	Duration float32
}

AsepriteFrame contains the frame information

Jump to

Keyboard shortcuts

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