ganim8

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: MIT Imports: 10 Imported by: 0

README

ganim8

ganim8 is an Animation library for Ebiten which is a golang version of anim8.

GoDoc

Example

type Game struct {
	prevUpdateTime time.Time
	anim           *ganim8.Animation
}

func NewGame() *Game {
	g := &Game{ prevUpdateTime: time.Now() }

	// Grids are just a convenient way of getting frames of the same size from a
	// single sprite texture.
	// animation frames are represented as groups of rectangles (image.Rectangle).
	// They need to know only 2 things: the size of frames and the size of
	// the image they will be applied to, and those are the first 4 parameters of NewGrid()
	// 
	// NewGrid() parameters accpets 4 - 6 parameters:
	// ganim8.NewGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, top)
	// "left" and "top" are optional, and both default to 0. They are "the left
	// and top coordinates of the point in the image where you want to put the
	// origin of coordinates of the grid".
	//
	// In this example, it make a grid with the frame size of {100,100} and
	// the image size of {500,600}
	grid := ganim8.NewGrid(100, 100, 500, 600)

	// Grids have one important method: Grid.GetFrames(...).
	// Grid.GetFrames() accepts an arbitrary number of parameters.
	// They can be either number or strings.
	// Each two numbers are interpreted as rectangle coordinates in
	// the format (column, row).
	// 
	// This way, grid.GetFrames(3, 4) will return the frame in column 3,
	// row 4 of the grid.
	// They can be more than just two: grid.GetFrames(1,1, 1,2, 1,3) will
	// return frames in {1,2}, {1,2} and {1,3} respectively.
	//
	// Using numbers for long rows or columns is tedious - so grid
	// also accpet strings indicating range plus a row/column index.
	// A row can be fetch by calling grid.GetFrames('range', rowNumber) and
	// a column by calling grid.GetFrames(columnNumber, 'range').
	//
	// It's also possible to combine both formats.
	// For example: grid.GetFrames(1, 3, 1, '1-3') will get the frame in {1,4}
	// plus the frames 1 to 3 in column 1.
	// 
	// The below code get frames 1 to 5 column in row 5
	frames := grid.GetFrames("1-5", 5)

	// Animations are groups of frames that are interchanged every now and then.
	// 
	// NewAnimation() accepts 3 parameters:
	// ganim8.NewAnimation(frames, durations, onLoop)
	// 
	// frames is an array of frames ([]*image.Rectangle). You could provide
	// your own slice if you wanted to, but using a grid to get them is very convenient.
	// 
	// durations is a time.Duration or []time.Duration or
	// map[string]time.Duration.
	// When it's a time.Duration, it represents the duration of all frames
	// in the animation.
	// When it's a slice, it can represent different durations for different frames.
	// You can specify durations for all frames individually or you can
	// specify duration for ranges of frames:
	// map[string]time.Duration{ "3-5" : 2 * time.Millisecond }
	// 
	// onLoop is function of the animetion methods or callback.
	// If ganim8.Nop is specified, it does nothing.
	// If ganim8.PauseAtEnd is specified, it pauses at the end of the animation.
	// It can be any function that follows the type "func(anim *Animation, loops int)".
	// The first parameter is the animation object and the second parameter is
	// the count of the loops that elapsed since the previous Animation.Update().
	g.anim = ganim8.NewAnimation(frames, 100*time.Millisecond, ganim8.Nop)

	return g
}

func (g *Game) Draw(screen *ebiten.Image) {
	screen.Clear()

	// Animation.Draw() draws the current frame of the animation.
	// It accepts screen image to draw on, source texture image, and draw options.
	// Draw options are x, y, angle (radian), scaleX, scaleY, originX and originY.
	// OriginX and OriginY are useful to draw the animation with scaling, centering,
	// rotating etc.
	// 
	// DrawOpts() is just a shortcut of creating DrawOption object.
	// It only needs the first 2 parameters x and y.
	// The rest of the parameters (angle, scaleX, scaleY, originX, orignY)
	// are optional.
	// If those are not specified, defaults values will be applied.
	// 
	// In this example, it draws the animation at the center of the screen.
	g.anim.Draw(screen, monsterImage, ganim8.DrawOpts(screenWidth/2, screenHeight/2, 0, 1, 1, 0.5, 0.5))
}

func (g *Game) Update() error {
	now := time.Now()

	// Animation.Update() updates the animation.
	// It receives time.Duration value and set the current frame of the animation. 
	// Each duration time of each frames can be customized for example like this:
	// ganim8.NewAnimation(
	//   grid.GetFrames("1-5", 5), 
	//   map[string]time.Duration {
	//     "1-2" : 100*time.Millisecond,
	//     "3"   : 300*time.Millisecond,
	//     "4-5" : 100*time.Millisecond,
	// })
	g.anim.Update(now.Sub(g.prevUpdateTime))

	g.prevUpdateTime = now
	return nil
}

source code

Output

The texture used in the example

Documentation

Index

Constants

View Source
const (
	Playing = iota
	Paused
)

Variables

This section is empty.

Functions

func Nop

func Nop(anim *Animation, loops int)

Nop does nothing.

func Pause

func Pause(anim *Animation, loops int)

Pause pauses the animation on loop finished.

func PauseAtEnd

func PauseAtEnd(anim *Animation, loops int)

PauseAtEnd pauses the animation and set the position to the last frame.

func PauseAtStart

func PauseAtStart(anim *Animation, loops int)

PauseAtStart pauses the animation and set the position to the first frame.

Types

type Animation

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

Animation represents an animation created from specified frames and an *ebiten.Image

func NewAnimation

func NewAnimation(frames []*image.Rectangle, durations interface{}, onLoop OnLoop) *Animation

NewAnimation returns a new animation object

durations is a time.Duration or a []time.Duration or a map[string]time.Duration. When it's a time.Duration, it represents the duration of all frames in the animation. When it's a []time.Duration, it can represent different durations for different frames. You can specify durations for all frames individually, like this: []time.Duration { 100 * time.Millisecond, 100 * time.Millisecond } or you can specify durations for ranges of frames: map[string]time.Duration { "1-2": 100 * time.Millisecond, "3-5": 200 * time.Millisecond }.

func (*Animation) Clone

func (anim *Animation) Clone() *Animation

Clone return a copied animation object.

func (*Animation) Draw

func (anim *Animation) Draw(screen *ebiten.Image, img *ebiten.Image, opts *DrawOptions)

Draw draws the animation with the specified option parameters.

func (*Animation) Durations

func (anim *Animation) Durations() []time.Duration

Duration returns the current durations of each frames.

func (*Animation) FlipH

func (anim *Animation) FlipH()

FlipH flips the animation horizontally.

func (*Animation) FlipV

func (anim *Animation) FlipV()

FlipV flips the animation horizontally.

func (*Animation) GoToFrame

func (anim *Animation) GoToFrame(position int)

GoToFrame sets the position of the animation and sets the timer at the start of the frame.

func (*Animation) Pause

func (anim *Animation) Pause()

Pause pauses the animation.

func (*Animation) PauseAtEnd

func (anim *Animation) PauseAtEnd()

PauseAtEnd pauses the animation and set the position to the last frame.

func (*Animation) PauseAtStart

func (anim *Animation) PauseAtStart()

PauseAtStart pauses the animation and set the position to the first frame.

func (*Animation) Position

func (anim *Animation) Position() int

Position returns the current position of the frame. The position counts from 1 (not 0).

func (*Animation) Resume

func (anim *Animation) Resume()

Resume resumes the animation

func (*Animation) Size

func (anim *Animation) Size() (int, int)

Size returns the size of the current frame.

func (*Animation) Status

func (anim *Animation) Status() Status

Status returns the status of the animation.

func (*Animation) Timer

func (anim *Animation) Timer() time.Duration

Timer returns the current accumulated times of current frame.

func (*Animation) TotalDuration

func (anim *Animation) TotalDuration() time.Duration

TotalDuration returns the total duration of the animation.

func (*Animation) Update

func (anim *Animation) Update(elapsedTime time.Duration)

Update updates the animation.

type DrawOptions

type DrawOptions struct {
	X, Y             float64
	Rotate           float64
	ScaleX, ScaleY   float64
	OriginX, OriginY float64
}

DrawOptions represents the option for Animation.Draw(). For shortcut, DrawOpts() function can be used.

func DrawOpts

func DrawOpts(x, y float64, args ...float64) *DrawOptions

DrawOpts returns DrawOptions pointer with specified settings. The paramters are x, y, rotate (in radian), scaleX, scaleY originX, originY. If scaleX and ScaleY is not specified the default value will be 1.0, 1.0. If OriginX and OriginY is not specified the default value will be 0, 0

type Grid

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

Grid represents a grid

func NewGrid

func NewGrid(frameWidth, frameHeight, imageWidth, imageHeight int, args ...int) *Grid

NewGrid returns a new grid with specified frame size, image size, and offsets (left, top).

Grids have only one purpose: To build groups of quads of the same size as easily as possible.

They need to know only 2 things: the size of each frame and the size of the image they will be applied to.

Grids are just a convenient way of getting frames from a sprite. Frames are assumed to be distributed in rows and columns. Frame 1,1 is the one in the first row, first column.

func (*Grid) G

func (g *Grid) G(args ...interface{}) []*image.Rectangle

G is a shortcut of GetFrames.

func (*Grid) GetFrames

func (g *Grid) GetFrames(args ...interface{}) []*image.Rectangle

GetFrames accepts an arbitrary number of parameters. They can be either numbers or strings.

Each two numbers are interpreted as quad coordinates in the format (column, row). This way, grid:getFrames(3,4) will return the frame in column 3, row 4 of the grid.

There can be more than just two: grid:getFrames(1,1, 1,2, 1,3) will return the frames in {1,1}, {1,2} and {1,3} respectively.

type OnLoop

type OnLoop func(anim *Animation, loops int)

OnLoop is callback function which representing one of the animation methods. it will be called every time an animation "loops".

It will have two parameters: the animation instance, and how many loops have been elapsed.

The value would be Nop (No operation) if there's nothing to do except for looping the animation.

The most usual value (apart from none) is the string 'pauseAtEnd'. It will make the animation loop once and then pause and stop on the last frame.

type Status

type Status int

Status represents the animation status.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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