goaseprite

package module
v0.0.0-...-8ddb57c Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: MIT Imports: 9 Imported by: 0

README

goaseprite

GoDoc link

Hello! 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 Output File and JSON data checked, and the values set to Hash with Frame Tags and Slices (optionally) on.

Then you'll want to load the Aseprite data. To do this, you'll call goaseprite.Open() with a string argument of where to find the Aseprite JSON data file, or goaseprite.ReadFile() with the data itself in something that implements io.Reader. Either way, you'll get a goaseprite.File. It's from here that you control your animation.

After you have a File, you can just call the File.Update() function with an argument of delta time (the time between the previous frame and the current one) to get it updating. After that, use File.Play() to play an animation, and call File.GetFrameXY() to get the X and Y position of the current frame on the sprite sheet for where on the source sprite sheet to pull the frame of animation from.

Here's a quick pseudo-example for a simple "Player" class using raylib-go:

package main

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


type Player struct {
    Ase         *goaseprite.File
    Texture     rl.Texture2D
    TextureRect rl.Rectangle
}

func NewPlayer() *Player {

    player := Player{}
    
    // goaseprite.Open() returns an AsepriteFile, assuming it finds the JSON file. You can also use goaseprite.Read( io.Reader ) to read 
    // in JSON info from data.
    player.Ase = goaseprite.Open("assets/graphics/Player.json")
    
    // Note that while File.ImagePath exists, it will be the absolute path to the image file as exported from Aseprite, so it's best to load 
    // the texture yourself using relative paths so you can distribute it for others' computers.
    player.Texture = rl.LoadTexture("assets/graphics/Player.png")
    
    // Set up the texture rectangle for drawing the sprite.
    player.TextureRect = rl.Rectangle{0, 0, player.Ase.FrameWidth, player.Ase.FrameHeight}
    
    // Queues up the "Play" animation.
    player.Ase.Play("Idle")
    
    return &player
    
}

func (player *Player) Update() {
    
    // Call this every frame with the delta-time (time since the last frame); this will update the File's 
    // currently playing animation.
    player.Ase.Update(rl.GetFrameTime())
    
    // Set up the source rectangle for drawing the sprite (on the sprite sheet). File.GetFrameXY() will return the X and Y position
    // of the current frame of animation for the File.
    x, y := this.Ase.GetFrameXY()
    player.TextureRect.X = float32(x)
    player.TextureRect.Y = float32(y)
}

func (player *Player) Draw() {
    // Draw it!
    rl.DrawTextureRec(player.Texture, player.TextureRect, rl.Vector2{0, 0}, rl.White)
}

Additional Notes

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

Documentation

Overview

Package goaseprite is an Aseprite JSON loader written in Golang.

Index

Constants

View Source
const (
	// PlayForward plays animations forward
	PlayForward = "forward"
	// PlayBackward plays animations backwards
	PlayBackward = "reverse"
	// PlayPingPong plays animation forward then backward
	PlayPingPong = "pingpong"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Animation

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

Animation contains details regarding each animation from Aseprite. This also represents a tag in Aseprite and in goaseprite. Start and End are the starting and ending frame of the Animation. Direction is a string, and can be assigned one of the playback constants.

type File

type File struct {
	ImagePath        string
	Path             string
	FrameWidth       int32
	FrameHeight      int32
	Frames           []Frame
	Animations       []Animation
	Layers           []Layer
	CurrentAnimation *Animation

	CurrentFrame int32

	PrevFrame int32
	PlaySpeed float32
	Playing   bool
	Slices    []*Slice
	// contains filtered or unexported fields
}

File contains all properties of an exported aseprite file. ImagePath is the absolute path to the image as reported by the exported Aseprite JSON data. Path is the string used to open the File if it was opened with the Open() function; otherwise, it's blank.

func Open

func Open(jsonPath string) *File

Open will use os.Open() to open the Aseprite JSON file path specified to return a *goaseprite.File, relative to the current working directory. This can be your starting point. Files created with Open() will put the JSON filepath used in the returned File's Path field.

func ReadFile

func ReadFile(file io.Reader) *File

ReadFile returns a *goaseprite.File for a given file handle, in case you are opening the file yourself from another source (i.e. using the mobile asset package). This can also be your starting point,

func (*File) FinishedAnimation

func (asf *File) FinishedAnimation() bool

FinishedAnimation returns true if the animation is finished playing. When playing forward or backward, it returns true on the frame that the File loops the animation (assuming the File gets Update() called every game frame). When playing using ping-pong, this function will return true when a full loop is finished (when the File plays forwards and then backwards, and loops again).

func (*File) GetAnimation

func (asf *File) GetAnimation(animName string) *Animation

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

func (*File) GetFrameXY

func (asf *File) GetFrameXY() (int32, int32)

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

func (*File) GetSlice

func (asf *File) GetSlice(sliceName string) *Slice

GetSlice returns a Slice that has the name specified. Note that a File can have multiple Slices by the same name.

func (*File) HasAnimation

func (asf *File) HasAnimation(animName string) bool

HasAnimation returns true if the File has an Animation of the specified name.

func (*File) HasSlice

func (asf *File) HasSlice(sliceName string) bool

HasSlice returns true if the File has a Slice of the specified name.

func (*File) HitTag

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

HitTag returns if the File's playback just touched a tag by the specified name.

func (*File) HitTags

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

HitTags returns a list of tags the File just touched by the file's playback on this frame.

func (*File) IsPlaying

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

IsPlaying returns if the named animation is playing.

func (*File) LeftTag

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

LeftTag returns if the File's playback just left a tag by the specified name.

func (*File) LeftTags

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

LeftTags returns a list of tags the File's playback just passed through on the last frame.

func (*File) Play

func (asf *File) Play(animName string)

Play queues playback of the specified animation (assuming it's in the File).

func (*File) TouchingTag

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

TouchingTag returns if the File's playback is touching a tag by the specified name.

func (*File) TouchingTags

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

TouchingTags returns a list of tags the playback is currently touching.

func (*File) Update

func (asf *File) Update(deltaTime float32)

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

type Frame

type Frame struct {
	X        int32
	Y        int32
	Duration float32
}

Frame contains timing and position information for the frame on the spritesheet. Note that Duration is in seconds.

type Layer

type Layer struct {
	Name      string
	Opacity   uint8
	BlendMode string
}

Layer contains details regarding the layers exported from Aseprite, including the layer's name (string), opacity (0-255), and blend mode (string).

type Slice

type Slice struct {
	Name  string
	Data  string
	Color int64
	Keys  []*SliceKey
}

Slice represents a Slice (rectangle) that was defined in Aseprite and exported in the JSON file. Data by default is blank, but can be specified on export from Aseprite to be whatever you need it to be.

type SliceKey

type SliceKey struct {
	Frame      int32
	X, Y, W, H int32
}

SliceKey represents a Slice's size and position in the Aseprite file. An individual Aseprite File can have multiple Slices inside, which can also have multiple frames in which the Slice's position and size changes. The SliceKey's Frame indicates which frame the key is operating on.

Jump to

Keyboard shortcuts

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