Skip to content

Commit

Permalink
bugfix: Creature 코드 리펙토링 및 조건 버그 수정 (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarleter99 committed Apr 29, 2024
1 parent 49c1f1a commit bcf3485
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 112 deletions.
Binary file modified Client/Assets/Resources/Sounds/Effects/Crew/Damaged.mp3
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions Client/Assets/Resources/Sounds/Effects/Crew/GameOver.mp3.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 7 additions & 14 deletions Client/Assets/Scripts/Contents/Cameras/CreatureCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class CreatureCamera : MonoBehaviour
public Transform Transform { get; protected set; }
public Camera Camera { get; protected set; }

public float MouseSensitivity { get; protected set; }
public float XRotation { get; protected set; } // 카메라의 상하 회전 값
public float CurrentAngle { get; protected set; }
public float MouseSensitivity { get; protected set; } = 1.5f;
public float XRotation { get; protected set; } = 0f; // 카메라의 상하 회전 값
public float CurrentAngle { get; protected set; } = 0f;

public Vector3 LastForward { get; protected set; }

Expand All @@ -23,10 +23,6 @@ protected void Init()
{
Transform = transform;
Camera = GetComponent<Camera>();

CurrentAngle = 0;
MouseSensitivity = 1.5f;
XRotation = 0;
}

public void SetInfo(Creature creature)
Expand All @@ -37,15 +33,12 @@ public void SetInfo(Creature creature)

public void UpdateCameraAngle()
{
if (!Creature.HasStateAuthority || Creature.CreatureState == Define.CreatureState.Dead || !Creature.IsSpawned)
return;

try
{
if (Creature.CreatureState == Define.CreatureState.Damaged || Creature.CreatureState == Define.CreatureState.Dead)
{
Transform.forward = Creature.transform.forward;
return;
}

if (Creature.CreatureState == Define.CreatureState.Interact || Creature.CreatureState == Define.CreatureState.Use)
if (Creature.CreatureState == Define.CreatureState.Damaged || Creature.CreatureState == Define.CreatureState.Interact || Creature.CreatureState == Define.CreatureState.Use)
{
Transform.forward = LastForward;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ protected void Rpc_PlayDamagedSound()
CreatureAudioSource.Play();
}

[Rpc(RpcSources.StateAuthority, RpcTargets.All)]
[Rpc(RpcSources.StateAuthority, RpcTargets.StateAuthority)]
protected void Rpc_PlayDeadSound()
{
CreatureAudioSource.clip = Managers.SoundMng.GetOrAddAudioClip($"{Define.EFFECT_PATH}/Crew/Dead");
CreatureAudioSource.clip = Managers.SoundMng.GetOrAddAudioClip($"{Define.EFFECT_PATH}/Crew/GameOver");
CreatureAudioSource.volume = 1f;
CreatureAudioSource.pitch = 1f;
CreatureAudioSource.spatialBlend = 1.0f;
Expand All @@ -67,9 +67,6 @@ protected void Rpc_PlayDeadSound()

public override void CheckChasing()
{
if (!HasStateAuthority || !Creature.IsSpawned)
return;

Collider[] hitColliders = new Collider[1];
if (Physics.OverlapBoxNonAlloc(CreatureCamera.Transform.position, new Vector3(25f, 1f, 25f),
hitColliders, Quaternion.identity, LayerMask.GetMask("Alien")) > 0)
Expand Down
19 changes: 3 additions & 16 deletions Client/Assets/Scripts/Contents/Stats/CrewStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ public class CrewStat : BaseStat
public bool Exhausted { get; set; }
public bool Doped { get; set; }

public Action<int> OnHpChanged;
public Action<float> OnSanityChanged;

protected override void Init()
{
if (Managers.GameMng.RenderingSystem)
{
OnHpChanged += Managers.GameMng.RenderingSystem.DamageEffect;

OnSanityChanged += Managers.GameMng.RenderingSystem.SetChromaticAberration;
OnSanityChanged += Managers.GameMng.RenderingSystem.SetVignette;
}
}

public override void SetStat(CreatureData creatureData)
{
base.SetStat(creatureData);
Expand All @@ -57,7 +43,7 @@ public void ChangeHp(int value)

if (value < 0)
{
OnHpChanged.Invoke(Hp);
Managers.GameMng.RenderingSystem.DamageEffect(Hp);
}
}

Expand Down Expand Up @@ -105,7 +91,8 @@ public void ChangeSanity(float value)
RunSpeed = CrewData.RunSpeed * ratio;
SitSpeed = CrewData.SitSpeed * ratio;

OnSanityChanged?.Invoke(Sanity);
Managers.GameMng.RenderingSystem.SetChromaticAberration(Sanity);
Managers.GameMng.RenderingSystem.SetVignette(Sanity);
}

#endregion
Expand Down
25 changes: 9 additions & 16 deletions Client/Assets/Scripts/Creatures/Alien.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override void SetInfo(int templateID)

public void OnDrawGizmos()
{
if (!IsSpawned)
if (!CreatureCamera || !HasStateAuthority || !IsSpawned)
return;

Gizmos.color = Color.red;
Expand All @@ -73,13 +73,6 @@ protected override void HandleInput()
if (CreatureState == Define.CreatureState.Interact || CreatureState == Define.CreatureState.Use)
return;

if (CreatureState == Define.CreatureState.Interact)
{
if (Input.GetKeyDown(KeyCode.F))
CreatureState = Define.CreatureState.Idle;
return;
}

if (Input.GetKeyDown(KeyCode.F))
{
if (CheckInteractable(true))
Expand Down Expand Up @@ -122,14 +115,6 @@ protected override void HandleInput()
}
}

protected bool CheckAndUseSkill(int skillIdx)
{
if (!HasStateAuthority || !IsSpawned)
return false;

return SkillController.CheckAndUseSkill(skillIdx);
}

protected override void UpdateIdle()
{
switch (CreaturePose)
Expand Down Expand Up @@ -161,5 +146,13 @@ protected override void UpdateMove()
KCC.Move(Velocity * (AlienStat.Speed * Runner.DeltaTime), 0f);
}

protected bool CheckAndUseSkill(int skillIdx)
{
if (!HasStateAuthority || !IsSpawned)
return false;

return SkillController.CheckAndUseSkill(skillIdx);
}

#endregion
}
81 changes: 42 additions & 39 deletions Client/Assets/Scripts/Creatures/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override void Spawned()

protected virtual void Init()
{
Transform = gameObject.GetComponent<Transform>();
Transform = transform;
AudioSource = gameObject.GetComponent<AudioSource>();
Collider = gameObject.GetComponent<CapsuleCollider>();
RigidBody = gameObject.GetComponent<Rigidbody>();
Expand Down Expand Up @@ -134,66 +134,42 @@ public void ReturnToIdle(float time)
#region Update
private void Update()
{
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead || !IsSpawned)
return;

HandleInput();
OnUpdate();
}

private void LateUpdate()
{
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead || !IsSpawned)
if (!CreatureCamera || !HasStateAuthority || CreatureState == Define.CreatureState.Dead || !IsSpawned)
return;

CreatureCamera.UpdateCameraAngle();
}

protected virtual void OnUpdate()
{
if (!CreatureCamera || !HasStateAuthority || CreatureState == Define.CreatureState.Dead || !IsSpawned)
return;

HandleInput();
BaseSoundController.CheckChasing();
}

public override void FixedUpdateNetwork()
{
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead)
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead || !IsSpawned)
return;

UpdateByState();
UpdateAnimation();
ApplyAnimation();
}

protected virtual void HandleInput()
{
if (CreatureCamera == null)
return;

Quaternion cameraRotationY = Quaternion.Euler(0, CreatureCamera.transform.rotation.eulerAngles.y, 0);
Direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
Velocity = (cameraRotationY * Direction).normalized;
}

protected bool CheckInteractable(bool tryInteract)
{
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead || IngameUI == null || CreatureState == Define.CreatureState.Interact)
return false;

Ray ray = CreatureCamera.Camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, CreatureCamera.Camera.nearClipPlane));

Debug.DrawRay(ray.origin, ray.direction * 1.5f, Color.red);

if (Physics.Raycast(ray, out RaycastHit rayHit, maxDistance: 1.5f, layerMask: LayerMask.GetMask("MapObject")))
{
if (rayHit.transform.gameObject.TryGetComponent(out IInteractable interactable))
{
if (interactable.CheckInteractable(this) && tryInteract)
{
return interactable.Interact(this);
}

return false;
}
}

IngameUI.InteractInfoUI.Hide();
IngameUI.ErrorTextUI.Hide();
return false;
}

protected void UpdateByState()
{
switch (CreatureState)
Expand All @@ -211,7 +187,7 @@ protected void UpdateByState()

protected abstract void UpdateMove();

protected void UpdateAnimation()
protected void ApplyAnimation()
{
switch (CreatureState)
{
Expand All @@ -224,6 +200,33 @@ protected void UpdateAnimation()
}
}

protected bool CheckInteractable(bool tryInteract)
{
if (!HasStateAuthority || CreatureState == Define.CreatureState.Dead || IngameUI == null || CreatureState == Define.CreatureState.Interact)
return false;

Ray ray = CreatureCamera.Camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, CreatureCamera.Camera.nearClipPlane));

Debug.DrawRay(ray.origin, ray.direction * 1.5f, Color.red);

if (Physics.Raycast(ray, out RaycastHit rayHit, maxDistance: 1.5f, layerMask: LayerMask.GetMask("MapObject")))
{
if (rayHit.transform.gameObject.TryGetComponent(out IInteractable interactable))
{
if (interactable.CheckInteractable(this) && tryInteract)
{
return interactable.Interact(this);
}

return false;
}
}

IngameUI.InteractInfoUI.Hide();
IngameUI.ErrorTextUI.Hide();
return false;
}

#endregion

[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
Expand Down
Loading

0 comments on commit bcf3485

Please sign in to comment.