C#:
using Oxide.Game.Rust.Cui;
using System.Collections.Generic;
namespace Oxide.Plugins {
[Info("Health Bar", "Obkurenniy", "1.0")]
class HealthBar : RustPlugin
{
class GuiControl
{
private Dictionary<BasePlayer, Timer> _Timers = new Dictionary<BasePlayer, Timer>();
private readonly PluginTimers timer;
private readonly float destroySeconds;
public GuiControl(PluginTimers timer, float destroySeconds)
{
this._Timer = timer;
this._DestroySeconds = destroySeconds;
}
public void TriggerDamage(BasePlayer player, float health, float maxHealth)
{
RemovePlayer(player);
var progress = health / maxHealth + "";
var healthText = (int)health + "";
DrawUI(player, progressText, healthText);
AddPlayer(player);
}
public void TriggerDeath(BasePlayer player)
{
RemovePlayer(player);
var progress = "0";
var healthText = "Убит";
DrawUI(player, progressText, healthText);
AddPlayer(player);
}
void AddPlayer(BasePlayer player)
{
_Timers[player] = _Timer.Once(_DestroySeconds, () =>
{
RemovePlayer(player);
});
}
void RemovePlayer(BasePlayer player)
{
Timer pTimer;
if(_Timers.TryGetValue(player, out pTimer))
{
_Timer.Destroy(ref pTimer);
DestroyUI(player);
_Timers.Remove(player);
}
}
void DrawUI(BasePlayer player, string progress, string text)
{
string json = CUI_MAIN
.Replace("{progress}", progress)
.Replace("{text}", text);
CuiHelper.AddUi(player, json);
}
void DestroyUI(BasePlayer player)
{
CuiHelper.DestroyUi(player, "hpbar_background");
}
}
GuiControl _GuiControl;
void Loaded()
{
_GuiControl = new GuiControl(timer, 5f);
}
void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
{
var player = info.InitiatorPlayer;
if (player && player.IsAlive())
{
NextTick(() =>
{
if (entity.IsAlive() && player?.IsConnected == true)
{
var health = entity.health;
var maxHealth = entity.MaxHealth();
var progress = health / maxHealth;
var text = (int) health;
_GuiControl.TriggerDamage(player, health, maxHealth);
}
});
}
}
void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
{
var player = info.InitiatorPlayer;
if (player && player.IsAlive())
{
_GuiControl.TriggerDeath(player);
}
}
[ChatCommand("testgui")]
void cmdChatTestGui(BasePlayer player)
{
string json = CUI_MAIN
.Replace("{progress}", "0.5")
.Replace("{text}", "50");
CuiHelper.AddUi(player, json);
timer.Once(10f, () => {
CuiHelper.DestroyUi(player, "hpbar_background");
});
}
string CUI_MAIN = @"
[
{
""name"": ""hpbar_background"",
""parent"": ""Overlay"",
""components"": [
{
""type"": ""UnityEngine.UI.Image"",
""material"": """",
""color"": ""0.5254902 0.5254902 0.5254902 0.2980392""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0.5 0"",
""anchormax"": ""0.5 0"",
""offsetmin"": ""-106 86"",
""offsetmax"": ""86 112""
}
]
},
{
""name"": ""hpbar_img"",
""parent"": ""hpbar_background"",
""components"": [
{
""type"": ""UnityEngine.UI.Image"",
""sprite"": ""assets/icons/health.png"",
""material"": ""assets/icons/iconmaterial.mat"",
""color"": ""0.9686275 0.9176471 0.8784314 0.6745098""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0 0"",
""anchormax"": ""0 1"",
""offsetmin"": ""4 4"",
""offsetmax"": ""22 -4""
}
]
},
{
""name"": ""hpbar_wrapper"",
""parent"": ""hpbar_background"",
""components"": [
{
""type"": ""UnityEngine.UI.Image"",
""material"": """",
""color"": ""1 0.4039216 0.4039216 0""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0 0"",
""anchormax"": ""1 1"",
""offsetmin"": ""25 3"",
""offsetmax"": ""-3 -3""
}
]
},
{
""name"": ""hpbar_progress"",
""parent"": ""hpbar_wrapper"",
""components"": [
{
""type"": ""UnityEngine.UI.Image"",
""sprite"": ""assets/content/ui/ui.background.tile.psd"",
""material"": """",
""color"": ""0.6941177 0.2039216 0.003921569 0.5450981""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0 0"",
""anchormax"": ""{progress} 1"",
""offsetmin"": ""0 0"",
""offsetmax"": ""0 0""
}
]
},
{
""name"": ""hpbar_text"",
""parent"": ""hpbar_wrapper"",
""components"": [
{
""type"": ""UnityEngine.UI.Text"",
""text"": ""{text}"",
""fontSize"": 15,
""font"": ""robotocondensed-bold.ttf"",
""align"": ""MiddleLeft"",
""color"": ""0.9686275 0.9176471 0.8784314 0.8945568""
},
{
""type"": ""RectTransform"",
""anchormin"": ""0 0"",
""anchormax"": ""1 1"",
""offsetmin"": ""2 0"",
""offsetmax"": ""0 0""
}
]
}
]
";
}
}
Error while compiling: HealthBar.cs(63,31): error CS0038: Cannot access a nonstatic member of outer type `Oxide.Plugins.HealthBar' via nested type `Oxide.Plugins.HealthBar.GuiControl'
Если запенить
public GuiControl(PluginTimers timer, float destroySeconds)
{
this._Timer = timer;
this._DestroySeconds = destroySeconds;
}
на
public GuiControl(HealthBar plugin, PluginTimers timer, float destroySeconds)
{
this._Plugin = plugin;
this._Timer = timer;
this._DestroySeconds = destroySeconds;
}
Тогда будет проблема с 2 аргументами