2
This commit is contained in:
50
unity/Assets/2.5D Engine/Scripts/HealthSystem.cs
Normal file
50
unity/Assets/2.5D Engine/Scripts/HealthSystem.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Handles health, damage, and death for entities
|
||||
public class HealthSystem : MonoBehaviour
|
||||
{
|
||||
[Range(1, 100)]
|
||||
[SerializeField]
|
||||
private int maxHealth = 100; // Maximum health
|
||||
private int health = 100; // Current health
|
||||
|
||||
[SerializeField]
|
||||
private GameObject deathEffect; // Effect prefab on death
|
||||
|
||||
[SerializeField]
|
||||
private bool isPlayer; // Is this the player?
|
||||
|
||||
// Initializes health
|
||||
private void Start()
|
||||
{
|
||||
health = maxHealth;
|
||||
}
|
||||
|
||||
// Applies damage and checks for death
|
||||
public void Damage(int damageAmount)
|
||||
{
|
||||
health -= damageAmount;
|
||||
|
||||
// Update UI if player
|
||||
if (isPlayer)
|
||||
HealthUI.Instance.UpdateHealthBar(maxHealth, health);
|
||||
|
||||
// If dead, reload scene if player, then die
|
||||
if (health <= 0)
|
||||
{
|
||||
if (isPlayer)
|
||||
HealthUI.Instance.ReloadScene();
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
// Handles death logic and effects
|
||||
public void Die()
|
||||
{
|
||||
Instantiate(deathEffect, transform.position + new Vector3(0f, .5f, 0f), Quaternion.identity);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/HealthSystem.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/HealthSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9e36f6d2e0ea0541bad29e784e0841b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
38
unity/Assets/2.5D Engine/Scripts/HealthUI.cs
Normal file
38
unity/Assets/2.5D Engine/Scripts/HealthUI.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Handles updating and displaying the health UI
|
||||
public class HealthUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image healthFill; // UI image for health bar
|
||||
public static HealthUI Instance; // Singleton instance
|
||||
|
||||
// Assigns singleton instance
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
// Updates the health bar fill based on health values
|
||||
public void UpdateHealthBar(float maxHealth, float currentHealth)
|
||||
{
|
||||
healthFill.fillAmount = currentHealth / maxHealth;
|
||||
}
|
||||
|
||||
// Starts coroutine to reload scene
|
||||
public void ReloadScene()
|
||||
{
|
||||
StartCoroutine(_ReloadScene());
|
||||
}
|
||||
|
||||
// Waits and reloads current scene
|
||||
IEnumerator _ReloadScene()
|
||||
{
|
||||
yield return new WaitForSeconds(1f);
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/HealthUI.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/HealthUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c8e10d31f74f214b9b18e7e378bbb30
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
38
unity/Assets/2.5D Engine/Scripts/Plantation.cs
Normal file
38
unity/Assets/2.5D Engine/Scripts/Plantation.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Handles plantation (plant) interactions and cutting logic
|
||||
public class Plantation : MonoBehaviour
|
||||
{
|
||||
public GameObject plantationCutParticles; // Particle effect prefab for cutting
|
||||
private Animator animator; // Animator for plant animations
|
||||
|
||||
// Triggered when another collider enters this object's trigger collider
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Player"))
|
||||
{
|
||||
// If animator already assigned, trigger a random animation
|
||||
if (animator)
|
||||
{
|
||||
animator.SetTrigger("Trigger" + Random.Range(1, 4));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Assign animator and trigger a random animation
|
||||
animator = GetComponent<Animator>();
|
||||
animator.SetTrigger("Trigger" + Random.Range(1, 4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Called to cut the plant, spawn particles, and destroy the object
|
||||
public void Cut()
|
||||
{
|
||||
// Instantiate cut particles and set their sprite to match the plant
|
||||
Instantiate(plantationCutParticles, transform.position + new Vector3(0f, 0.5f, 0f), Quaternion.identity)
|
||||
.GetComponent<ParticleSystem>().textureSheetAnimation.SetSprite(0, GetComponent<SpriteRenderer>().sprite);
|
||||
Destroy(gameObject); // Remove plant from scene
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/Plantation.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/Plantation.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53d1d05b715330a4ca6ca79f6457e809
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
104
unity/Assets/2.5D Engine/Scripts/Player.cs
Normal file
104
unity/Assets/2.5D Engine/Scripts/Player.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Ensures the GameObject has these essential components
|
||||
[RequireComponent(typeof(HealthSystem), typeof(SwordAttack), typeof(ProjectileShooter))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float moveSpeed = 5f; // Speed at which the player moves
|
||||
[SerializeField] private float rollForce = 8f; // Force applied during roll
|
||||
[SerializeField] private float rollCooldown = 1f; // Cooldown time between rolls
|
||||
|
||||
private Rigidbody rb; // Rigidbody reference for physics
|
||||
private Animator animator; // Animator reference for animations
|
||||
private Vector2 inputDirection; // Stores player input direction
|
||||
private float lastRollTime; // Timestamp of last roll
|
||||
private bool isRolling; // Is player currently rolling?
|
||||
|
||||
[SerializeField] private KeyCode rollKeyCode; // Key to trigger roll
|
||||
|
||||
// Enum to switch between sword or projectile attack types
|
||||
[SerializeField] private enum AttackType
|
||||
{
|
||||
SwordSlash, ProjectileShoot
|
||||
}
|
||||
|
||||
[SerializeField] private AttackType attackType; // Current selected attack type
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Application.targetFrameRate = 60; // Set target frame rate
|
||||
|
||||
rb = GetComponent<Rigidbody>();
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
// Enable only the selected attack script
|
||||
if (attackType == AttackType.SwordSlash)
|
||||
GetComponent<SwordAttack>().enabled = true;
|
||||
else
|
||||
GetComponent<ProjectileShooter>().enabled = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleInput(); // Read movement input
|
||||
|
||||
// Animate movement only when not rolling
|
||||
if (!isRolling)
|
||||
AnimateMovement();
|
||||
|
||||
// Trigger roll if key is pressed and cooldown passed
|
||||
if (Input.GetKeyDown(rollKeyCode) && Time.time > lastRollTime + rollCooldown)
|
||||
StartCoroutine(PerformRoll());
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Move only if not rolling
|
||||
if (!isRolling)
|
||||
Move();
|
||||
}
|
||||
|
||||
// Reads directional input from keyboard
|
||||
private void HandleInput()
|
||||
{
|
||||
inputDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
|
||||
}
|
||||
|
||||
// Applies movement to the Rigidbody based on input
|
||||
private void Move()
|
||||
{
|
||||
Vector3 movement = new Vector3(inputDirection.x, 0f, inputDirection.y) * moveSpeed * Time.fixedDeltaTime;
|
||||
rb.MovePosition(rb.position + movement);
|
||||
}
|
||||
|
||||
// Sets the "Run" animation based on movement input
|
||||
private void AnimateMovement()
|
||||
{
|
||||
animator.SetBool("Run", inputDirection != Vector2.zero);
|
||||
}
|
||||
|
||||
// Coroutine to perform roll movement and animation
|
||||
private IEnumerator PerformRoll()
|
||||
{
|
||||
isRolling = true;
|
||||
lastRollTime = Time.time;
|
||||
|
||||
float timer = 0.2f;
|
||||
Vector3 rollDir = new Vector3(inputDirection.x, 0f, inputDirection.y).normalized;
|
||||
|
||||
// Move the player during the roll duration
|
||||
while (timer > 0f)
|
||||
{
|
||||
rb.MovePosition(rb.position + rollDir * rollForce * Time.fixedDeltaTime);
|
||||
timer -= Time.fixedDeltaTime;
|
||||
yield return new WaitForFixedUpdate();
|
||||
}
|
||||
|
||||
isRolling = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/Player.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/Player.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc58f41e91ed8614f85e78cafee33058
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
64
unity/Assets/2.5D Engine/Scripts/Projectile.cs
Normal file
64
unity/Assets/2.5D Engine/Scripts/Projectile.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Handles projectile movement, explosion, and effects
|
||||
public class Projectile : MonoBehaviour
|
||||
{
|
||||
public float speed = 10f; // Projectile speed
|
||||
public GameObject explosionEffect; // Effect prefab
|
||||
private Vector3 target; // Target position
|
||||
|
||||
[Range(.2f, 3f)]
|
||||
public float radius = 1f; // Explosion radius
|
||||
public LayerMask whatIsEnemy; // Layer for enemies
|
||||
public LayerMask whatIsPlant; // Layer for plants
|
||||
|
||||
// Sets the target point and rotates projectile to face it
|
||||
public void SetTarget(Vector3 point)
|
||||
{
|
||||
target = point;
|
||||
transform.LookAt(new Vector3(point.x, transform.position.y, point.z)); // Optional: face target
|
||||
}
|
||||
|
||||
// Moves projectile towards target each frame
|
||||
void Update()
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
|
||||
|
||||
// Explode if close to target
|
||||
if (Vector3.Distance(transform.position, target) < 0.1f)
|
||||
{
|
||||
Explode();
|
||||
}
|
||||
}
|
||||
|
||||
// Handles explosion logic and effects
|
||||
void Explode()
|
||||
{
|
||||
Instantiate(explosionEffect, transform.position, Quaternion.identity);
|
||||
|
||||
// Damage enemies in radius
|
||||
Collider[] enemyColliders = Physics.OverlapSphere(transform.position, radius, whatIsEnemy);
|
||||
|
||||
if (enemyColliders != null)
|
||||
{
|
||||
foreach (Collider C in enemyColliders)
|
||||
{
|
||||
C.GetComponent<HealthSystem>().Die();
|
||||
}
|
||||
}
|
||||
|
||||
// Affect plants in radius
|
||||
Collider[] plantationColliders = Physics.OverlapSphere(transform.position, radius, whatIsPlant);
|
||||
|
||||
if (plantationColliders != null)
|
||||
{
|
||||
foreach (Collider C in plantationColliders)
|
||||
{
|
||||
C.GetComponent<Plantation>().Cut();
|
||||
}
|
||||
}
|
||||
Destroy(gameObject); // Destroy projectile
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/Projectile.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/Projectile.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c83f4497aa24424aa493db527fe0139
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
38
unity/Assets/2.5D Engine/Scripts/ProjectileShooter.cs
Normal file
38
unity/Assets/2.5D Engine/Scripts/ProjectileShooter.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using UnityEngine;
|
||||
namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
// Handles shooting projectiles towards mouse click position
|
||||
public class ProjectileShooter : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject projectilePrefab; // Prefab for the projectile to shoot
|
||||
[SerializeField] private KeyCode attackKeyCode; // Key to trigger shooting
|
||||
[SerializeField] private LayerMask groundLayer; // Layer to detect ground for aiming
|
||||
|
||||
// Called once per frame
|
||||
void Update()
|
||||
{
|
||||
// Check if attack key is pressed
|
||||
if (Input.GetKeyDown(attackKeyCode)) // Left-click
|
||||
{
|
||||
// Create a ray from the camera to the mouse position
|
||||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
|
||||
// Raycast to ground layer to find target point
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, groundLayer))
|
||||
{
|
||||
Vector3 targetPoint = hit.point;
|
||||
// Instantiate projectile at shooter's position
|
||||
GameObject projectile = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
|
||||
// Set projectile's target point
|
||||
projectile.GetComponent<Projectile>().SetTarget(targetPoint);
|
||||
|
||||
// Flip shooter sprite based on target direction
|
||||
if (hit.point.x > transform.position.x)
|
||||
transform.localScale = new Vector3(1, 1, 1);
|
||||
else
|
||||
transform.localScale = new Vector3(-1, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
unity/Assets/2.5D Engine/Scripts/ProjectileShooter.cs.meta
Normal file
11
unity/Assets/2.5D Engine/Scripts/ProjectileShooter.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 295e53bd22a52084cb9fcba3240e3739
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user