Documentation ¶
Overview ¶
Package rpg provides a base API for a role playing game.
Example ¶
package main import ( "fmt" "github.com/Rnoadm/rpg" "github.com/Rnoadm/rpg/history" "io" "io/ioutil" "os" ) func printContainers(s *rpg.State) { for _, id := range s.ByComponent(rpg.ContainerType) { p := s.Get(id) for _, o := range p.Component(rpg.ContainerType).(*rpg.Container).Contents() { fmt.Println(p.Component(rpg.NameType), "has", o.Component(rpg.NameType)) } } } func main() { f, err := ioutil.TempFile(os.TempDir(), "rnoadm") if err != nil { panic(err) } defer os.Remove(f.Name()) defer f.Close() h := history.NewHistory(f) global := rpg.NewState() var personA, personB, itemA, itemB rpg.ObjectIndex if !global.Atomic(func(s *rpg.State) bool { var pa, pb, ia, ib *rpg.Object personA, pa = s.Create(rpg.NameFactory("person A"), rpg.ContainerFactory) personB, pb = s.Create(rpg.NameFactory("person B"), rpg.ContainerFactory) itemA, ia = s.Create(rpg.NameFactory("item A")) itemB, ib = s.Create(rpg.NameFactory("item B")) if !pa.Component(rpg.ContainerType).(*rpg.Container).Add(ia) { panic("unreachable") } if !pb.Component(rpg.ContainerType).(*rpg.Container).Add(ib) { panic("unreachable") } return true }) { panic("unreachable") } err = h.Append(global) if err != nil { panic(err) } printContainers(global) fmt.Println("Trade succeeded:", global.Atomic(func(s *rpg.State) bool { pa, pb, ia, ib := s.Get(personA), s.Get(personB), s.Get(itemA), s.Get(itemB) if !pa.Component(rpg.ContainerType).(*rpg.Container).Remove(ia) { return false } if !pb.Component(rpg.ContainerType).(*rpg.Container).Remove(ib) { return false } if !pa.Component(rpg.ContainerType).(*rpg.Container).Add(ib) { return false } if !pb.Component(rpg.ContainerType).(*rpg.Container).Add(ia) { return false } return true })) err = h.Append(global) if err != nil { panic(err) } printContainers(global) fmt.Println() h.Reset() for { s, err := h.Seek(1, history.SeekCur) if err == io.EOF { break } if err != nil { panic(err) } fmt.Println("Forward:", h.Tell()) printContainers(s) } fmt.Println() h.Reset() for { s, err := h.Seek(-1, history.SeekCur) if err == io.EOF { break } if err != nil { panic(err) } fmt.Println("Reverse:", h.Tell()) printContainers(s) } }
Output: person A has item A person B has item B Trade succeeded: true person A has item B person B has item A Forward: 0 person A has item A person B has item B Forward: 1 person A has item B person B has item A Reverse: 1 person A has item B person B has item A Reverse: 0 person A has item A person B has item B
Index ¶
- Variables
- func RegisterComponent(f ComponentFactory) reflect.Type
- type Component
- type ComponentFactory
- type Container
- func (c *Container) Add(v *Object) bool
- func (c *Container) ByComponent(t reflect.Type) []*Object
- func (c *Container) Clone(o *Object) Component
- func (c *Container) Contents() []*Object
- func (c *Container) GobDecode(data []byte) (err error)
- func (c *Container) GobEncode() (data []byte, err error)
- func (c *Container) Remove(v *Object) bool
- type Location
- func (l *Location) Clone(o *Object) Component
- func (l *Location) Dist(x, y, z int64) int64
- func (l *Location) Equal(o *Location) bool
- func (l *Location) Get() (x, y, z int64)
- func (l *Location) GobDecode(data []byte) (err error)
- func (l *Location) GobEncode() (data []byte, err error)
- func (l *Location) Set(x, y, z int64)
- type Message
- type Messages
- type Name
- type Object
- func (o *Object) Component(t reflect.Type) Component
- func (o *Object) ComponentAny(t reflect.Type) Component
- func (o *Object) Create(factories ...ComponentFactory) (ObjectIndex, *Object)
- func (o *Object) GobDecode(data []byte) (err error)
- func (o *Object) GobEncode() (data []byte, err error)
- func (o *Object) ID() ObjectIndex
- func (o *Object) Modified()
- func (o *Object) Parent() *Object
- func (o *Object) State() *State
- type ObjectIndex
- type Resources
- type State
- func (s *State) Atomic(f func(*State) bool) bool
- func (s *State) ByComponent(t reflect.Type) []ObjectIndex
- func (s *State) Create(factories ...ComponentFactory) (id ObjectIndex, o *Object)
- func (s *State) Delete(id ObjectIndex)
- func (s *State) Get(id ObjectIndex) *Object
- func (s *State) GobDecode(data []byte) (err error)
- func (s *State) GobEncode() (data []byte, err error)
- func (s *State) IDs() []ObjectIndex
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrStateParent = errors.New("rpg: cannot encode a child State") ErrStateVersion = errors.New("rpg: unrecognized State version") ErrObjectStateless = errors.New("rpg: cannot decode Object directly") ErrObjectVersion = errors.New("rpg: unrecognized Object version") ErrContainerVersion = errors.New("rpg: unrecognized Container version") ErrContainerOutOfOrder = errors.New("rpg: Container is out of order") ErrResourcesVersion = errors.New("rpg: unrecognized Resources version") ErrResourcesDuplicate = errors.New("rpg: duplicate key in Resources") ErrLocationVersion = errors.New("rpg: unrecognized Location version") ErrMessagesVersion = errors.New("rpg: unrecognized Messages version") )
var ContainerType = RegisterComponent(ContainerFactory)
ContainerType can be used with Object.Component to retrieve a Container.
var LocationType = RegisterComponent(LocationFactory)
LocationType can be used with Object.Component to retrieve a Location.
var MaxMessages = 200
var MessagesType = RegisterComponent(MessagesFactory)
var NameType = RegisterComponent(NameFactory(""))
var ResourcesType = RegisterComponent(ResourcesFactory)
Functions ¶
func RegisterComponent ¶
func RegisterComponent(f ComponentFactory) reflect.Type
RegisterComponent allows a ComponentFactory to be used with State.Create. It returns the reflect.Type of the Component which can be used in Object.Component.
Types ¶
type Component ¶
type Component interface { // Clone duplicates this Component and replaces the cloned Component's Object // with the given Object. Clone(*Object) Component }
Component represents a feature of an Object.
func ContainerFactory ¶
ContainerFactory is a ComponentFactory.
func LocationFactory ¶
LocationFactory is a ComponentFactory.
func MessagesFactory ¶
func ResourcesFactory ¶
type ComponentFactory ¶
ComponentFactory constructs a Component for use with the given Object. The underlying type of the Component must be a pointer type. The Object is nil when the function is called from RegisterComponent.
func NameFactory ¶
func NameFactory(name string) ComponentFactory
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is a Component that holds references to other Objects.
func (*Container) Add ¶
Add adds v to c, returning false if v is already in c. If v has a Location, it is modified to the containing object's Location.
func (*Container) ByComponent ¶
ByComponent returns the sorted set of Objects that have Component t in c.
type Location ¶
type Location struct {
// contains filtered or unexported fields
}
Location represents the position of an Object.
type Messages ¶
type Messages struct {
// contains filtered or unexported fields
}
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object represents a person, place, or thing in a State.
func (*Object) Component ¶
Component returns the Component of the given type if one exists in this Object.
func (*Object) ComponentAny ¶
ComponentAny returns the Component of the given type if one exists in this Object. If o.Parent is non-nil, ComponentAny will try to return the parent's component, recursively. The returned Component should not be modified.
func (*Object) Create ¶
func (o *Object) Create(factories ...ComponentFactory) (ObjectIndex, *Object)
Create is the same as State.Create but the Object derives from o.
func (*Object) Modified ¶
func (o *Object) Modified()
Modified notifies o that one of its Components has been modified. This is required for State.Atomic to function properly.
type Resources ¶
type Resources struct {
// contains filtered or unexported fields
}
type State ¶
type State struct {
// contains filtered or unexported fields
}
State represents a set of Object that can be modified concurrently using compare-and-set.
func (*State) Atomic ¶
Atomic calls f and tries to apply its changes. This is the only way a State should be modified. f may be called multiple times if other calls to Atomic are being processed at the same time. Returning false from f causes Atomic to return false without applying the changes.
This does not currently work recursively, but it may in the future.
func (*State) ByComponent ¶
func (s *State) ByComponent(t reflect.Type) []ObjectIndex
ByComponent returns a sorted set of IDs of objects that have the given component type.
func (*State) Create ¶
func (s *State) Create(factories ...ComponentFactory) (id ObjectIndex, o *Object)
Create initializes a new Object and returns it and its ObjectIndex. The factories must not be duplicate and must be pre-registered. The id is unique for all Objects in this State heirarchy.
func (*State) Delete ¶
func (s *State) Delete(id ObjectIndex)
Delete removes an object from the State. Future calls to Get will return nil. If the object is referenced anywhere, Bad Things™ will happen.
func (*State) Get ¶
func (s *State) Get(id ObjectIndex) *Object
Get returns the Object identified by id. The object is specific to this State.
func (*State) IDs ¶
func (s *State) IDs() []ObjectIndex
IDs returns the set of ObjectIndex accessible from s in ascending order.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package gui provides a text/graphical rendering system for tile-based games.
|
Package gui provides a text/graphical rendering system for tile-based games. |
Package history provides a seekable sequence of rpg.State.
|
Package history provides a seekable sequence of rpg.State. |