maint: added livewatch asset
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ingvar.LiveWatch.TowerDefenceDemo
|
||||
{
|
||||
public class MobHealth : MonoBehaviour
|
||||
{
|
||||
public Action HealthChanged;
|
||||
|
||||
public int CurrentHealth
|
||||
{
|
||||
get => _currentHealth;
|
||||
set
|
||||
{
|
||||
if (_currentHealth == value)
|
||||
return;
|
||||
|
||||
_currentHealth = value;
|
||||
HealthChanged?.Invoke();
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
_deathCallback?.Invoke();
|
||||
}
|
||||
}
|
||||
public int MaxHealth => maxHealth;
|
||||
|
||||
[SerializeField] private int maxHealth;
|
||||
private int _currentHealth;
|
||||
private Action _deathCallback;
|
||||
|
||||
public void ResetHealth()
|
||||
{
|
||||
CurrentHealth = MaxHealth;
|
||||
}
|
||||
|
||||
public void TakeDamage(int damage)
|
||||
{
|
||||
CurrentHealth = Mathf.Clamp(CurrentHealth - damage, 0, MaxHealth);
|
||||
}
|
||||
|
||||
public void SetOnDeath(Action deathCallback)
|
||||
{
|
||||
_deathCallback = deathCallback;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc0bc970ef50473ab4dd1d71ec6d3213
|
||||
timeCreated: 1725985325
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 324001
|
||||
packageName: LiveWatch Lite | Debug with full history of changes
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/LiveWatchLite/TowerDefenceDemo/Scripts/Mob/MobHealth.cs
|
||||
uploadId: 770587
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ingvar.LiveWatch.TowerDefenceDemo
|
||||
{
|
||||
public class MobHealthBar : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private MobMain mob;
|
||||
[SerializeField] private Transform bar;
|
||||
|
||||
private Camera _mainCamera;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_mainCamera = Camera.main;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
mob.Health.HealthChanged += RefreshBar;
|
||||
RefreshBar();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
mob.Health.HealthChanged -= RefreshBar;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
transform.rotation = _mainCamera.transform.rotation;
|
||||
}
|
||||
|
||||
private void RefreshBar()
|
||||
{
|
||||
var progress = (float)mob.Health.CurrentHealth / mob.Health.MaxHealth;
|
||||
bar.localScale = new Vector3(progress, bar.localScale.y, bar.localScale.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8c4923e295d4d9986ac6214a28db302
|
||||
timeCreated: 1725985707
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 324001
|
||||
packageName: LiveWatch Lite | Debug with full history of changes
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/LiveWatchLite/TowerDefenceDemo/Scripts/Mob/MobHealthBar.cs
|
||||
uploadId: 770587
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ingvar.LiveWatch.TowerDefenceDemo
|
||||
{
|
||||
public class MobMain : MonoBehaviour
|
||||
{
|
||||
public string Id { get; private set; }
|
||||
public MobType Type => _type;
|
||||
public bool IsAlive { get; private set; }
|
||||
public int CurrentHealth => Health.CurrentHealth;
|
||||
public float SpawnTime { get; private set; }
|
||||
public MobWaypointMover WaypointMover => _waypointMover;
|
||||
public MobHealth Health => _health;
|
||||
|
||||
[SerializeField] private MobType _type;
|
||||
[SerializeField] private MobWaypointMover _waypointMover;
|
||||
[SerializeField] private MobHealth _health;
|
||||
[SerializeField] private GameObject _deathFxPrefab;
|
||||
|
||||
private LevelScene _levelScene;
|
||||
|
||||
public void Spawn(string id, LevelScene level)
|
||||
{
|
||||
Id = id;
|
||||
IsAlive = true;
|
||||
SpawnTime = Time.time;
|
||||
|
||||
_levelScene = level;
|
||||
_waypointMover.StartMoving(level.Waypoints);
|
||||
_health.ResetHealth();
|
||||
|
||||
var mobWatch = Watch.GetOrAdd<Any>("Mobs").GetOrAdd<Any>(Id);
|
||||
Watch.Push(mobWatch.GetOrAdd<string>("Type"), Type.ToString());
|
||||
mobWatch.GetOrAdd("Health", () => CurrentHealth);
|
||||
}
|
||||
|
||||
public void Despawn(bool showDeathFx = false, float delay = 0f)
|
||||
{
|
||||
if (!IsAlive)
|
||||
return;
|
||||
|
||||
IsAlive = false;
|
||||
_waypointMover.StopMoving();
|
||||
|
||||
StopAllCoroutines();
|
||||
|
||||
if (delay <= 0f)
|
||||
Destroy(showDeathFx);
|
||||
else
|
||||
StartCoroutine(DelayedDestroying(showDeathFx, delay));
|
||||
}
|
||||
|
||||
private IEnumerator DelayedDestroying(bool showDeathFx, float delay)
|
||||
{
|
||||
yield return new WaitForSeconds(delay);
|
||||
Destroy(showDeathFx);
|
||||
}
|
||||
|
||||
private void Destroy(bool showDeathFx)
|
||||
{
|
||||
_levelScene.PoolManager.Push(gameObject);
|
||||
|
||||
if (showDeathFx)
|
||||
{
|
||||
var deathFx = _levelScene.PoolManager.Get(_deathFxPrefab);
|
||||
deathFx.transform.position = transform.position + Vector3.up;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2554fbc067f54ad08dbf5f7be16a1f23
|
||||
timeCreated: 1725791025
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 324001
|
||||
packageName: LiveWatch Lite | Debug with full history of changes
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/LiveWatchLite/TowerDefenceDemo/Scripts/Mob/MobMain.cs
|
||||
uploadId: 770587
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Ingvar.LiveWatch.TowerDefenceDemo
|
||||
{
|
||||
public enum MobType
|
||||
{
|
||||
Small,
|
||||
Medium,
|
||||
Big
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 132d1aa57e044b64a4962b10d8fa4d65
|
||||
timeCreated: 1726052963
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 324001
|
||||
packageName: LiveWatch Lite | Debug with full history of changes
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/LiveWatchLite/TowerDefenceDemo/Scripts/Mob/MobType.cs
|
||||
uploadId: 770587
|
||||
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ingvar.LiveWatch.TowerDefenceDemo
|
||||
{
|
||||
public class MobWaypointMover : MonoBehaviour
|
||||
{
|
||||
public float Speed;
|
||||
public float RotationSpeed = 5f;
|
||||
public float TravelledDist { get; private set; }
|
||||
|
||||
private const float ArriveThreshold = 0.01f;
|
||||
|
||||
private int _currentPointIndex;
|
||||
private bool _isMoving;
|
||||
private List<Transform> _waypoints;
|
||||
|
||||
private Transform nextPoint => _waypoints[_currentPointIndex];
|
||||
private Vector2 currentPosition => new Vector2(transform.position.x, transform.position.z);
|
||||
private Vector2 pointPosition => new Vector2(nextPoint.position.x, nextPoint.position.z);
|
||||
|
||||
public void StartMoving(List<Transform> waypoints)
|
||||
{
|
||||
_isMoving = true;
|
||||
_currentPointIndex = 0;
|
||||
_waypoints = waypoints;
|
||||
TravelledDist = 0;
|
||||
}
|
||||
|
||||
public void StopMoving()
|
||||
{
|
||||
_isMoving = false;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_isMoving)
|
||||
return;
|
||||
|
||||
UpdateTargetPoint();
|
||||
UpdatePosition();
|
||||
UpdateRotation();
|
||||
}
|
||||
|
||||
private void UpdateTargetPoint()
|
||||
{
|
||||
var sqrDist = (currentPosition - pointPosition).sqrMagnitude;
|
||||
|
||||
if (sqrDist <= ArriveThreshold * ArriveThreshold)
|
||||
_currentPointIndex = Mathf.Clamp(_currentPointIndex + 1, 0, _waypoints.Count - 1);
|
||||
}
|
||||
|
||||
private void UpdatePosition()
|
||||
{
|
||||
var vector = pointPosition - currentPosition;
|
||||
var distLeft = (pointPosition - currentPosition).magnitude;
|
||||
|
||||
if (distLeft < ArriveThreshold)
|
||||
return;
|
||||
|
||||
var direction = vector / distLeft;
|
||||
var moveDelta = Mathf.Clamp(Speed * Time.deltaTime, 0, distLeft);
|
||||
var vectorMoveDelta = moveDelta * new Vector3(direction.x, 0, direction.y);
|
||||
|
||||
transform.position += vectorMoveDelta;
|
||||
TravelledDist += moveDelta;
|
||||
}
|
||||
|
||||
private void UpdateRotation()
|
||||
{
|
||||
var vector = pointPosition - currentPosition;
|
||||
var distLeft = (pointPosition - currentPosition).magnitude;
|
||||
|
||||
if (distLeft < ArriveThreshold)
|
||||
return;
|
||||
|
||||
var directionCurrent = new Vector2(transform.forward.x, transform.forward.z);
|
||||
var directionTarget = vector;
|
||||
var directionNew = Vector2.Lerp(directionCurrent, directionTarget, Time.unscaledDeltaTime * RotationSpeed);
|
||||
|
||||
transform.forward = new Vector3(directionNew.x, 0, directionNew.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af506fca87704f09b836f009d20f8f89
|
||||
timeCreated: 1725791044
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 324001
|
||||
packageName: LiveWatch Lite | Debug with full history of changes
|
||||
packageVersion: 1.0.1
|
||||
assetPath: Assets/LiveWatchLite/TowerDefenceDemo/Scripts/Mob/MobWaypointMover.cs
|
||||
uploadId: 770587
|
||||
Reference in New Issue
Block a user