Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class CoinCounter : MonoBehaviour
- {
- [SerializeField] private TMP_Text _coinText;
- private void Awake()
- {
- _coinText.text = "Coins: 1";
- }
- public void UpdateCoins(int amount)
- {
- _coinText.text = $"Coins: {amount}";
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DataHandler : MonoBehaviour
- {
- public static string Horizontal = "Horizontal";
- public static string Jump = "Jump";
- public static int DeathLimit = 3;
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Enemy : MonoBehaviour
- {
- [SerializeField] private string _name = "Enemy";
- [SerializeField] private float _damage = 10;
- private Animator _animator;
- private SpriteRenderer _spriteRenderer;
- private float _maxHealth;
- private ParticleSystem _particleSystem;
- private void Start()
- {
- _animator = GetComponent<Animator>();
- _particleSystem = GetComponentInChildren<ParticleSystem>();
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.transform.TryGetComponent(out Player player) & collision.transform.TryGetComponent(out HealthBar healthBar))
- {
- healthBar.GetDamage(_damage);
- healthBar.UpdateHealthBar();
- Rigidbody2D rigidbody2D = player.GetComponent<Rigidbody2D>();
- Vector2 direction = (player.transform.position - transform.position).normalized;
- rigidbody2D.AddForce(direction * 2f, ForceMode2D.Force);
- }
- }
- }
- using System.Collections;
- using UnityEngine;
- public class EnemyMovement : MonoBehaviour
- {
- [SerializeField] private float _forceJump;
- [SerializeField] private float _delay;
- private Rigidbody2D _rigidbody2D;
- private bool _canJump = true;
- private void Start()
- {
- _rigidbody2D = GetComponent<Rigidbody2D>();
- }
- private void FixedUpdate()
- {
- if (_canJump)
- {
- StartCoroutine(JumpWithDelay());
- }
- }
- private IEnumerator JumpWithDelay()
- {
- _canJump = false;
- Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 5f);
- bool isFound = false;
- WaitForSeconds waitForSeconds = new WaitForSeconds(_delay);
- foreach (var collider in colliders)
- {
- if (collider.TryGetComponent(out Player player))
- {
- isFound = true;
- Vector2 direction = (player.transform.position - transform.position).normalized;
- _rigidbody2D.AddForce((direction + new Vector2(0,_forceJump)), ForceMode2D.Impulse);
- transform.localScale = new Vector3(-direction.x >= 0 ? 1 : -1, transform.localScale.y, transform.localScale.z);
- }
- }
- if (isFound == false)
- {
- Vector2 direction = new Vector2((Random.Range(-1, 1) == 0 ? 1f : -1f), 1f);
- _rigidbody2D.AddForce(direction * _forceJump, ForceMode2D.Impulse);
- }
- yield return waitForSeconds;
- _canJump = true;
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using Random = UnityEngine.Random;
- public abstract class Health : MonoBehaviour
- {
- [SerializeField] protected float _maxHealth;
- [SerializeField] protected GameObject _coinPrefab;
- [SerializeField] private TMP_Text _livesText;
- [SerializeField] private bool _isHero = false;
- [SerializeField] private AudioClip _clip;
- [SerializeField] private AudioSource _audioSource;
- protected float _curentHealth;
- private static int _deathLimit = 3;
- private void Awake()
- {
- _curentHealth = _maxHealth;
- _livesText.text = $"Lives: {_deathLimit}";
- }
- public void GetDamage(float damage)
- {
- _curentHealth -= damage;
- if (_curentHealth < 0 && _isHero)
- {
- Death();
- }
- else if (_curentHealth < 0)
- {
- _audioSource.clip = _clip;
- _audioSource.Play();
- gameObject.SetActive(false);
- }
- }
- public void Death()
- {
- _audioSource.clip = _clip;
- _audioSource.Play();
- if (_deathLimit <= 0)
- {
- SetHealth(DataHandler.DeathLimit);
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
- }
- else
- {
- gameObject.SetActive(false);
- _deathLimit -= 1;
- _livesText.text = $"Lives: {_deathLimit}";
- transform.position = Player.GetStartPosition();
- _curentHealth = _maxHealth;
- gameObject.SetActive(true);
- }
- }
- public void Heal(float heal)
- {
- _curentHealth += heal;
- if (_curentHealth > _maxHealth)
- {
- _curentHealth = _maxHealth;
- }
- }
- public static int GetSavedDeathLimit()
- {
- return _deathLimit;
- }
- public static void SetHealth(int amount)
- {
- _deathLimit = amount;
- }
- }
- using TMPro;
- using UnityEngine;
- public class HealthBar : Health
- {
- [SerializeField] private SpriteRenderer _healthBarImage;
- [SerializeField] private Color _color = Color.green;
- private void Start()
- {
- _healthBarImage.color = _color;
- }
- public void UpdateHealthBar()
- {
- float fillAmount = _curentHealth / _maxHealth;
- _healthBarImage.transform.localScale = new Vector2(fillAmount, 1f);
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class KeyCollector : MonoBehaviour
- {
- [SerializeField] private int _neededKeys = 3;
- [SerializeField] private TMP_Text _counterKeys;
- private int _currentKeys = 0;
- private void Start()
- {
- _counterKeys.text = $"Keys: {_currentKeys}";
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.transform.TryGetComponent(out Key key))
- {
- _currentKeys += 1;
- _counterKeys.text = $"Keys: {_currentKeys}";
- Destroy(key.gameObject);
- }
- }
- public bool CheckAccess()
- {
- if (_currentKeys >= _neededKeys)
- return true;
- return false;
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class PortalKeyChecker : MonoBehaviour
- {
- [SerializeField] private int _indexScene = 0;
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.transform.TryGetComponent(out KeyCollector keyCollector))
- {
- if (keyCollector.CheckAccess())
- {
- SceneManager.LoadScene(_indexScene);
- }
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class CorrectRestart : MonoBehaviour
- {
- public void Restart()
- {
- Health.SetHealth(DataHandler.DeathLimit);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class MenuButtons : MonoBehaviour
- {
- [SerializeField] private GameObject _menuPanel;
- [SerializeField] private GameObject _optionsPanel;
- [SerializeField] private int _sceneIndex = 0;
- public void Exit()
- {
- Application.Quit();
- }
- public void Play()
- {
- SceneManager.LoadScene(_sceneIndex);
- }
- public void OpenOptions()
- {
- if (_menuPanel != null)
- {
- _menuPanel.SetActive(false);
- }
- if (_optionsPanel != null)
- {
- _optionsPanel.SetActive(true);
- }
- }
- public void Back()
- {
- if (_menuPanel != null)
- {
- _menuPanel.SetActive(true);
- }
- if (_optionsPanel != null)
- {
- _optionsPanel.SetActive(false);
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MenuKeyBinds : MonoBehaviour
- {
- [SerializeField] private GameObject _menu;
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- if (_menu.activeSelf == false)
- {
- _menu.SetActive(true);
- }
- else
- {
- _menu.SetActive(false);
- }
- }
- }
- }
- using UnityEngine;
- using UnityEngine.UI;
- public class MusicSlider : MonoBehaviour
- {
- [SerializeField] private AudioSource _audioSource;
- private Slider _slider;
- private static float _savedValume = 0.5f;
- private void Start()
- {
- _slider = GetComponent<Slider>();
- _slider.value = _audioSource.volume;
- _savedValume = _audioSource.volume;
- }
- private void OnEnable()
- {
- _slider.value = _audioSource.volume;
- }
- public void UpdateVolume()
- {
- _audioSource.volume = _slider.value;
- _savedValume = _audioSource.volume;
- }
- public static float GetSavedValume()
- {
- return _savedValume;
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AIEnabler : MonoBehaviour
- {
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.transform.TryGetComponent(out EnemyMovement enemyMovement))
- {
- if (enemyMovement.enabled == false)
- {
- enemyMovement.enabled = true;
- }
- }
- }
- private void OnTriggerExit2D(Collider2D other)
- {
- if (other.transform.TryGetComponent(out EnemyMovement enemyMovement))
- {
- if (enemyMovement.enabled == true)
- {
- enemyMovement.enabled = false;
- }
- }
- }
- }
- using System;
- using TMPro;
- using Unity.VisualScripting;
- using UnityEngine;
- [RequireComponent(typeof(SpriteRenderer), typeof(HealthBar))]
- public class Player : MonoBehaviour
- {
- private static Vector3 _startPosition;
- private HealthBar _healthBar;
- private void Awake()
- {
- _healthBar = GetComponent<HealthBar>();
- _startPosition = transform.position;
- }
- public static Vector3 GetStartPosition()
- {
- return _startPosition;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(CircleCollider2D))]
- public class PlayerAttack : MonoBehaviour
- {
- [SerializeField] private float _attackSpeed;
- [SerializeField] private float _timeBeforeAttack = .33f;
- [SerializeField] private float _timeAfterAttack = .67f;
- [SerializeField] private Animator _animator;
- [SerializeField] private float _damage = 12;
- [SerializeField] private float _attackForce;
- [SerializeField] private List<AudioClip> _attackSounds;
- [SerializeField] private AudioSource _audioSource;
- private bool _canAttack = true;
- private float _attackTime;
- private int _attackState;
- private CircleCollider2D _circleCollider2D;
- private float _tempBeforeAttack;
- private float _tempAfterAttack;
- private void Start()
- {
- _circleCollider2D = gameObject.GetComponent<CircleCollider2D>();
- _tempBeforeAttack = _timeBeforeAttack;
- _tempAfterAttack = _timeAfterAttack;
- }
- private void Update()
- {
- if (Input.GetKey(KeyCode.Mouse0) && _canAttack)
- {
- StartCoroutine(AttackWithDelay());
- }
- if (Input.GetKeyUp(KeyCode.Mouse0))
- {
- _animator.SetBool("Attack", false);
- }
- }
- private void Attack()
- {
- Collider2D[] hits = Physics2D.OverlapCircleAll(_circleCollider2D.transform.position + new Vector3(_circleCollider2D.offset.x, _circleCollider2D.offset.y, 0) * transform.localScale.x, _circleCollider2D.radius);
- if (_attackSounds.Count > 0 && _audioSource != null)
- {
- AudioClip randomClip = _attackSounds[Random.Range(0, _attackSounds.Count)];
- _audioSource.clip = randomClip;
- _audioSource.Play();
- }
- foreach (var collider in hits)
- {
- if (collider.TryGetComponent(out Enemy enemy) & collider.TryGetComponent(out HealthBar healthBar))
- {
- healthBar.GetDamage(_damage);
- healthBar.UpdateHealthBar();
- Rigidbody2D rigidbody2D = enemy.GetComponent<Rigidbody2D>();
- Vector2 direction = (enemy.transform.position - transform.position).normalized;
- rigidbody2D.AddForce(new Vector2(direction.x * _attackForce, _attackForce), ForceMode2D.Impulse);
- }
- }
- }
- private IEnumerator AttackWithDelay()
- {
- _canAttack = false;
- float attackSpeed = 1f + (_attackSpeed / 100);
- _timeBeforeAttack = (_tempBeforeAttack / attackSpeed);
- _timeAfterAttack = (_tempAfterAttack / attackSpeed);
- _attackTime = _timeBeforeAttack + _timeAfterAttack;
- _animator.SetFloat("AttackSpeed", attackSpeed);
- _animator.SetBool("Attack", true);
- WaitForSeconds waitForSeconds = new WaitForSeconds(_timeBeforeAttack);
- yield return waitForSeconds;
- Attack();
- _canAttack = true;
- }
- }
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour
- {
- [SerializeField] private float _minGroundNormalY = .65f;
- [SerializeField] private float _gravityModifier = 1f;
- [SerializeField] private Vector2 _velocity;
- [SerializeField] private LayerMask _layerMask;
- [SerializeField] private float _speed;
- [SerializeField] private float _jumpPower;
- protected Vector2 TargetVelocity;
- protected bool Grounded;
- protected Vector2 GroundNormal;
- protected Rigidbody2D RigidBody;
- protected ContactFilter2D ContactFilter;
- protected RaycastHit2D[] HitBuffer = new RaycastHit2D[16];
- protected List<RaycastHit2D> HitBufferList = new List<RaycastHit2D>(16);
- protected const float minMoveDistance = 0.001f;
- protected const float shellRadius = 0.01f;
- private Animator _animator;
- private bool isFacingRight = true;
- void OnEnable()
- {
- RigidBody = GetComponent<Rigidbody2D>();
- }
- void Start()
- {
- ContactFilter.useTriggers = false;
- ContactFilter.SetLayerMask(_layerMask);
- ContactFilter.useLayerMask = true;
- _animator = GetComponent<Animator>();
- }
- void Update()
- {
- TargetVelocity = new Vector2(Input.GetAxis("Horizontal") * _speed, 0);
- if (TargetVelocity.x != 0 && Grounded)
- {
- _animator.SetBool("Jump",false);
- _animator.SetBool("Run", true);
- }
- else if (TargetVelocity.x == 0 && Grounded)
- {
- _animator.SetBool("Jump",false);
- _animator.SetBool("Run",false);
- _animator.SetBool("Idle",true);
- }
- if (Input.GetKey(KeyCode.Space) && Grounded)
- {
- _velocity.y = _jumpPower;
- _animator.SetBool("Run",false);
- _animator.SetBool("Idle",false);
- _animator.SetBool("Jump",true);
- }
- }
- void FixedUpdate()
- {
- _velocity += _gravityModifier * Physics2D.gravity * Time.deltaTime;
- _velocity.x = TargetVelocity.x;
- Grounded = false;
- Vector2 deltaPosition = _velocity * Time.deltaTime;
- Vector2 moveAlongGround = new Vector2(GroundNormal.y, -GroundNormal.x);
- Vector2 move = moveAlongGround * deltaPosition.x;
- Movement(move, false);
- move = Vector2.up * deltaPosition.y;
- Movement(move, true);
- Flip();
- }
- void Movement(Vector2 move, bool yMovement)
- {
- float distance = move.magnitude;
- if (distance > minMoveDistance)
- {
- int count = RigidBody.Cast(move, ContactFilter, HitBuffer, distance + shellRadius);
- HitBufferList.Clear();
- for (int i = 0; i < count; i++)
- {
- HitBufferList.Add(HitBuffer[i]);
- }
- for (int i = 0; i < HitBufferList.Count; i++)
- {
- Vector2 currentNormal = HitBufferList[i].normal;
- if (currentNormal.y > _minGroundNormalY)
- {
- Grounded = true;
- _animator.SetBool("Idle",true);
- if (yMovement)
- {
- GroundNormal = currentNormal;
- currentNormal.x = 0;
- }
- }
- float projection = Vector2.Dot(_velocity, currentNormal);
- if (projection < 0)
- {
- _velocity = _velocity - projection * currentNormal;
- }
- float modifiedDistance = HitBufferList[i].distance - shellRadius;
- distance = modifiedDistance < distance ? modifiedDistance : distance;
- }
- }
- RigidBody.position = RigidBody.position + move.normalized * distance;
- }
- private void Flip()
- {
- if (isFacingRight && TargetVelocity.x < 0f || !isFacingRight && TargetVelocity.x > 0f)
- {
- isFacingRight = !isFacingRight;
- Vector3 localScale = transform.localScale;
- localScale.x *= -1f;
- transform.localScale = localScale;
- }
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- [RequireComponent(typeof(AudioSource))]
- public class LoadMusic : MonoBehaviour
- {
- private AudioSource _audioSource;
- private void Start()
- {
- _audioSource = GetComponent<AudioSource>();
- _audioSource.volume = MusicSlider.GetSavedValume();
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class CaptionsAnimate : MonoBehaviour
- {
- [SerializeField] private TMP_Text _text;
- [SerializeField] private float _speed = 1f;
- [SerializeField] private float _targetY = 100;
- private void Start()
- {
- StartCoroutine(MoveTextUp());
- }
- private IEnumerator MoveTextUp()
- {
- float currentY = _text.rectTransform.anchoredPosition.y;
- while (currentY < _targetY)
- {
- currentY += _speed * Time.deltaTime;
- _text.rectTransform.anchoredPosition = new Vector2(_text.rectTransform.anchoredPosition.x, currentY);
- yield return null;
- }
- SceneManager.LoadScene(0);
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class DeathBox : MonoBehaviour
- {
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.transform.TryGetComponent(out Player player))
- {
- HealthBar healthBar = player.gameObject.GetComponent<HealthBar>();
- if (healthBar != null)
- {
- healthBar.Death();
- healthBar.UpdateHealthBar();
- }
- }
- else
- {
- Destroy(other.transform.gameObject);
- }
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- public class ParalaxBackground : MonoBehaviour
- {
- [SerializeField] private Transform _target;
- [SerializeField, Range(0f, 1f)] private float _strength = 0.1f;
- [SerializeField] private bool _isVerticalParalax;
- private Vector3 _targetPreviousPosition;
- private void Start()
- {
- if (!_target)
- {
- _target = Camera.main.transform;
- }
- _targetPreviousPosition = _target.position;
- }
- private void Update()
- {
- Vector3 delta = _target.position - _targetPreviousPosition;
- if (_isVerticalParalax == false)
- {
- delta.y = 0;
- }
- _targetPreviousPosition = _target.position;
- transform.position += delta * _strength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment