Skip to content

Commit 4e7a866

Browse files
refactored code
1 parent 6f9e9c2 commit 4e7a866

File tree

17 files changed

+87
-74
lines changed

17 files changed

+87
-74
lines changed

Attributes/Health.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public class TakeDamageEvent : UnityEvent<float> { }
2929
[System.Serializable]
3030
public class OnDieEvent : UnityEvent<Health> { }
3131

32+
public bool IsDead { get; private set; } = false;
33+
public float Percentage => Fraction * 100;
34+
public float HealthPoints => healthPoints.value;
35+
public float MaxHealthPoints => baseStats.GetStat(Stat.Health);
36+
public float Fraction => healthPoints.value / baseStats.GetStat(Stat.Health);
37+
3238
private void Awake() {
3339
animator = GetComponent<Animator>();
3440
actionScheduler = GetComponent<ActionScheduler>();
@@ -55,16 +61,6 @@ private float GetInitialHealth() {
5561
return baseStats.GetStat(Stat.Health);
5662
}
5763

58-
public bool IsDead { get; private set; } = false;
59-
60-
public float Percentage => Fraction * 100;
61-
62-
public float HealthPoints => healthPoints.value;
63-
64-
public float MaxHealthPoints => baseStats.GetStat(Stat.Health);
65-
66-
public float Fraction => healthPoints.value / baseStats.GetStat(Stat.Health);
67-
6864
public void TakeDamage(GameObject instigator, float damage) {
6965
if (IsDead) {
7066
return;

Audio/AudioRandomizer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using UnityEngine;
22

33
namespace RPG.Audio {
4+
/// <summary>
5+
/// Allows playing random audio FX from a collection.
6+
/// </summary>
47
public class AudioRandomizer : MonoBehaviour {
58
[SerializeField] private AudioClip[] audioClips;
69

Cinematics/CinematicControlRemover.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
namespace RPG.Cinematics {
88

99
public class CinematicControlRemover : MonoBehaviour {
10-
public event Action onCinematicStart;
11-
public event Action onCinematicEnd;
10+
public event Action<bool> onCinematicStart;
11+
public event Action<bool> onCinematicEnd;
1212
private PlayableDirector playableDirector;
1313
private ActionScheduler actionScheduler;
1414
private GameObject player;
@@ -35,13 +35,13 @@ public void EnableControl(PlayableDirector pd) {
3535
if (playableDirector != null) {
3636
playerController.enabled = true;
3737
}
38-
onCinematicEnd();
38+
onCinematicEnd(false);
3939
}
4040

4141
public void DisableControl(PlayableDirector pd) {
4242
actionScheduler.CancelCurrentAction();
4343
playerController.enabled = false;
44-
onCinematicStart();
44+
onCinematicStart(true);
4545
}
4646
}
4747
}

Combat/CombatTarget.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using UnityEngine;
44

55
namespace RPG.Combat {
6-
6+
/// <summary>
7+
/// GameObjects with this component will become attackable.
8+
/// </summary>
79
[RequireComponent(typeof(Health))]
810
public class CombatTarget : MonoBehaviour, IRaycastable {
911

Combat/Fighter.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class Fighter : MonoBehaviour, IAction, ISaveable, IModifierProvider {
3232
private float timeSinceLastAttack = Mathf.Infinity;
3333
private WeaponConfig currentWeaponConfig;
3434
private LazyValue<Weapon> currentWeapon;
35-
private bool attackDisabled = false;
3635
private CinematicControlRemover[] cinematicControlRemovers;
3736

37+
public bool attackDisabled = false;
3838
public event Action updateTargetUI;
3939
public Health Target => target;
4040

@@ -56,15 +56,15 @@ private void Start() {
5656

5757
private void OnEnable() {
5858
for (int i = 0; i < cinematicControlRemovers.Length; i++) {
59-
cinematicControlRemovers[i].onCinematicStart += DisableAttack;
60-
cinematicControlRemovers[i].onCinematicEnd += EnableAttack;
59+
cinematicControlRemovers[i].onCinematicStart += ToggleAttack;
60+
cinematicControlRemovers[i].onCinematicEnd += ToggleAttack;
6161
}
6262
}
6363

6464
private void OnDisable() {
6565
for (int i = 0; i < cinematicControlRemovers.Length; i++) {
66-
cinematicControlRemovers[i].onCinematicStart -= DisableAttack;
67-
cinematicControlRemovers[i].onCinematicEnd -= EnableAttack;
66+
cinematicControlRemovers[i].onCinematicStart -= ToggleAttack;
67+
cinematicControlRemovers[i].onCinematicEnd -= ToggleAttack;
6868
}
6969
}
7070

@@ -208,12 +208,8 @@ public IEnumerable<float> GetPercentageModifiers(Stat stat) {
208208
}
209209
}
210210

211-
public void EnableAttack() {
212-
attackDisabled = false;
213-
}
214-
215-
public void DisableAttack() {
216-
attackDisabled = true;
211+
private void ToggleAttack(bool disabled) {
212+
attackDisabled = disabled;
217213
}
218214
}
219215
}

Combat/Projectile.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@
66
namespace RPG.Combat {
77

88
public class Projectile : MonoBehaviour {
9-
private const string TAG_CINEMATIC = "Cinematic";
10-
private const string TAG_PICKUP = "Pickup";
119
[SerializeField] private float speed = 1f;
1210
[SerializeField] private bool isHoming = false;
13-
[SerializeField] private GameObject hitEffect = null;
1411
[SerializeField] private float projectileTTL = 10f;
1512
[SerializeField] private float afterHitTTL = 0.2f;
13+
[SerializeField] private GameObject hitEffect = null;
1614
[SerializeField] private GameObject[] destroyAfterHit;
1715
[SerializeField] private UnityEvent onHit;
1816
[SerializeField] private UnityEvent onLaunch;
17+
private const string TAG_CINEMATIC = "Cinematic";
18+
private const string TAG_PICKUP = "Pickup";
19+
private float damage = 0;
1920
private Health target = null;
2021
private GameObject instigator = null;
22+
/// <summary>
23+
/// Can update UI when damaging an enemy (e.g. update enemy health on HUD)
24+
/// </summary>
2125
private Action updateUI = null;
22-
private float damage = 0;
2326

24-
void Start() {
27+
private void Start() {
2528
transform.LookAt(GetAimLocation());
2629
onLaunch.Invoke();
2730
}
@@ -36,7 +39,7 @@ private void Update() {
3639
transform.Translate(Vector3.forward * speed * Time.deltaTime);
3740
}
3841

39-
void OnTriggerEnter(Collider other) {
42+
private void OnTriggerEnter(Collider other) {
4043
// make projectiles go trough cinematic triggers and items on the ground
4144
if (other.gameObject.CompareTag(TAG_CINEMATIC) || other.gameObject.CompareTag(TAG_PICKUP)) {
4245
return;

Combat/WeaponConfig.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ private enum HAND { LEFT, RIGHT }
2121
public float Damage => weaponDamage;
2222
public float PercentageBous => percentageBonus;
2323

24-
public Transform GetTransform(Transform rightHand, Transform leftHand) {
24+
private void DestroyOldWeapon(Transform rightHand, Transform leftHand) {
25+
Transform currentWeapon = rightHand.Find(WEAPON_NAME);
26+
if (currentWeapon == null) {
27+
currentWeapon = leftHand.Find(WEAPON_NAME);
28+
}
29+
if (currentWeapon == null) {
30+
return;
31+
}
32+
currentWeapon.name = DESTORYING;
33+
Destroy(currentWeapon.gameObject);
34+
}
35+
36+
private Transform GetTransform(Transform rightHand, Transform leftHand) {
2537
return hand == HAND.RIGHT ? rightHand : leftHand;
2638
}
2739

@@ -42,22 +54,20 @@ public Weapon Spawn(Transform rightHand, Transform leftHand, Animator animator)
4254
return weapon;
4355
}
4456

45-
private void DestroyOldWeapon(Transform rightHand, Transform leftHand) {
46-
Transform currentWeapon = rightHand.Find(WEAPON_NAME);
47-
if (currentWeapon == null) {
48-
currentWeapon = leftHand.Find(WEAPON_NAME);
49-
}
50-
if (currentWeapon == null) {
51-
return;
52-
}
53-
currentWeapon.name = DESTORYING;
54-
Destroy(currentWeapon.gameObject);
55-
}
56-
5757
public bool HasProjectile() {
5858
return projectile != null;
5959
}
6060

61+
//TODO finish this
62+
/// <summary>
63+
///
64+
/// </summary>
65+
/// <param name="rightHand"></param>
66+
/// <param name="leftHand"></param>
67+
/// <param name="target"></param>
68+
/// <param name="instigator"></param>
69+
/// <param name="calculatedDamage"></param>
70+
/// <param name="updateUI"></param>
6171
public void LaunchProjectile(Transform rightHand, Transform leftHand, Health target, GameObject instigator, float calculatedDamage, Action updateUI) {
6272
Projectile projectileInstance = Instantiate(projectile, GetTransform(rightHand, leftHand).position, Quaternion.identity);
6373
projectileInstance.SetTarget(target, instigator, calculatedDamage, updateUI);

Control/AIController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ private void Update() {
6464
UpdateTimers();
6565
}
6666

67+
private void OnDrawGizmosSelected() {
68+
Gizmos.color = Color.blue;
69+
Gizmos.DrawWireSphere(transform.position, chaseDistance);
70+
}
71+
6772
private void UpdateTimers() {
6873
timeSinceLastSawPlayer += Time.deltaTime;
6974
timeSinceLastWaypoint += Time.deltaTime;
@@ -132,11 +137,6 @@ private Transform GetCurrentWaypoint() {
132137
return patrolPath.GetWayPoint(currentWaypointIndex);
133138
}
134139

135-
private void OnDrawGizmosSelected() {
136-
Gizmos.color = Color.blue;
137-
Gizmos.DrawWireSphere(transform.position, chaseDistance);
138-
}
139-
140140
public void Aggrevate() {
141141
timeSinceAggrevated = 0;
142142
}

Control/PatrolPath.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using UnityEngine;
22

33
namespace RPG.Control {
4-
4+
/// <summary>
5+
/// Used for calculating patrol path of NPCs and visualizing it with gizmos in the editor.
6+
/// </summary>
57
public class PatrolPath : MonoBehaviour {
68
private const float waypointRadius = 0.3f;
79

Control/PlayerController.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ public class PlayerController : MonoBehaviour {
1414
struct CursorMapping {
1515
public CursorType type;
1616
public Texture2D texture;
17-
private Vector2 hotspot;
18-
19-
public Vector2 HotSpot {
20-
get;
21-
}
17+
public Vector2 hotspot;
2218
}
2319

2420
[SerializeField] private CursorMapping[] mappings = null;
@@ -138,7 +134,7 @@ private Ray GetMouseRay() {
138134

139135
public void SetCursor(CursorType type) {
140136
CursorMapping mapping = GetCursorMapping(type);
141-
Cursor.SetCursor(mapping.texture, mapping.HotSpot, CursorMode.Auto);
137+
Cursor.SetCursor(mapping.texture, mapping.hotspot, CursorMode.Auto);
142138
}
143139
}
144140
}

0 commit comments

Comments
 (0)