I’ve always been interested in mimicing organic systems in Flash, and tolerance based behaviours in particular. When I built out my ProximityManager class, the first thing I thought of building with it was a simple aquatic ecosystem, here’s my first attempt….
This eco-system is only 6kb in size (well, 27kb with the sprites and background image) but manages to acheive some fairly complex, organic behaviours as a result of using a tolerance methodology. It contains 3 different organisms, each with their own set of tolerances and behaviours:
These little white sprites don’t really do much except drift in the tide. They only exists in the upper region of the ecosystem, and are the energy source all other organisms rely on (directly or indirectly). Over time, they lose energy and shrink until they die and are removed from the system. Larger, more energy rich Food drifts at higher depths, which rewards more aggressive Prey organisms.
These orange sprites are the most complex organism in the system. They survive by eating the Food near the surface, but predatorial pressure forces them to return to the depths when they aren’t hungry. As their energy levels drop, they shrink in size until they die and are removed from the system. When hungry, they will move to the surface and pursue any Food within their range.
They have a number of tolerances that govern their behaviour, including a depth preference, a tolerance for hunger and a tolerance for “fear”. Their speed also varies – higher speed improves their ability to chase food and escape enemies, but also increases the rate at which they burn energy.
When their energy needs are satisfied, they may move to the surface to try to reproduce asexually – if successful, they will create a single child which shares most of their traits, mutated slightly. In this manner, a form of evolution occurs in the system over time (you may notice that the Prey get faster and more skittish if you let it run for awhile).
When scared by being chased, or merely by the presence of a Predator (larger predators are more frightening), they will beat a hasty retreat to the depths. They are less aware of Predators while chasing a Food sprite, and are oblivious to threats while reproducing.
The large “jelly fish” sprites are Predators. They drift into the system randomly, and there can be up to 3 predators in the system at a time. Predators swim slowly and aimlessly, until they become hungry or a Prey organism catches their attention. Like Prey, different Predators have different tolerances for hunger, and different speeds. As their energy decreases, they will shrink until they die and are removed from the system.
When hungry, Predators will search for Prey, and then rush at them quickly in an attempt to eat them. While attacking, Predators will constantly look for a bigger, closer meal to chase. Predators are sensitive to pressure however, and cannot follow Prey below their individual depth tolerance. When a Predator catches a Prey organism, its energy is added to the Predators and the Prey is removed from the system.
The ecosystem does a pretty good job of maintaining balance. Prey population is controlled by predation and Food availability. Predators are limited by Prey population. I have seen the system die when 2 or 3 fast predators over-hunt the Prey, but hey, that happens in real life too.
I’ll probably tweak the numbers further, and add some more behaviours in the future, but I thought this was a decent first try.