3
This commit is contained in:
52
unity/Assets/2.5D Engine/Scripts/EnemyAI.cs
Normal file
52
unity/Assets/2.5D Engine/Scripts/EnemyAI.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Controls enemy behavior: follows player and handles collision.
|
||||
public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
private Transform target; // Reference to the player
|
||||
[SerializeField] private float speed; // Movement speed
|
||||
[SerializeField] private int damageAmount; // Damage dealt to player
|
||||
[SerializeField] private SpriteRenderer spriteRenderer; // For flipping sprite
|
||||
|
||||
// Called on start
|
||||
public void Start()
|
||||
{
|
||||
Invoke("AssignPlayer", 1f); // Delay to ensure player exists
|
||||
}
|
||||
|
||||
// Finds and assigns the player as target
|
||||
public void AssignPlayer()
|
||||
{
|
||||
target = GameObject.FindGameObjectWithTag("Player").transform;
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
public void Update()
|
||||
{
|
||||
if (target)
|
||||
{
|
||||
// Flip sprite based on direction to player
|
||||
if (target.position.x > transform.position.x)
|
||||
spriteRenderer.flipX = false;
|
||||
else
|
||||
spriteRenderer.flipX = true;
|
||||
|
||||
// Move towards player
|
||||
transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
|
||||
}
|
||||
}
|
||||
|
||||
// Handles collision with player
|
||||
void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
if (collision.collider.CompareTag("Player"))
|
||||
{
|
||||
// Damage player and kill self
|
||||
collision.collider.GetComponent<HealthSystem>().Damage(damageAmount);
|
||||
GetComponent<HealthSystem>().Die();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/EnemyAI.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/EnemyAI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 04126beb49efb8c49a7ecc822750a0c7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
46
unity/Assets/2.5D Engine/Scripts/EnemySpawner.cs
Normal file
46
unity/Assets/2.5D Engine/Scripts/EnemySpawner.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using UnityEngine;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Spawns enemies at random positions and intervals
|
||||
public class EnemySpawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject enemyEntity; // Enemy prefab
|
||||
|
||||
[SerializeField]
|
||||
private Transform[] spawnPosition; // Possible spawn points
|
||||
|
||||
[SerializeField]
|
||||
private float spawnTime = 4f; // Time between spawns
|
||||
|
||||
// Starts repeated spawning
|
||||
private void Start()
|
||||
{
|
||||
InvokeRepeating("SpawnEnemies", 0f, spawnTime);
|
||||
}
|
||||
|
||||
// Spawns a random number of enemies at random positions
|
||||
public void SpawnEnemies()
|
||||
{
|
||||
int enemyNo = Random.Range(1, 4);
|
||||
|
||||
for (int i = 0; i < enemyNo; i++)
|
||||
{
|
||||
int randomSpawnPosition = Random.Range(0, spawnPosition.Length);
|
||||
|
||||
if (spawnPosition[randomSpawnPosition])
|
||||
{
|
||||
// Randomize spawn position slightly
|
||||
Vector3 randomUnitCircle = new Vector3(Random.Range(-1f, 1f), .5f, Random.Range(-1f, 1f));
|
||||
Instantiate(enemyEntity, spawnPosition[randomSpawnPosition].position + randomUnitCircle, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
|
||||
// Decrease spawn time to increase difficulty, but not below 2 seconds
|
||||
spawnTime -= .5f;
|
||||
|
||||
if (spawnTime <= 2f)
|
||||
spawnTime = 2f;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/EnemySpawner.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/EnemySpawner.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c755496b98cf3a743b854c203125bdff
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user