private void SetStateToEating(EdibleTrait trait) { currentState = eWolfStates.Eating; //GetComponentInChildren<MeshRenderer>().materials[0].color = Color.black; eatingStateVars.target = trait.transform; }
private EdibleTrait[] GetEdibleTraits(RaycastHit[] hits) { List <EdibleTrait> edibleTraits = new List <EdibleTrait>(); foreach (RaycastHit hit in hits) { if (ReferenceEquals(hit.transform.gameObject, this.gameObject)) { continue; } //Is the thing edible? EdibleTrait edibleTrait = hit.transform.GetComponent <EdibleTrait>(); if (edibleTrait) { // wolves won't eat wolves WolfController wolfController = hit.transform.GetComponent <WolfController>(); if (!wolfController) { // They aren't vegetarien MeatTrait meatTrait = hit.transform.GetComponent <MeatTrait>(); if (meatTrait) { HealthTrait healthTrait = hit.transform.GetComponent <HealthTrait>(); if (healthTrait && !healthTrait.isAlive) { edibleTraits.Add(edibleTrait); } } } } } return(edibleTraits.ToArray()); }
/// <summary> /// Gets all the food things that are alive, edible, attackable, not a wolf and not self /// </summary> /// <param name="hits"></param> /// <returns></returns> private AttackableTrait[] GetAllAliveAttackableFoodTraits(RaycastHit[] hits) { List <AttackableTrait> attackingTraits = new List <AttackableTrait>(); foreach (RaycastHit hit in hits) { if (ReferenceEquals(hit.transform.gameObject, this.gameObject)) { continue; } //Is the thing edible? EdibleTrait edibleTrait = hit.transform.GetComponent <EdibleTrait>(); if (edibleTrait) { // wolves won't eat wolves WolfController wolfController = hit.transform.GetComponent <WolfController>(); if (!wolfController) { // They aren't vegetarien MeatTrait meatTrait = hit.transform.GetComponent <MeatTrait>(); if (meatTrait) { AttackableTrait attackableTrait = hit.transform.GetComponent <AttackableTrait>(); if (attackableTrait) { attackingTraits.Add(attackableTrait); } } } } } return(attackingTraits.ToArray()); }
// Update is called once per frame void Update() { if (isBurning) { //Check if the thing is hit with a water trait if (CheckForWater()) { PutOutFire(); return; } //TODO: Make this check if it can be lit on fire rather then lighting on fire //After a defined time, check if something burnable exists burnCheckTimer -= Time.deltaTime; if (burnCheckTimer <= 0) { //Random check to see if the fire spreads if (Random.Range(0f, 1f) < randomChanceToLightSomethingOnFire) { RaycastHit[] hits = Physics.SphereCastAll(this.transform.position, burnRadius, Vector3.up, 0); if (hits.Length > 0) { foreach (RaycastHit hit in hits) { if (hit.transform.gameObject.Equals(this.gameObject)) { continue; } if (Random.Range(0f, 1f) < randomChanceToLightSomethingOnFire) //TODO: Make this a seperate chance { FlammableTrait trait = hit.transform.GetComponent <FlammableTrait>(); if (trait) { trait.LightFire(); } } } } } burnCheckTimer = burnCheckTime; } burntime -= Time.deltaTime; if (burntime <= 0) { PutOutFire(); EdibleTrait trait = GetComponent <EdibleTrait>(); if (trait) { Destroy(trait); //.enabled = false; } //Destroy the fire trait to indicate that the thing isn't flammable anymore Destroy(this, 1); GetComponent <MeshRenderer>().materials[0].color = Color.grey; //TODO: Sizzle out effect } } }
// Use this for initialization protected override void Start() { base.Start(); rb = this.GetComponent <Rigidbody>(); targetExists = false; foundFood = false; targetEdibleTrait = null; currentState = new Stack <SheepStates>(); currentState.Push(startingState); scaredOfObjects = new List <GameObject>(); maxFoodLevel += UnityEngine.Random.Range(-5f, 5f); hungerLevel += UnityEngine.Random.Range(-1f, 1f); foodConsumeRate += UnityEngine.Random.Range(-0.005f, 0.005f); cantTakeItAnymoreHungerLevel = maxFoodLevel / 2; healthTrait = GetComponent <HealthTrait>(); isAlive = true; healthTrait.thingDied.AddListener(() => { Debug.Log("The sheep died"); isAlive = false; }); }
private void HungryStateFunction() { // If the sheep is hungry, get food! if (hungerLevel <= maxFoodLevel) { if (foundFood) { // If the sheep has reached the end of the path, walk straight to the target and eat if (MoveOnPath(speed, turnSpeed)) { if (Helper.DistanceToVector(this.transform.position, target) < eatDistance) { if (targetEdibleTrait != null) { //TODO: Add cooldown to eating hungerLevel += targetEdibleTrait.GetFoodValue(); } else { foundFood = false; } } else { MoveToTarget(target, speed, turnSpeed); } } } else { // Look for edible things in the enviroment. RaycastHit[] hits = GetVisibleObjects(); List <EdibleTrait> traitHits = new List <EdibleTrait>(); //TODO: This is quite ineffective if there is only one element foreach (RaycastHit hit in hits) { if (ReferenceEquals(hit.transform.gameObject, this.gameObject)) { continue; } EdibleTrait edibleTrait = hit.transform.GetComponent <EdibleTrait>(); PlantTrait plantTrait = hit.transform.GetComponent <PlantTrait>(); if (edibleTrait && plantTrait) { traitHits.Add(edibleTrait); } } if (traitHits.Count == 0) { // If the sheep is close to its target or if it doesn't have one, get a new target if (MoveOnPath(speed, turnSpeed) || !targetExists) { GetNewTargetVector(); } } //Get the closest edible thing else if (traitHits.Count == 1) { targetEdibleTrait = traitHits[0]; target = hits[0].transform.position; targetExists = true; foundFood = true; } else if (traitHits.Count > 0) { float closestDistance = viewRadius * 2; //Make the min more then it could possibly be GameObject closetObject = traitHits[0].gameObject; //default the closest object to something // Get the closest edible thing foreach (EdibleTrait trait in traitHits) { float dis = Vector3.Distance(this.transform.position, trait.gameObject.transform.position); if (dis < closestDistance) { closestDistance = dis; closetObject = trait.gameObject; targetEdibleTrait = trait; foundFood = true; } } // If I found food, make it the target target = closetObject.transform.position; } // Either the sheep will be going to eat some food or it will roam targetExists = !MoveOnPath(speed, turnSpeed); } } else { targetEdibleTrait = null; currentState.Pop(); } }