void OnPersonEndedPath(PersonBehavior person) { if (person.transform == targetInstance && gameObject.activeSelf) { SpawnPerson(); } }
public void WhenIndependentReachedDestination() { reachedDestination = false; nextDependent = CheckDependentsInSection(); if (nextDependent != null) { agent.SetDestination(nextDependent.GetPos()); pathIndex--; responsibleForIDs.Remove(nextDependent.GetID()); followers.Add(nextDependent); if (nextDependent.GetSpeed() < groupSpeed) { groupSpeed = nextDependent.GetSpeed(); } nextDependent.GetAgent().speed = groupSpeed; nextDependent.SetDestSection(destSection); nextDependent.startedMovement = true; destSection.AddPerson(nextDependent); } else { agent.SetDestination(nodesPath[pathIndex].GetPos()); } UpdateFollowersMovement(); }
override public void SetEditableElement(GameObject element_) { p = element_.GetComponent <PersonBehavior>(); if (p != null) { addTypeDD.onValueChanged.RemoveAllListeners(); removeTypeDD.onValueChanged.RemoveAllListeners(); familyIdDD.onValueChanged.RemoveAllListeners(); addRespDD.onValueChanged.RemoveAllListeners(); removeRespDD.onValueChanged.RemoveAllListeners(); familyMenu.SetActive(false); independentMenu.SetActive(false); IDText.text = "ID: " + p.GetID(); ageText.text = "Age: " + p.GetAge(); ageSlider.value = p.GetAge(); speedText.text = "Speed: " + p.GetSpeed() / 2; speedSlider.value = p.GetSpeed() / 2; UpdateTypes(); manualToggle.isOn = p.GetManual(); dependentToggle.isOn = p.GetDependent(); UpdateFamilyMenu(); addTypeDD.onValueChanged.AddListener(delegate { AddTypeCheck(); }); removeTypeDD.onValueChanged.AddListener(delegate { RemoveTypeCheck(); }); familyIdDD.onValueChanged.AddListener(delegate { SetToFamily(); }); addRespDD.onValueChanged.AddListener(delegate { AddDependentCheck(); }); removeRespDD.onValueChanged.AddListener(delegate { RemoveDependentCheck(); }); } }
/// <summary> /// What to do when this person is hit by the syringe public bool Hit() { bool rv = false; PersonBehavior behaviorScript = gameObject.GetComponent <PersonBehavior>(); //if(this.transform.tag == "Target") { // // Correct target // rv = true; // // DEBUG // Debug.Log("Target hit"); //} if (behaviorScript.typePersonBehavior == TypePersonBehavior.Target) { rv = true; // DEBUG Debug.Log("Target hit"); } // TODO: play a SFX // Kill this person behaviorScript.bnIsDead = true; if (sfxPersonDying != null) { AudioSource.PlayClipAtPoint(sfxPersonDying, transform.position); } return(rv); }
private PersonBehavior PlacePerson(Section s_, Vector3 pos_) { // Place the person in the NavMesh NavMeshHit hit; bool validPos = NavMesh.SamplePosition(pos_, out hit, 2.0f, NavMesh.AllAreas); if (validPos) { actualPerson = GameObject.Instantiate(personPrefab, this.transform).GetComponent <PersonBehavior>(); actualPerson.Setup(nextPerson.GetID(), nextPerson.GetAge(), new List <string>(nextPerson.GetPersonTypes()), nextPerson.GetSpeed(), nextPerson.GetFamilyID(), nextPerson.GetDependent(), nextPerson.GetManual(), new List <int>(nextPerson.GetResponsibleForIDs())); actualPerson.SetOnFloor(hit.position, s_, sc.GetGraph().GetClosestNode(hit.position, s_)); people.Add(actualPerson); nextPerson.SetID(currentPersonID += 1); nextPerson.ClearResponsibilities(); nextPerson.SetTutorID(-1); // COMMUNICATE THAT THE MENU NEEDS TO CHANGE ITS EDITABLE ELEMENT sc.PersonAdded(actualPerson, nextPerson); return(actualPerson); } else { return(null); } }
public void PersonAdded(PersonBehavior actualPerson_, PersonBehavior nextPerson_) { // change stats //sceneInfo.PersonAdded(actualPerson_.GetPersonTypes()); // change menu menu.EnableEditMenu(nextPerson_.gameObject); }
private Path FindPathToCP(PersonBehavior person, Node CPNode) { personPath = new List <Element>(); personPath.Add(new Element(person.GetInitNode(), 0, Vector3.Distance(person.GetInitNode().GetPos(), CPNode.GetPos()), null)); Element actual; int countdown = 100; while (countdown > 0) { countdown--; if (personPath.Count < 1) { Utils.Print("ERROR. Could not find a path to destination " + CPNode.GetData() + " from " + person.GetInitNode().GetData()); return(null); } actual = personPath[0]; // Goal reached if (actual.n == CPNode) { float fsol; List <Node> sol = new List <Node>(); Element solNode = actual; sol.Add(solNode.n); fsol = solNode.f; while (solNode.parent != null) { sol.Add(solNode.parent.n); solNode = solNode.parent; } sol.Reverse(); return(new Path(person, sol, fsol)); } // Expand node personPath.RemoveAt(0); foreach (Edge e in actual.n.GetAdjacentEdges()) { Node childNode = e.GetOtherNode(actual.n); if (!(actual.parent != null && childNode == actual.parent.n)) { float walkedChild = actual.walked + e.GetDistance(); float remainingChild = Vector3.Distance(CPNode.GetPos(), childNode.GetPos()); Element child = new Element(childNode, walkedChild, remainingChild, actual); personPath.Add(child); } } // Sort list by f value personPath.Sort((a, b) => (a.f.CompareTo(b.f))); } return(null); }
private void SeekHunter() { PersonBehavior hunter = FindClosestHunter(); Vector2 currentPos = new Vector2(transform.position.x, transform.position.y); Vector2 hunterPos = hunter.transform.position; Vector2 newPos = Vector2.MoveTowards(currentPos, hunterPos, myPerson.MoveSpeed * Time.deltaTime); transform.position = newPos; }
public void ResetSimulation() { peopleArrivedCounter = 0; peopleTotalCount = 0; timer = 0f; lastArrivedPerson = null; foreach (PersonBehavior p_ in people) { p_.ResetSimulation(); } }
void PickStalker() { // Get someone from the list to be the target int nStalkerIdx = Random.Range(0, goAllPersonInTheLevel.Length); PersonBehavior personBehaviorScript = goAllPersonInTheLevel[nStalkerIdx].GetComponent <PersonBehavior>(); personBehaviorScript.typePersonBehavior = TypePersonBehavior.Stalker; // Keep the stalker info PersonChangeMaterials personChangeScript = goAllPersonInTheLevel[nStalkerIdx].GetComponent <PersonChangeMaterials>(); objectivesCardScript.SetStalkerInfo(personChangeScript.personColors); }
public void SetDependenciesFromData(int tID_) { tutorID = -1; if (tID_ != -1 && familyID != -1) { Family family = peopleController.FindFamilyByID(familyID); if (family != null) { PersonBehavior tutor = family.FindMemberByID(tID_); if (tutor != null) { tutor.AddToTutory(this); } } } }
private Path FindClosestCP(PersonBehavior person) { Path actualPath, assignedPath = null; foreach (Node cpn in CPNodes) { actualPath = FindPathToCP(person, cpn); if (Random.Range(0, 2) == 0 || assignedPath == null) { assignedPath = actualPath; } } return(assignedPath); }
private void AvoidHunter() { PersonBehavior hunter = FindClosestHunter(); Vector2 currentPos = new Vector2(transform.position.x, transform.position.y); Vector2 hunterPos = hunter.transform.position; if (Vector2.Distance(currentPos, hunterPos) > myPerson.HuntingRange) { return; } Vector2 newPos = Vector2.MoveTowards(currentPos, hunterPos, -myPerson.MoveSpeed * Time.deltaTime); //the minus does the away thing transform.position = newPos; }
private Path FindClosestCP(PersonBehavior person) { float minF = Mathf.Infinity; Path actualPath, assignedPath = null; foreach (Node cpn in CPNodes) { actualPath = FindPathToCP(person, cpn); if (actualPath.f < minF) { assignedPath = actualPath; minF = actualPath.f; } } return(assignedPath); }
private Path FindFarCP(PersonBehavior person) { float maxF = Mathf.NegativeInfinity; Path actualPath, assignedPath = null; foreach (Node cpn in CPNodes) { actualPath = FindPathToCP(person, cpn); if (actualPath.f > maxF) // if remaining length path is > actual max length path { assignedPath = actualPath; maxF = actualPath.f; } } return(assignedPath); }
void SpawnPerson(bool isTarget = true, int activeIndex = -1) { Transform target = isTarget ? (spawnTargets [heroPreset.female ? 1 : 0]) : spawnTargets [Random.Range(2, spawnTargets.Length)]; if (isTarget) { PathSample[] path = HeroPathManager.GetPath(); if (targetInstance != null) { Destroy(targetInstance.gameObject); } targetInstance = Instantiate(target); targetInstance.position = path[0].position; targetInstance.eulerAngles = new Vector3(0, Random.Range(0f, 360f), 0); targetInstance.GetComponent <CharacterCustomization> ().SetAppearance(heroPreset); targetInstance.gameObject.SetActive(true); followCam.target = targetInstance; targetInstance.name = "Hero"; targetInstance.GetComponent <PersonBehavior> ().UsePath(path, OnPersonEndedPath); } else { PathSample spawn = GetRandomPoint(); Transform person = Instantiate(target); person.position = spawn.position; person.eulerAngles = new Vector3(0, Random.Range(0f, 360f), 0); person.gameObject.SetActive(true); PersonBehavior behavior = person.GetComponent <PersonBehavior> (); int timerIndex = timerLength * 2; if (activeIndex != -1) { Destroy(activePeople [activeIndex].gameObject); activePeople [activeIndex] = behavior; spawnTimers [activeIndex] = Time.time + Random.Range(timerValues [timerIndex], timerValues [timerIndex + 1]); } else { activePeople.Add(behavior); spawnTimers.Add(Time.time + Random.Range(timerValues [timerIndex], timerValues [timerIndex + 1])); } SetLayerRecursively(person, peopleLayer); person.GetComponent <PersonBehavior> ().Wander(); person.parent = peopleParent; } }
private Path FindClosestCP(PersonBehavior person) { float minF = Mathf.Infinity; Path actualPath, assignedPath = null; foreach (Node cpn in CPNodes) { actualPath = FindPathToCP(person, cpn); if (actualPath.f < minF) { assignedPath = actualPath; minF = actualPath.f; UnityEngine.Debug.LogWarning("Dummy ID N" + cpn.GetID().ToString()); } } return(assignedPath); }
private PersonBehavior CheckDependentsInSection() { if (!dependent && responsibleForIDs.Count > 0) { Family f = peopleController.FindFamilyByID(familyID); PersonBehavior found = null; foreach (int i in responsibleForIDs) { found = f.FindMemberByID(i); if (found.GetInitSection() == nodesPath[pathIndex - 1].GetData()) { return(found); } } return(null); } return(null); }
public void LoadPathsFromData(PathsData pd_) { List <Node> nodesPath = new List <Node>(); PersonBehavior person = null; foreach (PathData p in pd_.paths) { person = FindPersonByID(p.personID); if (person != null) { foreach (int n in p.nodes) { nodesPath.Add(sc.GetGraph().FindNodeByID(n)); } person.SetNodesPath(nodesPath); nodesPath = new List <Node>(); } } alreadyBaked = true; }
public PersonBehavior FindClosestHunter() { var hunterList = FindObjectsOfType <PersonBehavior>(); float closestDistance = Mathf.Infinity; PersonBehavior hunter = null; foreach (var potentialHunter in hunterList) { if (potentialHunter.Role == 0) { continue; } float distance = Vector2.Distance(transform.position, potentialHunter.transform.position); if (distance < closestDistance) { closestDistance = distance; hunter = potentialHunter; } } return(hunter); }
public void PersonArrived(PersonBehavior p_, float timer_, bool isInCP) { // peopleTotalCount++; if (isInCP) { peopleArrivedCounter++; IDsPeopleArrivedToCP.Add(p_.GetID()); Utils.Print("The person " + p_.GetID() + " is safe in " + timer_ + "s in a CP!!"); } if (peopleArrivedCounter >= people.Count) { timer = timer_; Utils.Print("Everyone is safe in " + timer_ + "s !!!!!!"); lastArrivedPerson = p_; foreach (PersonBehavior p in people) { p.StopMovement(); } sc.SimulationFinished(true); } //if (peopleTotalCount >= people.Count && peopleArrivedCounter < people.Count) if (peopleTotalCount >= people.Count && peopleArrivedCounter < people.Count) { timer = timer_; Utils.Print("Everyone is not safe in " + timer_ + "s !!!!!!"); lastArrivedPerson = p_; foreach (PersonBehavior p in people) { p.StopMovement(); } sc.SimulationFinished(true); } // }
// This func goes through graph and return a path when the collection point is found private Path FindPathToCP(PersonBehavior person, Node CPNode) { Path actualPath = null; float maxF = Mathf.NegativeInfinity; personPath = new List <Element>(); // Element list - Node Path personPath.Add(new Element(person.GetInitNode(), 0, Vector3.Distance(person.GetInitNode().GetPos(), CPNode.GetPos()), null)); Element actual; int countdown = 3000; while (countdown > 0) { if (personPath.Count < 1 && actualPath != null) { return(actualPath); } else if (personPath.Count < 1) // if personPath { Utils.Print("ERROR. Could not find a path to destination " + CPNode.GetData() + " from " + person.GetInitNode().GetData()); return(null); } actual = personPath[0]; // First Element's List personPath.RemoveAt(0); // Delete actual Element // Goal Reached if (actual.n == CPNode) { // Assign Path Variables List <Node> sol = new List <Node>(); Element solNode = actual; float fsol = solNode.f; sol.Add(solNode.n); while (solNode.parent != null) { sol.Add(solNode.parent.n); solNode = solNode.parent; } sol.Reverse(); // Start in CPNode --> Reverse --> Finish in CPNode --> Path if (fsol > maxF) // if remaining length path is > actual max length path { actualPath = new Path(person, sol, fsol); maxF = actualPath.f; UnityEngine.Debug.LogWarning("Far ID N" + actual.n.GetID().ToString()); } } // Expand node foreach (Edge e in actual.n.GetAdjacentEdges()) { Node childNode = e.GetOtherNode(actual.n); // Associate node to actual node if (childNode.GetCurrentCapacity() == 0) { } else // If there is capacity { if (!(actual.parent != null && childNode == actual.parent.n)) // Avoid simple loops { float walkedChild = actual.walked + e.GetDistance(); float remainingChild = Vector3.Distance(CPNode.GetPos(), childNode.GetPos()); Element child = new Element(childNode, walkedChild, remainingChild, actual); personPath.Add(child); } } } // Sort list by f value personPath.Sort((a, b) => a.f.CompareTo(b.f)); countdown--; } Debug.Log("countdown expired"); return(actualPath); }
public bool AddPerson(PersonBehavior p) { peopleAssigned.Add(p); return(peopleAssigned.Count > currentCapacity); }
private void Start() { myPerson = GetComponent <PersonBehavior>(); }
//private List<Element> personPath; public List <Path> FindPaths(Graph graph_, List <PersonBehavior> people_) { List <Path> foundPaths = new List <Path>(); CPNodes = graph_.GetNodes().FindAll(x => x.GetIsCP()); // Return Collection Points Nodes List Path path = null; int count = 0; string line; List <PersonBehavior> peopleInFile = new List <PersonBehavior>(); System.IO.StreamReader file = new System.IO.StreamReader(Directory.GetCurrentDirectory() + @"\Assets\SavedData\aimedIndicatePerson.txt"); while ((line = file.ReadLine()) != null) { string[] nodesLines = line.Split(' '); int personID = Convert.ToInt32(nodesLines[0]); if ((personID >= 0) && (personID < nodesLines.Length)) { peopleInFile.Add(people_[personID]); if (people_[personID].GetDependent()) { count++; } else if (count < people_.Count) { PersonBehavior person = people_[personID]; List <Node> personPath = new List <Node>(); int lastNodeID = 0; float fperson = 0; for (int j = 1; j < nodesLines.Length; j++) //foreach (string nodeL in nodesLines) { if (nodesLines[j].Equals(person.GetInitNode().GetID())) // We See if the second number is the first node { personPath.Add(person.GetInitNode()); lastNodeID = person.GetInitNode().GetID(); path = new Path(person, personPath, fperson); if (path != null) { foundPaths.Add(path); } else { Utils.Print("PERSON W/O PATH"); } } else { if (!nodesLines[j].Equals(" ") && (!nodesLines[j].Equals(""))) { int currentlyNode = Convert.ToInt32(nodesLines[j]); if (graph_.GetNodes().Contains(graph_.GetNode(currentlyNode))) // If the node exists { //if (graph_.GetAdjacentNodes(graph_.GetNode(lastNodeID)).Contains(graph_.GetNode(currentlyNode))) //{ personPath.Add(graph_.GetNode(currentlyNode)); fperson = fperson + graph_.GetNode(lastNodeID).ConnectedTo(graph_.GetNode(currentlyNode)).GetDistance(); lastNodeID = currentlyNode; path = new Path(person, personPath, fperson); if (path != null) { foundPaths.Add(path); } else { Utils.Print("PERSON W/O PATH"); } //} } else { Debug.LogError("The Node " + currentlyNode + ", line " + count + ", in the txt doesn't exit"); } } } } count++; } else { Debug.LogError("More data that persons in txt"); } } } file.Close(); int countAllPeople = 0; while (count < people_.Count) { if (peopleInFile.Contains(people_[countAllPeople])) { countAllPeople++; } else { if (people_[countAllPeople].GetDependent()) { count++; } else { PersonBehavior person = people_[countAllPeople]; List <Node> personPath = new List <Node>(); float fperson = 0; personPath.Add(person.GetInitNode()); path = new Path(person, personPath, fperson); if (path != null) { foundPaths.Add(path); } else { Utils.Print("PERSON W/O PATH"); } count++; } peopleInFile.Add(people_[countAllPeople]); countAllPeople++; } } return(foundPaths); }
// Use this for initialization void Start() { behaviorScript = gameObject.GetComponent <PersonBehavior>(); }
public Path(PersonBehavior p_, List <Node> path_, float f_) { person = p_; path = path_; f = f_; }
public void RemoveMember(PersonBehavior p_) { members.Remove(p_); }
public void AddMember(PersonBehavior p_) { members.Add(p_); }
//private List<Element> personPath; public List <Path> FindPaths(Graph graph_, List <PersonBehavior> people_) { CPNodes = graph_.GetNodes().FindAll(x => x.GetIsCP()); // Return Collection Points Nodes List Path path = null; int count = 0; //for (int count = 0; count < people_.Count; count++) //{ if (!people_[count].GetDependent()) { if (people_[count].GetID() == 0) { //Path(PersonBehavior p_, List < Node > path_, float f_) PersonBehavior person = people_[count]; List <Node> personPath = new List <Node>(); personPath.Add(person.GetInitNode()); float fperson = 0; personPath.Add(graph_.GetNode(2)); fperson = fperson + person.GetInitNode().ConnectedTo(graph_.GetNode(2)).GetDistance(); personPath.Add(graph_.GetNode(3)); fperson = fperson + graph_.GetNode(2).ConnectedTo(graph_.GetNode(3)).GetDistance(); //personPath.Add(graph_.GetNode(1)); //fperson = fperson + graph_.GetNode(3).ConnectedTo(graph_.GetNode(1)).GetDistance(); path = new Path(person, personPath, fperson); if (path != null) { foundPaths.Add(path); } else { Utils.Print("PERSON W/O PATH"); } } count++; if (people_[count].GetID() == 1) { //Path(PersonBehavior p_, List < Node > path_, float f_) PersonBehavior person = people_[count]; List <Node> personPath = new List <Node>(); personPath.Add(person.GetInitNode()); float fperson = 0; personPath.Add(graph_.GetNode(2)); fperson = fperson + person.GetInitNode().ConnectedTo(graph_.GetNode(2)).GetDistance(); personPath.Add(graph_.GetNode(3)); fperson = fperson + graph_.GetNode(2).ConnectedTo(graph_.GetNode(3)).GetDistance(); personPath.Add(graph_.GetNode(1)); fperson = fperson + graph_.GetNode(3).ConnectedTo(graph_.GetNode(1)).GetDistance(); path = new Path(person, personPath, fperson); if (path != null) { foundPaths.Add(path); } else { Utils.Print("PERSON W/O PATH"); } } } //} return(foundPaths); }