Advertisement
Pro_Unit

Animal

Aug 28th, 2023
2,592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. using Game.Boosters;
  6. using Game.Breeds;
  7. using Game.CameraLogic.Animals.Behaviours;
  8. using Game.Characteristics;
  9. using Game.Data;
  10. using Game.Infrastructure.Factory;
  11. using Game.Infrastructure.GamePreparing;
  12. using Game.Logic;
  13. using Game.Logic.Characteristics;
  14. using Game.StaticData;
  15. using Game.UI.Elements;
  16. using Game.UtilityAI;
  17.  
  18. using MalbersAnimations;
  19. using MalbersAnimations.Controller;
  20. using MalbersAnimations.Controller.AI;
  21.  
  22. using UnityEngine;
  23.  
  24. using Zenject;
  25.  
  26. using Stats = Game.Characteristics.Stats;
  27.  
  28. namespace Game.CameraLogic.Animals
  29. {
  30.     [RequireComponent(typeof(PreviewComponent))]
  31.     [RequireComponent(typeof(CameraPoints))]
  32.     public class Animal : MonoBehaviour
  33.     {
  34.         public PreviewComponent PreviewComponent;
  35.         public CameraPoints CameraPoints;
  36.  
  37.         private readonly Dictionary<Type, IAnimalService> _services = new();
  38.  
  39.         public AnimalSaveData Save { get; private set; }
  40.  
  41.         [SerializeField] private AnimalUI _animalUI;
  42.         [SerializeField] private MalbersInput _input;
  43.         [SerializeField] private MAnimalAIControl _aiControl;
  44.         [SerializeField] private StatsStaticData _statsStaticData;
  45.         [SerializeField] private Animator _animator;
  46.         [SerializeField] private MAnimal _mAnimal;
  47.         [SerializeField] private Renderer _renderer;
  48.         [SerializeField] private Tameable _tameable;
  49.         [SerializeField] private Tamer _tamer;
  50.         [SerializeField] private AnimalTrigger _animalTrigger;
  51.         [SerializeField] private AnimalAI _ai;
  52.         [SerializeField] private FollowPoint[] _followPoints;
  53.  
  54.         private void OnValidate()
  55.         {
  56.             CameraPoints ??= GetComponent<CameraPoints>();
  57.             PreviewComponent ??= GetComponentInChildren<PreviewComponent>();
  58.             _aiControl ??= GetComponentInChildren<MAnimalAIControl>(true);
  59.             _input ??= GetComponent<MalbersInput>();
  60.             _animalUI ??= GetComponentInChildren<AnimalUI>();
  61.  
  62.             _animator ??= GetComponent<Animator>();
  63.  
  64.             _followPoints = GetComponentsInChildren<FollowPoint>();
  65.  
  66.             _mAnimal ??= GetComponent<MAnimal>();
  67.             _animalTrigger ??= GetComponentInChildren<AnimalTrigger>();
  68.             _tameable ??= GetComponent<Tameable>();
  69.             _tamer ??= GetComponent<Tamer>();
  70.         }
  71.  
  72.         private readonly List<ITickable> _tickables = new();
  73.  
  74.         public void Inject(Stats stats, SpecieAnimal specie, AnimalSaveData save)
  75.         {
  76.             Specie = specie;
  77.             Save = save;
  78.             Breed = specie.Breed(save);
  79.  
  80.             AIAnimalService aiAnimalService = AIAnimalService();
  81.             FollowPointsService followPointsService = FollowPointsService();
  82.             StatModificatorContainer statModificatorContainer = StatModificatorContainer(stats);
  83.             AnimalCustomization animalCustomization = AnimalCustomization(specie);
  84.             animalCustomization.ReadFromSave();
  85.  
  86.             var level = new Level(save.Level);
  87.             var experience = new Experience(level, Specie, save.Experience);
  88.  
  89.             _tamer.Inject(followPointsService);
  90.             _services.Add(typeof(IStats), stats);
  91.             _services.Add(typeof(IAnimalUI), _animalUI);
  92.             _services.Add(typeof(ITameable), _tameable);
  93.             _services.Add(typeof(IFollowPointsService), followPointsService);
  94.             _services.Add(typeof(IAIAnimalService), aiAnimalService);
  95.             _services.Add(typeof(IAnimalInputService), new AnimalInputService(_input));
  96.             _services.Add(typeof(IStatModificatorContainer), statModificatorContainer);
  97.             _services.Add(typeof(IAnimalBehaviourSwitcher), AnimalBehaviourSwitcher(aiAnimalService));
  98.             _services.Add(typeof(IAnimalCustomization), animalCustomization);
  99.             _services.Add(typeof(IStatsRequirementSystem), StatsRequirementSystem(stats));
  100.             _services.Add(typeof(IAnimalCameraService), new AnimalCameraService(MAnimal));
  101.             _services.Add(typeof(IAnimalTameService), new AnimalTameService(_animalTrigger, Specie, _tamer));
  102.             _services.Add(typeof(ILevel), level);
  103.             _services.Add(typeof(IExperience), experience);
  104.  
  105.             _tickables.Add(statModificatorContainer);
  106.         }
  107.  
  108.         private AIAnimalService AIAnimalService() => new(AIControl);
  109.  
  110.         public DamageProvider DamageProvider { get; } = new(10);
  111.  
  112.         [field: SerializeField]
  113.         public SpecieAnimal Specie { get; private set; }
  114.  
  115.         public AnimalGender Gender => Save.Gender;
  116.  
  117.         public Age Age => Save.Age;
  118.  
  119.         public BreedData Breed { get; private set; }
  120.  
  121.         public Renderer Renderer => _renderer;
  122.  
  123.         public MAnimalAIControl AIControl => _aiControl;
  124.  
  125.         public MAnimal MAnimal => _mAnimal;
  126.  
  127.         private void Update()
  128.         {
  129.             for (int i = _tickables.Count - 1; i >= 0; i--)
  130.                 _tickables[i].Tick();
  131.         }
  132.  
  133.         public TAnimalService GetService<TAnimalService>() where TAnimalService : IAnimalService =>
  134.             (TAnimalService)_services[typeof(TAnimalService)];
  135.  
  136.         private AnimalCustomization AnimalCustomization(SpecieAnimal specieAnimal)
  137.         {
  138.             CustomizationData customizationData = specieAnimal.Customization;
  139.  
  140.             List<string> mouthProperties = customizationData.MouthProperties;
  141.  
  142.             var animationController = new AnimationCustomizationController(_animator, MAnimal, mouthProperties);
  143.  
  144.             return new AnimalCustomization(Renderer, animationController, Save);
  145.         }
  146.  
  147.         private static StatModificatorContainer StatModificatorContainer(Stats stats) =>
  148.             new(stats);
  149.  
  150.         private StatsRequirementSystem StatsRequirementSystem(IStats stats) =>
  151.             new(_statsStaticData, stats, new MostRequirementStatChanged());
  152.  
  153.         private FollowPointsService FollowPointsService() =>
  154.             new(_followPoints.Select(point => point.transform));
  155.  
  156.         private AnimalBehaviourSwitcher AnimalBehaviourSwitcher(IAIAnimalService aiService)
  157.         {
  158.             var switcher = new AnimalBehaviourSwitcher();
  159.  
  160.             switcher.Add<IPlayerAnimalBehaviour>(new PlayerAnimalBehaviour(_input));
  161.             switcher.Add<IFamilyMemberAnimalBehaviour>(new FamilyMemberAnimalBehaviour(aiService));
  162.  
  163.             return switcher;
  164.         }
  165.     }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement