routine

package module
v0.0.0-...-0fca884 Latest Latest
Warning

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

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

README

routine 🐱 ➡️ 😺💬

Go Reference

routine is a package for handling and executing routines, written in Go. The primary reason I made this was for creating cutscenes or running routines for gamedev with Go.

The general idea is that you create a Routine, and then define a Block. Blocks are "modes of execution". They contain groups of Actions, which are objects that are "units of execution" - they perform actions or manipulate the execution of a Block. Multiple Blocks can be active at any given time.

By utilizing Actions and Blocks, you can make up complex behaviors, or sequences of events.

Note: This package supercedes gocoro, which was my previous attempt at coroutines. If you want something with a more functional design and that is more traditionally coroutine-y, check out stealthrocket's coroutine.

How do I get it?

go get github.com/solarlune/routine

Example


import "github.com/solarlune/routine"
import "github.com/solarlune/routine/actions"

func main() {

    // Create a new Routine.
    routine := routine.New()

    // And now we begin to define our Blocks, which consist of Actions 
    // that execute in sequence.
    // A Block has an ID (in this case, a string set to "first block"), 
    // but the ID can be anything.
    routine.DefineBlock("first block", 
    
        // actions.NewFunction() returns a Function action. 
        // You provide it with a customizeable function.
        // Depending on the Flow object returned, the Block 
        // will either idle on this Action, or move on to another one.
        actions.NewFunction(func(block *routine.Block) routine.Flow { 
            fmt.Println("Hi!")
            return routine.FlowNext // FlowNext means to move to the next Action.
        }),

        actions.NewWait(time.Second * 2),

        actions.NewFunction(func() routine.Flow { 
            fmt.Println("OK, that's it. Goodbye!")
            return routine.FlowFinish 
        })
    
    )

    // And now we begin running the Routine. By default, the first defined Block
    // is activated when we run the Routine.
    routine.Run()
    
    for routine.Running() {

        // While the routine runs, we call Routine.Update(). This allows
        // the routine to execute, but also gives action back to the main
        // thread when it's idling so we can do other stuff, like take input
        // or update a game's screen.
        routine.Update()

    }

    // After Running() is over, we're done with the Routine.

}

Check out the examples directory for more in-depth examples.

Anything else?

Not really, that's it. Peace~

Documentation

Overview

routine is a package for creating sequences of events, primarily for game development in Golang.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	Init(block *Block)      // The Init function is called when a Action is switched to.
	Poll(block *Block) Flow // The Poll function is called every frame and can return a Flow, indicating what the Routine should do next.
}

Action is an interface that represents an object that can Action and direct the flow of a Routine.

type ActionCollectionable

type ActionCollectionable interface {
	Actions() []Action
}

ActionCollectionable identifies an interface for an Action that allows it to return a slice of Actions to be added to Blocks, Gates, or Collections in definition.

type ActionIdentifiable

type ActionIdentifiable interface {
	ID() any
}

ActionIdentifiable identifies an interface for an action that allows that Action to be used for jumping (as though it were a label).

type Block

type Block struct {
	ID      any
	Actions []Action
	// contains filtered or unexported fields
}

Block represents a block of Actions. Blocks execute Actions in sequence, and have an ID that allows them to be activated or deactivated at will by their owning Routine.

func (*Block) CurrentFrame

func (b *Block) CurrentFrame() int

CurrentFrame returns the current frame of the Block's execution of the currently executed Action. This increases by 1 every Routine.Update() call until the Block executes another Action.

func (*Block) Index

func (b *Block) Index() int

Index returns the index of the currently active Action in the Block.

func (*Block) JumpTo

func (b *Block) JumpTo(labelID any) int

JumpTo sets the Block's execution index to the index of a ActionLabel, using the label provided. If it finds the Label, then it will jump to and return that index. Otherwise, it will return -1.

func (*Block) Pause

func (b *Block) Pause()

Pause pauses the specified block, so that it isn't active when the Routine is run. When it is run again, it resumes execution at its current action.

func (*Block) Restart

func (b *Block) Restart()

Restart restarts the block.

func (*Block) Routine

func (b *Block) Routine() *Routine

Routine returns the currently running routine.

func (*Block) Run

func (b *Block) Run()

Run runs the specified block.

func (*Block) Running

func (b *Block) Running() bool

Running returns if the Block is active.

func (*Block) SetIndex

func (b *Block) SetIndex(index int)

SetIndex sets the index of the Action sequence of the Block to the value given. This effectively "sets the playhead" of the Block to point to the Action in the given slot.

func (*Block) Stop

func (b *Block) Stop()

Stop stops the Block, so that it restarts when it is run again.

type Flow

type Flow uint8

Flow is simply a uint8, and represents what a Routine should do following a Action's action.

const (
	// FlowIdle means that the Routine should cycle and do the same Action again the following Update() cycle.
	FlowIdle Flow = iota
	// FlowNext means that the Routine should move on to the next Action in the Block.
	// If this is returned from the last Action in a Block, the Block will loop.
	FlowNext
	// FlowFinish indicates the Block should finish its execution, deactivating afterwards.
	FlowFinish
)

type Properties

type Properties map[any]any

Properties represents a kind of "local memory" for an Execution object.

func (*Properties) Clear

func (p *Properties) Clear()

Clear clears the properties map.

func (*Properties) Delete

func (p *Properties) Delete(keyName string)

Delete deletes a key out of the properties map.

func (Properties) Get

func (p Properties) Get(propName any) any

Get returns the value associated with the given property identifier.

func (Properties) Has

func (p Properties) Has(propName any) bool

Has returns if the Properties object has a property associated with the given identifier.

func (*Properties) Init

func (p *Properties) Init(propName any, toValue any)

Init will initialize a property by the given name with the given value if it doesn't already exist.

func (*Properties) Set

func (p *Properties) Set(propName any, value any)

Set sets the Properties object with the given property name to the value passed.

type Routine

type Routine struct {
	Blocks []*Block
	// contains filtered or unexported fields
}

Routine represents a container to run Blocks of code.

func New

func New() *Routine

New creates a new Routine.

func (*Routine) BlockByID

func (r *Routine) BlockByID(id any) *Block

BlockByID returns any Block found with the given ID. If no Block with the given id is found, nil is returned.

func (*Routine) Define

func (r *Routine) Define(id any, Actions ...Action) *Block

Define defines a Block using the ID given and the list of Actions provided and adds it to the Routine. The ID can be of any comparable type. Define returns the new Block as well. If a block with the given blockID already exists, Define will remove the previous one.

func (*Routine) Pause

func (r *Routine) Pause(blockIDs ...any)

Pause pauses Blocks with the given IDs. If no block IDs are given, then all blocks contained in the Routine are paused.

func (*Routine) Properties

func (r *Routine) Properties() *Properties

Properties returns the Properties object for the Routine.

func (*Routine) Restart

func (r *Routine) Restart(blockIDs ...any)

Restart restarts Blocks with the given IDs. If no block IDs are given, then all blocks contained in the Routine are restarted.

func (*Routine) Run

func (r *Routine) Run(blockIDs ...any)

Run runs Blocks with the given IDs. If no block IDs are given, then all blocks contained in the Routine are run.

func (*Routine) Running

func (r *Routine) Running(ids ...any) bool

Running returns true if at least one Block is running with at least one of the given IDs in the Routine. If no IDs are given, then any running Blocks will return.

func (*Routine) Stop

func (r *Routine) Stop(blockIDs ...any)

Stop stops Blocks with the given IDs. If no block IDs are given, then all blocks contained in the Routine are stopped.

func (*Routine) Update

func (r *Routine) Update()

Update updates the Routine - this should be called once per frame.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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