Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Spine.Unity;
- using UnityEngine;
- public class AnimationSwitcher : MonoBehaviour
- {
- private SkeletonAnimation _skeletonAnimation;
- public void Init(SkeletonAnimation skeletonAnimation)
- {
- _skeletonAnimation = skeletonAnimation;
- }
- public void SetAnimation(string animationName, bool isLoop)
- {
- int trackIndex = 0;
- if (animationName != _skeletonAnimation.AnimationName)
- {
- _skeletonAnimation.state.SetAnimation(trackIndex, animationName, isLoop);
- }
- }
- }
- using System.Collections;
- using UnityEngine;
- public class Attacker : MonoBehaviour
- {
- private Transform _projectilePrefab;
- private float _attackReload;
- private bool _canAttack = true;
- private Coroutine _coroutine;
- public float AttackReload => _attackReload;
- public bool CanAttack => _canAttack;
- public void Init(Transform projectile, float attackReload)
- {
- _projectilePrefab = projectile;
- _attackReload = attackReload;
- }
- public void ApplyRangeAttack(float direction)
- {
- if (_canAttack)
- {
- var projectile = Instantiate(_projectilePrefab);
- projectile.transform.position = transform.position;
- projectile.transform.right = transform.right * direction;
- _canAttack = false;
- _coroutine = StartCoroutine(Reload(_attackReload));
- }
- }
- private IEnumerator Reload(float reloadTime)
- {
- yield return new WaitForSeconds(reloadTime);
- _canAttack = true;
- }
- }
- using System.Collections.Generic;
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Mover), typeof(Jumper), typeof(Attacker))]
- [RequireComponent(typeof(CollideDetector), typeof(DirectionSwitcher), typeof(AnimationSwitcher))]
- public class Character : Entity
- {
- private float _groundSpeed;
- private float _airHorizontalSpeed;
- private float _jumpHeight;
- private float _reloadTime;
- private Transform _projectile;
- private Mover _mover;
- private Jumper _jumper;
- private Attacker _attacker;
- private CollideDetector _collideDetector;
- private DirectionSwitcher _directionSwitcher;
- private AnimationSwitcher _animationSwitcher;
- public TMPro.TextMeshProUGUI _textMeshPro;
- protected override void Init()
- {
- base.Init();
- Conditions = new List<StateConditions>
- {
- new IdleStateConditions(_collideDetector),
- new WalkStateConditions(),
- new JumpStateConditions(_jumper),
- new CharacterRangeAttackConditions(_attacker)
- };
- }
- protected override void Update()
- {
- base.Update();
- }
- protected override void ApplyStateActions()
- {
- switch (CurrentState)
- {
- case EntityStates.Idle:
- ApplyIdleActions();
- break;
- case EntityStates.Walk:
- ApplyWalkStateActions();
- break;
- case EntityStates.Jump:
- ApplyJumpStateActions();
- break;
- case EntityStates.RangeAttack:
- ApplyRangeAttackStateActions();
- break;
- }
- }
- protected override void ApplyIdleActions()
- {
- _animationSwitcher.SetAnimation("Idle", true);
- }
- protected override void ApplyWalkStateActions()
- {
- _directionSwitcher.SetDirection(Input.GetAxis("Horizontal"));
- _mover.Move(_groundSpeed * _directionSwitcher.Direction);
- _animationSwitcher.SetAnimation("Walk", true);
- }
- protected override void ApplyJumpStateActions()
- {
- _jumper.Jump();
- _directionSwitcher.SetDirection(Input.GetAxis("Horizontal"));
- _mover.Move(_airHorizontalSpeed * Input.GetAxis("Horizontal"));
- }
- protected override void LoadConfig()
- {
- CurrentState = _config.State;
- _groundSpeed = _config.GroundSpeed;
- _airHorizontalSpeed = _config.AirHorizontalSpeed;
- _jumpHeight = _config.JumpPower;
- _reloadTime = _config.ReloadTime;
- _projectile = _config.Projectile;
- }
- protected override void InitComponents()
- {
- _mover = GetComponent<Mover>();
- _jumper = GetComponent<Jumper>();
- _attacker = GetComponent<Attacker>();
- _collideDetector = GetComponent<CollideDetector>();
- _directionSwitcher = GetComponent<DirectionSwitcher>();
- _animationSwitcher = GetComponent<AnimationSwitcher>();
- _jumper.Init(_jumpHeight);
- _attacker.Init(_projectile, _reloadTime);
- _directionSwitcher.Init(_config.StartDirection);
- _animationSwitcher.Init(_skeletonAnimation);
- _directionSwitcher.DirectionChanged += FlipSprites;
- // _collideDetector.PlatformCollided += _jumper.SetStatus;
- }
- }
- using System;
- using UnityEngine;
- [RequireComponent(typeof(Collider2D))]
- public class CharacterDetector : MonoBehaviour
- {
- private Vector2 _direction;
- private Collider2D _collider;
- private float _distance = 15;
- private bool _isDetected;
- public bool IsDetected => _isDetected;
- private void Update()
- {
- TryDetectCharacter();
- }
- public void Init(float direction)
- {
- SetDirection(direction);
- _collider = GetComponent<Collider2D>();
- }
- public void SetDirection(float direction)
- {
- _direction = Vector2.right * direction;
- }
- private void TryDetectCharacter()
- {
- RaycastHit2D hit = Physics2D.Raycast(_collider.bounds.center, _direction.normalized, _distance);
- if (hit != false)
- {
- if (hit.collider.gameObject.TryGetComponent<Character>(out Character character))
- {
- _isDetected = true;
- }
- else
- {
- _isDetected = false;
- }
- }
- }
- }
- using System;
- using UnityEngine;
- [RequireComponent(typeof(Collider2D))]
- public class CollideDetector : MonoBehaviour
- {
- private Collider2D _collider;
- public Action<bool> PlatformCollided;
- public Action ObstacleCollided;
- private void Start()
- {
- _collider = GetComponent<Collider2D>();
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- //if (collision.gameObject.TryGetComponent<Platform>(out Platform platform)){
- // PlatformCollided?.Invoke(true);
- //}
- if(collision.gameObject.TryGetComponent<Obstacle>(out Obstacle obstacle)){
- ObstacleCollided?.Invoke();
- }
- }
- //private void OnCollisionExit2D(Collision2D collision)
- //{
- // PlatformCollided?.Invoke(false);
- //}
- }
- using System;
- using UnityEngine;
- public class DirectionSwitcher : MonoBehaviour
- {
- private float _direction;
- public float Direction => _direction;
- public Action<float> DirectionChanged;
- public void Init(float direction)
- {
- SetDirection(direction);
- }
- public void SetDirection(float direction)
- {
- if (direction != 0)
- {
- _direction = direction;
- DirectionChanged?.Invoke(_direction);
- }
- }
- public void ReverseDirection()
- {
- _direction = -_direction;
- DirectionChanged?.Invoke(_direction);
- }
- }
- using Spine.Unity;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Mover), typeof(Attacker), typeof(Patroler))]
- [RequireComponent(typeof(CollideDetector), typeof(DirectionSwitcher), typeof(CharacterDetector))]
- [RequireComponent(typeof(AnimationSwitcher))]
- public class Enemy : Entity
- {
- private float _groundSpeed;
- private float _reloadTime;
- private Transform _projectile;
- private Mover _mover;
- private Attacker _attacker;
- private Patroler _patroler;
- private CollideDetector _collideDetector;
- private DirectionSwitcher _directionSwitcher;
- private CharacterDetector _characterDetector;
- private AnimationSwitcher _animationSwitcher;
- protected override void Init()
- {
- base.Init();
- Conditions = new List<StateConditions>
- {
- new EnemyRangeAttackConditions(_attacker,_characterDetector),
- new PatrolingStateConditions(_characterDetector, _attacker),
- };
- }
- protected override void Update()
- {
- base.Update();
- }
- protected override void ApplyStateActions()
- {
- switch (CurrentState)
- {
- case EntityStates.Patroling:
- ApplyPatrolingStateActions();
- break;
- case EntityStates.RangeAttack:
- ApplyRangeAttackStateActions();
- break;
- }
- }
- protected override void ApplyRangeAttackStateActions()
- {
- _attacker.ApplyRangeAttack(_directionSwitcher.Direction);
- _animationSwitcher.SetAnimation("Attack", true);
- }
- protected override void ApplyPatrolingStateActions()
- {
- _mover.Move(_groundSpeed * _directionSwitcher.Direction);
- _animationSwitcher.SetAnimation("Walk", true);
- }
- protected override void LoadConfig()
- {
- _groundSpeed = _config.GroundSpeed;
- _reloadTime = _config.ReloadTime;
- CurrentState = _config.State;
- _projectile = _config.Projectile;
- }
- protected override void InitComponents()
- {
- _mover = GetComponent<Mover>();
- _attacker = GetComponent<Attacker>();
- _patroler = GetComponent<Patroler>();
- _characterDetector = GetComponent<CharacterDetector>();
- _collideDetector = GetComponent<CollideDetector>();
- _directionSwitcher = GetComponent<DirectionSwitcher>();
- _animationSwitcher = GetComponent<AnimationSwitcher>();
- _attacker.Init(_projectile, _reloadTime);
- _directionSwitcher.SetDirection(_config.StartDirection);
- _patroler.Init(_collideDetector, _directionSwitcher.Direction);
- _characterDetector.Init(_directionSwitcher.Direction);
- _animationSwitcher.Init(_skeletonAnimation);
- _collideDetector.ObstacleCollided += _directionSwitcher.ReverseDirection;
- _directionSwitcher.DirectionChanged += FlipSprites;
- _directionSwitcher.DirectionChanged += _characterDetector.SetDirection;
- }
- }
- using Spine.Unity;
- using System.Collections.Generic;
- using UnityEngine;
- public class Entity : MonoBehaviour
- {
- [SerializeField] protected EntityConfig _config;
- [SerializeField] protected SkeletonAnimation _skeletonAnimation;
- public List<StateConditions> Conditions { get; protected set; }
- protected EntityStates CurrentState;
- private void Awake()
- {
- Init();
- }
- protected virtual void Update()
- {
- SwitchState();
- ApplyStateActions();
- }
- protected virtual void Init()
- {
- LoadConfig();
- InitComponents();
- Conditions = new List<StateConditions>
- {
- };
- }
- protected virtual void SwitchState()
- {
- foreach (var condition in Conditions)
- {
- if (condition.CanChange(CurrentState))
- {
- CurrentState = condition.Type;
- return;
- }
- }
- }
- protected virtual void ApplyIdleActions() { }
- protected virtual void ApplyStateActions() { }
- protected virtual void ApplyWalkStateActions() { }
- protected virtual void ApplyJumpStateActions() { }
- protected virtual void ApplyRangeAttackStateActions() { }
- protected virtual void ApplyPatrolingStateActions() { }
- protected virtual void LoadConfig() { }
- protected virtual void InitComponents() { }
- protected void FlipSprites(float direction)
- {
- float negativeScale = -1;
- float positiveScale = 1;
- float scaleX = direction <= 0 ? negativeScale : positiveScale;
- _skeletonAnimation.Skeleton.ScaleX = scaleX;
- }
- }
- using UnityEngine;
- [CreateAssetMenu(fileName = "NewCharacterConfig", menuName = "Configs/Character Config")]
- public class EntityConfig : ScriptableObject
- {
- [SerializeField]private float _groundSpeed;
- [SerializeField] private float _airHorizontalSpeed;
- [SerializeField] private float _jumpPower;
- [SerializeField] private float _reloadTime;
- [SerializeField] private float _startDirection;
- [SerializeField] private EntityStates _startState;
- [SerializeField] private Transform _projectile;
- public float GroundSpeed => _groundSpeed;
- public float AirHorizontalSpeed => _airHorizontalSpeed;
- public float JumpPower => _jumpPower;
- public float ReloadTime => _reloadTime;
- public float StartDirection => _startDirection;
- public EntityStates State => _startState;
- public Transform Projectile => _projectile;
- }
- public enum EntityStates
- {
- Any,
- Idle,
- Walk,
- Jump,
- MeleeAttack,
- RangeAttack,
- Patroling
- }
- using UnityEngine;
- using UnityEngine;
- public class Jumper : MonoBehaviour
- {
- private bool _isGrounded;
- private float _jumpHeight;
- public bool IsGrounded => _isGrounded;
- public void Init(float jumpHeight)
- {
- _jumpHeight = jumpHeight;
- }
- public void Jump()
- {
- }
- }
- using UnityEngine;
- public class Mover : MonoBehaviour
- {
- public void Move(float speed)
- {
- transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
- }
- }
- using UnityEngine;
- public class Obstacle : MonoBehaviour
- {
- }
- using UnityEngine;
- public class Patroler : MonoBehaviour
- {
- private float _xDirection;
- private CollideDetector _collideDetector;
- public void Init(CollideDetector collideDetector, float direction)
- {
- _xDirection = direction;
- _collideDetector = collideDetector;
- _collideDetector.ObstacleCollided += ReverseDirection;
- }
- public void ReverseDirection()
- {
- int value = -1;
- _xDirection *= value;
- }
- }
- using System.Collections;
- using UnityEngine;
- [RequireComponent (typeof(Mover))]
- public class Projectile : MonoBehaviour
- {
- private float _speed = 5;
- private Mover _mover;
- private float _lifeTime = 3f;
- private Coroutine _coroutine;
- private void Start()
- {
- Init();
- }
- public void Init()
- {
- _mover = GetComponent<Mover>();
- _coroutine = StartCoroutine(Destroy());
- }
- private void Update()
- {
- _mover.Move(_speed );
- }
- private IEnumerator Destroy()
- {
- yield return new WaitForSeconds(_lifeTime);
- Destroy(gameObject);
- }
- }
- using System.Collections.Generic;
- using UnityEngine;
- public class IdleStateConditions : StateConditions
- {
- private readonly CollideDetector _detector;
- private bool _isGrounded;
- public IdleStateConditions(CollideDetector detector)
- {
- _stateType = EntityStates.Idle;
- _allowedStates.Add(EntityStates.Walk);
- _allowedStates.Add(EntityStates.Jump);
- _allowedStates.Add(EntityStates.RangeAttack);
- _detector = detector;
- _detector.PlatformCollided += SetIsGroundedStatus;
- }
- public override bool CanChange(EntityStates currenState)
- {
- if (HasAllowedState(currenState))
- {
- if (Input.anyKey == false && _isGrounded)
- {
- return true;
- }
- }
- return false;
- }
- private void SetIsGroundedStatus(bool value)
- {
- _isGrounded = value;
- }
- }
- using UnityEngine;
- pusing System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class JumpStateConditions : StateConditions
- {
- private Jumper _jumper;
- private bool _isJumping;
- public JumpStateConditions(Jumper jumper)
- {
- _jumper = jumper;
- _stateType = EntityStates.Jump;
- _allowedStates.Add(EntityStates.Idle);
- _allowedStates.Add(EntityStates.Walk);
- _jumper = jumper;
- }
- public override bool CanChange(EntityStates currenState)
- {
- if (_allowedStates.Contains(currenState))
- {
- if (_jumper.IsGrounded && Input.GetKeyDown(KeyCode.Space))
- {
- return true;
- }
- }
- return false;
- }
- }
- using UnityEngine;
- public class WalkStateConditions : StateConditions
- {
- public WalkStateConditions()
- {
- _stateType = EntityStates.Walk;
- _allowedStates.Add(EntityStates.Idle);
- }
- public override bool CanChange(EntityStates currentState)
- {
- if (HasAllowedState(currentState))
- {
- if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
- {
- return true;
- }
- }
- return false;
- }
- }
- using System.Collections.Generic;
- public class StateConditions
- {
- protected EntityStates _stateType;
- protected List<EntityStates> _allowedStates;
- public EntityStates Type => _stateType;
- public StateConditions()
- {
- _allowedStates = new List<EntityStates>();
- }
- public virtual bool CanChange(EntityStates currenState)
- {
- if (_allowedStates.Contains(EntityStates.Any))
- {
- return true;
- }
- return false;
- }
- protected bool HasAllowedState(EntityStates currentState)
- {
- bool isAlowedState = false;
- foreach (var state in _allowedStates)
- {
- if (state == currentState)
- {
- isAlowedState = true;
- break;
- }
- }
- return isAlowedState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement