public override Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock) { if (context.Count == 0) { //if there is no context return a vector with no magnitude return(Vector3.zero); } //check for each filtered context item if it is to be avoided and add direction away from it to the move if need be Vector3 move = Vector3.zero; int avoidNr = 0; foreach (ContextItem item in context.FilteredToLayer(objectLayers.value)) { Vector3 position = item.ContextTransform.position; if ((position - unit.transform.position).sqrMagnitude < flock.SqrAvoidanceRadius) { avoidNr++; move += (unit.transform.position - position); } } if (avoidNr > 0) { //if there where items to be avoided, divide the move vector by the number of items move /= avoidNr; } return(move); }
public override Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock) { if (context.Count == 0) { //if there is not context return a vector with no magnitude return(Vector3.zero); } //get average position of context by adding them toghether and then dividing it by the ammount of context Vector3 move = Vector3.zero; foreach (ContextItem item in context.FilteredToLayer(1 << unit.gameObject.layer)) { move += item.ContextTransform.position; } move /= context.Count; //subtract units position to make the move the direction towards the average position move -= unit.transform.position; //damp move to prefent flickering move = Vector3.SmoothDamp(unit.transform.forward, move, ref flock.Velocity, 0.5f); return(move); }
protected virtual void Start() { foreach (Transform t in transform) { FlockUnit unit = t.GetComponentInChildren <FlockUnit>(); if (unit != null) { AddUnit(unit); } } }
public override Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock) { if (!bounds.Contains(unit.transform.position)) { //if the unit is outside the bounds, return a move that is towards the closest point on the bounds from the units position Vector3 position = unit.transform.position; return(flock.FlockBounds.ClosestPoint(position) - position); } else { return(Vector3.zero); } }
public void RemoveUnit(FlockUnit unit, Transform toParent) { if(mUnits.Remove(unit)) { if(toParent != null) { unit.transform.parent = toParent; } OnRemoveUnit(unit); if(removeCallback != null) { removeCallback(unit); } } }
public void AddUnit(FlockUnit unit) { //put in this group unit.transform.parent = transform; if (mUnits.Add(unit)) { OnAddUnit(unit); if (addCallback != null) { addCallback(unit); } } }
public void AddUnit(FlockUnit unit) { //put in this group unit.transform.parent = transform; if(mUnits.Add(unit)) { unit.id = id; OnAddUnit(unit); if(addCallback != null) { addCallback(unit); } } }
public void RemoveUnit(FlockUnit unit, Transform toParent) { if (mUnits.Remove(unit)) { if (toParent != null) { unit.transform.parent = toParent; } OnRemoveUnit(unit); if (removeCallback != null) { removeCallback(unit); } } }
/*public Player owner { get { if(mFlockUnit != null) { int index = Player.GroupToIndex(mFlockUnit.type); if(index >= 0 && index < Player.playerCount) { return Player.GetPlayer(index); } } return null; } }*/ protected override void Awake() { base.Awake(); mFlockUnit = GetComponentInChildren<FlockUnit>(); mListener = GetComponentInChildren<ActionListener>(); if(mFlockUnit != null) { mFlockUnit.groupMoveEnabled = false; } if(mListener != null) { mListener.enterCallback += OnActionEnter; mListener.hitEnterCallback += OnActionHitEnter; mListener.hitExitCallback += OnActionHitExit; mListener.finishCallback += OnActionFinish; } }
private void Start() { if (flockSize < 1) { flockSize = 1; } //FLOCK UNITY ARRAY AND BUFFER FlockUnit[] flockUnits = new FlockUnit[flockSize]; for (int i = 0; i < flockSize; i++) { var randomVector = UnityEngine.Random.insideUnitSphere; randomVector = new Vector3(randomVector.x * spawnBounds.x, randomVector.y * spawnBounds.y, randomVector.z * spawnBounds.z); var spawnPosition = transform.position + randomVector; var rotation = new Vector3(0, UnityEngine.Random.Range(0, 360), 0); flockUnits[i].flockPosition = transform.position; flockUnits[i].position = spawnPosition; flockUnits[i].rotation = Vector3.forward; flockUnits[i].speed = UnityEngine.Random.Range(minSpeed, maxSpeed); } flockUnitBuffer = new ComputeBuffer(flockSize, FLOCK_UNIT_SIZE); flockUnitBuffer.SetData(flockUnits); //SEND DATA TO COMPUTE SHADER computeKernelIndex = flockComputeShader.FindKernel("CSMain"); flockComputeShader.SetBuffer(computeKernelIndex, "_flockUnitBuffer", flockUnitBuffer); flockComputeShader.SetInt("_flockSize", flockSize); flockComputeShader.SetFloat("_cohesionDistance", cohesionDistance); flockComputeShader.SetFloat("_FOVAngle", FOVAngle); flockComputeShader.SetFloat("_cohesionWeight", cohesionWeight); flockComputeShader.SetFloat("_smoothDamp", smoothDamp); flockComputeShader.SetFloat("_minSpeed", minSpeed); flockComputeShader.SetFloat("_maxSpeed", maxSpeed); flockComputeShader.SetFloat("_avoidanceDistance", avoidanceDistance); flockComputeShader.SetFloat("_avoidanceWeight", avoidanceWeight); flockComputeShader.SetFloat("_alignmentDistance", alignmentDistance); flockComputeShader.SetFloat("_alignmentWeight", alignmentWeight); flockComputeShader.SetFloat("_boundsDistance", boundsDistance); flockComputeShader.SetFloat("_boundsWeight", boundsWeight); //SEND DATA TO MATERIAL flockUnityMaterial.SetBuffer("_flockUnitBuffer", flockUnitBuffer); }
public override Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock) { if (context.Count == 0) { //if there is not context return the unit's forward vector return(unit.transform.forward); } //get average heading of context by adding them toghether and then dividing it by the ammount of context Vector3 move = Vector3.zero; foreach (ContextItem item in context.FilteredToLayer(1 << unit.gameObject.layer)) { move += item.ContextTransform.forward; } move /= context.Count; return(move.normalized); }
// Use this for initialization protected virtual void Start() { // instantiate the spiders GameObject spiderTmp; spiders = new List <GameObject> (); for (int i = 0; i < spiderCount; i++) { spiderTmp = (GameObject)GameObject.Instantiate(prefab); FlockUnit db = spiderTmp.GetComponent <FlockUnit> (); db.spiders = this.spiders; db.swarm = this; // spawn inside circle Vector2 pos = new Vector2(transform.position.x, transform.position.z) + Random.insideUnitCircle * spawnRadius; spiderTmp.transform.position = new Vector3(pos.x, transform.position.y, pos.y); spiderTmp.transform.parent = transform; spiders.Add(spiderTmp); } }
public override Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock) { if (behaviours.Length == 0) { //if no behaviours are set return a vector without any magnitude return(Vector3.zero); } Vector3 move = Vector3.zero; foreach (var behaviour in behaviours) { //add clamped partial moves to the to be returned move Vector3 partialMove = behaviour.Behaviour.CalculateStep(unit, context, flock) * behaviour.Weight; if (partialMove.sqrMagnitude > behaviour.SqrWeight) { partialMove = partialMove.normalized * behaviour.Weight; } move += partialMove; } return(move); }
protected virtual void OnAddUnit(FlockUnit unit) { }
protected override void OnAddUnit(FlockUnit unit) { UnitEntity unitEntity = unit.GetComponentInChildren<UnitEntity>(); UnitStat stats = unitEntity.stats; if(unitEntity != null && stats != null) { HashSet<UnitEntity> units; if(!mUnitsByType.TryGetValue(stats.type, out units)) { units = new HashSet<UnitEntity>(); mUnitsByType.Add(stats.type, units); } units.Add(unitEntity); } }
protected override void Awake() { base.Awake(); mStats = GetComponentInChildren<UnitStat>(); mFlockUnit = GetComponentInChildren<FlockUnit>(); mListener = GetComponentInChildren<ActionListener>(); mActTarget = GetComponentInChildren<ActionTarget>(); mSpriteControl = GetComponentInChildren<UnitSpriteController>(); mWeapon = GetComponentInChildren<Weapon>(); mSpellCaster = GetComponentInChildren<SpellCaster>(); //hook calls up mStats.statChangeCallback += OnStatChange; if(mFlockUnit != null) { mFlockUnit.groupMoveEnabled = false; } if(mListener != null) { mListener.enterCallback += OnActionEnter; mListener.hitEnterCallback += OnActionHitEnter; mListener.hitExitCallback += OnActionHitExit; mListener.finishCallback += OnActionFinish; } }
protected virtual void OnRemoveUnit(FlockUnit unit) { }
public void Awake() { mFlockUnit = GetComponent<FlockUnit>(); }
void OnGroupRemove(FlockUnit flockUnit) { UnitEntity unit = flockUnit.GetComponent<UnitEntity>(); if(unit != null) { FlockActionController actionListen = unit.listener as FlockActionController; if(actionListen != null && actionListen.leader == transform) { actionListen.defaultTarget = null; actionListen.leader = null; } } }
void OnGroupAdd(FlockUnit flockUnit) { UnitEntity unit = flockUnit.GetComponent<UnitEntity>(); if(unit != null && unit != this) { FlockActionController actionListen = unit.listener as FlockActionController; if(actionListen != null && actionListen.leader == null) { actionListen.defaultTarget = actionTarget; actionListen.leader = transform; } } }
void RemoveUnit(FlockUnit unit) { UnitEntity unitEnt = unit.GetComponent<UnitEntity>(); if(unitEnt != null && unitEnt.stats != null && unitEnt.stats.type == mSummonType) { mSummonCount--; UpdateUnitCount(); } }
//at this point, unit is fully spawned void OnGroupUnitAdd(FlockUnit flockUnit) { UnitEntity unit = flockUnit.GetComponent<UnitEntity>(); if(unit != null) { FlockActionController actionListen = unit.listener as FlockActionController; if(actionListen != null) { actionListen.autoAttack = mAutoAttack; //actionListen.autoSpell = mAutoAttack; actionListen.defaultTarget = followAction; actionListen.leader = transform; } else { Debug.LogWarning("no action listener?"); } } else { Debug.LogWarning("no unit?"); } }
void Update() { //Internal variables float speed = velocity.magnitude; Vector3 averageVelocity = Vector3.zero; Vector3 averagePosition = Vector3.zero; float count = 0; float f = 0.0f; float lengthVelocityForce = 0.0f; Vector3 currentFlockPosition = currentFlock.position; Vector3 velocityForce; Vector3 average; Vector3 newVelocity; for (int i = 0; i < flockPositions.Length; i++) { Transform transform = flockPositions[i]; if (transform != currentFlock) { Vector3 currentPosition = transform.position; averagePosition += currentPosition; count++; velocityForce = currentFlockPosition - currentPosition; lengthVelocityForce = velocityForce.magnitude; if (lengthVelocityForce < followRadius) { if (lengthVelocityForce < avoidanceRadius) { f = 1.0f - (lengthVelocityForce / avoidanceRadius); if (lengthVelocityForce > 0) { averageVelocity += (velocityForce / lengthVelocityForce) * f * avoidanceForce; } } f = lengthVelocityForce / followRadius; FlockUnit tempOtherFlock = otherFlocks[i]; averageVelocity += tempOtherFlock.normalizedVelocity * f * followVelocity; } } } if (count > 0) { averageVelocity /= count; average = (averagePosition / count) - currentFlockPosition; } else { average = Vector3.zero; } velocityForce = originPoint.position - currentFlockPosition; lengthVelocityForce = velocityForce.magnitude; f = lengthVelocityForce / toOriginRange; if (lengthVelocityForce > 0) { originPush = (velocityForce / lengthVelocityForce) * f * toOriginForce; } if (speed < minSpeed && speed > 0) { velocity = (velocity / speed) * minSpeed; } newVelocity = velocity; newVelocity -= newVelocity * Time.deltaTime; newVelocity += randomPush * Time.deltaTime; newVelocity += originPush * Time.deltaTime; newVelocity += averageVelocity * Time.deltaTime; newVelocity += average.normalized * gravity * Time.deltaTime; velocity = Vector3.RotateTowards(velocity, newVelocity, turnSpeed * Time.deltaTime, 100.00f); currentFlock.rotation = Quaternion.LookRotation(velocity); currentFlock.Translate(velocity * Time.deltaTime, Space.World); normalizedVelocity = velocity.normalized; }
//unit is also about to be destroyed or released to entity manager void OnGroupUnitRemove(FlockUnit unit) { FlockActionController actionListen = unit.GetComponent<FlockActionController>(); if(actionListen != null) { actionListen.defaultTarget = null; actionListen.leader = null; } UnitEntity ent = unit.GetComponent<UnitEntity>(); if(ent != null) { //check unsummoning if(mCurActMode == ActMode.UnSummon && mCurUnSummonUnit == ent) { //refund based on hp percent mPlayer.stats.curResource += ent.stats.loveHPScale; mCurUnSummonUnit = null; } } }
public abstract Vector3 CalculateStep(FlockUnit unit, List <ContextItem> context, Flock flock);