using System;
using System.Globalization;
using UnityEngine;
using Oxide.Game.Rust.Cui;
using VLB;
namespace Oxide.Plugins
{
[Info("CHit", "TF Crazy", "1.0.1")]
[Description("Display hit damage")]
public class CHit : RustPlugin
{
#region Fields
private static string permUse = "chit.use";
#endregion
#region Server
/// Iniialize
private void OnServerInitialized() => permission.RegisterPermission(permUse, this);
/// Display hit damage
/// BasePlayer: Contain information about the player
/// HitInfo: Contain information about hit impact
private void OnPlayerAttack(BasePlayer player, HitInfo info)
{
if (player == null || player.IsNpc)
return;
if (info == null || info.HitEntity == null || info.HitEntity.IsDestroyed)
return;
if (!permission.UserHasPermission(player.UserIDString, permUse))
return;
NextTick(() =>
{
var posX = (UnityEngine.Random.Range(42.5f, 57.5f) / 100f);
var posY = (UnityEngine.Random.Range(42.5f, 57.5f) / 100f);
var container = new CuiElementContainer();
var layer = $"CHit.{DateTime.Now}";
float damage = (float)Math.Round(info.damageTypes.Total());
string color = UI.Color("#ffffff", 1f);
if (damage >= 1)
{
if (damage >= 50)
color = UI.Color("#cd4632", 1f);
UI.CreateTextElement(ref container, layer, color, $"{damage}", 15, posX, posY, 0.05f, 0.05f);
}
CuiHelper.AddUi(player, container);
CuiHelper.DestroyUi(player, layer);
});
}
#endregion
#region UI
public class UI
{
/// Create text element
/// ref CuiElementContainer: Contain container element
/// String: Panel name
/// String: Color in rust format "r g b a"
/// String: Text to display
/// Int: Text size
/// Float: Anchor x
/// Float: Anchor y
/// Float: Width of element
/// Float: Height of element
/// TextAnchor: Text alignement
/// String: Overlay layer
/// Float: Fade effect
/// String: Font name
static public void CreateTextElement(ref CuiElementContainer container, string panel, string color, string text, int size, float x, float y, float width, float height, TextAnchor align = TextAnchor.MiddleCenter, string parent = "Hud", float fadeout = 1.5f, string font = "robotocondensed-bold.ttf")
{
string aMin = $"{x} {y}";
string aMax = $"{x + width} {y + height}";
CuiElement element = new CuiElement
{
Name = panel,
Parent = parent,
FadeOut = fadeout,
Components =
{
new CuiTextComponent
{
Text = text,
Color = color,
Font = font,
FontSize = size,
Align = align
},
new CuiRectTransformComponent()
{
AnchorMin = aMin,
AnchorMax = aMax
}
}
};
container.Add(element);
}
/// Convert Hex color to Rust format
/// String: Hex code color
/// Float: opacity
/// Return string
static public string Color(string hexColor, float alpha)
{
if (hexColor.StartsWith("#"))
hexColor = hexColor.TrimStart('#');
int red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
int green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
int blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
return $"{(double)red / 255} {(double)green / 255} {(double)blue / 255} {alpha}";
}
}
#endregion
}
}