Skip to content

Commit afb70e7

Browse files
code refactoring
1 parent 7fbbace commit afb70e7

26 files changed

+54
-32
lines changed

Audio/AudioRandomizer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace RPG.Audio {
44
public class AudioRandomizer : MonoBehaviour {
5-
[SerializeField]
6-
private AudioClip[] audioClips;
5+
[SerializeField] private AudioClip[] audioClips;
76

87
private AudioSource audioSource;
98

Audio/Footsteps.cs

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

33
namespace RPG.Audio {
4+
//TODO
5+
/// <summary>
6+
/// The implementation for footsteps sounds is still TODO
7+
/// </summary>
48
public class Footsteps : MonoBehaviour {
59
[SerializeField] private AudioClip walkClip;
610
[SerializeField] private AudioClip runClip;

Combat/CombatTarget.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using RPG.Attributes;
22
using RPG.Core;
3+
using RPG.NPC;
34
using UnityEngine;
45

56
namespace RPG.Combat {
67

78
[RequireComponent(typeof(Health))]
8-
public class CombatTarget : MonoBehaviour, IRaycastable {
9+
public class CombatTarget : NPCBase {
910

10-
public CursorType Cursor => CursorType.Combat;
11+
public new CursorType Cursor => CursorType.Combat;
1112

12-
public bool HandleRaycast(GameObject callingObject) {
13+
public new bool HandleRaycast(GameObject callingObject) {
1314
Fighter fighter = callingObject.GetComponent<Fighter>();
1415
if (!fighter.CanAttack(gameObject)) {
1516
return false;

Combat/Fighter.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Fighter : MonoBehaviour, IAction, ISaveable, IModifierProvider {
3535
private CinematicControlRemover[] cinematicControlRemovers;
3636

3737
public event Action updateTargetUI;
38+
public Health Target => target;
3839

3940
private void Awake() {
4041
mover = GetComponent<Mover>();
@@ -72,7 +73,7 @@ private void Update() {
7273
return;
7374
}
7475

75-
if (!GetIsInRange()) {
76+
if (!GetIsInRange(target.transform)) {
7677
mover.MoveTo(target.transform.position, 1f);
7778
stopped = false;
7879
} else {
@@ -99,16 +100,16 @@ private void AttackBehaviour() {
99100

100101
private void MakeEyeContact() {
101102
characterBehaviour.LookAtTarget(target.transform);
102-
target.GetComponent<CharacterBehaviour>().AttackedBy(gameObject);
103+
target.GetComponent<CharacterBehaviour>().LookAtTarget(gameObject.transform);
103104
}
104105

105106
private void TriggerAttack() {
106107
animator.ResetTrigger(STOP_ATTACK_TRIGGER);
107108
animator.SetTrigger(ATTACK_TRIGGER);
108109
}
109110

110-
private bool GetIsInRange() {
111-
return IsTargetInRange(transform, target.transform, currentWeaponConfig.Range);
111+
private bool GetIsInRange(Transform targetTransform) {
112+
return IsTargetInRange(transform, targetTransform, currentWeaponConfig.Range);
112113
}
113114

114115
// Animation Trigger
@@ -140,15 +141,13 @@ private void StopAttack() {
140141
animator.SetTrigger(STOP_ATTACK_TRIGGER);
141142
}
142143

143-
public Health Target => target;
144-
145144
public void EquipWeapon(WeaponConfig weapon) {
146145
currentWeaponConfig = weapon;
147146
currentWeapon.value = weapon.Spawn(rightHandTransform, leftHandTransform, animator);
148147
}
149148

150149
public bool CanAttack(GameObject target) {
151-
if (target == null || !mover.CanMoveTo(target.transform.position)) {
150+
if (target == null || (!mover.CanMoveTo(target.transform.position) && !GetIsInRange(target.transform))) {
152151
return false;
153152
}
154153

Combat/PickupBase.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using System.Collections;
2-
using RPG.Attributes;
32
using RPG.Core;
43
using RPG.Movement;
54
using UnityEngine;
65
using UnityEngine.Events;
76

87
namespace RPG.Combat {
98
public class PickupBase : MonoBehaviour, IRaycastable {
10-
[SerializeField] public OnPickupEvent onPickup;
9+
private Collider objectCollider;
1110

12-
public CursorType Cursor => CursorType.Pickup;
11+
[SerializeField] public OnPickupEvent onPickup;
1312

1413
[System.Serializable]
1514
public class OnPickupEvent : UnityEvent<PickupBase> { }
1615

17-
private Collider objectCollider;
16+
public CursorType Cursor => CursorType.Pickup;
1817

1918
private void Awake() {
2019
objectCollider = GetComponent<Collider>();
@@ -33,7 +32,7 @@ private void ShowPickup(bool shouldShow) {
3332
}
3433
}
3534

36-
public void Pickup(bool respawnable, float respawnTime) {
35+
protected void Pickup(bool respawnable, float respawnTime) {
3736
if (respawnable) {
3837
StartCoroutine(HideForSeconds(respawnTime));
3938
} else {

Core/CharacterBehaviour.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,5 @@ public void LookAtTarget(Transform targetTransform) {
99
Quaternion targetRotation = Quaternion.LookRotation(targetTransform.position - transform.position);
1010
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
1111
}
12-
13-
public void AttackedBy(GameObject target) {
14-
LookAtTarget(target.transform);
15-
}
1612
}
1713
}

Events/EventSystem.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using RPG.Attributes;
22
using RPG.Combat;
3+
using RPG.Core;
34
using RPG.NPC;
45
using UnityEngine;
56

@@ -32,6 +33,7 @@ public void ItemPickedUp(PickupBase pickup) {
3233
public void InitiatedDialogue(DialogueInitiator npc) {
3334
if (OnTalkedToNPC != null) {
3435
OnTalkedToNPC(npc);
36+
GetComponent<ActionScheduler>().CancelCurrentAction();
3537
}
3638
}
3739
}

NPC/DialogueInitiator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
using UnityEngine.Events;
55

66
namespace RPG.NPC {
7-
public class DialogueInitiator : NPC {
7+
public class DialogueInitiator : NPCBase {
88
[SerializeField] private Dialogue dialogue;
99
[SerializeField] private DialogueManager dialogueManager;
10-
[SerializeField] public OnPickupEvent onDialogeInitiated;
10+
[SerializeField] public OnDialogueInitiatedEvent onDialogeInitiated;
1111

1212
[System.Serializable]
13-
public class OnPickupEvent : UnityEvent<DialogueInitiator> { }
13+
public class OnDialogueInitiatedEvent : UnityEvent<DialogueInitiator> { }
1414

1515
public new CursorType Cursor => CursorType.Converse;
1616
public DialogueManager DialogueManager => dialogueManager;

NPC/NPC.cs renamed to NPC/NPCBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
namespace RPG.NPC {
77
[DisallowMultipleComponent]
8-
public class NPC : MonoBehaviour, IRaycastable {
8+
public class NPCBase : MonoBehaviour, IRaycastable {
99

1010
public CursorType Cursor => CursorType.NPC;
1111
public virtual void Interact() { }
1212

1313
public bool HandleRaycast(GameObject callingObject) {
1414
if (Input.GetMouseButtonDown(0)) {
1515
if (IsTargetInRange(callingObject.transform, transform, 2.5f)) {
16-
callingObject.GetComponent<CharacterBehaviour>().LookAtTarget(transform);
16+
//TODO make this work with CharacterBehaviour's smooth LookAt. Maybe use a Coroutine?
17+
transform.LookAt(callingObject.transform);
18+
callingObject.transform.LookAt(transform);
1719
Interact();
1820
} else {
1921
callingObject.GetComponent<Mover>().StartMovement(transform.position, 1f).InteractWithTarget(delegate { Interact(); });
File renamed without changes.

0 commit comments

Comments
 (0)