From 74a95468a51d44982d99fd8d190d95800c9c43a3 Mon Sep 17 00:00:00 2001 From: Stedd Date: Fri, 14 Oct 2022 18:25:19 +0200 Subject: [PATCH] Cleanup --- Assets/Enemies/Enemy.prefab | 9 +++-- Assets/Enemies/EnemyAI.cs | 72 ++++++++++++++++++----------------- Assets/Enemies/EnemyAttack.cs | 20 ++++------ Assets/Enemies/EnemyHealth.cs | 24 +++++------- Assets/Enemies/IDamageable.cs | 6 +-- Assets/PlayerHealth.cs | 14 +++---- Assets/Weapons/Weapon.cs | 25 +++++------- 7 files changed, 76 insertions(+), 94 deletions(-) diff --git a/Assets/Enemies/Enemy.prefab b/Assets/Enemies/Enemy.prefab index 2664ce2..f1ee00f 100644 --- a/Assets/Enemies/Enemy.prefab +++ b/Assets/Enemies/Enemy.prefab @@ -63,10 +63,11 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 9288de7ba2b80ce43be15a0ff0aa307a, type: 3} m_Name: m_EditorClassIdentifier: - target: {fileID: 0} - detectRange: 15 - attackRange: 1.5 - isProvoked: 0 + _detectRange: 15 + _attackRange: 5 + _rotationSpeed: 0.9 + _target: {fileID: 0} + _isProvoked: 0 --- !u!195 &3924901601269037083 NavMeshAgent: m_ObjectHideFlags: 0 diff --git a/Assets/Enemies/EnemyAI.cs b/Assets/Enemies/EnemyAI.cs index ecfe4b1..283f482 100644 --- a/Assets/Enemies/EnemyAI.cs +++ b/Assets/Enemies/EnemyAI.cs @@ -1,58 +1,61 @@ -using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { - [SerializeField] Transform target; - [SerializeField] float detectRange = 15f; - [SerializeField] float attackRange = 5f; - [SerializeField] private bool isProvoked = false; + [Header("Configuration")] + [SerializeField] private float _detectRange = 15f; + [SerializeField] private float _attackRange = 5f; + [SerializeField] private float _rotationSpeed = 0.1f; + [Header("State")] + [SerializeField] private Transform _target; + [SerializeField] private bool _isProvoked; - private NavMeshAgent navMeshAgent; + private NavMeshAgent _navMeshAgent; private Animator _animator; + private static readonly int MoveAnimation = Animator.StringToHash("Move"); + private static readonly int AttackAnimation = Animator.StringToHash("Attack"); + private static readonly int IdleAnimation = Animator.StringToHash("Idle"); public bool IsProvoked { - get => isProvoked; - set => isProvoked = value; - } - private void Awake() - { - target = FindObjectOfType().transform; + get => _isProvoked; + set => _isProvoked = value; } - void Start() + private void Awake() { - navMeshAgent = GetComponent(); + _target = FindObjectOfType().transform; + } + + private void Start() + { + _navMeshAgent = GetComponent(); _animator = GetComponent(); } - void Update() + private void Update() { - SetStopDistance(attackRange * 0.9f); + SetStopDistance(_attackRange * 0.9f); - if (DistanceToTarget(target.position) < detectRange) + if (DistanceToTarget(_target.position) < _detectRange) { - isProvoked = true; + _isProvoked = true; } - if (isProvoked) + if (_isProvoked) { EngageTarget(); } - } private void EngageTarget() { - if (DistanceToTarget(target.position) <= attackRange) + if (DistanceToTarget(_target.position) <= _attackRange) { AttackTarget(); } - else if (isProvoked) + else if (_isProvoked) { FollowTarget(); } @@ -64,25 +67,25 @@ public class EnemyAI : MonoBehaviour private void FollowTarget() { - _animator.SetTrigger("Move"); - _animator.SetBool("Attack", false); - navMeshAgent.SetDestination(target.position); + _animator.SetTrigger(MoveAnimation); + _animator.SetBool(AttackAnimation, false); + _navMeshAgent.SetDestination(_target.position); } private void AttackTarget() { - _animator.SetBool("Attack", true); + _animator.SetBool(AttackAnimation, true); //print("Die Human!"); } private void Idle() { - _animator.SetTrigger("Idle"); + _animator.SetTrigger(IdleAnimation); } private void SetStopDistance(float stopDistance) { - navMeshAgent.stoppingDistance = stopDistance; + _navMeshAgent.stoppingDistance = stopDistance; } private float DistanceToTarget(Vector3 targetPosition) @@ -93,12 +96,11 @@ public class EnemyAI : MonoBehaviour void OnDrawGizmosSelected() { Gizmos.color = Color.red; - Gizmos.DrawWireSphere(transform.position, detectRange); - if (navMeshAgent != null) + Gizmos.DrawWireSphere(transform.position, _detectRange); + if (_navMeshAgent != null) { Gizmos.color = Color.blue; - Gizmos.DrawWireSphere(transform.position, navMeshAgent.stoppingDistance); + Gizmos.DrawWireSphere(transform.position, _navMeshAgent.stoppingDistance); } } - -} +} \ No newline at end of file diff --git a/Assets/Enemies/EnemyAttack.cs b/Assets/Enemies/EnemyAttack.cs index 55cc4e0..1ce9429 100644 --- a/Assets/Enemies/EnemyAttack.cs +++ b/Assets/Enemies/EnemyAttack.cs @@ -1,25 +1,21 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class EnemyAttack : MonoBehaviour { - [SerializeField] Transform target; - [SerializeField] float damage = 40f; + [Header("Config")] + [SerializeField] private Transform _target; + [SerializeField] private float _damage = 40f; private void Awake() { - target = FindObjectOfType().transform; + _target = FindObjectOfType().transform; } private void AttackHitEvent() { - if (target == null) return; - if(target.GetComponent() != null) - { - Debug.Log($"{transform.name} Hits {target.transform.name}"); - target.GetComponent().ModifyHealth(-damage); - } + if (_target == null) return; + if (_target.GetComponent() != null) return; + Debug.Log($"{transform.name} Hits {_target.transform.name}"); + _target.GetComponent().ModifyHealth(-_damage); } -} diff --git a/Assets/Enemies/EnemyHealth.cs b/Assets/Enemies/EnemyHealth.cs index 8e5b4c0..9669bad 100644 --- a/Assets/Enemies/EnemyHealth.cs +++ b/Assets/Enemies/EnemyHealth.cs @@ -1,12 +1,11 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; - public class EnemyHealth : MonoBehaviour, IDamageable { - [SerializeField] float _maxHealth = 100f; - [SerializeField] float _health ; + [Header("Config")] + [SerializeField] private float _maxHealth = 100f; + [Header("State")] + [SerializeField] private float _health; private void OnEnable() { @@ -15,16 +14,14 @@ public class EnemyHealth : MonoBehaviour, IDamageable private void Update() { - if (_health <= 0) - { - print("Ded"); - Destroy(gameObject); - } + if (!(_health <= 0)) return; + print("Ded"); + Destroy(gameObject); } - public void ModifyHealth(float _healthChange) + public void ModifyHealth(float healthChange) { - _health += _healthChange; + _health += healthChange; } public void SetHealth(float newHealth) @@ -51,5 +48,4 @@ public class EnemyHealth : MonoBehaviour, IDamageable { return _health / _maxHealth; } - -} +} \ No newline at end of file diff --git a/Assets/Enemies/IDamageable.cs b/Assets/Enemies/IDamageable.cs index 5736578..5b48820 100644 --- a/Assets/Enemies/IDamageable.cs +++ b/Assets/Enemies/IDamageable.cs @@ -1,10 +1,6 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - interface IDamageable { - public void ModifyHealth(float _healthChange); + public void ModifyHealth(float healthChange); public void SetHealth(float newHealth); diff --git a/Assets/PlayerHealth.cs b/Assets/PlayerHealth.cs index dc23685..7850cfb 100644 --- a/Assets/PlayerHealth.cs +++ b/Assets/PlayerHealth.cs @@ -1,11 +1,9 @@ -using System.Collections; -using System.Collections.Generic; using UnityEngine; public class PlayerHealth : MonoBehaviour, IDamageable { - [SerializeField] float _maxHealth; - [SerializeField] float _health; + [SerializeField] private float _maxHealth; + [SerializeField] private float _health; private void Awake() { @@ -35,9 +33,9 @@ public class PlayerHealth : MonoBehaviour, IDamageable return _maxHealth; } - public void ModifyHealth(float _healthChange) + public void ModifyHealth(float healthChange) { - _health += _healthChange; + _health += healthChange; } public void SetHealth(float newHealth) @@ -49,6 +47,4 @@ public class PlayerHealth : MonoBehaviour, IDamageable { _maxHealth = newHealth; } - - -} +} \ No newline at end of file diff --git a/Assets/Weapons/Weapon.cs b/Assets/Weapons/Weapon.cs index 7df2d65..7d508dc 100644 --- a/Assets/Weapons/Weapon.cs +++ b/Assets/Weapons/Weapon.cs @@ -1,21 +1,17 @@ -using System; -using System.Collections; -using System.Collections.Generic; using UnityEngine; -using UnityEngine.InputSystem; public class Weapon : MonoBehaviour { - [SerializeField] private Camera _FpCamera; - [SerializeField] private ParticleSystem _MuzzleFlash; - [SerializeField] private GameObject _BulletImpact; + [SerializeField] private Camera _fpCamera; + [SerializeField] private ParticleSystem _muzzleFlash; + [SerializeField] private GameObject _bulletImpact; [SerializeField] private float _range = 100f; [SerializeField] private float _weaponDamage = 25f; private void Awake() { - _FpCamera = FindObjectOfType(); - _MuzzleFlash = GetComponentInChildren(); + _fpCamera = FindObjectOfType(); + _muzzleFlash = GetComponentInChildren(); } public void OnFire() @@ -32,17 +28,16 @@ public class Weapon : MonoBehaviour { Animation(); ProcessHit(); - } private void Animation() { - _MuzzleFlash.Play(); + _muzzleFlash.Play(); } private void ProcessHit() { - if (Physics.Raycast(_FpCamera.transform.position, _FpCamera.transform.forward, out RaycastHit hit, _range)) + if (Physics.Raycast(_fpCamera.transform.position, _fpCamera.transform.forward, out RaycastHit hit, _range)) { //print($"{hit.transform.name} was hit!"); ImpactAnimation(hit); @@ -57,7 +52,7 @@ public class Weapon : MonoBehaviour hit.transform.GetComponent().ModifyHealth(-_weaponDamage); } - if(hit.transform.GetComponent()!= null) + if (hit.transform.GetComponent() != null) { hit.transform.GetComponent().IsProvoked = true; } @@ -65,7 +60,7 @@ public class Weapon : MonoBehaviour private void ImpactAnimation(RaycastHit hit) { - GameObject impactEffect = Instantiate(_BulletImpact, hit.point, Quaternion.LookRotation(hit.normal)); + GameObject impactEffect = Instantiate(_bulletImpact, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactEffect, 1); } -} +} \ No newline at end of file