void GrowUp() { LiveEntety live = GetComponent <LiveEntety>(); float growAmount = Mathf.Min(0.5f, Time.deltaTime * 1 / eatDuration); live.GrowUp(growAmount); }
void EatPlant() { LiveEntety foodLive = food.GetComponent <LiveEntety>(); Animal foodAnimal = food.GetComponent <Animal>(); bool willDie = false; bool wontDie = false; if (hunger >= foodLive.amountRemaining) { willDie = true; } else if (hunger < foodLive.amountRemaining) { wontDie = true; } agent.SetDestination(transform.position); float eatAmount = Mathf.Min(hunger, Time.deltaTime * 1 / eatDuration); float eat = eatAmount; eatAmount = foodLive.Consume(eat); hunger -= eatAmount; if (wontDie == true && hunger <= 0) { food = null; } if (willDie == true && foodLive.amountRemaining <= 0) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.plants.Remove(foodAnimal); food = null; } }
private void OnTriggerEnter(Collider other) { if (species != Species.plant) { LiveEntety live = other.GetComponent <LiveEntety>(); LiveEntety liveThis = this.GetComponent <LiveEntety>(); if (live != null && live != liveThis) { Animal animal = other.GetComponent <Animal>(); if (diet.HasFlag(live.species) && tooErgentForFood == false && foundFood == false && genes.reperductiveUrge < hunger && hunger >= criticalPercent) { if (live.species == Species.plant) { if (animal.young == false) { food = other.transform; } } food = other.transform; } else if (live.species == liveThis.species && genes.reperductiveUrge > hunger && animal.genes.reperductiveUrge > animal.hunger && canReperduse == true) { if (animal.mate == null || animal.mate == this.transform) { if (genes.isMale == false && animal.genes.isMale == true || genes.isMale == true && animal.genes.isMale == false) { mate = other.transform; } } } else if (animal.diet.HasFlag(species)) { preditor = other.transform; } } } }
// Update is called once per frame void Update() { criticalPercent = genes.criticalPercent; if (transform.localScale.y > Vector3.one.y) { transform.localScale = Vector3.one; } if (young == true) { if (transform.localScale.y < Vector3.one.y) { GrowUp(); } if (transform.localScale == Vector3.one) { young = false; } } if (species == Species.plant) { if (transform.position.y != -0.7f) { Vector3 newPos = new Vector3(transform.position.x, -0.7f, transform.position.z); transform.position = newPos; } hunger += Time.deltaTime * 1 / timeToDeathByHunger; if (hunger >= criticalPercent) { Reperduse(); } } if (species != Species.plant) { if (state == State.eating) { if (food != null) { EatPlant(); } else { state = State.exploring; } } else if (state == State.waitingForMate) { if (food != null) { transform.LookAt(mate.position); } else { state = State.exploring; } } speed = genes.speed; visonDistance = genes.visonDistance; walkPointRange = visonDistance * 2; trigger.radius = visonDistance; agent.speed = speed; float hungerTime = speed / 6; hunger += Time.deltaTime * hungerTime / timeToDeathByHunger; //cool = Enviroment.allAnimals; if (food != null) { agent.SetDestination(food.position); foundFood = true; } else { foundFood = false; } if (preditor != null) { tooErgentForFood = true; } else { tooErgentForFood = false; } if (tooErgentForFood == true) { WalkRandom(); float distace = Vector3.Distance(preditor.transform.position, transform.position); Genes animalGenes = preditor.GetComponent <Animal>().genes; if (distace <= 1 && genes.strength > animalGenes.strength) { Camera cam = preditor.GetComponentInChildren <Camera>(); if (cam != null) { cam.transform.parent = null; cam.GetComponent <FocusOnObject>().UnFocus(); } Animal animal = preditor.GetComponent <Animal>(); if (diet.HasFlag(animal.species)) { LiveEntety live = preditor.gameObject.GetComponent <LiveEntety>(); float eatAmount = Mathf.Min(hunger, Time.deltaTime * 1 / eatDuration); eatAmount = live.Consume(eatAmount); hunger -= eatAmount; animal.food = null; preditor = null; } else { LiveEntety live = preditor.gameObject.GetComponent <LiveEntety>(); live.Die(causeOfDeath.fighting); preditor = null; } Animal preAnimal = preditor.GetComponent <Animal>(); if (preAnimal.species == Species.plant) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.plants.Remove(preAnimal); } else if (preAnimal.species == Species.fox) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.foxs.Remove(preAnimal); } else if (preAnimal.species == Species.wildBoar) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wildBoars.Remove(preAnimal); } else if (preAnimal.species == Species.bear) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bears.Remove(preAnimal); } else if (preAnimal.species == Species.bunny) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bunnys.Remove(preAnimal); } else if (preAnimal.species == Species.wolf) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wolfs.Remove(preAnimal); } } } if (mate != null && mate.GetComponent <Animal>().mate == this.transform) { if (genes.isMale == true) { agent.SetDestination(mate.position); } else { agent.SetDestination(this.transform.position); } foundMate = true; } else { foundMate = false; } if (foundFood == true) { state = State.goingToFood; float distace = Vector3.Distance(food.transform.position, transform.position); Genes animalGenes = food.GetComponent <Animal>().genes; if (distace <= 1 && genes.strength >= animalGenes.strength) { LiveEntety live = food.gameObject.GetComponent <LiveEntety>(); bool isNotPlant = false; Animal foodAnimal = food.GetComponent <Animal>(); if (foodAnimal.species == Species.plant) { state = State.eating; agent.SetDestination(transform.position); } else { isNotPlant = true; } if (isNotPlant == true) { Camera cam = food.GetComponentInChildren <Camera>(); if (cam != null) { cam.transform.parent = null; cam.GetComponent <FocusOnObject>().UnFocus(); } if (foodAnimal.species == Species.plant) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.plants.Remove(foodAnimal); } else if (foodAnimal.species == Species.fox) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.foxs.Remove(foodAnimal); } else if (foodAnimal.species == Species.wildBoar) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wildBoars.Remove(foodAnimal); } else if (foodAnimal.species == Species.bear) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bears.Remove(foodAnimal); } else if (foodAnimal.species == Species.bunny) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bunnys.Remove(foodAnimal); } else if (foodAnimal.species == Species.wolf) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wolfs.Remove(foodAnimal); } live.Die(causeOfDeath.eaten); hunger = 0; food = null; } } } if (foundMate == true) { if (genes.isMale == true) { state = State.goingToMate; } else { state = State.waitingForMate; } float distace = Vector3.Distance(mate.transform.position, transform.position); if (distace <= 1 || species == Species.bear && distace <= 3) { LiveEntety live = mate.gameObject.GetComponent <LiveEntety>(); Reperduse(); } } if (hunger >= 1) { Camera cam = GetComponentInChildren <Camera>(); if (cam != null) { cam.transform.parent = null; cam.GetComponent <FocusOnObject>().UnFocus(); } if (species == Species.plant) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.plants.Remove(this); } else if (this.species == Species.fox) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.foxs.Remove(this); } else if (this.species == Species.wildBoar) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wildBoars.Remove(this); } else if (this.species == Species.bear) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bears.Remove(this); } else if (this.species == Species.bunny) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.bunnys.Remove(this); } else if (this.species == Species.wolf) { Enviroment enviroment = FindObjectOfType <Enviroment>(); enviroment.wolfs.Remove(this); } LiveEntety live = this.GetComponent <LiveEntety>(); live.Die(causeOfDeath.hunger); } if (foundFood == false && tooErgentForFood == false && foundMate == false && hunger < criticalPercent && hunger > genes.reperductiveUrge || foundMate == false && hunger < genes.reperductiveUrge && tooErgentForFood == false && foundFood == false && canReperduse == false) { WalkRandom(); state = State.exploring; } else if (foundFood == false && hunger >= criticalPercent && hunger > genes.reperductiveUrge && tooErgentForFood == false && foundMate == false) { WalkRandom(); state = State.searchingForFood; } else if (foundMate == false && hunger < genes.reperductiveUrge && tooErgentForFood == false && foundFood == false && canReperduse == true) { WalkRandom(); state = State.searchingForMate; } else if (tooErgentForFood == true) { //WalkRandom(); agent.SetDestination(new Vector3(-preditor.transform.position.x, preditor.transform.position.y, -preditor.transform.position.z)); state = State.runningFromPreditor; } } }