Advertisement
TeT91

Untitled

Feb 22nd, 2025 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.85 KB | None | 0 0
  1. using Spine.Unity;
  2. using UnityEngine;
  3.  
  4. public class AnimationSwitcher : MonoBehaviour
  5. {
  6.     private SkeletonAnimation _skeletonAnimation;
  7.  
  8.     public  void Init(SkeletonAnimation skeletonAnimation)
  9.     {
  10.         _skeletonAnimation = skeletonAnimation;
  11.     }
  12.  
  13.     public void SetAnimation(string animationName, bool isLoop)
  14.     {
  15.         int trackIndex = 0;
  16.         if (animationName != _skeletonAnimation.AnimationName)
  17.         {
  18.             _skeletonAnimation.state.SetAnimation(trackIndex, animationName, isLoop);
  19.         }
  20.     }
  21. }
  22. using System.Collections;
  23. using UnityEngine;
  24.  
  25. public class Attacker : MonoBehaviour
  26. {
  27.     private Transform _projectilePrefab;
  28.     private float _attackReload;
  29.     private bool _canAttack = true;
  30.  
  31.     private Coroutine _coroutine;
  32.  
  33.     public float AttackReload => _attackReload;
  34.     public bool CanAttack => _canAttack;
  35.  
  36.     public void Init(Transform projectile, float attackReload)
  37.     {
  38.         _projectilePrefab = projectile;
  39.         _attackReload = attackReload;
  40.     }
  41.  
  42.     public void ApplyRangeAttack(float direction)
  43.     {
  44.         if (_canAttack)
  45.         {
  46.             var projectile = Instantiate(_projectilePrefab);
  47.             projectile.transform.position = transform.position;
  48.             projectile.transform.right = transform.right * direction;
  49.             _canAttack = false;
  50.             _coroutine = StartCoroutine(Reload(_attackReload));
  51.         }
  52.     }
  53.  
  54.     private IEnumerator Reload(float reloadTime)
  55.     {
  56.         yield return new WaitForSeconds(reloadTime);
  57.         _canAttack = true;
  58.     }
  59. }
  60. using System.Collections.Generic;
  61. using UnityEngine;
  62.  
  63. using System.Collections.Generic;
  64. using UnityEngine;
  65.  
  66. [RequireComponent(typeof(Mover), typeof(Jumper), typeof(Attacker))]
  67. [RequireComponent(typeof(CollideDetector), typeof(DirectionSwitcher), typeof(AnimationSwitcher))]
  68. public class Character : Entity
  69. {
  70.     private float _groundSpeed;
  71.     private float _airHorizontalSpeed;
  72.     private float _jumpHeight;
  73.     private float _reloadTime;
  74.     private Transform _projectile;
  75.  
  76.     private Mover _mover;
  77.     private Jumper _jumper;
  78.     private Attacker _attacker;
  79.     private CollideDetector _collideDetector;
  80.     private DirectionSwitcher _directionSwitcher;
  81.     private AnimationSwitcher _animationSwitcher;
  82.  
  83.     public TMPro.TextMeshProUGUI _textMeshPro;
  84.  
  85.     protected override void Init()
  86.     {
  87.         base.Init();
  88.  
  89.         Conditions = new List<StateConditions>
  90.         {
  91.             new IdleStateConditions(_collideDetector),
  92.             new WalkStateConditions(),
  93.             new JumpStateConditions(_jumper),
  94.             new CharacterRangeAttackConditions(_attacker)
  95.         };
  96.     }
  97.  
  98.     protected override void Update()
  99.     {
  100.         base.Update();
  101.  
  102.  
  103.     }
  104.  
  105.     protected override void ApplyStateActions()
  106.     {
  107.         switch (CurrentState)
  108.         {
  109.             case EntityStates.Idle:
  110.                 ApplyIdleActions();
  111.                 break;
  112.  
  113.             case EntityStates.Walk:
  114.                 ApplyWalkStateActions();
  115.                 break;
  116.  
  117.             case EntityStates.Jump:
  118.                 ApplyJumpStateActions();
  119.                 break;
  120.  
  121.             case EntityStates.RangeAttack:
  122.                 ApplyRangeAttackStateActions();
  123.                 break;
  124.         }
  125.     }
  126.  
  127.     protected override void ApplyIdleActions()
  128.     {
  129.         _animationSwitcher.SetAnimation("Idle", true);
  130.     }
  131.  
  132.     protected override void ApplyWalkStateActions()
  133.     {
  134.         _directionSwitcher.SetDirection(Input.GetAxis("Horizontal"));
  135.         _mover.Move(_groundSpeed * _directionSwitcher.Direction);
  136.         _animationSwitcher.SetAnimation("Walk", true);
  137.     }
  138.  
  139.     protected override void ApplyJumpStateActions()
  140.     {
  141.         _jumper.Jump();
  142.         _directionSwitcher.SetDirection(Input.GetAxis("Horizontal"));
  143.         _mover.Move(_airHorizontalSpeed * Input.GetAxis("Horizontal"));
  144.  
  145.     }
  146.  
  147.     protected override void LoadConfig()
  148.     {
  149.         CurrentState = _config.State;
  150.         _groundSpeed = _config.GroundSpeed;
  151.         _airHorizontalSpeed = _config.AirHorizontalSpeed;
  152.         _jumpHeight = _config.JumpPower;
  153.         _reloadTime = _config.ReloadTime;
  154.         _projectile = _config.Projectile;
  155.     }
  156.  
  157.     protected override void InitComponents()
  158.     {
  159.         _mover = GetComponent<Mover>();
  160.         _jumper = GetComponent<Jumper>();
  161.         _attacker = GetComponent<Attacker>();
  162.         _collideDetector = GetComponent<CollideDetector>();
  163.         _directionSwitcher = GetComponent<DirectionSwitcher>();
  164.         _animationSwitcher = GetComponent<AnimationSwitcher>();
  165.  
  166.         _jumper.Init(_jumpHeight);
  167.         _attacker.Init(_projectile, _reloadTime);
  168.         _directionSwitcher.Init(_config.StartDirection);
  169.         _animationSwitcher.Init(_skeletonAnimation);
  170.  
  171.         _directionSwitcher.DirectionChanged += FlipSprites;
  172.         // _collideDetector.PlatformCollided += _jumper.SetStatus;
  173.     }
  174. }
  175.  
  176.  
  177. using System;
  178. using UnityEngine;
  179.  
  180. [RequireComponent(typeof(Collider2D))]
  181. public class CharacterDetector : MonoBehaviour
  182. {
  183.     private Vector2 _direction;
  184.     private Collider2D _collider;
  185.     private float _distance = 15;
  186.     private bool _isDetected;
  187.  
  188.     public bool IsDetected => _isDetected;
  189.  
  190.     private void Update()
  191.     {
  192.         TryDetectCharacter();
  193.     }
  194.  
  195.     public void Init(float direction)
  196.     {
  197.         SetDirection(direction);
  198.         _collider = GetComponent<Collider2D>();
  199.     }
  200.  
  201.     public void SetDirection(float direction)
  202.     {
  203.         _direction = Vector2.right * direction;
  204.     }
  205.  
  206.     private void TryDetectCharacter()
  207.     {
  208.         RaycastHit2D hit = Physics2D.Raycast(_collider.bounds.center, _direction.normalized, _distance);
  209.  
  210.         if (hit != false)
  211.         {
  212.             if (hit.collider.gameObject.TryGetComponent<Character>(out Character character))
  213.             {
  214.                 _isDetected = true;
  215.             }
  216.  
  217.             else
  218.             {
  219.                 _isDetected = false;
  220.             }
  221.         }
  222.     }
  223. }
  224. using System;
  225. using UnityEngine;
  226.  
  227. [RequireComponent(typeof(Collider2D))]
  228. public class CollideDetector : MonoBehaviour
  229. {
  230.     private Collider2D _collider;
  231.  
  232.     public Action<bool> PlatformCollided;
  233.     public Action ObstacleCollided;
  234.  
  235.     private void Start()
  236.     {
  237.         _collider = GetComponent<Collider2D>();
  238.     }
  239.  
  240.     private void OnCollisionEnter2D(Collision2D collision)
  241.     {
  242.         //if (collision.gameObject.TryGetComponent<Platform>(out Platform platform)){
  243.         //    PlatformCollided?.Invoke(true);
  244.         //}
  245.  
  246.         if(collision.gameObject.TryGetComponent<Obstacle>(out Obstacle obstacle)){
  247.             ObstacleCollided?.Invoke();
  248.         }
  249.     }
  250.  
  251.     //private void OnCollisionExit2D(Collision2D collision)
  252.     //{
  253.     //    PlatformCollided?.Invoke(false);
  254.     //}
  255. }
  256. using System;
  257. using UnityEngine;
  258.  
  259. public class DirectionSwitcher : MonoBehaviour
  260. {
  261.     private float _direction;
  262.  
  263.     public float Direction => _direction;
  264.  
  265.     public Action<float> DirectionChanged;
  266.  
  267.     public void Init(float direction)
  268.     {
  269.         SetDirection(direction);
  270.     }
  271.  
  272.     public void SetDirection(float direction)
  273.     {
  274.         if (direction != 0)
  275.         {
  276.             _direction = direction;
  277.             DirectionChanged?.Invoke(_direction);
  278.         }
  279.     }
  280.  
  281.     public void ReverseDirection()
  282.     {
  283.         _direction = -_direction;
  284.         DirectionChanged?.Invoke(_direction);
  285.     }
  286. }
  287.  
  288. using Spine.Unity;
  289. using System.Collections.Generic;
  290. using UnityEngine;
  291.  
  292. [RequireComponent(typeof(Mover), typeof(Attacker), typeof(Patroler))]
  293. [RequireComponent(typeof(CollideDetector), typeof(DirectionSwitcher), typeof(CharacterDetector))]
  294. [RequireComponent(typeof(AnimationSwitcher))]
  295. public class Enemy : Entity
  296. {
  297.     private float _groundSpeed;
  298.     private float _reloadTime;
  299.     private Transform _projectile;
  300.  
  301.     private Mover _mover;
  302.     private Attacker _attacker;
  303.     private Patroler _patroler;
  304.     private CollideDetector _collideDetector;
  305.     private DirectionSwitcher _directionSwitcher;
  306.     private CharacterDetector _characterDetector;
  307.     private AnimationSwitcher _animationSwitcher;
  308.  
  309.     protected override void Init()
  310.     {
  311.         base.Init();
  312.  
  313.         Conditions = new List<StateConditions>
  314.         {
  315.             new EnemyRangeAttackConditions(_attacker,_characterDetector),
  316.             new PatrolingStateConditions(_characterDetector, _attacker),
  317.         };
  318.     }
  319.  
  320.     protected override void Update()
  321.     {
  322.         base.Update();
  323.     }
  324.  
  325.     protected override void ApplyStateActions()
  326.     {
  327.         switch (CurrentState)
  328.         {
  329.             case EntityStates.Patroling:
  330.                 ApplyPatrolingStateActions();
  331.                 break;
  332.  
  333.             case EntityStates.RangeAttack:
  334.                 ApplyRangeAttackStateActions();
  335.                 break;
  336.         }
  337.     }
  338.  
  339.     protected override void ApplyRangeAttackStateActions()
  340.     {
  341.         _attacker.ApplyRangeAttack(_directionSwitcher.Direction);
  342.         _animationSwitcher.SetAnimation("Attack", true);
  343.     }
  344.  
  345.     protected override void ApplyPatrolingStateActions()
  346.     {
  347.         _mover.Move(_groundSpeed * _directionSwitcher.Direction);
  348.         _animationSwitcher.SetAnimation("Walk", true);
  349.     }
  350.  
  351.     protected override void LoadConfig()
  352.     {
  353.         _groundSpeed = _config.GroundSpeed;
  354.         _reloadTime = _config.ReloadTime;
  355.         CurrentState = _config.State;
  356.         _projectile = _config.Projectile;
  357.     }
  358.  
  359.     protected override void InitComponents()
  360.     {
  361.         _mover = GetComponent<Mover>();
  362.         _attacker = GetComponent<Attacker>();
  363.         _patroler = GetComponent<Patroler>();
  364.         _characterDetector = GetComponent<CharacterDetector>();
  365.         _collideDetector = GetComponent<CollideDetector>();
  366.         _directionSwitcher = GetComponent<DirectionSwitcher>();
  367.         _animationSwitcher = GetComponent<AnimationSwitcher>();
  368.  
  369.         _attacker.Init(_projectile, _reloadTime);
  370.         _directionSwitcher.SetDirection(_config.StartDirection);
  371.         _patroler.Init(_collideDetector, _directionSwitcher.Direction);
  372.         _characterDetector.Init(_directionSwitcher.Direction);
  373.         _animationSwitcher.Init(_skeletonAnimation);
  374.  
  375.         _collideDetector.ObstacleCollided += _directionSwitcher.ReverseDirection;
  376.         _directionSwitcher.DirectionChanged += FlipSprites;
  377.         _directionSwitcher.DirectionChanged += _characterDetector.SetDirection;
  378.     }
  379. }
  380. using Spine.Unity;
  381. using System.Collections.Generic;
  382. using UnityEngine;
  383.  
  384. public class Entity : MonoBehaviour
  385. {
  386.     [SerializeField] protected EntityConfig _config;
  387.     [SerializeField] protected SkeletonAnimation _skeletonAnimation;
  388.  
  389.     public List<StateConditions> Conditions { get; protected set; }
  390.     protected EntityStates CurrentState;
  391.  
  392.     private void Awake()
  393.     {
  394.         Init();
  395.     }
  396.  
  397.     protected virtual void Update()
  398.     {
  399.         SwitchState();
  400.         ApplyStateActions();
  401.     }
  402.  
  403.     protected virtual void Init()
  404.     {
  405.         LoadConfig();
  406.         InitComponents();
  407.  
  408.         Conditions = new List<StateConditions>
  409.         {
  410.         };
  411.     }
  412.  
  413.     protected virtual void SwitchState()
  414.     {
  415.         foreach (var condition in Conditions)
  416.         {
  417.             if (condition.CanChange(CurrentState))
  418.             {
  419.                 CurrentState = condition.Type;
  420.                 return;
  421.             }
  422.         }
  423.     }
  424.  
  425.     protected virtual void ApplyIdleActions() { }
  426.     protected virtual void ApplyStateActions() { }
  427.     protected virtual void ApplyWalkStateActions() { }
  428.     protected virtual void ApplyJumpStateActions() { }
  429.     protected virtual void ApplyRangeAttackStateActions() { }
  430.     protected virtual void ApplyPatrolingStateActions() { }
  431.     protected virtual void LoadConfig() { }
  432.     protected virtual void InitComponents() { }
  433.  
  434.     protected void FlipSprites(float direction)
  435.     {
  436.         float negativeScale = -1;
  437.         float positiveScale = 1;
  438.         float scaleX = direction <= 0 ? negativeScale : positiveScale;
  439.         _skeletonAnimation.Skeleton.ScaleX = scaleX;
  440.     }
  441. }
  442. using UnityEngine;
  443.  
  444. [CreateAssetMenu(fileName = "NewCharacterConfig", menuName = "Configs/Character Config")]
  445. public class EntityConfig : ScriptableObject
  446. {
  447.     [SerializeField]private float _groundSpeed;
  448.     [SerializeField] private float _airHorizontalSpeed;
  449.     [SerializeField] private float _jumpPower;
  450.     [SerializeField] private float _reloadTime;
  451.     [SerializeField] private float _startDirection;
  452.  
  453.     [SerializeField] private EntityStates _startState;
  454.     [SerializeField] private Transform _projectile;
  455.  
  456.     public float GroundSpeed => _groundSpeed;
  457.     public float AirHorizontalSpeed => _airHorizontalSpeed;
  458.     public float JumpPower => _jumpPower;
  459.     public float ReloadTime => _reloadTime;
  460.     public float StartDirection => _startDirection;
  461.     public EntityStates State => _startState;
  462.     public Transform Projectile => _projectile;
  463. }
  464.  
  465. public enum EntityStates
  466. {
  467.     Any,
  468.     Idle,
  469.     Walk,
  470.     Jump,
  471.     MeleeAttack,
  472.     RangeAttack,
  473.     Patroling
  474. }
  475. using UnityEngine;
  476.  
  477. using UnityEngine;
  478.  
  479. public class Jumper : MonoBehaviour
  480. {
  481.     private bool _isGrounded;
  482.     private float _jumpHeight;
  483.  
  484.     public bool IsGrounded => _isGrounded;
  485.  
  486.     public void Init(float jumpHeight)
  487.     {
  488.         _jumpHeight = jumpHeight;
  489.     }
  490.  
  491.     public void Jump()
  492.     {
  493.  
  494.     }
  495. }
  496. using UnityEngine;
  497.  
  498. public class Mover : MonoBehaviour
  499. {
  500.     public void Move(float speed)
  501.     {
  502.         transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
  503.     }
  504. }
  505. using UnityEngine;
  506.  
  507. public class Obstacle : MonoBehaviour
  508. {
  509. }
  510. using UnityEngine;
  511.  
  512. public class Patroler : MonoBehaviour
  513. {
  514.     private float _xDirection;
  515.  
  516.     private CollideDetector _collideDetector;
  517.  
  518.     public void Init(CollideDetector collideDetector, float direction)
  519.     {
  520.         _xDirection = direction;
  521.         _collideDetector = collideDetector;
  522.         _collideDetector.ObstacleCollided += ReverseDirection;
  523.     }
  524.  
  525.     public void ReverseDirection()
  526.     {
  527.         int value = -1;
  528.         _xDirection *= value;
  529.     }
  530. }
  531.  
  532. using System.Collections;
  533. using UnityEngine;
  534.  
  535. [RequireComponent (typeof(Mover))]
  536. public class Projectile : MonoBehaviour
  537. {
  538.     private float _speed = 5;
  539.     private Mover _mover;
  540.     private float _lifeTime = 3f;
  541.     private Coroutine _coroutine;
  542.  
  543.     private void Start()
  544.     {
  545.         Init();
  546.     }
  547.  
  548.     public void Init()
  549.     {
  550.         _mover = GetComponent<Mover>();
  551.         _coroutine = StartCoroutine(Destroy());
  552.     }
  553.  
  554.     private void Update()
  555.     {
  556.         _mover.Move(_speed );
  557.     }
  558.  
  559.     private IEnumerator Destroy()
  560.     {
  561.         yield return new WaitForSeconds(_lifeTime);
  562.         Destroy(gameObject);
  563.     }
  564. }
  565. using System.Collections.Generic;
  566. using UnityEngine;
  567.  
  568. public class IdleStateConditions : StateConditions
  569. {
  570.     private readonly CollideDetector _detector;
  571.  
  572.     private bool _isGrounded;
  573.  
  574.     public IdleStateConditions(CollideDetector detector)
  575.     {
  576.         _stateType = EntityStates.Idle;
  577.         _allowedStates.Add(EntityStates.Walk);
  578.         _allowedStates.Add(EntityStates.Jump);
  579.         _allowedStates.Add(EntityStates.RangeAttack);
  580.  
  581.         _detector = detector;
  582.         _detector.PlatformCollided += SetIsGroundedStatus;
  583.     }
  584.  
  585.     public override bool CanChange(EntityStates currenState)
  586.     {
  587.         if (HasAllowedState(currenState))
  588.         {
  589.             if (Input.anyKey == false && _isGrounded)
  590.             {
  591.                 return true;
  592.             }
  593.         }
  594.  
  595.         return false;
  596.     }
  597.  
  598.     private void SetIsGroundedStatus(bool value)
  599.     {
  600.         _isGrounded = value;
  601.     }
  602. }
  603. using UnityEngine;
  604.  
  605. pusing System.Collections;
  606. using System.Collections.Generic;
  607. using UnityEngine;
  608.  
  609. public class JumpStateConditions : StateConditions
  610. {
  611.     private Jumper _jumper;
  612.     private bool _isJumping;
  613.  
  614.     public JumpStateConditions(Jumper jumper)
  615.     {
  616.         _jumper = jumper;
  617.  
  618.         _stateType = EntityStates.Jump;
  619.  
  620.         _allowedStates.Add(EntityStates.Idle);
  621.         _allowedStates.Add(EntityStates.Walk);
  622.         _jumper = jumper;
  623.     }
  624.  
  625.     public override bool CanChange(EntityStates currenState)
  626.     {
  627.         if (_allowedStates.Contains(currenState))
  628.         {
  629.             if (_jumper.IsGrounded && Input.GetKeyDown(KeyCode.Space))
  630.             {
  631.                 return true;
  632.             }
  633.  
  634.         }
  635.         return false;
  636.     }
  637. }
  638. using UnityEngine;
  639.  
  640. public class WalkStateConditions : StateConditions
  641. {
  642.     public WalkStateConditions()
  643.     {
  644.         _stateType = EntityStates.Walk;
  645.         _allowedStates.Add(EntityStates.Idle);
  646.     }
  647.  
  648.     public override bool CanChange(EntityStates currentState)
  649.     {
  650.         if (HasAllowedState(currentState))
  651.         {
  652.             if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
  653.             {
  654.                 return true;
  655.             }
  656.         }
  657.  
  658.         return false;
  659.     }
  660. }
  661. using System.Collections.Generic;
  662.  
  663. public class StateConditions
  664. {
  665.     protected EntityStates _stateType;
  666.     protected List<EntityStates> _allowedStates;
  667.  
  668.     public EntityStates Type => _stateType;
  669.  
  670.     public StateConditions()
  671.     {
  672.         _allowedStates = new List<EntityStates>();
  673.     }
  674.  
  675.     public virtual bool CanChange(EntityStates currenState)
  676.     {
  677.         if (_allowedStates.Contains(EntityStates.Any))
  678.         {
  679.             return true;
  680.         }
  681.  
  682.         return false;
  683.     }
  684.  
  685.     protected bool HasAllowedState(EntityStates currentState)
  686.     {
  687.         bool isAlowedState = false;
  688.  
  689.         foreach (var state in _allowedStates)
  690.         {
  691.             if (state == currentState)
  692.             {
  693.                 isAlowedState = true;
  694.                 break;
  695.             }
  696.         }
  697.  
  698.         return isAlowedState;
  699.     }
  700. }
  701.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement