Virtual Life App with Health
Feature Specification
- Make living world items’ life based on health.
- Plants will have the following behavior on each
turn:
- Reduce health by one and check if still alive.
- Chance to increase or decrease health.
- If health above a certain level, chance to reproduce.
- Reproduce only to an empty space.
- Reduce health if it does reproduce.
- Animals will have the following behavior on each
turn:
- Reduce health by one and check if still alive.
- If health above a certain level, chance to reproduce:
- Reproduce only to an empty space.
- Reduce health if it does reproduce.
- If health above a certain level, chance to move:
- Move only to an empty space.
- Reduce health if it does move.
- Chance to eat a plant. If it does eat:
- Take a percentage of the plant's health:
- Add health to the animal.
- Subtract health from the plant.
- Carnivores will have the following behavior on each
turn
- Reduce health by one and check if still alive.
- If health above a certain level, chance to reproduce:
- Reproduce only to an empty space.
- Reduce health if it does reproduce.
- If health above a certain level, chance to move:
- Move only to an empty space.
- Reduce health if it does move.
- Chance to eat an animal. If it does eat:
- Take part or all of the animal’s health:
- Add health to the carnivore.
- Subtract health from the animal.
Code Design
- Create a LivingWorldItem
prototyped object that inherits from world item.
- Will have a health
property.
- May have common living world item methods!
- Plant, animal, and carnivore will inherit from the living
world item. Optionally the carnivore can inherit from the animal.
- Update your code to provide health information (and/or
other properties of your choosing) to the WorldView, so that you can
represent the vitality (e.g. vibrant versus sickly) on the screen visually.
For example, when health is above 75% or below 25%, you could add an
appropriate CSS class to your item to provide a filter or alternate
appearance.
- In doing the above, do not modify the animator to provide
the entire world or its grid of WorldItems to the WorldView rendering
cycle. This will not scale well and demonstrates why the MVC pattern is
so important as you increase the number of WorldItems and refresh rate on
the screen. You may want to change the definition of your worldPlan
string so that it includes health information, or better yet – come up
with an alternate object called the viewGrid which contains only the vital
pieces of information. The latter approach is easier to modify later, if
additional properties are needed.
- Use your imagination. Can you create a world which is
self-sustaining in perpetuity? You might need to consider the following
actions:
- Animals or carnivores plant a tree!
- When carnivores reproduce, there is some chance that the
offspring will be herbivorous.
- Plants technically can move as well, even if
relatively slowly. Allow them to move a bit.
- Based on your judgment, you may add additional
controlling/moderating factors.
- Here’s a sample itemOptions object:
let itemOptions = {
wall: { type: "wall",
symbol: "w", buildChance: .1 },
plant: { type: "plant",
symbol: "p", buildChance: 0.02,
ableToMoveOn: [
"empty" ], ableToEat: [],
healthToReproduce: 57,
healthToMove: 60 },
animal: { type: "animal",
symbol: "a", buildChance: 0.02,
ableToMoveOn: [
"empty" ], ableToEat: [ "plant" ],
plantChance: 0.07,
healthToReproduce: 62, healthToMove: 62 },
carnivore: { type:
"carnivore", symbol: "c", buildChance: 0.02,
ableToMoveOn: [
"empty" ], ableToEat: [ "animal" ],
plantChance: 0.07,
vegetarianChance: 0.48, healthToReproduce: 62, healthToMove: 62 },
empty: { type: "empty",
symbol: "z", buildChance: null }
};