// Run the main algorithm performing the operations // required of the "Seperation" behaviour. private Vector2 computeSeperation() { ReturnVector vecFunc = seperationVector; Finalization finalizeFunc = seperationFinalize; return(runAlgorithm(vecFunc, finalizeFunc)); }
// Run the main algorithm performing the operations // required of the "Alignment" behaviour. private Vector2 computeAlignment() { ReturnVector vecFunc = alignmentVector; Finalization finalizeFunc = alignmentFinalize; return(runAlgorithm(vecFunc, finalizeFunc)); }
// Main algorithmic 'formula' of alignment, cohesion and seperation. // The passed functions handle the minute differences. private Vector2 runAlgorithm(ReturnVector vecFunc, Finalization finalizeFunc) { Vector2 velocity = Vector2.zero; uint neighbourCount = 0; // Ugh... Some super-annoying bugs can occur with the agent list when the frog eats a fly or the game is restarted. // This check as well as "staleAgents" below means that we should avoid any null reference crap. EnsureFlocksOK(); Hashtable agents = (Hashtable)flocks[this.tag]; List <GameObject> staleAgents = new List <GameObject>(); foreach (object o in agents.Keys) { GameObject agent = (GameObject)o; if (agent == null) { staleAgents.Add(agent); continue; } if (agent == gameObject) { continue; } // Find neighbours of our agent to include in the calculation. if (Vector2.Distance(agent.transform.position, transform.position) < neighbourDist) { //Debug.Log("Found neighbour"); velocity += vecFunc(agent); neighbourCount += 1; } } foreach (GameObject staleAgent in staleAgents) { agents.Remove(staleAgent); } if (neighbourCount == 0) { return(velocity); } return(finalizeFunc(velocity, neighbourCount)); }
public override Statement EmitStatement(EmitMode mode) { if (mode == EmitMode.ForStorage) { SaveObjectID(); SaveLocator(); } else { RemoveObjectID(); RemoveLocator(); } try { IMetaData result; if ((mode != EmitMode.ForRemote) && (DeclarationText != null)) { SourceStatement statement = new SourceStatement(); statement.Source = DeclarationText + InitializationText + AggregationText + FinalizationText; result = statement; } else { CreateAggregateOperatorStatement statement = new CreateAggregateOperatorStatement(); statement.OperatorName = Schema.Object.EnsureRooted(OperatorName); foreach (Operand operand in Operands) { FormalParameter formalParameter = new FormalParameter(); formalParameter.Identifier = operand.Name; formalParameter.TypeSpecifier = operand.DataType.EmitSpecifier(mode); formalParameter.Modifier = operand.Modifier; statement.FormalParameters.Add(formalParameter); } statement.ReturnType = ReturnDataType.EmitSpecifier(mode); #if USEVIRTUAL statement.IsVirtual = IsVirtual; statement.IsAbstract = IsAbstract; statement.IsOverride = IsOverride; statement.IsReintroduced = IsReintroduced; #endif if ((mode == EmitMode.ForRemote) && !IsRemotable) { statement.Initialization.Block = new Block(); statement.Aggregation.Block = new Block(); statement.Finalization.Block = new Block(); } else { Initialization.EmitStatement(mode, statement.Initialization); Aggregation.EmitStatement(mode, statement.Aggregation); Finalization.EmitStatement(mode, statement.Finalization); } result = statement; } result.MetaData = MetaData == null ? null : MetaData.Copy(); if (SessionObjectName != null) { if (result.MetaData == null) { result.MetaData = new MetaData(); } result.MetaData.Tags.AddOrUpdate("DAE.GlobalObjectName", OperatorName, true); } return((Statement)result); } finally { if (mode == EmitMode.ForStorage) { RemoveObjectID(); RemoveLocator(); } } }