MayHeCome/Assets/Scripts/Battle/BattleWall.cs
2024-12-18 17:55:34 +08:00

141 lines
3.6 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks.Triggers;
using UnityEngine;
using UnityEngine.UI;
public class WallOverallBuff : BuffElement
{
public float amount;
public BattleWall wall;
public WallOverallBuff(float amount)
{
this.amount = amount;
}
public virtual void OnStartingEffect(GameObject ob)
{
wall = ob.GetComponent<BattleWall>();
}
}
public class : WallOverallBuff
{
public Sprite wallImage;
public override void OnStartingEffect(GameObject ob)
{
base.OnStartingEffect(ob);
if (wall)
{
wall.WallSprite.sprite = wallImage;
wall.wallBuffs.Remove(this);
}
}
public void OnUpdatingEffect(GameObject ob){}
public void OnEndingEffect(GameObject ob){}
public (float amount) : base(amount)
{
}
}
public class : WallOverallBuff
{
public override void OnStartingEffect(GameObject ob)
{
base.OnStartingEffect(ob);
if (wall)
{
wall.MaxWallHP += (int)amount;
wall.wallBuffs.Remove(this);
}
}
public void OnUpdatingEffect(GameObject ob){}
public void OnEndingEffect(GameObject ob){}
public (float amount) : base(amount)
{
}
}
public class : WallOverallBuff
{
public float healthPercent;
public void OnBeforeComputing(GameObject ob)
{
healthPercent = (wall.MaxWallHP - wall.CurrentWallHP) / (float)wall.MaxWallHP;
wall.recoveryPerSecond += healthPercent * amount;
}
public void OnAfterComputing(GameObject ob)
{
wall.recoveryPerSecond -= healthPercent * amount;
}
public (float amount) : base(amount)
{
}
}
public class BattleWall : MonoBehaviour
{
public int MaxWallHP = 100;
private float currentWallHP = 100;
public Image WallHealthBar;
public SpriteRenderer WallSprite;
public List<BuffElement> wallBuffs = new();
public float recoveryPerSecond = 0f;
public float accumulatedRecoverying = 0f;
private float baseRecoveryRate = 3f; // 每秒回复的固定 3 点血量
private float percentRecoveryRate = 0.05f; // 已损失血量的 5%
public float CurrentWallHP
{
get => currentWallHP;
set
{
currentWallHP = Mathf.Clamp(value, 0, MaxWallHP);
if (WallHealthBar) WallHealthBar.fillAmount = (float)currentWallHP / MaxWallHP;
if (currentWallHP <= 0)
{
GameOverManager.Instance.LoseGame();
}
}
}
private void Awake()
{
WallSprite = GetComponent<SpriteRenderer>();
BuffElement buff = new (0.05f); // 示例: 0.05f 是回血系数,可根据实际需求调整
AddBuff(buff);
buff = new (1);
AddBuff(buff);
}
public void AddBuff(BuffElement element)
{
wallBuffs.Add(element);
element.OnStartingEffect(gameObject);
}
private void Update()
{
wallBuffs.ForEach(b=>b.OnBeforeComputing(this.gameObject));
wallBuffs.ForEach(b=>b.OnUpdatingEffect(this.gameObject));
float lostHealthPercent = (MaxWallHP - CurrentWallHP) * percentRecoveryRate;
accumulatedRecoverying += (baseRecoveryRate + lostHealthPercent) * Time.deltaTime;
if (!(accumulatedRecoverying >= 1)) return;
CurrentWallHP += (int)accumulatedRecoverying;
accumulatedRecoverying -= (int)accumulatedRecoverying;
wallBuffs.ForEach(b=>b.OnAfterComputing(this.gameObject));
}
}