private static void AfterSceneLoad() { // Initialize basic game systems. var userInterfaceSystem = new UserInterfaceSystem(); var controlSystem = new ControlSystem(resourcesData.gameplaySettings); var keysSystem = new KeysSystem(resourcesData.stagePrefabs); var stageSystem = new StageSystem(resourcesData.stagePrefabs, keysSystem); var ballSystem = new BallSystem(resourcesData.ballPrefabs, stageSystem); var gameCurrencySystem = new GameCurrencySystem(userInterfaceSystem); var monetizationSystem = new MonetizationSystem(resourcesData.monetizationSettings, gameCurrencySystem); var shopSystem = new ShopSystem(resourcesData.shopModel, ballSystem, gameCurrencySystem, monetizationSystem); // Initialize gameplay systems. var gameInteractionsSystem = new GameInteractionsSystem(ballSystem, keysSystem, stageSystem); var gameInterfaceSystem = new GameInterfaceSystem(stageSystem, gameInteractionsSystem); // Register a proxy service for gameplay systems. var gameplaySystemsProxy = new GameplaySystemsProxy(); gameplaySystemsProxy.Add( gameInteractionsSystem, gameInterfaceSystem); // Initialize the state machine system with all dependencies. var gameStateMachine = new GameStateMachine( gameplaySystemsProxy, userInterfaceSystem, controlSystem, stageSystem, ballSystem, keysSystem, gameCurrencySystem, monetizationSystem); // Preserve links to game services. var storage = new GameObject("[Game Services]").AddComponent <GameServices>(); storage.Add( userInterfaceSystem, controlSystem, keysSystem, stageSystem, ballSystem, gameCurrencySystem, monetizationSystem, shopSystem); storage.Add( gameplaySystemsProxy, gameStateMachine); // Free the static reference to resources data - it's held by services from now. resourcesData = null; // Since there is only one scene in the project, we just link this very bootstrap script to the event of reloading scene. SceneManager.sceneLoaded += RebootOnNextSceneLoaded; }
public GameCurrencySystem(UserInterfaceSystem ui) { map = new Dictionary <GameCurrency, CurrencyValue>(); buffers = new Dictionary <GameCurrency, CurrencyValue>(); var currencies = (GameCurrency[])System.Enum.GetValues(typeof(GameCurrency)); System.Action <GameCurrency, int> onSetMainCurrencyValueCallback = null; onSetMainCurrencyValueCallback += (currency, value) => PersistentData.SetIntEncoded(GetType(), (int)currency, value); onSetMainCurrencyValueCallback += ui.DisplayCurrency; System.Action <GameCurrency, int> onSetBufferCurrencyValueCallback = ui.DisplayBufferCurrency; foreach (var currency in currencies) { var persistentCurrencyValue = new CurrencyValue(true, currency, onSetMainCurrencyValueCallback); persistentCurrencyValue.Value = PersistentData.GetIntDecoded(GetType(), (int)currency); map.Add(currency, persistentCurrencyValue); var bufferCurrencyValue = new CurrencyValue(true, currency, onSetBufferCurrencyValueCallback); buffers.Add(currency, bufferCurrencyValue); } }
private void DefineScene() { var device = Services.GetService <IGraphicsDeviceService>().DirectXDevice; scene = new Scene(Services); var cube = CubeMesh.New(device, modelOperations: ModelAnalyzer.DetectModelRequirements(vsFlags)); Content.Store(cube.Name, cube); var modelSystem = new ContentLoadingSystem(); var sInitialization = new InitializationSystem(); var cameraSystem = new PerspectiveCameraSystem(); var lightSystem = new PointLightSystem(); var positionSystem = new TransformSystem(); var sOptimization = new OptimizationSystem(); var sRendering = new RenderSystem(); var sUI = new UserInterfaceSystem(); scene.BeginDesign(); scene.AddSystem(modelSystem); scene.AddSystem(cameraSystem); scene.AddSystem(lightSystem); scene.AddSystem(sInitialization); scene.AddSystem(positionSystem); scene.AddSystem(sOptimization); scene.AddSystem(sRendering); scene.AddSystem(sUI); Entity root = scene.CreateEntity("Root"); root.RegisterComponent(new TransformComponent()); root.RegisterComponent(new PositionComponent()); Entity camera = scene.CreateEntity("Camera"); camera.RegisterComponent(new CameraComponent { }); camera.RegisterComponent(new PositionComponent { Position = new Vector3(0, 0f, 3f) }); camera.RegisterComponent(new UpdateComponent { UpdateFrequency = UpdateFrequency.RealTime }); Entity pointLight = scene.CreateEntity("PointLight"); pointLight.RegisterComponent(new PointLightComponent { }); pointLight.RegisterComponent(new PositionComponent { Position = new Vector3(0, 0f, 3f) }); pointLight.RegisterComponent(new UpdateComponent { UpdateFrequency = UpdateFrequency.RealTime }); pointLight.RegisterComponent(new ParentComponent() { Parent = root }); pointLight.RegisterComponent(new TransformComponent()); previewModel = scene.CreateEntity("PreviewModel"); previewModel.RegisterComponent(new PositionComponent()); previewModel.RegisterComponent(new TransformComponent()); previewModel.RegisterComponent(new ModelComponent() { AssetName = cube.Name }); previewModel.RegisterComponent(new UpdateComponent() { UpdateFrequency = UpdateFrequency.RealTime }); previewModel.RegisterComponent(new MaterialComponent() { Diffuse = Color.Blue }); previewModel.RegisterComponent(cShader); Entity ui = scene.CreateEntity("UI"); ui.RegisterComponent(new UserInterfaceComponent { Overlay = Viewer.CreateOverlay(Services, this) }); ui.RegisterComponent(new UpdateComponent() { UpdateFrequency = UpdateFrequency.RealTime }); scene.EndDesign(); }
public GameStateMachine( IGameplaySystem gameplayProxy, UserInterfaceSystem ui, ControlSystem control, StageSystem stage, BallSystem ball, KeysSystem keys, GameCurrencySystem currencies, MonetizationSystem monetization) : base() { // Iterate through all states and link UI (and other capable systems) to every state events. var allStates = (GameState[])System.Enum.GetValues(typeof(GameState)); foreach (var state in allStates) { AddCallbackOnEnter(state, () => ui.OnEnterState(state)); } // Define gameplay initialization callbacks. System.Action initFirstSubstageAction = stage.CreateFirstSubstage; System.Action initCurrentSubstageAction = stage.CreateCurrentSubstage; initFirstSubstageAction += ball.SpawnFirstBall; initCurrentSubstageAction += ball.SpawnFirstBall; // Link gameplay initialization systems to the correct callbacks. AddCallbackOnEnter(GameState.StartScreen, initFirstSubstageAction); AddCallbackOnEnter(GameState.PreStage, initFirstSubstageAction); AddCallbackOnEnter(GameState.Gameplay, control.Reset); AddCallbackOnTransition(GameState.NoMoreBalls, GameState.Gameplay, initCurrentSubstageAction); AddCallbackOnTransition(GameState.GetExtraBall, GameState.Gameplay, ball.SpawnFirstBall); AddCallbackOnTransition(GameState.NextSubstage, GameState.Gameplay, initCurrentSubstageAction); // Link currency gain system to the correct callbacks. AddCallbackOnEnter(GameState.StageFinished, () => { int coinsAmount = monetization.GetCoinsForVictory(); currencies.SetBuffer(GameCurrency.Coins, coinsAmount); ball.SpawnBallsInCollector(coinsAmount); }); AddCallbackOnExit(GameState.BallsSold, () => { currencies.AddFromBuffer(GameCurrency.Coins); ball.ClearCollector(); }); // Link resolutions to the "instant pass" states. AddCallbackOnEnter(GameState.GetExtraBall, () => Advance(GameState.Gameplay)); AddCallbackOnEnter(GameState.NextSubstage, () => { bool didPassLastSubstage; stage.PassCurrentSubstage(out didPassLastSubstage); if (didPassLastSubstage) { Advance(GameState.StageFinished); } else { Advance(GameState.Gameplay); } }); AddCallbackOnEnter(GameState.Chests, () => { if (!keys.IsEnough(3)) { Advance(GameState.PreStage); } }); AddCallbackOnExit(GameState.Chests, () => { if (keys.IsEnough(3)) { keys.Spend(3); } }); AddCallbackOnEnter(GameState.PreStage, () => Advance(GameState.Gameplay)); // Link gameplay proxy to the gameplay state callbacks. AddCallbackOnEnter(GameState.Gameplay, gameplayProxy.OnGameplayStarted); AddCallbackOnExit(GameState.Gameplay, gameplayProxy.OnGameplayStopped); // Link the state machine operations to UI (and other capable systems). ui.RegisterAdvanceToStateAction(Advance); control.AddCallbackOnBeginPull(() => Advance(GameState.Gameplay)); stage.SetSubstageFailedAction(() => Advance(GameState.NoMoreBalls)); stage.SetNextSubstageAction(() => Advance(GameState.NextSubstage)); monetization.SetAdvanceToStateCallbackForVideoRewardButton(rewardType => { switch (rewardType) { case VideoRewardType.ExtraBall: Advance(GameState.GetExtraBall); break; case VideoRewardType.SellBalls: Advance(GameState.BallsSold); break; } }); // Register every possible transition. AddTransition(GameState.StartScreen, GameState.Gameplay); AddTransition(GameState.StartScreen, GameState.BallShop); AddTransition(GameState.BallShop, GameState.StartScreen); AddTransition(GameState.Gameplay, GameState.NoMoreBalls); AddTransition(GameState.Gameplay, GameState.NextSubstage); AddTransition(GameState.NoMoreBalls, GameState.GetExtraBall); AddTransition(GameState.NoMoreBalls, GameState.Gameplay); AddTransition(GameState.GetExtraBall, GameState.Gameplay); AddTransition(GameState.NextSubstage, GameState.Gameplay); AddTransition(GameState.NextSubstage, GameState.StageFinished); AddTransition(GameState.StageFinished, GameState.BallsSold); AddTransition(GameState.BallsSold, GameState.Chests); AddTransition(GameState.Chests, GameState.PreStage); AddTransition(GameState.PreStage, GameState.Gameplay); // Finally, enter the initial state... Enter(GameState.StartScreen); }