E. Darwin Hartshorn: //Boilerplate

Code

Therion Development: Foodness

I’m biting the bullet and working in Unity.

What’s basic Virtual Pet behavior?

You feed it, and it digests.  I’m going to leave elimination out of the process, because I don’t see that adds anything for the gamer.

Now, I want the nutrients in the food to matter, so if we are representing food definitions as an array of structs, we can represent the stomach as a queue of ints that refer to indices in the aforementioned array.  When he’s fed, drop the food on the queue; when he’s digested remove the earliest thing eaten from the queue.

That, and we’ll want a stomach capacity that may change as the character grows.

So that gives us a bit to work with.  In my project’s Assets->Scripts->Player I create a stomach.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class Stomach : MonoBehaviour
{
//How long between game ticks. Fairly long in the final game, but short for now.
public int secondsPerTick = 600;

//Stomach manager.
public int stomachCapacity = 3;
private Queue stomach;

public void Start()
{
stomach = new Queue(7);
}

// When the player tries to feed something with a stomach, add the food to the stomach (if there's room)
// and tell the calling function if there isn't so that, e.g., a "I'm full" animation can be played.
public bool Feed(int food)
{
if (stomach.Count < stomachCapacity)
{
stomach.Enqueue(food);
return true;
}
else return false;
}

public void Update()
{
LiveDigestion();
}

private IEnumerable LiveDigestion()
{
Digest();
yield return new WaitForSeconds(secondsPerTick);
}

public int digestionThreshold = 5; // This may eventually be determined by food types (e.g. bricks may take longer to digest than milk).
public float digestionRate = 1f; // This will be changed by things like exercise and play in game.
private float digestionAmount = 0;
// This is public so that it can be called repeatedly on startup to simulate time passing while the player
//was not playing.
public void Digest()
{
if (stomach.Count > 0)
{
digestionAmount+= digestionRate;
if(digestionAmount >= digestionThreshold)
{
//Remove an item from the stomach, and
stomach.Dequeue();
digestionAmount = 0;
}
}
}

}

Leave a Reply