Pro_Unit

LoadGameplayState

May 14th, 2021
721
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Cinemachine;
  2. using UnityEngine;
  3.  
  4. namespace AnimalSimulator.Infrastructure.States
  5. {
  6.     using Factory;
  7.     using Tags;
  8.     using Player;
  9.     using Services;
  10.     using Services.Gameplay;
  11.     using Services.Inputs;
  12.  
  13.     public class LoadGameplayState : IPayloadedState<ServicesContainer>
  14.     {
  15.         private readonly GameStateMachine _stateMachine;
  16.  
  17.         public LoadGameplayState(GameStateMachine gameStateMachine) =>
  18.             _stateMachine = gameStateMachine;
  19.  
  20.         public void Enter(ServicesContainer mainServices)
  21.         {
  22.             //GetSubContainers
  23.             var gameplayContainer = mainServices.GetSubContainers(Constants.Containers.Gameplay);
  24.             var uiContainer = mainServices.GetSubContainers(Constants.Containers.UI);
  25.  
  26.             // Get Services
  27.             var gameplayUIService = uiContainer.Single<IGameplayUIService>();
  28.             // - Gameplay
  29.             var islandsService = gameplayContainer.Single<IIslandsService>();
  30.             var gameFactory = gameplayContainer.Single<IGameplayFactory>();
  31.             var inputService = gameplayContainer.Single<IInputService>();
  32.             var inputFactory = gameplayContainer.Single<IInputFactory>();
  33.            
  34.             // Get Services properties
  35.             var camera = gameFactory.CreateCameraBrain();
  36.            
  37.             // UI
  38.             gameplayUIService.Initialize(camera, onRootCreated: InitInput);
  39.            
  40.             // Input
  41.             void InitInput() =>
  42.                 inputService.CreateInputs(inputFactory);
  43.  
  44.             var player = InitPlayer(gameFactory, inputService);
  45.  
  46.             InitPlayerCamera(player, gameFactory);
  47.  
  48.             islandsService.Initialize(player);
  49.  
  50.             _stateMachine.Enter<GameLoopState>();
  51.         }
  52.  
  53.         public void Exit()
  54.         {
  55.         }
  56.  
  57.         private GameObject InitPlayer(IGameplayFactory factory, IInputService input)
  58.         {
  59.             var player = factory.CreatePlayer();
  60.  
  61.             var inputController = player.GetComponent<PlayerInputController>();
  62.  
  63.             inputController.Construct(input);
  64.  
  65.             return player;
  66.         }
  67.  
  68.         private void InitPlayerCamera(GameObject player, IGameplayFactory gameplayFactory)
  69.         {
  70.             var camera = gameplayFactory.CreateLookCamera();
  71.  
  72.             var freeLook = camera.GetComponent<CinemachineFreeLook>();
  73.  
  74.             var cameraAim = player.GetComponentInChildren<CameraAim>();
  75.             var cameraTarget = player.GetComponentInChildren<CameraTarget>();
  76.  
  77.             freeLook.Follow = cameraAim.transform;
  78.             freeLook.LookAt = cameraTarget.transform;
  79.         }
  80.     }
  81. }
RAW Paste Data