I did the 3 first courses with the same project.
I have inventory and Quest & Dialogues actually, Starting RPG Shop.
I dont understand how i can fix my issue. I would like to have weapon damage working as before.
Should i add the IModifier provider on fighter script ?
Thnaks for your help.
here is my fighter script :
using UnityEngine;
using RPG.Movement;
using RPG.Core;
using System;
using GameDevTV.Saving;
using RPG.Attributes;
using RPG.Stats;
using System.Collections.Generic;
using GameDevTV.Utils;
using GameDevTV.Inventories;
namespace RPG.Combat
{
public class Fighter : MonoBehaviour, IAction, ISaveable
{
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] Transform rightHandTransform = null;
[SerializeField] Transform leftHandTransform = null;
[SerializeField] WeaponConfig defaultweapon = null;
[SerializeField] float autoAttackRange = 4f;
Health target;
Equipment equipment;
float timeSinceLastAttack = Mathf.Infinity;
WeaponConfig currentWeaponConfig;
LazyValue<Weapon> currentWeapon;
private void Awake()
{
currentWeaponConfig = defaultweapon;
currentWeapon = new LazyValue<Weapon>(SetupDefaultWeapon);
equipment = GetComponent<Equipment>();
if (equipment)
{
equipment.equipmentUpdated += UpdateWeapon;
}
}
private Weapon SetupDefaultWeapon()
{
return AttachWeapon(defaultweapon);
}
private void Start()
{
currentWeapon.ForceInit();
}
private void Update()
{
timeSinceLastAttack += Time.deltaTime;
if (target == null) return;
//if (target.IsDead()) return;
if (target.IsDead())
{
target = FindNewTargetInRange();
if (target == null) return;
}
if (target != null && !GetIsInRange(target.transform))
{
GetComponent<Mover>().MoveTo(target.transform.position, 1f);
}
else
{
GetComponent<Mover>().Cancel();
AttackBehaviour();
}
}
public void EquipWeapon(WeaponConfig weapon)
{
currentWeaponConfig = weapon;
currentWeapon.value = AttachWeapon(weapon);
}
private void UpdateWeapon()
{
var weapon = equipment.GetItemInSlot(EquipLocation.Weapon) as WeaponConfig;
if (weapon == null)
{
EquipWeapon(defaultweapon);
}
else
{
EquipWeapon(weapon);
}
}
private Weapon AttachWeapon(WeaponConfig weapon)
{
Animator animator = GetComponent<Animator>();
return weapon.Spwan(rightHandTransform, leftHandTransform, animator);
}
public Health GetTarget()
{
return target;
}
private bool GetIsInRange(Transform targetTransform)
{
return Vector3.Distance(transform.position, targetTransform.position) < currentWeaponConfig.GetRange();
}
public bool CanAttack(GameObject combatTarget)
{
if (combatTarget == null) return false;
if (!GetComponent<Mover>().CanMoveTo(combatTarget.transform.position) && !GetIsInRange(combatTarget.transform))
{
return false;
}
Health targetToTest = combatTarget.GetComponent<Health>();
return targetToTest != null && !targetToTest.IsDead();
}
public void Attack(GameObject combatTarget)
{
GetComponent<ActionScheduler>().StartAction(this);
target = combatTarget.GetComponent<Health>();
//print("Take that you short, squat paesant!");
}
private void AttackBehaviour()
{
transform.LookAt(target.transform);
if (timeSinceLastAttack > timeBetweenAttacks)
{
// This will trigger the Hit() event
TriggerAttack();
timeSinceLastAttack = 0;
}
}
private Health FindNewTargetInRange()
{
Health best = null;
float bestDistance = Mathf.Infinity;
foreach (var candidate in FindAllTargetsInRange())
{
float candidateDistance = Vector3.Distance(
transform.position, candidate.transform.position);
if (candidateDistance < bestDistance)
{
best = candidate;
bestDistance = candidateDistance;
}
}
return best;
}
private IEnumerable<Health> FindAllTargetsInRange()
{
RaycastHit[] raycastHits = Physics.SphereCastAll(transform.position,
autoAttackRange, Vector3.up);
foreach (var hit in raycastHits)
{
Health health = hit.transform.GetComponent<Health>();
if (health == null) continue;
if (health.IsDead()) continue;
if (health.gameObject == gameObject) continue;
yield return health;
}
}
private void TriggerAttack()
{
GetComponent<Animator>().ResetTrigger("stopAttack");
GetComponent<Animator>().SetTrigger("attack");
}
// Animation Event
void Hit()
{
if (target == null) return;
float damage = GetComponent<BaseStats>().GetStat(Stat.Damage);
if (currentWeapon.value != null)
{
currentWeapon.value.OnHit();
}
if (currentWeaponConfig.HasProjectile())
{
currentWeaponConfig.LaunchProjectile(rightHandTransform, leftHandTransform, target, gameObject, damage);
}
else
{
target.TakeDamage(gameObject, damage);
}
}
//Animation Event
void Shoot()
{
Hit();
}
public void Cancel()
{
StopAttack();
target = null;
GetComponent<Mover>().Cancel();
}
private void StopAttack()
{
GetComponent<Animator>().ResetTrigger("attack");
GetComponent<Animator>().SetTrigger("stopAttack");
}
public object CaptureState()
{
return currentWeaponConfig.name;
}
public void RestoreState(object state)
{
string weaponName = (string)state;
WeaponConfig weapon = Resources.Load<WeaponConfig>(weaponName);
EquipWeapon(weapon);
}
}
}