Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Cinemachine;
- using UnityEngine;
- namespace AnimalSimulator.Infrastructure.States
- {
- using Factory;
- using Tags;
- using Player;
- using Services;
- using Services.Gameplay;
- using Services.Inputs;
- public class LoadGameplayState : IPayloadedState<ServicesContainer>
- {
- private readonly GameStateMachine _stateMachine;
- public LoadGameplayState(GameStateMachine gameStateMachine) =>
- _stateMachine = gameStateMachine;
- public void Enter(ServicesContainer mainServices)
- {
- //GetSubContainers
- var gameplayContainer = mainServices.GetSubContainers(Constants.Containers.Gameplay);
- var uiContainer = mainServices.GetSubContainers(Constants.Containers.UI);
- // Get Services
- var gameplayUIService = uiContainer.Single<IGameplayUIService>();
- // - Gameplay
- var islandsService = gameplayContainer.Single<IIslandsService>();
- var gameFactory = gameplayContainer.Single<IGameplayFactory>();
- var inputService = gameplayContainer.Single<IInputService>();
- var inputFactory = gameplayContainer.Single<IInputFactory>();
- // Get Services properties
- var camera = gameFactory.CreateCameraBrain();
- // UI
- gameplayUIService.Initialize(camera, onRootCreated: InitInput);
- // Input
- void InitInput() =>
- inputService.CreateInputs(inputFactory);
- var player = InitPlayer(gameFactory, inputService);
- InitPlayerCamera(player, gameFactory);
- islandsService.Initialize(player);
- _stateMachine.Enter<GameLoopState>();
- }
- public void Exit()
- {
- }
- private GameObject InitPlayer(IGameplayFactory factory, IInputService input)
- {
- var player = factory.CreatePlayer();
- var inputController = player.GetComponent<PlayerInputController>();
- inputController.Construct(input);
- return player;
- }
- private void InitPlayerCamera(GameObject player, IGameplayFactory gameplayFactory)
- {
- var camera = gameplayFactory.CreateLookCamera();
- var freeLook = camera.GetComponent<CinemachineFreeLook>();
- var cameraAim = player.GetComponentInChildren<CameraAim>();
- var cameraTarget = player.GetComponentInChildren<CameraTarget>();
- freeLook.Follow = cameraAim.transform;
- freeLook.LookAt = cameraTarget.transform;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement