public void FlyweightSavesTime() { Stopwatch fly = new Stopwatch(); Stopwatch non = new Stopwatch(); fly.Start(); var df = new DragonFactory(); Parallel.For(0, 100, (i) => { var n = i % 10; var dragon = df.GetDragon("Dragon " + n); }); fly.Stop(); non.Start(); Parallel.For(0, 100, (i) => { var n = i % 10; var dragon = new Dragon { Name = "Dragon " + n }; }); non.Stop(); Assert.IsTrue(fly.ElapsedMilliseconds < non.ElapsedMilliseconds); }
public void CreateRedDragon() { var dragonFactory = new DragonFactory(); var dragon = dragonFactory.Create(DragonFactory.DragonBreed.Red); Assert.AreEqual("Red Dragon", dragon.Name); }
public void CreateBlackDragon() { var dragonFactory = new DragonFactory(); var dragon = dragonFactory.Create(DragonFactory.DragonBreed.Black); Assert.AreEqual("Black Dragon", dragon.Name); }
// Start is called before the first frame update void Start() { SSDirector director = SSDirector.getInstance(); director.currentSceneController = this; dragonFactory = Singleton <DragonFactory> .GetInstance; crystalFactory = Singleton <CrystalFactory> .GetInstance; actionManager = gameObject.AddComponent <DragonActionManager>() as DragonActionManager; LoadResources(); mainCamera.GetComponent <CameraMove>().target = player; scoreController = Singleton <ScoreController> .GetInstance; }
public static void AddPossibleSamples(bool randomAllowed) { if (freeIndices.Count > 0) // If there are free slots { if (readyPhenotypes.Count > 0) // If there are phenotypes to use { int range = Mathf.Min(freeIndices.Count, readyPhenotypes.Count); for (int i = 0; i < range; i++) { // Create dragons with the stored phenotypes Vector3 position = populationPositions[freeIndices[i]]; GameObject body = DragonFactory.CreateDragon(readyPhenotypes[i], position, Quaternion.Euler(20, 0, 0), freeIndices[i]); population.Add(body); GlobalSettings.SetBodyAndBonesRendering(body, GlobalSettings.renderingBodyAndBones); } freeIndices.RemoveRange(0, range); readyPhenotypes.RemoveRange(0, range); } else if (randomAllowed) { int range = freeIndices.Count; for (int i = 0; i < range; i++) { // Create dragons with the new, random phenotypes Phenotype phenotype = PhenotypeFactory.CreateRandom(); Vector3 position = populationPositions[freeIndices[i]]; GameObject body = DragonFactory.CreateDragon(phenotype, position, Quaternion.Euler(20, 0, 0), freeIndices[i]); population.Add(body); GlobalSettings.SetBodyAndBonesRendering(body, GlobalSettings.renderingBodyAndBones); } freeIndices.Clear(); } } }
static void FactoryMethod() { Console.WriteLine("\n---------------- PATRÓN FACTORY METHOD ----------------\n"); Console.WriteLine("Por favor, seleccione el enemigo a crear:"); Console.WriteLine("1. Orco"); Console.WriteLine("2. Dragón"); Console.WriteLine("3. Araña"); Console.Write("Ingrese una opción: "); var opcion = Convert.ToInt32(Console.ReadLine()); IEnemigoFactory factory = null; switch (opcion) { case 1: factory = new OrcoFactory(); break; case 2: factory = new DragonFactory(); break; case 3: factory = new ArañaFactory(); break; default: break; } ; Enemigo enemigo1 = factory.CrearEnemigo(); Enemigo enemigo2 = factory.CrearEnemigo(100, 100, 100); Console.WriteLine(enemigo1.ToString()); Console.WriteLine(enemigo2.ToString()); Console.ReadLine(); }
static void Main(string[] args) { Console.SetIn(new StreamReader("ZeroTests.txt")); var dragonFactory = new DragonFactory(); var numberOfDragons = int.Parse( Console.ReadLine()); var dragons = new DragonCollection(new TypeNameEqualityComparer(), new DragonNameComparer(), new DragonCollectionConsoleVisualizer()); for (var i = 0; i < numberOfDragons; i++) { var dragonInfo = Console.ReadLine(); var dragon = dragonFactory.Create(dragonInfo); dragons.Add(dragon); } dragons.Print(); }
static void Main(string[] args) { int dragonsCount = int.Parse(Console.ReadLine()); DragonFactory dragonFactory = new DragonFactory(); var dragons = new Dictionary <string, Dictionary <string, Dragon> >(); for (int counter = 0; counter < dragonsCount; counter++) { string[] dragonArgs = Console.ReadLine() .Split() .ToArray(); Dragon dragon = dragonFactory.CreateDragon(dragonArgs); if (dragons.ContainsKey(dragon.Type) == false) { dragons.Add(dragon.Type, new Dictionary <string, Dragon>()); } dragons[dragon.Type][dragon.Name] = dragon; } foreach (var type in dragons) { double averageDamage = type.Value.Values.Average(x => x.Damage); double averageHealth = type.Value.Values.Average(x => x.Health); double averageArmor = type.Value.Values.Average(x => x.Armor); string typeResult = $"{type.Key}::({averageDamage:F2}/{averageHealth:F2}/{averageArmor:F2})"; Console.WriteLine(typeResult); foreach (var name in type.Value.OrderBy(x => x.Key)) { string dragonResult = $"-{name.Key} -> damage: {name.Value.Damage}, health: {name.Value.Health}, armor: {name.Value.Armor}"; Console.WriteLine(dragonResult); } } }
// Initializes various environment variables and elements void Awake() { // Initialise static objects Global.Init(); PopulationControler.Init(); GlobalSettings.Init(); DragonFactory.Init(); CompleteDragonModel = (GameObject)Resources.Load("Dragon/CompleteDragon/TheDragon"); // Screen fade sceneChangeTimer = sceneChangeDuration; screenRect = new Rect(0, 0, Screen.width, Screen.height); fadeTexture = new Texture2D(1, 1); if (Global.GetCurrentScene() == Global.mainAIScene) { // Create Floors BaseQuad = (GameObject)Resources.Load("DragonWorld/Surroundings/BaseQuad"); standardFloorScale = BaseQuad.transform.localScale; standardFloorTextureScale = BaseQuad.GetComponent <Renderer>().sharedMaterial.mainTextureScale; // Local settings int numOfFloor = 5; int floorDist = 5; float floorAlpha = 0.225f; float wallAlpha = 0.1f; testfieldHeight = numOfFloor * floorDist; Color col, midcol = Color.white; GameObject floor; for (int pos = -numOfFloor * floorDist; pos <= numOfFloor * floorDist; pos += floorDist) { if (pos < 0) { col = Color.Lerp(Color.white, Color.red, (float)((-pos) / (float)(testfieldHeight)) * 1.6f); } else if (pos > 0) { col = Color.Lerp(Color.white, GlobalSettings.focusColor, (float)((pos) / (float)(testfieldHeight)) * 1.6f); } else { col = midcol; } col.a = floorAlpha; floor = createQuad(new Vector3(0, pos, 0), Quaternion.Euler(new Vector3(90, 0, 0)), col, standardFloorScale); col.a = 1; floorLevels.Add(new FloorLevel(floor, pos, col)); // *Mathf.Min(1f, ((float)(numOfFloor * floorDist) / (float)(Mathf.Abs(pos)))); } // Vertical floors simulating wind wallVerticalWind.Add(createQuad(new Vector3(0, 0, 0), Quaternion.Euler(new Vector3(0, 0, 0)), new Color(0, 0, 1, 0.5f), new Vector3(standardFloorScale.x, 2 * testfieldHeight, 1), new Vector2(1, 1))); // Floor around best testsample floorOfBest = createQuad(new Vector3(0, 0, 0), Quaternion.Euler(new Vector3(90, 0, 0)), new Color(0, 1, 0, 0.85f), standardFloorScale * 0.9f); // Walls wallBack = createQuad(new Vector3(0, 0, -floorLevels[1].floor.transform.localScale.y / 2), Quaternion.Euler(new Vector3(0, 0, 0)), new Color(1, 1, 1, wallAlpha), new Vector3(standardFloorScale.x, 2 * testfieldHeight, 1), new Vector2(1, 1)); wallFront = createQuad(new Vector3(0, 0, floorLevels[1].floor.transform.localScale.y / 2), Quaternion.Euler(new Vector3(0, 0, 0)), new Color(1, 1, 1, wallAlpha), new Vector3(standardFloorScale.x, 2 * testfieldHeight, 1), new Vector2(1, 1)); wallLeft = createQuad(new Vector3(-standardFloorScale.x / 2, 0, 0), Quaternion.Euler(new Vector3(0, 90, 0)), new Color(1, 1, 1, wallAlpha), new Vector3(floorLevels[1].floor.transform.localScale.y, 2 * testfieldHeight, 1), new Vector2(1, 1)); wallRight = createQuad(new Vector3(+standardFloorScale.x / 2, 0, 0), Quaternion.Euler(new Vector3(0, 90, 0)), new Color(1, 1, 1, wallAlpha), new Vector3(floorLevels[1].floor.transform.localScale.y, 2 * testfieldHeight, 1), new Vector2(1, 1)); } else if (Global.GetCurrentScene() == Global.cliffScene) { GlobalSettings.SetPhysInterpolation(true); } Debug.Log("Environment Started"); }
public void ChangeTaskMode(string mode) { GlobalSettings.taskMode = mode; switch (mode) { case "Learning": Debug.Log("Learning Mode"); // Destroy the full dragon and recreate population if (inLearningMode == false) { Destroy(completeDragon); Destroy(bestSample.gameObject); completeDragon = null; bestSample = null; PopulationControler.AddPossibleSamples(); GlobalSettings.SetBodyAndBonesRendering(renderPhTemp); GlobalSettings.SetMembraneRendering(renderMemTemp); ResizeFloorsFitToPopulation(); } renderPhTemp = GlobalSettings.renderingBodyAndBones; renderMemTemp = GlobalSettings.renderingMembranes; inLearningMode = true; if (bestSample == null) { Global.mainCameraScr.GotoWatchObject(PopulationControler.population[0]); } else { Global.mainCameraScr.GotoWatchObject(bestSample); } Global.mainCameraScr.GotoWatchObjectDistance(35 + Mathf.Sqrt(GlobalSettings.populationSize) * 35); testSampleScript = null; break; case "Testing": if (completeDragon != null) { return; } Debug.Log("Testing Mode"); renderPhTemp = GlobalSettings.renderingBodyAndBones; renderMemTemp = GlobalSettings.renderingMembranes; // Enable rendering of the new phenotype GlobalSettings.SetBodyAndBonesRendering(true); GlobalSettings.SetMembraneRendering(true); // // Save the current population for future use PopulationControler.SaveAndDestroyPopulation(); // Copy the phenotype of the best dragon Phenotype bestPhenotype; if (bestSample == null) { bestPhenotype = PopulationControler.GetBestPhenotype(); } else { bestPhenotype = bestSample.GetComponent <DragonScript>().wingSet.phenotype; } if (bestPhenotype == null) { bestPhenotype = PhenotypeFactory.CreateRandom(); } bestPhenotype.SetDependantBonethickness(0.6f, 0.1f); // Sets bone thicknes depending on the distance from the origin bestPhenotype.SetDependantBonecolor(new Color(0.3f, 1, 0.3f), new Color(0.25f, 0.25f, 1)); // Sets bone color depending on the distance from the origin bestSample = DragonFactory.CreateDragon(bestPhenotype, GlobalSettings.originPoint, Quaternion.Euler(20, 0, 0), 0); // // Calculate optimal gravity for floating float curMax; if (Global.GUIControlerScr.sampleToObserve != null) { curMax = Mathf.Max(bestSampleHeight, Global.GUIControlerScr.sampleVisScriptToObserve.localBestHeight); } else { curMax = bestSampleHeight; } customGravityOnBestSample = new Vector3(0, -(curMax / (bestPhenotype.maxFlapSteps * (GlobalSettings.requiredEvaluationFlaps + 0.5f))) * 75, 0); // ResizeFloors(1); inLearningMode = false; // Make everything visible GlobalSettings.SetBodyAndBonesRendering(bestSample, true); // Make body invisible (leaves wings visible) bestSample.gameObject.GetComponent <MeshRenderer>().enabled = false; // Set interpolation GlobalSettings.SetPhysInterpolation(bestSample, true); // Reenable membrane testSampleScript = bestSample.GetComponent <DragonScript>(); testSampleScript.wingSet.RenderVisualisation(true); Global.mainCameraScr.GotoWatchObject(bestSample); floorOfBest.transform.position = bestSample.transform.position; Global.GUIControlerScr.SwitchObjectToObserve(bestSample); justMovedToWatchNew = true; Global.mainCameraScr.GotoWatchObjectDistance(70); // Create the new surrounding full dragon model completeDragon = (GameObject)Instantiate(CompleteDragonModel, bestSample.transform.position, bestSample.transform.rotation); completeDragon.GetComponent <DragonControler>().followerObject = bestSample; break; } }
void Start() { if (Global.GetCurrentScene() == Global.mainAIScene) { renderPhTemp = GlobalSettings.renderingBodyAndBones; renderMemTemp = GlobalSettings.renderingMembranes; ChangeTaskMode("Learning"); } else if (Global.GetCurrentScene() == Global.cliffScene) { print("Initialisizing Cliff Scene Mode"); PopulationControler.Init(); // Generate a standard-phenotype for a demonstrationd ragon Phenotype mainDragonType = PhenotypeFactory.CreateForDemoDragon(); // Activate some graphical effects (not used during the phenotypes for the genetic algorithm) mainDragonType.SetDependantBonethickness(0.75f, 0.2f); // Sets bone thicknes depending on the distance from the origin mainDragonType.SetDependantBonecolor(new Color(0.3f, 0.6f, 0.2f), new Color(0.5f, 0.8f, 0.4f)); // Sets bone color depending on the distance from the origin GameObject theDragon = GameObject.Find("TheDragon"); GameObject dragonBody = null, dragonHead = null; foreach (Transform child in theDragon.transform) { if (child.gameObject.name == "MainBody") { dragonBody = child.gameObject; } if (child.gameObject.name == "Head") { dragonHead = child.gameObject; } } PopulationControler.population.Add(dragonBody); DragonFactory.AttachWing(dragonBody, mainDragonType); //Disable the wing movement foreach (HingeJoint joint in dragonBody.GetComponent <DragonScript>().wingSet.joints) { joint.useMotor = false; } dragonBody.GetComponent <DragonScript>().wingSet.running = false; dragonBody.GetComponent <DragonScript>().index = 0; theDragon.GetComponent <DragonControler>().PrepareComponents(); theDragon.GetComponent <DragonControler>().SwitchGravity(true); // Set rendering settings GlobalSettings.SetBodyAndBonesRendering(true); GlobalSettings.SetMembraneRendering(true); // Make the camera keep looking at the head Global.mainCameraScr.SetCameraFocusObject(dragonHead); bestSample = dragonBody; inLearningMode = false; } }