tetraflow

package module
v0.0.0-...-557c184 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

TetraFlow 🚗💨

TetraFlow is a package specifically for creating a simple object-oriented game engine-like flow for Tetra3D projects.

How does it work

The general approach is to load in your Tetra3D library (a collection of Scenes), create an *tetraflow.Engine, and then register a function to create game objects (IReceivers) for each relevant node. TetraFlow will register these receivers in the connected nodes' INode.Data() slots. You then create Stages for each Scene in your Scene stack (e.g. one for your GUI, one for your game, and one for your background skybox), setting relevant scenes in each Stage. When you call Engine.Update(), the Engine object will continuously message IReceivers on engine events (when a node is added or removed from the scene tree, when the engine updates, etc) for all Stages.

To summarize, the Engine updates Stages, Stages contain Tetra3D Scenes, and you can create game logic structs to be IReceivers, which listen for the Engine's updates.

How to get it

go get github.com/solarlune/tetraflow

How to use it


func (g *Game) Init() {

    g.Engine = tetraflow.NewEngine(
        
        g.Library,

        func(node tetra3d.INode) tetraflow.IReceiver {

            // Create relevant engine receivers for a game object depending on what logic the game object should have
            if node.Properties().Has("gameobject") {
                switch node.Properties().Get("gameobject").AsString() {
                case "player":
                    return NewPlayer(node)
                case "enemy":
                    return NewEnemy(node)
                }
            }

            return nil

        },
    )

    gameStage := g.Engine.AddStage("Game")
    gameStage.SetScene("Level 1")

}

func (g *Game) Update() error {

    // Engine.Update() returns an error if you call Engine.Quit().
    return g.Engine.Update()
}

Messages

TetraFlow's engine flow is based around the concept of message passing.

The general idea is to have your game objects fulfill the tetraflow.IReceiver interface, which just defines a single function - ReceiveMessage(messages.IMessage). By declaring this function, your game object struct becomes something that can receive messages from the Engine instance.

The Engine will send a variety of messages as nodes live in a scene's node hierarchy. These messages can indicate changes in the node or the scene's life-cycle; for example, a message could indicate that the node is being updated (once per game frame), removed from the scene tree, or that the scene itself has just begun or just ended execution. By listening for these messages, GameObjects can behave appropriately:


type Player struct {
    Node tetra3d.INode
}

func NewPlayer(node tetra3d.INode) *Player {
    return &Player{ Node: node }
}

// This function allows Player to become an IReceiver.
func (player *Player) ReceiveMessage(msg messages.IMessage) {

    switch message := msg.(type) {
        case messages.MessageOnUpdate:
            // Update message, happens once per game-frame
        case messages.MessageOnAdd:
            // OnAdd, called once when the node is added to the scene tree.
    }

    if msg.Type() == messages.MessageTypeOnUpdate {
        // Update message, happens every game frame
    }

}

You can switch off against either IMessage.Type(), or by the type of the message objects itself (switch message := msg.(type)) to distinguish between IMessages. The latter is the more elegant approach, as this allows you to access custom message values like MessageOnSceneEnd.Scene.

By using messages, one can also make their own custom messages to pass around, by simply making a custom struct that implements IMessage. You can send messages to target objects, or to the entire engine using Engine.SendMessage().

Message Subscription

Messages can be filtered down by making your object implement ISubscriber, which entails creating a Subscribe() messages.MessageType function. This function returns the MessageTypes that should be received for that object. If you want to subscribe to more than one message type, you can simply add them together, as the types are bitwise values.


func (player *Player) Subscribe() messages.MessageType {
    return messages.TypeUpdate + messages.TypeSceneStart // Only subscribe to update messages or messages indicating the start of the current scene
}

Todo

Functionality

  • - Message subscription so all Receivers aren't receiving all Messages

Message types

  • - On Scene Start / End (OnSceneStart())
  • - Collision checking (OnCollision(other IBoundingObject))?
  • - Input messages?
  • - Scene Tree change

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EngineQuit = errors.New("quit")

Functions

This section is empty.

Types

type Engine

type Engine struct {
	Stages []*Stage
	// contains filtered or unexported fields
}

Engine represents an engine for a Tetra3D scene.

func NewEngine

func NewEngine(library *tetra3d.Library, registryFunction ReceiverRegistryFunction) *Engine

NewEngine creates a new Engine. library is the source library to be used for instantiating scenes from. ReceiverRegistryFunction is a function to return an IGameObject (something that can receive messages) for relevant INodes.

func (*Engine) AddLibrary

func (engine *Engine) AddLibrary(libraries ...*tetra3d.Library)

AddLibrary adds libraries to use for scene creation to the Engine.

func (*Engine) AddStage

func (engine *Engine) AddStage(name string) *Stage

AddStage creates a new Stage with the specified name. The Stage would then need to be set with a Scene to create. After that, it will be instantiated and begin updating on the next Engine.Update() call.

func (*Engine) FindStageByName

func (engine *Engine) FindStageByName(stageName string) *Stage

FindStageByName searches for a Stage by the name of the scene that should be running in it. If the scene isn't found, the function returns nil.

func (*Engine) FindStageByScene

func (engine *Engine) FindStageByScene(scene *tetra3d.Scene) *Stage

FindStageByScene searches for a Stage by the scene that should be running in it. If the scene isn't found, the function returns nil.

func (*Engine) Quit

func (engine *Engine) Quit()

Quit queues the engine to quit gracefully.

func (*Engine) RemoveStage

func (engine *Engine) RemoveStage(sceneName string)

RemoveStage queues a Stage for removal, and is done at the end of the Engine's update cycle.

func (*Engine) SendMessage

func (engine *Engine) SendMessage(msg messages.IMessage)

SendMessage sends the specified message to all Stages in the Engine.

func (*Engine) SendMessageToTargets

func (engine *Engine) SendMessageToTargets(msg messages.IMessage, targets ...tetra3d.INode)

SendMessageToTargets sends the specified message to all of the target Nodes, assuming they have IReceivers implemented in their Data() slots.

func (*Engine) Time

func (engine *Engine) Time() float64

Time returns the total time that the Engine has been running in seconds.

func (*Engine) Update

func (engine *Engine) Update() error

Update updates all relevant game object-implementers in the Engine's Scene's node hierarchy, and enables all of their callbacks (OnUpdate(), OnRemove(), etc). Update will return an error if it has been queued to quit.

type IReceiver

type IReceiver interface {
	ReceiveMessage(msg messages.IMessage)
}

type ReceiverRegistryFunction

type ReceiverRegistryFunction func(node tetra3d.INode) IReceiver

ReceiverRegistryFunction is a function to return something that implements IOnUpdate or IOnRemove (i.e. something game object-like or component-like) for relevant nodes.

type Stage

type Stage struct {
	Name   string
	Active bool

	Speed           float64 // Speed represents the speed of the engine's playback (for animations, delta time values, and timers).
	DebugUpdateTime time.Duration
	// contains filtered or unexported fields
}

Stage represents a layer or box in which Scenes are created, updated, replaced, and ended. The scene might change, but the stage generally always exists (i.e. there's one for GUI elements, one for the game world, and one for background skyboxes).

func (*Stage) CurrentScene

func (stage *Stage) CurrentScene() *tetra3d.Scene

CurrentScene returns the currently running Scene within the Stage. If the Stage is inactive or the scene is ended (or the Stage has not instantiated a scene yet), this function will return nil.

func (*Stage) End

func (stage *Stage) End()

End stops updating the Scene.

func (*Stage) Pause

func (stage *Stage) Pause()

Pause pauses the Stage.

func (*Stage) Paused

func (stage *Stage) Paused() bool

Paused returns if the Stage is paused.

func (*Stage) Remove

func (stage *Stage) Remove()

Remove queues the Stage for removal from its owning Engine instance.

func (*Stage) Restart

func (stage *Stage) Restart()

Restart restarts the Stage on the next Update() frame, effectively re-instantiating the Scene in the Stage. This calls End on the Stage before doing so.

func (*Stage) SetScene

func (stage *Stage) SetScene(scene *tetra3d.Scene) error

SetScene sets the source scene to be run If the Stage is running, this will reset the currently running scene. If no scene of the specified name is found, this function will return an error.

func (*Stage) SetSceneByName

func (stage *Stage) SetSceneByName(sceneName string) error

SetSceneByName sets the scene to be run by name, searching through the libraries associated with the Engine. If the Stage is running, this will reset the currently running scene. If no scene of the specified name is found, this function will return an error.

func (*Stage) TimerSystem

func (stage *Stage) TimerSystem() *ebitick.TimerSystem

TimerSystem returns the Stage's built-in TimerSystem for timing events.

func (*Stage) Unpause

func (stage *Stage) Unpause()

Unpause unpauses the Stage.

func (*Stage) Update

func (stage *Stage) Update()

Update updates the elements within the Stage.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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