static void Main(string[] args) { try { Console.WriteLine("****************************简单工厂*********************************"); #region 简单工厂 { //Gold gold=new Gold (); //gold.Show();//这种方式也可以,但是没有遵循依赖倒置原则,使得我们的上端依赖于细节 } { //IFactions gold =new Gold//我们的上端不能依赖详细,所以我们可以把右边创建对象的操作分离出去 //gold.Show(); } { IFactions gold = ObjectFactory.CreateInstance(ObjectFactory.FactionsType.Wood);//我们的上端不能依赖详细,所以我们可以把右边创建对象的操作分离出去 gold.Show(); } #endregion Console.WriteLine("****************************工厂方法*********************************"); #region 工厂方法 //从简单工厂我们可以看到,虽然我们上端遵循了依赖倒置原则,但是工厂内部严重违背了单一职责原则和开闭原则 //这时我们就想到了工厂方法(顾名思义,将简单工厂创建对象进行分离) { IFactoryMethod factoryMethod = new WaterFactory(); IFactions water = factoryMethod.CreateInstance(); water.Show(); } #endregion Console.WriteLine("****************************抽象工厂*********************************"); #region 抽象工厂 { AbstractFactory goldFactory = new AGoldFactory(); IAuxiliarySkill auxiliarySkill = goldFactory.CreateAuxiliarySkill(); auxiliarySkill.ShowAllSkill(); IObstacleSkill obstacleSkill = goldFactory.CreateObstacleSkill(); obstacleSkill.ShowAllSkill(); } { AbstractFactory woodFactory = new AWoodFactory(); IAuxiliarySkill auxiliarySkill = woodFactory.CreateAuxiliarySkill(); auxiliarySkill.ShowAllSkill(); IObstacleSkill obstacleSkill = woodFactory.CreateObstacleSkill(); obstacleSkill.ShowAllSkill(); } #endregion } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
} = new string[5]; // X,Y,AreaSideLength,TDBPath,DEMPath // as base game doesn't contain a full System.XML.LINQ namespace and the .NET 4.8 is easier to work with, // other external process was created for TDB parsing internal void ProcessTDBExternally() { UnityEngine.Debug.Log(modDirectory); var psi = new ProcessStartInfo(); psi.FileName = $"{modDirectory}GMLParserPL.exe"; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; psi.Arguments = "\"" + Arguments[0] + "\"" + " " + "\"" + Arguments[1] + "\"" + " " + "\"" + Arguments[2] + "\"" + " " + "\"" + Arguments[3] + "\""; var p = new Process(); p.StartInfo = psi; p.Start(); NetFactory netFactory = new NetFactory(); BuildingFactory buildingFactory = new BuildingFactory(); PropFactory propFactory = new PropFactory(); ResourceFactory resourceFactory = new ResourceFactory(); TreeFactory treeFactory = new TreeFactory(); WaterFactory waterFactory = new WaterFactory(); waterFactory.SetSeaLevelTo0(); while (!p.StandardOutput.EndOfStream) { string line = p.StandardOutput.ReadLine(); SelectFactory(line, netFactory, buildingFactory, propFactory, resourceFactory, treeFactory, waterFactory); } netFactory.UpdateSegments(); UnityEngine.Debug.Log("TDB loaded"); UnityEngine.Debug.Log($"Buildings loaded/called/max: {buildingFactory.Temp}/{timesBuildingUsed}/{BuildingManager.MAX_BUILDING_COUNT}"); UnityEngine.Debug.Log($"Segments loaded/called/max: {netFactory.TempS}/{timesNetUsed}/{NetManager.MAX_SEGMENT_COUNT}"); UnityEngine.Debug.Log($"Net nodes loaded/max: {netFactory.TempN}/{NetManager.MAX_NODE_COUNT}"); UnityEngine.Debug.Log($"Props loaded/called/max: {propFactory.Temp}/{timesPropUsed}/{PropManager.MAX_PROP_COUNT}"); UnityEngine.Debug.Log($"Trees loaded/called/max: {treeFactory.Temp}/{timesTreeUsed}/{TreeManager.MAX_TREE_COUNT}"); UnityEngine.Debug.Log($"Resources loaded/called: {resourceFactory.Temp}/{timesResourceUsed}"); UnityEngine.Debug.Log($"Resources loaded/called: {waterFactory.Temp}/{timesWaterUsed}"); }
// as DEM couldn't be transfered via pipes or MMF and was cousing troubles in async, another process was created internal void ProcessDEMExternally() { var psi = new ProcessStartInfo(); psi.FileName = $"{modDirectory}ASCIIParserPL.exe"; psi.CreateNoWindow = true; psi.RedirectStandardOutput = true; psi.RedirectStandardInput = true; psi.RedirectStandardError = true; psi.UseShellExecute = false; psi.Arguments = "\"" + Arguments[0] + "\"" + " " + "\"" + Arguments[1] + "\"" + " " + "\"" + Arguments[4] + "\""; var p = new Process(); p.StartInfo = psi; p.Start(); TerrainFactory terrainFactory = new TerrainFactory(); WaterFactory waterFactory = new WaterFactory(); waterFactory.SetSeaLevelTo0(); while (!p.StandardOutput.EndOfStream) { string line = p.StandardOutput.ReadLine(); List <string> arguments = line.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (arguments[0] == "Terrain") { byte[] map = System.Convert.FromBase64String(arguments[1]); terrainFactory.SetTerrain(map); UnityEngine.Debug.Log("DEM imported"); } if (arguments[0] == "Error") { UnityEngine.Debug.Log(arguments[1]); } } }
static void Main(string[] args) { map[19, 19] = 1; int haveInput = 0; // choose difficulty level Console.WriteLine("Choose difficulty (Easy, Medium or Hard)"); string d = Console.ReadLine().ToLower(); // toLower just in case Console.Clear(); // Remove the Choose difficulty text Difficulty difficult = new Difficulty(); switch (d) { case "easy": difficult = Difficulty.Easy; break; case "medium": difficult = Difficulty.Medium; break; case "hard": difficult = Difficulty.Hard; break; } if (difficult == Difficulty.Easy) { timer = 100 * 2; } else if (difficult == Difficulty.Medium) { timer = 70 * 2; } else if (difficult == Difficulty.Hard) { timer = 50; } drawMap(map, buildings, true); while (!lose) { Thread.Sleep(timer); if (Console.KeyAvailable) { keyInfo = Console.ReadKey(); if (Convert.ToString(keyInfo.Key) == "Enter") { Console.Clear(); Console.WriteLine("What kind of building you want to build?"); Console.WriteLine("e) Electricity factory "); Console.WriteLine("f) Food factory "); Console.WriteLine("w) Water factory "); Console.WriteLine("h) Hospital "); Console.WriteLine("r) Residence "); Console.WriteLine("s) Security "); Console.WriteLine("Write only letter a, b, c, d, e or f "); string letter = Console.ReadLine(); Position position = new Position(); position.x = cursorX; position.y = cursorY; switch (letter) { case "e": buildings[countOfBuildings] = new ElectricityFactory('E', position, Color.Blue, ref electricity); break; case "f": buildings[countOfBuildings] = new FoodFactory('F', position, Color.Gray, ref electricity, ref food); break; case "w": buildings[countOfBuildings] = new WaterFactory('W', position, Color.Green, ref electricity, ref water); break; case "h": buildings[countOfBuildings] = new HospitalBuilding('H', position, Color.Pink, 1, ref health, ref electricity); break; case "r": buildings[countOfBuildings] = new ResidenceBuilding('R', position, Color.Yellow, 1, ref security, ref health, ref electricity, ref food, ref water, ref money); residenceBuildingsCount++; break; case "s": buildings[countOfBuildings] = new SecurityBuilding('S', position, Color.Red, 1, ref security, ref electricity); break; } countOfBuildings++; drawMap(map, buildings, true); } if (Convert.ToString(keyInfo.Key) == "LeftArrow") { cursorX--; } if (Convert.ToString(keyInfo.Key) == "RightArrow") { cursorX++; } if (Convert.ToString(keyInfo.Key) == "UpArrow") { cursorY--; } if (Convert.ToString(keyInfo.Key) == "DownArrow") { cursorY++; } if (cursorX < 0) { cursorX = 0; } if (cursorX >= Map_Size_X) { cursorX = Map_Size_X - 1; } if (cursorY < 0) { cursorY = 0; } if (cursorY >= Map_Size_Y) { cursorY = Map_Size_Y - 1; } Console.Clear(); keyInfo = new ConsoleKeyInfo(); EnemyMove(ref map, ref buildings, sizeOfEnemy); sizeOfEnemy++; drawMap(map, buildings, true); Console.SetCursorPosition(0, 20); Console.WriteLine("Electricity: {0} Food: {1} Water: {2} Happiness: {3} \nHealth: {4} Money: {5} Population: {6} Security: {7}", electricity.Amount, food.Amount, water.Amount, happiness.Amount, health.Amount, money.Amount, population.Amount, security.Amount); Console.WriteLine("Use up, down, left and right arrow keys to choose your position to build!"); Console.WriteLine("Press enter to choose a building!"); Console.SetCursorPosition(cursorX, cursorY); Console.WriteLine("X"); haveInput = 1; } else { drawMap(map, buildings, false); Console.SetCursorPosition(0, 20); Console.WriteLine("Electricity: {0} Food: {1} Water: {2} Happiness: {3} \nHealth: {4} Money: {5} Population: {6} Security: {7}", electricity.Amount, food.Amount, water.Amount, happiness.Amount, health.Amount, money.Amount, population.Amount, security.Amount); Console.WriteLine("Use up, down, left and right arrow keys to choose your position to build!"); Console.WriteLine("Press enter to choose a building!"); Console.SetCursorPosition(cursorX, cursorY); Console.WriteLine("X"); } } }
private void SelectFactory(string line, NetFactory netFactory, BuildingFactory buildingFactory, PropFactory propFactory, ResourceFactory resourceFactory, TreeFactory treeFactory, WaterFactory waterFactory) { if (!line.IsNullOrWhiteSpace()) { List <string> arguments = line.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); float[] points; float angle; switch (arguments[0]) { case "Resource": points = ReturnFloatsFromArg(arguments); NaturalResourceManager.Resource resource = (NaturalResourceManager.Resource)Enum .Parse(typeof(NaturalResourceManager.Resource), arguments[1], true); resourceFactory.CreateResource(new Vector2(points[0], points[1]), resource); timesResourceUsed++; break; case "Water": points = ReturnFloatsFromArg(arguments); var flowInOut = arguments[3].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Select(x => uint.Parse(x, CultureInfo.InvariantCulture)) .ToArray(); waterFactory.NewWaterSource(new Vector2(points[0], points[1]), flowInOut[0], flowInOut[1], flowInOut[2]); timesWaterUsed++; break; case "Net": points = ReturnFloatsFromArg(arguments); netFactory.Create(new Vector2(points[0], points[1]), new Vector2(points[2], points[3]), arguments[1]); timesNetUsed++; break; case "Building": points = ReturnFloatsFromArg(arguments); angle = float.Parse(arguments[3], CultureInfo.InvariantCulture); buildingFactory.Create(new Vector2(points[0], points[1]), angle, arguments[1]); timesBuildingUsed++; break; case "Tree": points = ReturnFloatsFromArg(arguments); treeFactory.Create(new Vector2(points[0], points[1]), arguments[1]); timesTreeUsed++; break; case "Prop": points = ReturnFloatsFromArg(arguments); angle = float.Parse(arguments[3], CultureInfo.InvariantCulture); propFactory.Create(new Vector2(points[0], points[1]), angle, arguments[1]); timesPropUsed++; break; case "Error": UnityEngine.Debug.Log(arguments[1]); break; default: UnityEngine.Debug.Log(arguments[0]); break; } } }
public override void Init() { InitGameComponents(); mPostProcessor = new PostProcessor(); mUnderWaterFx = Game1.Inst.Content.Load <Effect>("Effects/UnderWater"); mRT = GfxUtil.CreateRT(); var physicsSys = new PhysicsSystem(); physicsSys.Bounds = new BoundingBox(-worldSize * Vector3.One, worldSize * Vector3.One); physicsSys.InvSpatPartSize = 0.07f; physicsSys.Gravity *= 2.0f; physicsSys.WaterY = configs.WaterHeight; var inputSys = new InputSystem(); inputSys.WaterY = configs.WaterHeight; AddSystems( physicsSys, inputSys, new AISystem(), new AnimationSystem(), mParticleSys = new ParticleSystem(), new InventorySystem(), new CameraSystem(), new LogicSystem(), mRenderer = new RenderingSystem(), new Rendering2DSystem(), new HealthSystem(), new HitSystem() ); #if DEBUG AddSystem(new DebugOverlay()); #endif var heightmap = Heightmap.Load("Textures/" + configs.Map, stepX: 8, stepY: 8, smooth: false, scale: configs.HeightMapScale, yScale: configs.YScaleMap, randomTris: true, blur: 16, colorFn: configs.colorsMap); physicsSys.Heightmap = heightmap; inputSys.Heightmap = heightmap; base.Init(); WaterFactory.Create(configs.WaterHeight, configs.HeightMapScale, configs.HeightMapScale); SceneUtils.SpawnEnvironment(heightmap, configs.HeightMapScale); //add network after init since we dont want reinit and lose our connections. if (_networkSystem != null) { var sync = new GameObjectSyncSystem(_networkSystem._isMaster); _networkSystem.AddGameEvents(); sync.Init(); AddSystem(_networkSystem); AddSystem(sync); } // Camera entity float fieldofview = MathHelper.PiOver2; float nearplane = 0.1f; float farplane = 100f; player = AddEntity(); AddComponent(player, new CBody() { MaxVelocity = 5f, InvMass = 0.01f, SpeedMultiplier = 1, Radius = 0.7f, Aabb = new BoundingBox(new Vector3(-0.5f, -0.9f, -0.5f), new Vector3(0.5f, 0.9f, 0.5f)), LinDrag = 0.8f, ReachableArea = new BoundingBox(new Vector3(-1.5f, -2.0f, -1.5f), new Vector3(1.5f, 2.0f, 1.5f)), Restitution = 0.1f }); AddComponent(player, new CInput()); var playery = (heightmap.HeightAt(configs.Playerx, configs.Playerz)); var chitid = AddEntity(); AddComponent(chitid, new CHit() { PlayerId = player }); AddComponent(chitid, new CTransform() { Heading = MathHelper.PiOver2, Position = new Vector3(configs.Playerx, playery, configs.Playerz) + new CHit().HitBoxOffset }); AddComponent(chitid, new CBody() { Aabb = new BoundingBox(new Vector3(-0.4f, -1.3f, -0.4f), new Vector3(0.4f, 0.9f, 0.4f)) }); AddComponent(player, new CPlayer() { HitId = chitid }); var playerTransf = new CTransform() { Heading = MathHelper.PiOver2, Position = new Vector3(configs.Playerx, playery, configs.Playerz), Scale = new Vector3(0.5f) }; AddComponent(player, playerTransf); // Glossy helmet, lol! //var cubeMap = Game1.Inst.Content.Load<Effect>("Effects/CubeMap"); //var envMap = new EnvMapMaterial(mRenderer, player, playerTransf, cubeMap); AddComponent <C3DRenderable>(player, new CImportedModel() { animFn = SceneUtils.playerAnimation(player, 24, 0.1f), model = Game1.Inst.Content.Load <Model>("Models/viking"), fileName = "viking", /*materials = new Dictionary<int, MaterialShader> { * { 8, envMap } * }*/ }); AddComponent(player, new CSyncObject { fileName = "viking" }); AddComponent(player, new CInventory()); AddComponent(player, new CHealth { MaxHealth = 3, Health = 3, DeathSound = "Sounds/Effects/entia" }); AddComponent(player, new CScore { }); /* * AddComponent(player, new CLogic { * InvHz = 1.0f/30.0f, * Fn = (t, dt) => { * var cam = (CCamera)GetComponentFromEntity<CCamera>(player); * envMap.Update(); * } * }); */ AddComponent(player, new CCamera { Height = 3.5f, Distance = 3.5f, Projection = Matrix.CreatePerspectiveFieldOfView(fieldofview, Game1.Inst.GraphicsDevice.Viewport.AspectRatio, nearplane, farplane) , ClipProjection = Matrix.CreatePerspectiveFieldOfView(fieldofview * 1.2f, Game1.Inst.GraphicsDevice.Viewport.AspectRatio, nearplane * 0.5f, farplane * 1.2f) }); // Heightmap entity var mapMat = new ToonMaterial(Vector3.One * 0.2f, new Vector3(1.0f, 0.0f, 1.0f), // ignored Vector3.Zero, 40.0f, null, // diftex null, // normtex 1.0f, // normcoeff 5, // diflevels 2, // spelevels, true); // use vert col int hme = AddEntity(); AddComponent <C3DRenderable>(hme, new C3DRenderable { model = heightmap.Model, enableVertexColor = true, specular = 0.03f, // materials = new Dictionary<int, MaterialShader> { {0, mapMat } } }); AddComponent(hme, new CTransform { Position = Vector3.Zero, Rotation = Matrix.Identity, Scale = Vector3.One }); int heightMapId = AddEntity(); var heightMapComp = new CHeightmap() { Image = Game1.Inst.Content.Load <Texture2D>("Textures/" + configs.Map) }; var heightTrans = new CTransform() { Position = new Vector3(-590, 0, -590), Rotation = Matrix.Identity, Scale = new Vector3(1, 0.5f, 1) }; AddComponent <C3DRenderable>(heightMapId, heightMapComp); AddComponent(heightMapId, heightTrans); // manually start loading all heightmap components, should be moved/automated OnEvent("hit", data => { var key = data as int?; if (key == null) { return; } var transform = (CTransform)GetComponentFromEntity <CTransform>(key.Value); var id = AddEntity(); Func <float> rndSize = () => 0.05f + 0.1f * (float)rnd.NextDouble(); Func <Vector3> rndVel = () => new Vector3((float)rnd.NextDouble() - 0.5f, (float)rnd.NextDouble(), (float)rnd.NextDouble() - 0.5f); mParticleSys.SpawnParticles(100, () => new EcsComponent[] { new CParticle { Position = transform.Position, Velocity = 6.0f * rndVel(), Life = 1.7f, F = () => new Vector3(0.0f, -9.81f, 0.0f) }, new C3DRenderable { model = Game1.Inst.Content.Load <Model>("Models/blood") }, new CTransform { Position = transform.Position, Rotation = Matrix.Identity, Scale = rndSize() * Vector3.One } }); SceneUtils.CreateSplatter(transform.Position.X, transform.Position.Z, heightmap); SfxUtil.PlaySound("Sounds/Effects/Hit", randomPitch: true); }); OnEvent("game_end", data => { won = Game1.Inst.Scene.EntityHasComponent <CInput>((int)data); shouldLeave = true; // We reached the goal and wants to leave the scene- }); if ((_networkSystem != null && _networkSystem._isMaster) || _networkSystem == null) { Utils.SceneUtils.CreateAnimals(configs.NumFlocks, configs.HeightMapScale / 2); Utils.SceneUtils.CreateTriggerEvents(configs.NumTriggers, configs.HeightMapScale / 2); Utils.SceneUtils.CreateCollectables(configs.NumPowerUps, configs.HeightMapScale / 2); SceneUtils.SpawnBirds(configs); // Add tree as sprint goal int sprintGoal = AddEntity(); AddComponent(sprintGoal, new CBody() { Radius = 5, Aabb = new BoundingBox(new Vector3(-5, -5, -5), new Vector3(5, 5, 5)), LinDrag = 0.8f }); AddComponent(sprintGoal, new CTransform() { Position = new Vector3(100, -0, 100), Scale = new Vector3(1f) }); var treefilename = "tree"; AddComponent(sprintGoal, new CSyncObject()); AddComponent <C3DRenderable>(sprintGoal, new CImportedModel() { model = Game1.Inst.Content.Load <Model>("Models/" + treefilename), fileName = treefilename }); OnEvent("collision", data => { foreach (var key in Game1.Inst.Scene.GetComponents <CPlayer>().Keys) { if ((((PhysicsSystem.CollisionInfo)data).Entity1 == key && ((PhysicsSystem.CollisionInfo)data).Entity2 == sprintGoal) || (((PhysicsSystem.CollisionInfo)data).Entity2 == key && ((PhysicsSystem.CollisionInfo)data).Entity1 == sprintGoal)) { Game1.Inst.Scene.Raise("network_game_end", key); Game1.Inst.Scene.Raise("game_end", key); } } }); } Log.GetLog().Debug("WorldScene initialized."); InitHud(); SfxUtil.PlaySound("Sounds/Effects/horn_start"); var billboards = new [] { new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Grass", 1.0f), new Tuple <string, float>("Bush", 1.2f), new Tuple <string, float>("Flowers", 0.6f) }; var billboards2 = new [] { new Tuple <string, float>("Seaweed1", 0.6f), new Tuple <string, float>("Seaweed2", 0.6f), }; ; for (var i = 0; i < 10000; i++) { var bbs = billboards; var x = configs.HeightMapScale * ((float)rnd.NextDouble() - 0.5f); var z = configs.HeightMapScale * ((float)rnd.NextDouble() - 0.5f); var y = heightmap.HeightAt(x, z); if (y < configs.WaterHeight) { bbs = billboards2; } var bb = bbs[rnd.Next(0, bbs.Length)]; var s = (1.0f + 0.8f * (float)rnd.NextDouble()) * bb.Item2; AddComponent(AddEntity(), new CBillboard { Pos = new Vector3(x, y + 0.5f * s, z), Tex = Game1.Inst.Content.Load <Texture2D>("Textures/" + bb.Item1), Scale = s }); } //CreatePlatforms(heightmap); }