Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/SolarLune/routine

Routine is a Go package for running routines, primarily for game development.
https://github.com/SolarLune/routine

coroutine ebiten events gamedev golang routine scheduling

Last synced: 8 days ago
JSON representation

Routine is a Go package for running routines, primarily for game development.

Awesome Lists containing this project

README

        

# routine 🐱 ➑️ πŸ˜ΊπŸ’¬

[![Go Reference](https://pkg.go.dev/badge/github.com/SolarLune/routine.svg)](https://pkg.go.dev/github.com/SolarLune/routine)

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](github.com/solarlune/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](https://github.com/stealthrocket/coroutine)._

## How do I get it?

`go get github.com/solarlune/routine`

## Example

```go

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~