public bool actionEat(string eater_id, string item_id) { LifeForm eater = new LifeForm(); Item eaten_item = new Item(); foreach (LifeForm entity in locale.inhabitants) { if (entity.id == eater_id) { eater = entity; break; } } if (eater.isNull()) return false; foreach (Item food in eater.inventory) { if (food.id == item_id) { eaten_item = food; break; } } if (eaten_item.isNull()) return false; eaten_item.usage("eat", eater); eater.inventory.Remove(eaten_item); eater.hungry = false; return true; }
// defines what happens when the item is "used" // items can be used in different ways. Food, for example can be eaten or stolen // pass the way it is used as the use_type // only life forms of animal type or their decendnats can use items // returns 0 if action successful, 1 if failed. public int usage(string use_type, LifeForm user) { int ret = 0; switch (use_type) { case "eat": user.nutrition += nutrient; if (user.nutrition > user.max_sation) user.nutrition = user.max_sation; //Console.WriteLine("Life form has eaten " + name); break; default: //Console.WriteLine("Invalid use type."); ret = 1; break; } return ret; }