Skip to content
Snippets Groups Projects
Commit cad87ac9 authored by Salomo309's avatar Salomo309
Browse files

refactor: done, tinggal hubung ke weapon

parent 792aee98
Branches
Tags
No related merge requests found
...@@ -4,177 +4,180 @@ using System.Collections.Generic; ...@@ -4,177 +4,180 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public class JenderalMovement : PausibleObject namespace Nightmare
{ {
public class JenderalMovement : PausibleObject
public float visionRange = 10f;
public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
float currentVision;
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
NavMeshAgent nav;
public float timer = 0f;
void Awake()
{ {
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
StartPausible(); public float visionRange = 10f;
} public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
void OnEnable() float currentVision;
{ Transform player;
nav.enabled = true; PlayerHealth playerHealth;
ClearPath(); EnemyHealth enemyHealth;
ScaleVision(1f); NavMeshAgent nav;
IsPsychic(); public float timer = 0f;
timer = 0f;
}
void ClearPath() void Awake()
{ {
if (nav.hasPath) player = GameObject.FindGameObjectWithTag("Player").transform;
nav.ResetPath(); playerHealth = player.GetComponent<PlayerHealth>();
} enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
void Update() StartPausible();
{ }
if (!isPaused)
void OnEnable()
{ {
// If both the enemy and the player have health left... nav.enabled = true;
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0) ClearPath();
{ ScaleVision(1f);
LookForPlayer(); IsPsychic();
WanderOrIdle(); timer = 0f;
} }
else
void ClearPath()
{
if (nav.hasPath)
nav.ResetPath();
}
void Update()
{
if (!isPaused)
{ {
nav.enabled = false; // If both the enemy and the player have health left...
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0)
{
LookForPlayer();
WanderOrIdle();
}
else
{
nav.enabled = false;
}
} }
} }
}
void OnDestroy() void OnDestroy()
{ {
nav.enabled = false; nav.enabled = false;
StopPausible(); StopPausible();
} }
public override void OnPause() public override void OnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = true; nav.isStopped = true;
} }
public override void OnUnPause() public override void OnUnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = false; nav.isStopped = false;
} }
private void LookForPlayer() private void LookForPlayer()
{ {
TestSense(player.position, currentVision); TestSense(player.position, currentVision);
} }
private void HearPoint(Vector3 position) private void HearPoint(Vector3 position)
{ {
TestSense(position, hearingRange); TestSense(position, hearingRange);
} }
private void TestSense(Vector3 position, float senseRange) private void TestSense(Vector3 position, float senseRange)
{
if (Vector3.Distance(this.transform.position, position) <= senseRange)
{ {
GoToPosition(position); if (Vector3.Distance(this.transform.position, position) <= senseRange)
{
GoToPosition(position);
}
} }
}
public void GoToPlayer() public void GoToPlayer()
{ {
GoToPosition(player.position); GoToPosition(player.position);
} }
private void GoToPosition(Vector3 position) private void GoToPosition(Vector3 position)
{
timer = -1f;
if (!enemyHealth.IsDead())
{ {
SetDestination(position); timer = -1f;
if (!enemyHealth.IsDead())
{
SetDestination(position);
}
} }
}
private void SetDestination(Vector3 position) private void SetDestination(Vector3 position)
{
if (nav.isOnNavMesh)
{ {
nav.SetDestination(position); if (nav.isOnNavMesh)
{
nav.SetDestination(position);
}
} }
}
private void WanderOrIdle() private void WanderOrIdle()
{
if (!nav.hasPath)
{ {
if (timer <= 0f) if (!nav.hasPath)
{ {
SetDestination(GetRandomPoint(wanderDistance, 5)); if (timer <= 0f)
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{ {
ClearPath(); SetDestination(GetRandomPoint(wanderDistance, 5));
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{
ClearPath();
}
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
} }
}
private void IsPsychic() private void IsPsychic()
{ {
GoToPlayer(); GoToPlayer();
} }
private Vector3 GetRandomPoint(float distance, int layermask) private Vector3 GetRandomPoint(float distance, int layermask)
{ {
Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ; Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ;
NavMeshHit navHit; NavMeshHit navHit;
NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask); NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask);
return navHit.position; return navHit.position;
} }
public void ScaleVision(float scale) public void ScaleVision(float scale)
{ {
currentVision = visionRange * scale; currentVision = visionRange * scale;
} }
private int GetCurrentNavArea() private int GetCurrentNavArea()
{ {
NavMeshHit navHit; NavMeshHit navHit;
nav.SamplePathPosition(-1, 0.0f, out navHit); nav.SamplePathPosition(-1, 0.0f, out navHit);
return navHit.mask; return navHit.mask;
} }
//void OnDrawGizmos() //void OnDrawGizmos()
//{ //{
// Vector3 position = this.transform.position; // Vector3 position = this.transform.position;
// Gizmos.color = Color.red; // Gizmos.color = Color.red;
// Gizmos.DrawWireSphere(position, currentVision); // Gizmos.DrawWireSphere(position, currentVision);
// Gizmos.color = Color.yellow; // Gizmos.color = Color.yellow;
// Gizmos.DrawWireSphere(position, hearingRange); // Gizmos.DrawWireSphere(position, hearingRange);
//} //}
}
} }
\ No newline at end of file
...@@ -5,8 +5,8 @@ public class KepalaKerocoAttack : PausibleObject ...@@ -5,8 +5,8 @@ public class KepalaKerocoAttack : PausibleObject
{ {
public float timeBetweenAttacks = 0.5f; public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10; public int attackDamage = 10;
public GameObject kerocoPrefab; // Prefab untuk Keroco yang akan dikeluarkan oleh Kepala Keroco public GameObject kerocoPrefab;
public Transform spawnPoint; // Titik spawn untuk Keroco public Transform spawnPoint;
private Animator anim; private Animator anim;
private GameObject player; private GameObject player;
...@@ -97,7 +97,7 @@ public class KepalaKerocoAttack : PausibleObject ...@@ -97,7 +97,7 @@ public class KepalaKerocoAttack : PausibleObject
} }
void SpawnKeroco() void SpawnKeroco()
{ {
if (kerocoPrefab != null && spawnPoint != null) if (kerocoPrefab != null && spawnPoint != null)
{ {
Instantiate(kerocoPrefab, spawnPoint.position, spawnPoint.rotation); Instantiate(kerocoPrefab, spawnPoint.position, spawnPoint.rotation);
......
...@@ -4,177 +4,180 @@ using System.Collections.Generic; ...@@ -4,177 +4,180 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public class KepalaKeroco : PausibleObject namespace Nightmare
{ {
public class KepalaKeroco : PausibleObject
public float visionRange = 10f;
public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
float currentVision;
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
NavMeshAgent nav;
public float timer = 0f;
void Awake()
{ {
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
StartPausible(); public float visionRange = 10f;
} public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
void OnEnable() float currentVision;
{ Transform player;
nav.enabled = true; PlayerHealth playerHealth;
ClearPath(); EnemyHealth enemyHealth;
ScaleVision(1f); NavMeshAgent nav;
IsPsychic(); public float timer = 0f;
timer = 0f;
}
void ClearPath() void Awake()
{ {
if (nav.hasPath) player = GameObject.FindGameObjectWithTag("Player").transform;
nav.ResetPath(); playerHealth = player.GetComponent<PlayerHealth>();
} enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
void Update() StartPausible();
{ }
if (!isPaused)
void OnEnable()
{ {
// If both the enemy and the player have health left... nav.enabled = true;
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0) ClearPath();
{ ScaleVision(1f);
LookForPlayer(); IsPsychic();
WanderOrIdle(); timer = 0f;
} }
else
void ClearPath()
{
if (nav.hasPath)
nav.ResetPath();
}
void Update()
{
if (!isPaused)
{ {
nav.enabled = false; // If both the enemy and the player have health left...
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0)
{
LookForPlayer();
WanderOrIdle();
}
else
{
nav.enabled = false;
}
} }
} }
}
void OnDestroy() void OnDestroy()
{ {
nav.enabled = false; nav.enabled = false;
StopPausible(); StopPausible();
} }
public override void OnPause() public override void OnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = true; nav.isStopped = true;
} }
public override void OnUnPause() public override void OnUnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = false; nav.isStopped = false;
} }
private void LookForPlayer() private void LookForPlayer()
{ {
TestSense(player.position, currentVision); TestSense(player.position, currentVision);
} }
private void HearPoint(Vector3 position) private void HearPoint(Vector3 position)
{ {
TestSense(position, hearingRange); TestSense(position, hearingRange);
} }
private void TestSense(Vector3 position, float senseRange) private void TestSense(Vector3 position, float senseRange)
{
if (Vector3.Distance(this.transform.position, position) <= senseRange)
{ {
GoToPosition(position); if (Vector3.Distance(this.transform.position, position) <= senseRange)
{
GoToPosition(position);
}
} }
}
public void GoToPlayer() public void GoToPlayer()
{ {
GoToPosition(player.position); GoToPosition(player.position);
} }
private void GoToPosition(Vector3 position) private void GoToPosition(Vector3 position)
{
timer = -1f;
if (!enemyHealth.IsDead())
{ {
SetDestination(position); timer = -1f;
if (!enemyHealth.IsDead())
{
SetDestination(position);
}
} }
}
private void SetDestination(Vector3 position) private void SetDestination(Vector3 position)
{
if (nav.isOnNavMesh)
{ {
nav.SetDestination(position); if (nav.isOnNavMesh)
{
nav.SetDestination(position);
}
} }
}
private void WanderOrIdle() private void WanderOrIdle()
{
if (!nav.hasPath)
{ {
if (timer <= 0f) if (!nav.hasPath)
{ {
SetDestination(GetRandomPoint(wanderDistance, 5)); if (timer <= 0f)
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{ {
ClearPath(); SetDestination(GetRandomPoint(wanderDistance, 5));
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{
ClearPath();
}
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
} }
}
private void IsPsychic() private void IsPsychic()
{ {
GoToPlayer(); GoToPlayer();
} }
private Vector3 GetRandomPoint(float distance, int layermask) private Vector3 GetRandomPoint(float distance, int layermask)
{ {
Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ; Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ;
NavMeshHit navHit; NavMeshHit navHit;
NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask); NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask);
return navHit.position; return navHit.position;
} }
public void ScaleVision(float scale) public void ScaleVision(float scale)
{ {
currentVision = visionRange * scale; currentVision = visionRange * scale;
} }
private int GetCurrentNavArea() private int GetCurrentNavArea()
{ {
NavMeshHit navHit; NavMeshHit navHit;
nav.SamplePathPosition(-1, 0.0f, out navHit); nav.SamplePathPosition(-1, 0.0f, out navHit);
return navHit.mask; return navHit.mask;
} }
//void OnDrawGizmos() //void OnDrawGizmos()
//{ //{
// Vector3 position = this.transform.position; // Vector3 position = this.transform.position;
// Gizmos.color = Color.red; // Gizmos.color = Color.red;
// Gizmos.DrawWireSphere(position, currentVision); // Gizmos.DrawWireSphere(position, currentVision);
// Gizmos.color = Color.yellow; // Gizmos.color = Color.yellow;
// Gizmos.DrawWireSphere(position, hearingRange); // Gizmos.DrawWireSphere(position, hearingRange);
//} //}
} }
}
\ No newline at end of file
...@@ -12,11 +12,11 @@ public class RajaAttack : PausibleObject ...@@ -12,11 +12,11 @@ public class RajaAttack : PausibleObject
public float playerDamageRange = 10f; public float playerDamageRange = 10f;
public float playerSlowdownFactor = 0.5f; public float playerSlowdownFactor = 0.5f;
public int playerDamagePerSecond = 5; public int playerDamagePerSecond = 5;
public float kerocoSpawnTimer; private float kerocoSpawnTimer;
private int petIncreaserCount = 0; private int petIncreaserCount = 0;
public GameObject kerocoPrefab; public GameObject kerocoPrefab;
public Transform[] spawnPoints; public Transform spawnPoint;
Animator anim; Animator anim;
GameObject player; GameObject player;
...@@ -112,8 +112,16 @@ public class RajaAttack : PausibleObject ...@@ -112,8 +112,16 @@ public class RajaAttack : PausibleObject
void SlowdownPlayerMovement() void SlowdownPlayerMovement()
{ {
// Perlambat gerakan pemain dengan faktor tertentu PlayerMovement playerMovement = player.GetComponent<PlayerMovement>();
player.GetComponent<NavMeshAgent>().speed *= playerSlowdownFactor;
if (playerMovement != null)
{
playerMovement.speed *= playerSlowdownFactor;
}
else
{
Debug.LogError("PlayerMovement component not found!");
}
} }
void InflictPlayerDamage() void InflictPlayerDamage()
...@@ -124,10 +132,8 @@ public class RajaAttack : PausibleObject ...@@ -124,10 +132,8 @@ public class RajaAttack : PausibleObject
void SpawnKeroco() void SpawnKeroco()
{ {
if (kerocoPrefab != null && spawnPoints != null && spawnPoints.Length > 0) if (kerocoPrefab != null && spawnPoint != null)
{ {
// Pilih secara acak salah satu titik spawn
Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(kerocoPrefab, spawnPoint.position, spawnPoint.rotation); Instantiate(kerocoPrefab, spawnPoint.position, spawnPoint.rotation);
} }
else else
......
...@@ -4,177 +4,180 @@ using System.Collections.Generic; ...@@ -4,177 +4,180 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
public class RajaMovement : PausibleObject namespace Nightmare
{ {
public class RajaMovement : PausibleObject
public float visionRange = 10f;
public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
float currentVision;
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
NavMeshAgent nav;
public float timer = 0f;
void Awake()
{ {
player = GameObject.FindGameObjectWithTag("Player").transform;
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
StartPausible(); public float visionRange = 10f;
} public float hearingRange = 20f;
public float wanderDistance = 10f;
public Vector2 idleTimeRange;
[Range(0f, 1f)]
public float psychicLevels = 0.2f;
void OnEnable() float currentVision;
{ Transform player;
nav.enabled = true; PlayerHealth playerHealth;
ClearPath(); EnemyHealth enemyHealth;
ScaleVision(1f); NavMeshAgent nav;
IsPsychic(); public float timer = 0f;
timer = 0f;
}
void ClearPath() void Awake()
{ {
if (nav.hasPath) player = GameObject.FindGameObjectWithTag("Player").transform;
nav.ResetPath(); playerHealth = player.GetComponent<PlayerHealth>();
} enemyHealth = GetComponent<EnemyHealth>();
nav = GetComponent<NavMeshAgent>();
void Update() StartPausible();
{ }
if (!isPaused)
void OnEnable()
{ {
// If both the enemy and the player have health left... nav.enabled = true;
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0) ClearPath();
{ ScaleVision(1f);
LookForPlayer(); IsPsychic();
WanderOrIdle(); timer = 0f;
} }
else
void ClearPath()
{
if (nav.hasPath)
nav.ResetPath();
}
void Update()
{
if (!isPaused)
{ {
nav.enabled = false; // If both the enemy and the player have health left...
if (enemyHealth.CurrentHealth() > 0 && playerHealth.currentHealth > 0)
{
LookForPlayer();
WanderOrIdle();
}
else
{
nav.enabled = false;
}
} }
} }
}
void OnDestroy() void OnDestroy()
{ {
nav.enabled = false; nav.enabled = false;
StopPausible(); StopPausible();
} }
public override void OnPause() public override void OnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = true; nav.isStopped = true;
} }
public override void OnUnPause() public override void OnUnPause()
{ {
if (nav.hasPath) if (nav.hasPath)
nav.isStopped = false; nav.isStopped = false;
} }
private void LookForPlayer() private void LookForPlayer()
{ {
TestSense(player.position, currentVision); TestSense(player.position, currentVision);
} }
private void HearPoint(Vector3 position) private void HearPoint(Vector3 position)
{ {
TestSense(position, hearingRange); TestSense(position, hearingRange);
} }
private void TestSense(Vector3 position, float senseRange) private void TestSense(Vector3 position, float senseRange)
{
if (Vector3.Distance(this.transform.position, position) <= senseRange)
{ {
GoToPosition(position); if (Vector3.Distance(this.transform.position, position) <= senseRange)
{
GoToPosition(position);
}
} }
}
public void GoToPlayer() public void GoToPlayer()
{ {
GoToPosition(player.position); GoToPosition(player.position);
} }
private void GoToPosition(Vector3 position) private void GoToPosition(Vector3 position)
{
timer = -1f;
if (!enemyHealth.IsDead())
{ {
SetDestination(position); timer = -1f;
if (!enemyHealth.IsDead())
{
SetDestination(position);
}
} }
}
private void SetDestination(Vector3 position) private void SetDestination(Vector3 position)
{
if (nav.isOnNavMesh)
{ {
nav.SetDestination(position); if (nav.isOnNavMesh)
{
nav.SetDestination(position);
}
} }
}
private void WanderOrIdle() private void WanderOrIdle()
{
if (!nav.hasPath)
{ {
if (timer <= 0f) if (!nav.hasPath)
{ {
SetDestination(GetRandomPoint(wanderDistance, 5)); if (timer <= 0f)
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{ {
ClearPath(); SetDestination(GetRandomPoint(wanderDistance, 5));
if (nav.pathStatus == NavMeshPathStatus.PathInvalid)
{
ClearPath();
}
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
timer = Random.Range(idleTimeRange.x, idleTimeRange.y);
}
else
{
timer -= Time.deltaTime;
} }
} }
}
private void IsPsychic() private void IsPsychic()
{ {
GoToPlayer(); GoToPlayer();
} }
private Vector3 GetRandomPoint(float distance, int layermask) private Vector3 GetRandomPoint(float distance, int layermask)
{ {
Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ; Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ;
NavMeshHit navHit; NavMeshHit navHit;
NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask); NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask);
return navHit.position; return navHit.position;
} }
public void ScaleVision(float scale) public void ScaleVision(float scale)
{ {
currentVision = visionRange * scale; currentVision = visionRange * scale;
} }
private int GetCurrentNavArea() private int GetCurrentNavArea()
{ {
NavMeshHit navHit; NavMeshHit navHit;
nav.SamplePathPosition(-1, 0.0f, out navHit); nav.SamplePathPosition(-1, 0.0f, out navHit);
return navHit.mask; return navHit.mask;
} }
//void OnDrawGizmos() //void OnDrawGizmos()
//{ //{
// Vector3 position = this.transform.position; // Vector3 position = this.transform.position;
// Gizmos.color = Color.red; // Gizmos.color = Color.red;
// Gizmos.DrawWireSphere(position, currentVision); // Gizmos.DrawWireSphere(position, currentVision);
// Gizmos.color = Color.yellow; // Gizmos.color = Color.yellow;
// Gizmos.DrawWireSphere(position, hearingRange); // Gizmos.DrawWireSphere(position, hearingRange);
//} //}
} }
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment