Wednesday, October 10, 2012

October 2012 challenge, Day 10

I only had a few hours to program over the weekend but I managed to get saving and continuing to work - which took much longer than I expected. If you're looking for advice on how to save objects to the SharedObject with Flash or haxe and nme, I ended up relying on the Memento Pattern.

Here's the relevant part of the Fruit class:

public function toMemento():Dynamic
 return {
  maxSpeed: maxSpeed,
  maxAge: maxAge,
  type: type,
  plantType: plantType,
  age: age,

public static function fromMemento(o:Dynamic):Fruit
 var c = new Fruit(o.x, o.y, o.type);
 c.velocity.x = o.vx;
 c.velocity.y = o.vy;
 c.maxSpeed = o.maxSpeed;
 c.maxAge = o.maxAge;
 c.age = o.age;
 c.plantType = o.plantType;
 return c;

One place where this was tricky was with the creatures' needs. Each creature has a list of needs that implement the Need interface. Since I don't know the real class at design time, it has to be handled differently. When creating the memento for needs, the class name is included and when converting the memento to the real object, a separate class takes the memento, looks at the need field, and has a switch statement that tells the appropriate class to create an instance from the memento. Here's the relevant part of the NeedToEatFruit class:

public function toMemento():Dynamic
 return {
  need: "NeedToEatFruit",
  target: target == null ? null : target.toMemento(),

public static function fromMemento(o:Dynamic):Need
 var n = new NeedToEatFruit(); = == null ? null : Fruit.fromMemento(;
 return n;

Apart from that, I made some simple placeholder graphics for the plants and got the basic plant lifecycle to work. There's twelve species of plants. Some float to the surface, some are buoyant enough to float around, and others are planted in place. The simple ones are just a sprite but the more complex ones grow separate leaves. They can reproduce by budding, spores, seeds, or fruit. When a fruit-eating animal eats a fruit, it will wait 5 seconds or so then drop a seed of that plant. And that's why the Creature class has a pooCountdown field.
Next up is animal graphics, lifecycle, and more needs as well as changing the world structure. It's currently Perlin noise but when targeting the mac, it runs out of memory trying to make it. Really odd.

No comments:

Post a Comment