public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } // add all points together and average Vector3 avoidanceMove = Vector3.zero; int nAvoid = 0; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior // if no filtered neighbors return no adjustment if (filterContext.Count == 0) { return(Vector3.zero); } foreach (Transform item in filterContext) { if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareIdleAvoidanceRadius) // if the distance to the item is within the avoidance radius { nAvoid++; avoidanceMove += agent.transform.position - item.position; // add vector pointing away from item } } if (nAvoid > 0) { avoidanceMove /= nAvoid; // average, avoidanceMove is now the transform of the destination } return(avoidanceMove); }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } Vector3 cohesionMove = Vector3.zero; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior // if no neighbors return no adjustment if (filterContext.Count == 0) { return(Vector3.zero); } foreach (Transform item in filterContext) // add all points together and average { cohesionMove += item.position; } cohesionMove /= filterContext.Count; // average, cohesionMove is now the transform of the destination // create offset from agent position, the actual move of the agent if (cohesionMove == Vector3.zero) { return(cohesionMove); } cohesionMove -= agent.transform.position; return(cohesionMove); }
public float[] weights; // the weights of those behaviors public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // handle data mismatch if (behaviors.Length != weights.Length) { Debug.LogError("Data missmatch in " + name, this); return(Vector3.zero); } // set up move Vector3 move = Vector3.zero; // iterate through behaviors for (int i = 0; i < behaviors.Length; i++) { Vector3 partialMove = behaviors[i].CalculateMove(agent, context, herd) * weights[i]; // do a behavior if (partialMove != Vector3.zero) { if (partialMove.sqrMagnitude > weights[i] * weights[i]) // the weight caps the magnitude of each behavior { partialMove.Normalize(); partialMove *= weights[i]; } move += partialMove; } } move.y = 0; // bison don't jump on their own return(move); }
// Make some new boys public void SpawnBison(int count, Vector3 origin, string name) { for (int i = 0; i < count; i++) { Vector3 spawn = (Vector3)Random.insideUnitCircle * spawnCircle; spawn.z = spawn.y; // we want z spawn.y = 2; // not y spawn += origin; // Yo, it's a new bison HerdAgent newAgent = Instantiate( agentPrefab, spawn, Quaternion.Euler(Vector3.up * Random.Range(0f, 360f)), // random start rotation transform // herd position ); //int index = RandomSelectionExcept(); newAgent.name = name; newAgent.Initialize(this); // agent has startup function agents.Add(newAgent); } /*for (int j = 0; j < usedIndexes.Count; j++) * { * Debug.Log("index " + usedIndexes[j]); * }*/ }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } // add all points together and average Vector3 avoidanceMove = Vector3.zero; int nAvoid = 0; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior foreach (Transform item in filterContext) { if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius) // if the distance to the item is within the avoidance radius { Vector3 dist = agent.transform.position - item.position; nAvoid++; // float forceMult = (dist.sqrMagnitude / herd.neighborRadius * herd.neighborRadius) * 100; // float forceMult = herd.neighborRadius - Vector3.Distance(agent.transform.position, item.position); //Debug.Log("player is " + Vector3.Distance(agent.transform.position, item.position) + " away."); avoidanceMove += dist; // add vector pointing away from item } } if (nAvoid > 0) { avoidanceMove /= nAvoid; // average, avoidanceMove is now the transform of the destination } return(avoidanceMove); }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // If there are no bison or players nearby, we chillin if (context.Count == 0) { return(Vector3.zero); } // This is what will be returned in the end Vector3 direction = Vector3.zero; //This should return the player(s), if there are any List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // For each player foreach (Transform item in filterContext) { // Determine if player is the right herd if (item.GetComponent <HerdShepherd>().myHerd != agent.AgentHerd) { continue; } //Use this to see distance between bison and player(s), and increase speed depending on this float distance = Vector3.Distance(item.transform.position, agent.transform.position); //Assuming the max distance in front of a player that this behavior triggers is 6 //multiplier output ranges from 1x to 7x float multiplier = 7f - distance; //This line makes the output instead range from 1/6 to 7/6, //where 1/6 is multiplier if 6 units away, and 7/6 is multiplier if 0 units away // (So you move slightly faster than player if right on top of them, and slower than them if further away) multiplier /= 6f; Vector3 playerDirection = item.transform.forward; Vector3 bisonDirection = agent.transform.forward; float directionOutcome = Vector3.Dot(playerDirection, bisonDirection); //If bison is pointing in the opposite direction, double multiplier if (directionOutcome == -1) { multiplier *= 2; } //If bison is poining in perpendicular direction, 1.5x multiplier else if (directionOutcome == 0) { multiplier *= 1.5f; } direction = playerDirection; //direction *= multiplier; // Comment this out if you don't want speed to change } direction *= 6; // Make sure it's visible return(direction); }
// keeps track of all bison that exit private void OnTriggerExit(Collider other) { HerdAgent exitAgent = other.gameObject.GetComponent <HerdAgent>(); if (exitAgent && exitAgent.AgentHerd == boxHerd) { exitAgent.inHerdBox--; BisonBeingHerded.Remove(exitAgent); } }
public float radius = 100f; // the radius of the circle public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { Vector3 centerOffset = center - agent.transform.position; float t = centerOffset.magnitude / radius; if (t < 0.95) { return(Vector3.zero); // if within 95% of the circle, don't move towards the center } return(centerOffset * t * t * t); // stronger the further from the center }
// keeps track of all bison that enter private void OnTriggerEnter(Collider other) { HerdAgent newAgent = other.gameObject.GetComponent <HerdAgent>(); if (newAgent && newAgent.AgentHerd == boxHerd) { newAgent.inHerdBox++; newAgent.hasMoved = true; BisonBeingHerded.Add(newAgent); } }
/// <summary> /// Returns the best match (assuming versions are ordered by preference) to the local machine's architecture /// </summary> /// <param name="versions"></param> public static AppVersion BestMatch(List <AppVersion> versions) { foreach (AppVersion version in versions) { if (HerdAgent.ArchitectureId() == version.Requirements.Architecture) { return(version); } } return(null); }
public AppVersion BestHostArchitectureMatch(List <AppVersion> appVersions) { string hostArchitecture = HerdAgent.ArchitectureId(); foreach (AppVersion version in appVersions) { if (version.Requirements.Architecture == hostArchitecture) { return(version); } } return(null); }
// Start is called before the first frame update void Start() { //may need to change this once we put in more than one particle system in the scene //playPS = FindObjectOfType<PlayParticleSystem>(); keepScore = FindObjectOfType <KeepScore>(); herdAgent = FindObjectOfType <HerdAgent>(); mediumMaturity = herdAgent.mediumMaturity; fullMaturity = herdAgent.fullMaturity; red = redScore.GetComponent <Animator>(); blue = blueScore.GetComponent <Animator>(); soundMidPlay = FMODUnity.RuntimeManager.CreateInstance(soundMid); soundFullPlay = FMODUnity.RuntimeManager.CreateInstance(soundFull); }
// Looks at all objects in the original context, then returns a new list with only the filtered objects public override List <Transform> Filter(HerdAgent agent, List <Transform> original) { List <Transform> filtered = new List <Transform>(); // the list to return foreach (Transform item in original) // for each transform { if (mask == (mask | (1 << item.gameObject.layer))) // if the gameObject is on the right layer { filtered.Add(item); // add it to the filtered list } } return(filtered); }
public bool IsHostArchitectureCompatible(AppVersion version) { string hostArchitecture = HerdAgent.ArchitectureId(); if (hostArchitecture == version.Requirements.Architecture || (hostArchitecture == PropValues.Win64 && version.Requirements.Architecture == PropValues.Win32)) //hard-coded for now { return(true); } else { return(false); } }
// Looks at all objects in the original context, then returns a new list with only the filtered objects public override List <Transform> Filter(OnlineHerdAgent agent, List <Transform> original) { List <Transform> filtered = new List <Transform>(); // list to return foreach (Transform item in original) { HerdAgent itemAgent = item.GetComponent <HerdAgent>(); if (itemAgent != null && itemAgent.AgentHerd == agent.AgentHerd) // if this item is in the same herd { filtered.Add(item); // add it to the list } } return(filtered); }
// Gets nearby objects for bison, and sets their state List <Transform> GetNearbyObjects(HerdAgent agent) { int layerMask = ~(1 << 10); // used to ignore objects in layer 10 List <Transform> context = new List <Transform>(); // the list to return // gets all colliders near the agent Collider[] contextColliders = Physics.OverlapSphere(agent.transform.position, neighborRadius, layerMask, QueryTriggerInteraction.Collide); agent.nearPlayer = false; agent.inHill = false; int drovingNeighbors = 0; foreach (Collider c in contextColliders) { if (c != agent.AgentCollider) // if the collider isn't its own { context.Add(c.transform); // add it to the context if (c.gameObject.layer == 12 && c.gameObject.GetComponent <HerdAgent>().inHerdBox > 0) { drovingNeighbors += 1; } } if (c.gameObject.layer == 11) // check if the player is near { //agent.state = 1; agent.nearPlayer = true; } else if (c.gameObject.tag == "Hill") { agent.inHill = true; } } // If the bison is in a player's herd box or near enough bison that are, it will drove if (agent.inHerdBox > 0 || (drovingNeighbors >= joinDroveThresh)) { agent.Drove(); } return(context); }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors, maintain current alignment if (context.Count == 0) { return(Vector3.zero); } // add all neighbor's alignment together and average Vector3 alignmentMove = Vector3.zero; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior foreach (Transform item in filterContext) { alignmentMove += item.GetComponent <Rigidbody>().velocity; // add the facing direction } alignmentMove /= context.Count; // average, alignmentMove is now the destination alignment return(alignmentMove); }
// Counts all pink and blue bison within 15 units the spawner, returns [blueCount, pinkCount] private int[] CountBison() { int blueCount = 0, pinkCount = 0; foreach (Collider c in Physics.OverlapSphere(transform.position, 15)) // <- number should match line 134 { if (c.GetComponent <HerdAgent>()) { HerdAgent herdAgent = c.GetComponent <HerdAgent>(); if (herdAgent.AgentHerd == herds[1]) { blueCount++; } else if (herdAgent.AgentHerd == herds[0]) { pinkCount++; } } } return(new int[] { blueCount, pinkCount }); }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors, maintain current alignment if (context.Count == 0) { return(agent.transform.forward); } // add all neighbor's alignment together and average Vector3 alignmentMove = Vector3.zero; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior foreach (Transform item in filterContext) { if (Vector3.SqrMagnitude(item.position - agent.transform.position) < herd.SquareAvoidanceRadius * 1.25f) // if the distance to the item is within the avoidance radius { alignmentMove += item.transform.forward; // add the facing direction } } alignmentMove /= context.Count; // average, alignmentMove is now the destination alignment return(alignmentMove); }
public override Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd) { // if no neighbors return no adjustment if (context.Count == 0) { return(Vector3.zero); } // add all points together and average Vector3 cohesionMove = Vector3.zero; List <Transform> filterContext = (filter == null) ? context : filter.Filter(agent, context); // this is a filtered behavior foreach (Transform item in filterContext) { cohesionMove += item.position; } cohesionMove /= context.Count; // average // create offset from agent position, the actual move of the agent cohesionMove -= agent.transform.position; cohesionMove = Vector3.SmoothDamp(agent.transform.forward, cohesionMove, ref currentVelocity, agentSmoothTime); // this is what makes this "smoothed" return(cohesionMove); }
public abstract List <Transform> Filter(HerdAgent agent, List <Transform> original);
public abstract Vector3 CalculateMove(HerdAgent agent, List <Transform> context, Herd herd);