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()); }
private void AttackingState() { if (attackStateVars.target != null) { Vector3 targetPosition = (attackStateVars.target.position - this.transform.position).normalized; MoveToTarget(this.transform.position + targetPosition, attackStateVars.speed, attackStateVars.turnSpeed); if (Helper.DistanceToVector(this.transform.position, attackStateVars.target.transform.position) < attackStateVars.distanceToTarget) { //TODO: Attack the thing if need be HealthTrait healthTrait = attackStateVars.target.GetComponent <HealthTrait>(); if (healthTrait) { if (healthTrait.isAlive) { attackStateVars.target.GetComponent <AttackableTrait>().Attack(wolfDamage); } else { SetStateToEating(attackStateVars.target.GetComponent <EdibleTrait>()); } } else { attackStateVars.target = null; } } } else { SetStateToHungry(GetThingsInView()); } }
// 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; }); }