void Start() { formation_velocity = max_velocity; // make sure your slots and npcs match if (SheepGroup.Count != slots.Count) { throw new System.Exception("The amount of sheep in a group must equal the amount of slots in that group!"); } // make sure slots are connected to anchor, and that each sheep object has a reference to its slot anchor = transform; for (int i = 0; i < slots.Count; i++) { slots[i].parent = anchor; Transform walkable = new GameObject().transform; walkable.position = slots[i].position; slots_walkable.Add(walkable); SheepBehavior sb = SheepGroup[i].GetComponent <SheepBehavior>(); sb.SetSlot(slots_walkable[i]); sb.pathingType = SheepBehavior.SheepPathingType.Patrolling; } // populate the patrol nodes list InitializePatrolNodes(); InvokeRepeating("CheckFormationIntegrity", 3, 5); }
void FindVisibleTargets() { SheepBehavior aSheep = transform.parent.GetComponent <SheepBehavior>(); if (wolf.howl) { aSheep.escapeSequence = true; } //Prioritize escape sequence above all if (!aSheep.escapeSequence) { // Prioritize chasing sweater over player (if naked of course) if (visibleTargets.Contains(wolf.transform) && aSheep.pathingType != SheepBehavior.SheepPathingType.ToSweater) { // Debug.Log("Wolf found!"); aSheep.SetOldPos(); aSheep.pathingType = SheepBehavior.SheepPathingType.ToPlayer; aSheep.travelPath.Clear(); aSheep.seesPlayer = true; if (aSheep.goldenSheep) { aSheep.giveUpTimer = SheepBehavior.GIVE_UP_TIMER_MAX_G; } else { aSheep.giveUpTimer = SheepBehavior.GIVE_UP_TIMER_MAX; } //wolf.escaped = false; } else { aSheep.seesPlayer = false; //wolf.escaped = true; } visibleTargets.Clear(); Collider2D[] targetsInViewRadius = Physics2D.OverlapCircleAll(transform.position, viewRadius, targetMask); foreach (Collider2D target in targetsInViewRadius) { Vector2 dirToTarget = (target.transform.position - transform.position).normalized; if (Vector2.Angle(transform.up, dirToTarget) < viewAngle / 2) { float distToTarget = Vector3.Distance(transform.position, target.transform.position); if (!Physics2D.Raycast(transform.position, dirToTarget, distToTarget, obstacleMask)) { visibleTargets.Add(target.transform); } } } } }
void Update() { if (this.tag != "Sweater") { // Player sets target position and sweater moves towards it targetPosition = GameObject.Find("Wolf").GetComponent <Wolf>().targetPosition; this.transform.position = Vector2.MoveTowards(this.transform.position, targetPosition, sweaterSpeed * Time.deltaTime); // Acquire "Sweater" tag once sweater reaches target position if (new Vector2(this.transform.position.x, this.transform.position.y) == targetPosition) { this.tag = "Sweater"; } } else { // Only closest naked sheep moves to this sweater once it acquires the "Sweater" tag if (GameObject.FindGameObjectsWithTag("Sheared").Length > 0) { GameObject[] nakedSheep = GameObject.FindGameObjectsWithTag("Sheared"); proximity = (nakedSheep[0].transform.position - this.transform.position).magnitude; nearestSheep = nakedSheep[0]; foreach (GameObject sheep in nakedSheep) { if ((sheep.transform.position - this.transform.position).magnitude < proximity) { proximity = (sheep.transform.position - this.transform.position).magnitude; nearestSheep = sheep; } } // Move nearest sheep to this sweater nearestSheepBehaviour = nearestSheep.GetComponent <SheepBehavior>(); nearestSheepBehaviour.SetOldPos(); nearestSheepBehaviour.pathingType = SheepBehavior.SheepPathingType.ToSweater; nearestSheepBehaviour.sweaterPos = transform.position; nearestSheepBehaviour.travelPath.Clear(); proximity = (nearestSheep.transform.position - this.transform.position).magnitude; if (proximity < 3) // change layer only if sheep is about to pick up the shirt { this.gameObject.layer = 0; // can throw sweater past sheep; sheep can only collide with sweater once it lands } } } }
void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag == "SheepEatCollider") { if (isCollidingSheep) { return; } isCollidingSheep = true; // Disable the sheep eating collider other.enabled = false; gameObject.GetComponentInParent <AudioSource> ().Play(); SheepBehavior sheepBehavior = other.gameObject.GetComponentInParent <SheepBehavior>(); sheepBehavior.OnPlayerAction(); } }