core

package
v0.0.0-...-1fef251 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package core provides the building blocks for creating domain-specific behavior tree nodes.

Index

Constants

View Source
const (
	CategoryInvalid   = Category("invalid")
	CategoryComposite = Category("composite")
	CategoryDecorator = Category("decorator")
	CategoryLeaf      = Category("leaf")
)

A list of behavior tree node categories.

Variables

This section is empty.

Functions

func ErrInvalidType

func ErrInvalidType(name string) error

func ErrParamNotFound

func ErrParamNotFound(name string) error

Types

type BaseNode

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

BaseNode contains properties shared by all categories of node. Do not use this type directly.

func (*BaseNode) GetCategory

func (n *BaseNode) GetCategory() Category

GetCategory returns the category of this node.

func (*BaseNode) GetStatus

func (n *BaseNode) GetStatus() Status

GetStatus returns the status of this node.

func (*BaseNode) SetStatus

func (n *BaseNode) SetStatus(status Status)

SetStatus sets the status of this node.

type Category

type Category string

Category denotes whether a node is a composite, decorator or leaf.

type Composite

type Composite struct {
	*BaseNode
	Children     []Node
	CurrentChild int // TODO - move into instance nodes
}

Composite is the base type for any specific composite node. Such a node may be domain-specific, but usually one of the common nodes will be used, such as Sequence or Selector.

func NewComposite

func NewComposite(name string, children []Node) *Composite

NewComposite creates a new composite base node.

func (*Composite) GetChildren

func (c *Composite) GetChildren() []Node

GetChildren returns a list containing the children of the composite node.

func (*Composite) String

func (c *Composite) String() string

String returns a string representation of the composite node.

type Context

type Context struct {
	Owner interface{}
	Data  interface{}
}

Context is data implicitly shared by all nodes in a behavior tree since a Context instance is propagated through the tree each tick.

func NewContext

func NewContext(owner, data interface{}) *Context

NewContext creates context containing references to an owner and a store.

type Decorator

type Decorator struct {
	*BaseNode
	Child  Node
	Params Params
}

Decorator is the base type for any specific decorator node. Such a node may be domain-specific, but usually one of the common nodes will be used, such as Inverter or Repeater. Each decorator node has Params: a key-value map used for setting variables for a specific decorator node, for instance Params{"n": 5} for a Repeater node or Params{"ms": 500} for a Delayer node.

func NewDecorator

func NewDecorator(name string, params Params, child Node) *Decorator

NewDecorator creates a new decorator base node.

func (*Decorator) GetChildren

func (d *Decorator) GetChildren() []Node

GetChildren returns a list containing the only child of the decorator node.

func (*Decorator) String

func (d *Decorator) String() string

String returns a string representation of the decorator node.

type Leaf

type Leaf struct {
	*BaseNode
	Params  Params
	Returns Returns
}

Leaf is the base type for any specific leaf node (domain-specific). Each leaf node has Params: data keys that the implementation imports and Returns: data keys that the implementation exports.

func NewLeaf

func NewLeaf(name string, params Params, returns Returns) *Leaf

NewLeaf creates a new leaf base node.

func (*Leaf) GetChildren

func (a *Leaf) GetChildren() []Node

GetChildren returns an empty list of Node, since a leaf has no children. This method is required for Leaf in order to implement Node.

func (*Leaf) String

func (a *Leaf) String() string

String returns a string representation of the leaf node.

type Node

type Node interface {

	// Automatically implemented by embedding a pointer to a
	// Composite, Decorator or Leaf node in the custom node.
	GetStatus() Status
	SetStatus(Status)
	GetCategory() Category
	GetChildren() []Node
	String() string

	// Must be implemented by the custom node.
	Enter(*Context)
	Tick(*Context) Status
	Leave(*Context)
}

The Node interface must be satisfied by any custom node.

type Params

type Params map[string]interface{}

Params denotes a list of parameters to a node.

func (Params) GetInt

func (p Params) GetInt(key string) (int, error)

func (Params) GetString

func (p Params) GetString(key string) (string, error)

type Returns

type Returns = Params

Returns is just a type alias for Params.

type Status

type Status int

Status denotes the return value of the execution of a node.

const (
	StatusInvalid Status = iota
	StatusSuccess
	StatusFailure
	StatusRunning
)

A list of possible statuses.

func Update

func Update(node Node, ctx *Context) Status

Update updates a node by calling its Enter method if it is not running, then its Tick method, and finally Leave if it is not still running.

Jump to

Keyboard shortcuts

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