| | 1 | | // NOTE: following separation lines are used only for clarity and do not take part of our commenting code style! |
| | 2 | |
|
| | 3 | | //---------------------------------------------------------------------------------------------------- Usings group |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using JetBrains.Annotations; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Events; |
| | 9 | |
|
| | 10 | | namespace DCL.UIComponents |
| | 11 | | { |
| | 12 | | //------------------------------------------------------------------------------------------------ Enum, delegates, |
| | 13 | | public enum Side { Left, Right } |
| | 14 | |
|
| | 15 | | public delegate void Destroction<T> (T current); |
| | 16 | |
|
| | 17 | | public interface IInitializable |
| | 18 | | { |
| | 19 | | bool IsInitialized { get; } |
| | 20 | |
|
| | 21 | | void Initialize(); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | [RequireComponent(typeof(Animation))] |
| | 25 | | public class CodeStyleExample : MonoBehaviour, IInitializable |
| | 26 | | { |
| | 27 | | //---------------------------------------------------------------------------------------- Enum, delegates decl |
| | 28 | | private enum Direction { North, South, West, East } |
| | 29 | | private delegate void Interaction<T> (T current); |
| | 30 | |
|
| | 31 | | //---------------------------------------------------------------------------------------- Const and static rea |
| | 32 | | public const string ASSET_PATH = "AvatarPrefab"; |
| | 33 | | private const float MAX_TIMER = 10f; |
| | 34 | |
|
| 0 | 35 | | public static readonly int WAVE_ANIM_HASH = Animator.StringToHash("Wave"); |
| 0 | 36 | | private static readonly int DANCE_ANIM_HASH = Animator.StringToHash("Dance"); |
| | 37 | |
|
| | 38 | | //---------------------------------------------------------------------------------------- Events and UnityEven |
| | 39 | | public event Destroction<CodeStyleExample> Destroyed; |
| | 40 | | [NonSerialized] public UnityEvent<bool> Closed; // use [NonSerialized] or [HideInInspector] for UnityEvents |
| | 41 | |
|
| | 42 | | //---------------------------------------------------------------------------------------- Fields group (other |
| 0 | 43 | | public readonly List<int> CachedNumbers = new List<int>(); |
| 0 | 44 | | protected readonly List<Vector2> poistions = new List<Vector2>(); |
| | 45 | |
|
| | 46 | | public int Timer; |
| | 47 | |
|
| | 48 | | [SerializeField] private AnimationClip[] clips; |
| | 49 | | [SerializeField] private float armor; |
| | 50 | | [SerializeField] private float health; |
| | 51 | | [SerializeField] private bool isVisitble; |
| | 52 | |
|
| | 53 | | protected float cooldown; |
| | 54 | |
|
| | 55 | | private Animation animation; |
| | 56 | | private Interaction<int> interactionsBuffer; |
| | 57 | |
|
| | 58 | | //---------------------------------------------------------------------------------------- Properties group |
| 0 | 59 | | public bool IsInitialized { get; private set; } |
| | 60 | |
|
| | 61 | | [PublicAPI] |
| 0 | 62 | | public bool IsVisitble => isVisitble; // [PublicAPI] Public - used only from outside of Unity solution (by other |
| | 63 | |
|
| | 64 | | //---------------------------------------------------------------------------------------- Public methods group |
| | 65 | | public void Initialize() => |
| 0 | 66 | | IsInitialized = true; |
| | 67 | |
|
| | 68 | | public void ApplyDamage(float damage) // Public - called from outside of the class (by other class) |
| | 69 | | { |
| 0 | 70 | | float resultDamage = ComputeDamage(damage); |
| 0 | 71 | | TakeHit(resultDamage); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private float ComputeDamage(float damage) => // Called by ApplyDamage |
| 0 | 75 | | damage - armor; |
| | 76 | |
|
| | 77 | | private void TakeHit(float amount) => // Called by ApplyDamage |
| 0 | 78 | | health -= amount; |
| | 79 | |
|
| | 80 | | [UsedImplicitly] |
| | 81 | | public void Hide() => // [UsedImplicitly] Public - called implicitly, for example, by Unity animation events or |
| 0 | 82 | | SetVisibility(visible: true); |
| | 83 | |
|
| | 84 | | private void SetVisibility(bool visible) => // Called by Hide |
| 0 | 85 | | isVisitble = visible; |
| | 86 | |
|
| | 87 | | //---------------------------------------------------------------------------------------- Unity callbacks meth |
| | 88 | | private void Awake() // is used for getting references |
| | 89 | | { |
| 0 | 90 | | animation = GetComponent<Animation>(); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | private void Start() // is used for initialization logic |
| | 94 | | { |
| 0 | 95 | | foreach (AnimationClip clip in clips) |
| 0 | 96 | | animation.AddClip(clip, clip.name); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | private void OnEnable() // is used for events subscription |
| | 100 | | { |
| 0 | 101 | | Closed.AddListener(OnClosed); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void OnClosed(bool _) => // use _, __, ___ for parameters name if required parameters are not used insid |
| 0 | 105 | | cooldown = MAX_TIMER; |
| | 106 | |
|
| | 107 | | private void OnDisable() // is used for events unsubscription |
| | 108 | | { |
| 0 | 109 | | Closed.RemoveListener(OnClosed); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | private void OnDestroy() // is used for any needed clean-up |
| | 113 | | { |
| 0 | 114 | | Destroyed?.Invoke(this); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | //---------------------------------------------------------------------------------------- internal-protected-p |
| | 118 | | // methods that weren't called inside methods above 👆 |
| 0 | 119 | | internal void Method1() { } |
| 0 | 120 | | protected void Method2() { } |
| 0 | 121 | | private void Method3() { } |
| | 122 | |
|
| | 123 | | //---------------------------------------------------------------------------------------- Nested classes/struc |
| | 124 | | private struct Data |
| | 125 | | { |
| | 126 | | public int A; |
| | 127 | | public bool B; |
| | 128 | | } |
| | 129 | | } |
| | 130 | | } |