-
Notifications
You must be signed in to change notification settings - Fork 0
/
Behaviour_LookingForGroup.cs
71 lines (58 loc) · 1.79 KB
/
Behaviour_LookingForGroup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections;
using System.Collections.Generic;
using TreeEditor;
using UnityEngine;
public class Behaviour_LookingForGroup : BehaviourState
{
public Group groupToJoin;
//Find groups that are nearby, otherwise create my own
public override void Enter() {
//Find a group to join
groupToJoin = null;
var groupsInScene = FindObjectsOfType<Group>();
Group closestGroup = null;
foreach (var group in groupsInScene) {
if (closestGroup != null) {
var distToNew = Vector3.Distance(transform.position, group.transform.position);
var distToOld = Vector3.Distance(transform.position, closestGroup.transform.position);
if (distToNew < distToOld) {
closestGroup = group;
}
} else {
closestGroup = group;
}
}
if (closestGroup != null) {
if (Vector3.Distance(transform.position, closestGroup.transform.position) <= animal.groupFindRange) {
groupToJoin = closestGroup;
}
}
else { //Create new group if there is none
CreateNewGroup();
}
//reneable decay and movement
animal.move = true;
animal.ToggleDecay(true);
}
private void CreateNewGroup() {
var group = gameObject.AddComponent<Group>();
group.groupLeader = animal;
group.Enter(animal);
animal.StateMachine(GetComponent<Behaviour_Wander>());
}
//If we have found a group we then head for that group
public override void Think() {
if (groupToJoin != null) {
animal.currentTarget = groupToJoin.transform;
if (Vector3.Distance(transform.position, groupToJoin.transform.position) < 10) {
groupToJoin.Enter(animal);
//Initialize offsetpursue using new group
GetComponent<OffsetPursue>().Init(groupToJoin.groupLeader);
animal.StateMachine(GetComponent<Behaviour_FollowLeader>());
}
}
}
public override void Exit() {
}
}