// Awake is called before the first frame update private void Awake() { // Get references to the game objects and their components dynamicAgent = GameObject.Find("DynamicAgent"); dynAgComp = dynamicAgent.GetComponent <DynamicAgent>(); targetController = GameObject.Find("TargetController"); stcComp = targetController.GetComponent <StaticTargetController>(); // Deactivate the game's game objects, so the game doesn't start // running right away Stop(); // Is this an optimization play or a single game play? if (optimize) { // Change the time scale as specified in the editor Time.timeScale = timeScale; // Should we show the game will optimizing? if (!showGame) { // If not, disable the camera GameObject goCam = GameObject.Find("Main Camera"); goCam.SetActive(false); } // Instantiate a new C# native random number generator, to be used // by the optimization thread threadRnd = new SRandom(); } }
public void TestNullAgentProgram() { DynamicAgent agent = new DynamicAgent(); Assert.AreEqual(DynamicAction.NO_OP, agent.Execute(null)); Assert.IsTrue(agent.IsAlive()); agent.SetAlive(false); Assert.IsFalse(agent.IsAlive()); Assert.IsInstanceOfType(agent, typeof(IAgent)); }
public void setUp() { IMap <ICollection <IPercept>, IAction> perceptSequenceActions = CollectionFactory.CreateInsertionOrderedMap <ICollection <IPercept>, IAction>(); perceptSequenceActions.Put(createPerceptSequence( new DynamicPercept("key1", "value1")), ACTION_1); perceptSequenceActions.Put(createPerceptSequence( new DynamicPercept("key1", "value1"), new DynamicPercept("key1", "value2")), ACTION_2); perceptSequenceActions.Put(createPerceptSequence( new DynamicPercept("key1", "value1"), new DynamicPercept("key1", "value2"), new DynamicPercept("key1", "value3")), ACTION_3); agent = new DynamicAgent(new TableDrivenAgentProgram(perceptSequenceActions)); }
// Use this for initialization void Start() { agent = GetComponent <DynamicAgent>(); rb = GetComponent <Rigidbody2D>(); }
// Collision avoidance behaviour public override SteeringOutput GetSteering(GameObject target) { // Initialize linear and angular forces to zero Vector2 linear = Vector2.zero; float angular = 0f; // 1. Find the target that's closest to collision // Get all possible targets DynamicAgent[] targets = FindObjectsOfType <DynamicAgent>(); // First collision time float shortestTime = float.PositiveInfinity; // Store the target that collides and auxiliar data DynamicAgent firstTarget = null; float firstMinSep = 0, firstDistance = 0; Vector2 firstRelPos = Vector2.zero, firstRelVel = Vector2.zero; // Loop through each target foreach (DynamicAgent currTarget in targets) { // Calculate the time to collision Vector2 relPos = transform.position - currTarget.transform.position; Vector2 relVel = currTarget.Velocity - Velocity; float relSpeed = relVel.magnitude; float timeToCollision = Vector2.Dot(relPos, relVel) / (relSpeed * relSpeed); // Check if it is going to be a collision at all float distance = relPos.magnitude; float minSep = distance - relSpeed * timeToCollision; if (minSep > 2 * radius) { continue; } // Check if it is the shortest if (timeToCollision > 0 && timeToCollision < shortestTime) { // Store the time, target and other data shortestTime = timeToCollision; firstTarget = currTarget; firstMinSep = minSep; firstDistance = distance; firstRelPos = relPos; firstRelVel = relVel; } } // 2. Calculate the steering // Was a target found? if (firstTarget != null) { Vector2 relPos; // If we're going to hit exactly or if we're already colliding, // then do the steering based on current position if (firstMinSep <= 0 || firstDistance < 2 * radius) { relPos = transform.position - firstTarget.transform.position; } // Otherwise determine the future relative position else { relPos = firstRelPos + firstRelVel * shortestTime; } // Avoid the target linear = relPos.normalized * MaxAccel; } // Output the steering return(new SteeringOutput(linear, angular)); }
// Use this for initialization protected virtual void Awake() { agent = GetComponent <DynamicAgent>(); rb = GetComponent <Rigidbody2D>(); }