animate

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

Animate

Animate is a simple sprite animation module made to work with Ebitengine.

Check Out the Example

go run github.com/KalebHawkins/animate/examples/basic@latest

Installation

Add the package to your project.

go get github.com/KalebHawkins/animate

Import the package into your project.

import (
    "github.com/KalebHawkins/animate"
)

Usage

Before scrolling through the readme, I highly recommend looking through the example to get a better idea of how to use this module.

To start off, allocate a slice of Animations. After that, you will define each animation. Then append individual animations to your slice.

// Allocate a slice to hold animation definitions.
animations := make(animate.Animations, 0)

// Define each animation.
idleAnimation := &animate.Animation{
    StartFrame: image.Rectangle{
        Min: image.Point{
            X: 0,
            Y: 0,
        },
        Max: image.Point{
            X: 32,
            Y: 32,
        },
    },
    FrameCount: 5,
    Speed:      8,
    Direction:  animate.Horizontal,
}

lookBehind := &animate.Animation{
    StartFrame: image.Rectangle{
        Min: image.Point{
            X: 0,
            Y: 32,
        },
        Max: image.Point{
            X: 32,
            Y: 32,
        },
    },
    FrameCount: 14,
    Speed:      8,
    Direction:  animate.Horizontal,
}

Once the animations are defined you can create an Animator. The Animator is responsible for playing, pausing, and resuming animations. Once declared, add your animations to the animator instance.

// Declare your animator.
animator := animate.NewAnimator()

// Add your animations. animator.Add() will return an error message if the 
// animation name already exists within that instance.
if err := animator.Add("idle", idleAnimation); err != nil {
    panic(err)
}

if err := animator.Add("look", lookBehind); err != nil {
    panic(err)
}

Now call your animator's Play() method every frame.

func (g *Game) Update() error {
    animator.Play("idle")
}

To stop the currently selected animation then use the Pause() method.

animator.Pause()

If you want to toggle an animation you can do so using the sample below.

if inpututil.IsKeyJustReleased(ebiten.KeySpace) {
    if animator.Paused() {
        animator.Resume()
    } else {
        animator.Pause()
    }
}

Spritesheet was created by Elthen's Pixel Art Shop.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAnimationExists = errors.New("animation already exists in animator instance")

ErrAnimationExists is an error returned if an Animation already exists in the animator instance.

View Source
var ErrAnimationNotFound = errors.New("animation was not found")

ErrAnimationNotFound is an error returned if an Animation called by string is not found in the animator instance.

Functions

This section is empty.

Types

type Animation

type Animation struct {
	// StartFrame is the frame's starting x and y location, width and height.
	StartFrame image.Rectangle
	// FrameCount the number of frames for a particular animation.
	FrameCount int
	// speed is the speed of the animation.
	Speed int
	// Direction is the Direction the spritesheet should be read.
	// This should be either Horizontal or Vertical.
	Direction Direction
	// contains filtered or unexported fields
}

Animation defines a spritesheet's animation.

func (*Animation) Frame added in v0.2.0

func (a *Animation) Frame() image.Rectangle

Frame returns a rect repesenting the current frame of the animation.

func (*Animation) FrameHeight

func (a *Animation) FrameHeight() int

FrameHeight returns the height of the frame.

func (*Animation) FrameWidth

func (a *Animation) FrameWidth() int

FrameWidth returns the width of the frame.

type Animations added in v0.2.0

type Animations []*Animation

Animations repesents a collection of playable animations.

type Animator

type Animator interface {
	// Add will add an animation to the map of the animator instance.
	//
	// If an animation already exists in the map this function will
	// return an ErrAnimationExists error.
	Add(animationName string, animation *Animation) error

	// Play will play the animation, iterating through each frame.
	//
	// This function should be called every frame in your update function.
	//
	// If the animation name passed is not found in the animator's map an
	// ErrAnimationNotFound error is returned.
	Play(animationName string) error

	// Pause will stop an animation from playing until the next call to Resume.
	Pause()

	// Resume will start an animation until the next call to Pause.
	//
	// Note: you must still call the `Play()` method to iterate through the
	// frames of an animation.
	Resume()

	// Animation returns the currently selected animation.
	//
	// If no animation has been set using the Play() method
	// a zero valued Animation will be returned.
	Animation() *Animation

	// Paused returns true if the animator's current animation is paused, otherwise it returns false.
	Paused() bool
}

Animator is an interface that acts as a handler for animations.

func NewAnimator added in v0.2.0

func NewAnimator() Animator

NewAnimator returns a new Animator interface.

type Direction added in v0.2.0

type Direction int

Direction repestents the Direction in which the animation is read from a spritesheet.

const (
	// Horizontal, when provided to an Animation's Direction, will read the spritesheet from left to right.
	Horizontal Direction = iota
	// Vertical, when provided to an Animation's Direction, will read the spritesheet from top to bottom.
	Vertical
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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