Upgraded to 6.0000000

Added enemies, health system, and a camera follow system.
This commit is contained in:
2025-02-26 11:53:44 -08:00
parent 06ac6e1588
commit ae58ce8b70
19 changed files with 1061 additions and 145 deletions
+49
View File
@@ -0,0 +1,49 @@
using UnityEngine;
public class FollowBox : MonoBehaviour
{
public Transform target;
public Rect box;
private Vector3 _position;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if (target == null)
{
return;
}
_position = transform.position;
if (_position.x + box.x > target.position.x)
{
//Debug.Log($"position {_position}\nBox eval {_position.x + box.x}\nTarget position {target.position}");
_position.x = target.position.x + box.x + box.width;
}
if (_position.x + (box.x + box.width) < target.position.x)
{
_position.x = target.position.x - (box.x + box.width);
}
if (_position.y + box.y > target.position.y)
{
_position.y = target.position.y + box.y + box.height;
}
if (_position.y + (box.y + box.height) < target.position.y)
{
_position.y = target.position.y - (box.y + box.height);
}
transform.position = _position;
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f26d2652963384d78a5a5c295ffea5fd
+30
View File
@@ -0,0 +1,30 @@
using UnityEngine;
public class Health : MonoBehaviour
{
public float maxHealth;
public System.Action OnDeath;
private float _currentHealth;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
_currentHealth = maxHealth;
}
public void DealDamage(float damage)
{
_currentHealth -= damage;
if (_currentHealth <= 0)
{
Debug.Log($"{gameObject.name} is dead!");
OnDeath();
}
}
public void HealHealth(float health)
{
_currentHealth = Mathf.Min(_currentHealth + health, maxHealth);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5f0a43b17e365425092c1fc543d7cbfe
+99
View File
@@ -0,0 +1,99 @@
using UnityEngine;
[RequireComponent(typeof(Health))]
public class MeleeEnemy : MonoBehaviour
{
enum State
{
Wandering = 0,
Seeking = 1,
Dead = 2,
};
//private State _currentState;
private System.Action updateFunc;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
//_currentState = State::Wandering;
updateFunc = DoWander;
var h = GetComponent<Health>();
h.OnDeath += ProcessDeath;
}
#region Wander
[Header("Wander")]
public float dirChangeDuration = 2;
public float wanderSpeed;
private Vector3 wanderDir;
private float _lastDirChange = -2;
private void DoWander()
{
if (_lastDirChange + dirChangeDuration < Time.time)
{
wanderDir.x = Random.Range(-1f, 1f);
wanderDir.y = Random.Range(-1f, 1f);
_lastDirChange = Time.time;
//Debug.Log($"New wander dir: {wanderDir}");
}
transform.position += wanderDir * wanderSpeed * Time.deltaTime;
}
#endregion Wander
#region Seek
[Header("Seek")]
public float seekSpeed = 2;
private Transform _target;
private void DoSeek()
{
var dir = (_target.position - transform.position).normalized;
transform.position += dir * seekSpeed * Time.deltaTime;
}
#endregion Seek
private void DoSleep()
{
}
// Update is called once per frame
private void Update()
{
updateFunc();
}
private void ProcessDeath()
{
// TODO: effects
Destroy(gameObject);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
//Debug.Log("Player is in our sphere");
_target = other.transform;
updateFunc = DoSeek;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.collider.CompareTag("Player"))
{
Debug.Log("We have hit the player!");
var health = other.gameObject.GetComponent<Health>();
if (health != null)
{
updateFunc = DoSleep;
Destroy(gameObject);
health.DealDamage(5);
}
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 36753dfc46d484df186dc8cc4b61b712
+21 -5
View File
@@ -2,13 +2,14 @@ using System;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Health))]
public class Player : MonoBehaviour
{
public InputAction movement;
public InputAction mousePosition;
public InputAction fireAction;
public float speed = 10;
public Camera mainCamera;
public float speed = 10;
public float fireRate;
public GameObject projectileGo;
public Transform projectileSpawn;
@@ -22,17 +23,27 @@ public class Player : MonoBehaviour
private Action _aimingFunction;
private void Start()
{
_transform = transform;
var h = GetComponent<Health>();
h.OnDeath += ProcessDeath;
}
private void OnEnable()
{
movement.Enable();
// TODO: if no controller detected, enable mouse.
mousePosition.Enable();
_aimingFunction = MouseAim;
// TODO: Aim stick
fireAction.Enable();
fireAction.started += StartFire;
fireAction.canceled += EndFire;
_transform = transform;
}
private void OnDisable()
{
movement.Disable();
mousePosition.Disable();
}
private void StartFire(InputAction.CallbackContext cbContext)
@@ -62,7 +73,6 @@ public class Player : MonoBehaviour
angle *= -1;
}
//_transform.eulerAngles = Vector3.forward * angle;
barrelTransform.eulerAngles = Vector3.forward * angle;
}
@@ -92,4 +102,10 @@ public class Player : MonoBehaviour
Instantiate(projectileGo, projectileSpawn.position, projectileSpawn.rotation);
}
}
private void ProcessDeath()
{
// TODO: effects, gameover screen, etc.
Destroy(gameObject);
}
}
+19
View File
@@ -5,18 +5,37 @@ public class Projectile : MonoBehaviour
{
private Transform _transform;
private Rigidbody2D _rigidbody;
private float _startTime;
public float speed = 7;
public float lifeTime = 5;
public float damage = 2;
private void Start()
{
_transform = transform;
_rigidbody = GetComponent<Rigidbody2D>();
_rigidbody.gravityScale = 0;
_startTime = Time.time;
}
private void Update()
{
_transform.position += _transform.up * (speed * Time.deltaTime);
if (_startTime + lifeTime < Time.time)
{
Destroy(gameObject);
}
}
// TODO: ignore owner
private void OnCollisionEnter2D(Collision2D collision)
{
var health = collision.gameObject.GetComponent<Health>();
if (health != null)
{
health.DealDamage(damage);
}
}
}