public static IVehicle GetStartedVehicle <TVehicle>() where TVehicle : class, IVehicle, new() { IVehicleFactory factory; switch (new TVehicle()) { case Car c: factory = new CarFactory(); break; case Truck t: factory = new TruckFactory(); break; default: throw new NotImplementedException(); } var result = factory.CreateVehicle(); result.Start(); //Return IVehicle and NOT TVehicle //TVehicle would be the concrete type, we want the abstract type (IVehicle). return(result); }
private static void CreateTruck() { VehicleFactory truckFactory = new TruckFactory(); VehicleShop truckShop = new VehicleShop(truckFactory); truckShop.ConstructVehicle(); }
private void AddVehicles() { for (int i = 1; i <= 3; i++) { string[] vehicleArgs = Console.ReadLine() .Split(" ", StringSplitOptions.RemoveEmptyEntries); Vehicle vehicle = null; if (i == 1) { vehicle = CarFactory.CreateCar(vehicleArgs); } else if (i == 2) { vehicle = TruckFactory.CreateTruck(vehicleArgs); } else { vehicle = BusFactory.CreateBus(vehicleArgs); } vehicles.Add(vehicle); } }
// This method is called whenever the "Assign" button is clicked in the user interface. // The method checks which building is selected and which factory implementation is needed. // If a building already exists, the existing building transform is reused. Otherwise it is created. // The same goes for the container ship public void AssignFactoryToBuilding() { Transform factoryBuildingTransform = null; Transform containerShipTransform = null; switch (buildingDropdown.captionText.text) { case "Building A": CreateFactoryBuildingAndShip(0, 48, out factoryBuildingTransform, out containerShipTransform); break; case "Building B": CreateFactoryBuildingAndShip(1, 34, out factoryBuildingTransform, out containerShipTransform); break; case "Building C": CreateFactoryBuildingAndShip(2, 20, out factoryBuildingTransform, out containerShipTransform); break; } if (factoryBuildingTransform == null) { Debug.Log("Could not initialize or load factory transform"); return; } // Before attaching a new vehicle factory implementation (script) to the factory building, we need to deactivate the transform // This is necessary as the transform might otherwise access the factory implementation (script) while we are trying to // change it which would cause all sorts of issues... factoryBuildingTransform.gameObject.SetActive(false); // Out of laziness, we simply destroy any existing / attached vehicle factory implementation even if the same one is assigned again :-) VehicleFactory vehicleFactory = factoryBuildingTransform.gameObject.GetComponent <VehicleFactory>(); Destroy(vehicleFactory); // Depending on the selected vehicle factory implementation type, we then assign a new vehicle factory implementation (script) to the transform switch (factoryTypeDropdown.captionText.text) { case "CarFactory": CarFactory carFactory = factoryBuildingTransform.gameObject.AddComponent <CarFactory>(); carFactory.containerShipTransform = containerShipTransform; carFactory.factoryBuildingTransform = factoryBuildingTransform; break; case "TruckFactory": TruckFactory truckFactory = factoryBuildingTransform.gameObject.AddComponent <TruckFactory>(); truckFactory.containerShipTransform = containerShipTransform; truckFactory.factoryBuildingTransform = factoryBuildingTransform; break; } // Finally we reactivate the transform and let the newly attached vehicle factory implementation do its magic... factoryBuildingTransform.gameObject.SetActive(true); }
private Vehicle CreateVehicle(string inputLine) { var arr = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var type = arr[0]; var fuelQuantity = double.Parse(arr[1]); var fuelConsumptionPerKm = double.Parse(arr[2]); var fuelTank = double.Parse(arr[3]); if (type == "Car") { return(CarFactory.CraeteCar(fuelQuantity, fuelConsumptionPerKm, fuelTank)); } else if (type == "Truck") { return(TruckFactory.CraeteTruck(fuelQuantity, fuelConsumptionPerKm, fuelTank)); } else if (type == "Bus") { return(BusFactory.CraeteBus(fuelQuantity, fuelConsumptionPerKm, fuelTank)); } throw new ArgumentException(); }
public Engine() { carFactory = new CarFactory(); truckFactory = new TruckFactory(); busFactory = new BusFactory(); }
public override void Enter(params object[] args) { _screen = new RenderTarget2D(StateMachine.Game.GraphicsDevice, 224, 256); _spriteBatch = new SpriteBatch(StateMachine.Game.GraphicsDevice); var resetForNewGame = true; if (args.Length > 0 && args[0] is bool) { resetForNewGame = (bool)args[0]; } if (resetForNewGame) { _playerModel = new PlayerModel(); } else { _playerModel.ResetAfterLevel(); } var goals = new GoalContainerModel(); _views.Add(new BackgroundView(StateMachine.Game.Content, _spriteBatch)); _views.Add(new ScoreView(StateMachine.Game.Content, _spriteBatch, _playerModel)); _views.Add(new FrogPositionView(StateMachine.Game.Content, _spriteBatch, _playerModel)); _views.Add(new GoalView(StateMachine.Game.Content, _spriteBatch, goals)); var playerController = new PlayerController(_playerModel); _controllers.Add(playerController); _controllers.Add(new GoalController(_playerModel, goals, playerController)); _controllers.Add(new HomeAnimationController(goals)); var bulldozerRowModel = new VehicleRowModel(BulldozerFactory.CreateFirstStage(), 32); var racingCarRowModel = new VehicleRowModel(RacingCarFactory.CreateFirstStage(), 0, 128, VehicleGhost.NoGhost, VehicleDirection.LeftToRight, 2); var sedanCarRowModel = new VehicleRowModel(GenericCarFactory.CreateFirstStage(), 0, 32, VehicleGhost.Ghost, VehicleDirection.RightToLeft); var duneBuggyRowModel = new VehicleRowModel(GenericCarFactory.CreateFirstStage(y: 208, frame: 9), 0, 32, VehicleGhost.Ghost, VehicleDirection.RightToLeft); var trucksRowModel = new VehicleRowModel(TruckFactory.CreateFirstStage(), 0, 36, VehicleGhost.Ghost, VehicleDirection.RightToLeft); var models = new[] { bulldozerRowModel, racingCarRowModel, sedanCarRowModel, duneBuggyRowModel, trucksRowModel }; var vehicleView = new VehicleView(StateMachine.Game.Content, _spriteBatch, models); _views.Add(vehicleView); var vehicleController = new VehicleController(_playerModel, playerController, models); _controllers.Add(vehicleController); var riverRow1 = new RiverRowModel(LogFactory.CreateRow1FirstStage(), 0, 36, VehicleDirection.LeftToRight, new Rectangle(0, 48, 216, 16)); var riverRow2 = new RiverRowModel(LogFactory.CreateRow2FirstStage(), 0, 36, VehicleDirection.RightToLeft, new Rectangle(0, 64, 216, 16)); var riverRow3 = new RiverRowModel(LogFactory.CreateRow3FirstStage(), 64, 36, VehicleDirection.LeftToRight, new Rectangle(0, 80, 216, 16)); var riverRow4 = new RiverRowModel(LogFactory.CreateRow4FirstStage(), 0, 36, VehicleDirection.RightToLeft, new Rectangle(0, 96, 216, 16)); var riverRow5 = new RiverRowModel(LogFactory.CreateRow5FirstStage(), 32, 36, VehicleDirection.LeftToRight, new Rectangle(0, 112, 216, 16)); var riverModels = new[] { riverRow1, riverRow2, riverRow3, riverRow4, riverRow5 }; var riverView = new VehicleView(StateMachine.Game.Content, _spriteBatch, riverModels); _views.Add(riverView); var riverController = new RiverObjectController(_playerModel, playerController, riverModels); _controllers.Add(riverController); _views.Add(new PlayerView(StateMachine.Game.Content, _spriteBatch, _playerModel)); }