Pro_Unit

LoadGameplayState

May 13th, 2021
672
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using AnimalSimulator.Data;
  3. using AnimalSimulator.GameLogic;
  4. using AnimalSimulator.Infrastructure.Factory;
  5. using AnimalSimulator.Infrastructure.Tags;
  6. using AnimalSimulator.Logic;
  7. using AnimalSimulator.Player;
  8. using AnimalSimulator.Services;
  9. using AnimalSimulator.Services.Inputs;
  10. using AnimalSimulator.Services.PersistentProgress;
  11. using AnimalSimulator.Services.StaticData;
  12. using AnimalSimulator.UI;
  13. using AnimalSimulator.UI.Elements;
  14. using AnimalSimulator.UI.Services.Factory;
  15. using Cinemachine;
  16. using UnityEngine;
  17.  
  18. namespace AnimalSimulator.Infrastructure.States
  19. {
  20.     public class LoadGameplayState : IPayloadedState<ServicesContainer>
  21.     {
  22.         private readonly GameStateMachine _stateMachine;
  23.  
  24.         public LoadGameplayState(GameStateMachine gameStateMachine) =>
  25.             _stateMachine = gameStateMachine;
  26.  
  27.         public void Enter(ServicesContainer services)
  28.         {
  29.             // Get Services
  30.             var progressService = services.Single<IPersistentProgressService>();
  31.  
  32.             var inputService = services.Single<IInputService>();
  33.             var gameFactory = services.Single<IGameFactory>();
  34.  
  35.             // Get Services properties
  36.             var progress = progressService.Progress;
  37.             var readers = gameFactory.ProgressReaders;
  38.             var camera = gameFactory.CreateCameraBrain();
  39.  
  40.             IZoneInfoObserver[] zoneInfoObservers = InitUI(services, camera);
  41.  
  42.             InitGameWorld(gameFactory, inputService, progress, zoneInfoObservers);
  43.             InformProgressReaders(readers, progress);
  44.  
  45.             _stateMachine.Enter<GameLoopState>();
  46.         }
  47.  
  48.         private static IZoneInfoObserver[] InitUI(ServicesContainer services, Camera camera)
  49.         {
  50.             var factory = services.Single<IUIFactory>();
  51.             var staticDataService = services.Single<IStaticDataService>();
  52.             var inputService = services.Single<IInputService>();
  53.  
  54.             factory.CreateRoot();
  55.            
  56.             factory.Input.CreateTouchpadForCamera();
  57.             inputService.Initialize();
  58.  
  59.             var uiRoot = factory.UIRoot;
  60.  
  61.             var actionIcon = uiRoot.GetComponentInChildren<ActionImageIcon>();
  62.  
  63.             actionIcon.Construct(staticDataService);
  64.             InteractableFollow follow = factory.CreateInteractableFollow();
  65.             follow.Construct(camera, staticDataService);
  66.  
  67.            
  68.  
  69.             return new IZoneInfoObserver[] {follow, actionIcon};
  70.         }
  71.  
  72.         public void Exit()
  73.         {
  74.         }
  75.  
  76.         private void InformProgressReaders(IEnumerable<ISavedProgressReader> readers, PlayerProgress progress)
  77.         {
  78.             foreach (var reader in readers)
  79.                 reader.LoadProgress(progress);
  80.         }
  81.  
  82.         private void InitGameWorld(IGameFactory factory, IInputService input, PlayerProgress progress,
  83.             params IZoneInfoObserver[] zoneInfoObservers)
  84.         {
  85.             var player = InitPlayer(factory, input);
  86.  
  87.             CameraFollow(player, factory);
  88.  
  89.             InitializeIslands(progress, player, zoneInfoObservers);
  90.         }
  91.  
  92.         private static void InitializeIslands(PlayerProgress progress, GameObject player,
  93.             params IZoneInfoObserver[] zoneInfoObservers)
  94.         {
  95.             var islands = Object.FindObjectsOfType<Island>();
  96.  
  97.             foreach (var island in islands)
  98.             {
  99.                 var zones = island.GetComponentsInChildren<InteractZone>();
  100.  
  101.                 foreach (var zone in zones)
  102.                     zone.Construct(zoneInfoObservers);
  103.  
  104.                 var uniqueId = island.GetComponent<UniqueId>();
  105.                 var id = uniqueId.Id;
  106.  
  107.                 var worldData = progress.WorldData;
  108.                 var islandID = worldData.IslandID;
  109.  
  110.                 if (islandID != id) continue;
  111.  
  112.                 var playerTransform = player.transform;
  113.  
  114.                 island.PlacePlayer(playerTransform);
  115.             }
  116.         }
  117.  
  118.         private GameObject InitPlayer(IGameFactory factory, IInputService input)
  119.         {
  120.             var player = factory.CreatePlayer();
  121.  
  122.             var inputController = player.GetComponent<PlayerInputController>();
  123.  
  124.             inputController.Construct(input);
  125.  
  126.             return player;
  127.         }
  128.  
  129.         private void CameraFollow(GameObject player, IGameFactory gameFactory)
  130.         {
  131.             var camera = gameFactory.CreateLookCamera();
  132.  
  133.             var freeLook = camera.GetComponent<CinemachineFreeLook>();
  134.  
  135.             var cameraAim = player.GetComponentInChildren<CameraAim>();
  136.             var cameraTarget = player.GetComponentInChildren<CameraTarget>();
  137.  
  138.             freeLook.Follow = cameraAim.transform;
  139.             freeLook.LookAt = cameraTarget.transform;
  140.         }
  141.     }
  142. }
RAW Paste Data