Up to Main Index                           Up to Journal for January, 2015

                    JOURNAL FOR TUESDAY 13TH JANUARY, 2015
______________________________________________________________________________

SUBJECT: Things are just a sum of their attributes
   DATE: Tue 13 Jan 20:26:19 GMT 2015

I've been a little lax recently with the journal due to having too much fun
coding on WolfMUD-mini :) It's turning out quite different from what I had
planned[1].

I now have a core Thing type which is a slice of attribute interfaces:


  type thing struct {
    a []has.Attribute
  }

  type Attribute interface {
    Parent() Thing
    SetParent(Thing)
  }


To make implementing an Attribute easy there is an embeddable parent type
which just implements an Attribute for you:


  type parent struct {
    p has.Thing
  }

  func (p *parent) Parent() has.Thing {
    return p.p
  }

  func (p *parent) SetParent(t has.Thing) {
    p.p = t
  }


So we have a Thing which has a slice of Attributes and each attribute can
refer back to it's parent (the Thing) by calling Parent(). This enables
attributes to refer to each other if they need to.

A simple Attribute is the name attribute:


  type name struct {
    parent
    name   string
  }

  func NewName(n string) *name {
    return &name{parent{}, n}
  }

  func (n *name) Name() string {
    return n.name
  }


There are similar description and alias attributes. Putting everything
together we can create simple items such as:


  attr.Thing(
    attr.NewName("some cheese"),
    attr.NewDescription("This is a chunk of very hard cheese."),
    attr.NewAlias("cheese"),
  )


For something a little more complicated how about a mug of coffee?


  attr.Thing(
    attr.NewName("a mug"),
    attr.NewDescription("This is a large mug."),
    attr.NewAlias("mug"),
    attr.NewInventory(
      attr.Thing(
        attr.NewName("some coffee"),
        attr.NewDescription("This is some hot, strong coffee."),
      ),
    ),
  )


Hmmm, coffee, nice ;) You can also add, remove and modify attributes on the
fly at runtime. So you could, as a simple example, dip a dart into a poison
bottle and transfer the 'poison' attribute to the dart and create a poison
dart or a poison dagger, even a poison banana I guess...

We now have items created by stuffing attributes into things. What glues
everything together are the commands. They behave in a very Go like way now:
if a Thing has this or these attributes you can do this with it.

For now that's it. Next time I'll describe commands in detail and how they are
currently being implemented.

--
Diddymus

  [1] See towards the end of: ../../2014/12/22.html


  Up to Main Index                           Up to Journal for January, 2015