Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Oxide.Core;
- using Oxide.Core.Plugins;
- using Rust;
- using UnityEngine;
- namespace Oxide.Plugins
- {
- // TODO LIST
- // Helicopter networking/targeting testing
- [Info("Deathmatch", "Kappasaurus", "1.0.0")]
- class Deathmatch : RustPlugin
- {
- #region Data and Classes
- private static Deathmatch Instance;
- private List<Spawn> Spawns = new List<Spawn>();
- private class Location
- {
- public float X, Y, Z;
- public Location()
- {
- }
- public Location(float x, float y, float z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- public static Location FromVector3(Vector3 vector) => new Location(vector.x, vector.y, vector.z);
- public Vector3 ToVector3() => new Vector3(X, Y, Z);
- }
- private class Spawn
- {
- public int Number;
- public Location Location;
- public Spawn()
- {
- }
- public Spawn(int number, Location location)
- {
- Number = number;
- Location = location;
- }
- }
- public void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Title, Spawns);
- public void ReadData() => Spawns = Interface.Oxide.DataFileSystem.ReadObject<List<Spawn>>(Title);
- #endregion
- #region Configuration
- private struct Configuration
- {
- public static string KitName = "event";
- public static int KillStreakMin = 5;
- public static int MaxPlayers = 30;
- public static bool ResetSpawnsWithNewMap = true;
- }
- private new void LoadConfig()
- {
- GetConfig(ref Configuration.KitName, "Event kit");
- GetConfig(ref Configuration.KillStreakMin, "Minimum kills for a streak");
- GetConfig(ref Configuration.MaxPlayers, "Maximum players at once");
- GetConfig(ref Configuration.ResetSpawnsWithNewMap, "Reset spawns with new map");
- SaveConfig();
- }
- protected override void LoadDefaultConfig() => PrintWarning("Generating new config file...");
- #endregion
- #region Chat Commands
- [ChatCommand("event")]
- private void EventCommand(BasePlayer player, string command, string[] args)
- {
- if (Spawns.Count == 0)
- {
- Message(player, "No Spawns");
- return;
- }
- if (args.Length == 0)
- {
- Message(player, "No Argument");
- return;
- }
- switch (args[0].ToLower())
- {
- case "join":
- if (players.Count >= BasePlayer.activePlayerList.Count && !HasPermission(player))
- {
- Message(player, "Arena Full");
- return;
- }
- if (usageTimes.ContainsKey(player))
- {
- if (DateTime.UtcNow - usageTimes[player] > new TimeSpan(0, 0, 5))
- {
- Message(player, "Cooldown");
- return;
- }
- }
- if (players.Contains(player))
- {
- Message(player, "Already Joined");
- return;
- }
- if (!usageTimes.ContainsKey(player))
- {
- usageTimes.Add(player, DateTime.UtcNow);
- }
- Add(player);
- break;
- case "leave":
- if (!players.Contains(player))
- {
- Message(player, "Hasn't Joined");
- return;
- }
- Remove(player);
- break;
- default:
- Message(player, "Unknown Argument");
- break;
- }
- }
- [ChatCommand("spawn")]
- private void SpawnCommand(BasePlayer player, string command, string[] args)
- {
- if (!HasPermission(player))
- {
- Message(player, "No Permission");
- return;
- }
- if (args.Length == 0)
- {
- Message(player, "No Argument");
- return;
- }
- switch (args[0].ToLower())
- {
- case "add":
- {
- PrintToChat(player, lang.GetMessage("Spawn Added", this, player.UserIDString), Spawns.Count + 1);
- var spawn = new Spawn(Spawns.Count + 1, Location.FromVector3(player.transform.position));
- Spawns.Add(spawn);
- SaveData();
- PrintWarning("Drawing sphere to accomodate for changes to spawns.");
- var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
- sphere.Remove();
- DrawSphere(spawns);
- }
- break;
- case "remove":
- {
- if (args.Length != 2)
- {
- Message(player, "No Argument");
- }
- var spawn = Spawns.FirstOrDefault(x => x.Number.ToString() == args[1]);
- if (spawn == null)
- {
- Message(player, "Spawn Not Found");
- return;
- }
- PrintToChat(player, lang.GetMessage("Spawn Removed", this, player.UserIDString), spawn.Number);
- Spawns.Remove(spawn);
- SaveData();
- PrintWarning("Drawing sphere to accomodate for changes to spawns.");
- var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
- sphere.Remove();
- DrawSphere(spawns);
- }
- break;
- case "reset":
- {
- if (Spawns.Count == 0)
- {
- Message(player, "No Spawns Saved");
- return;
- }
- Message(player, "Spawns Reset");
- Spawns.Clear();
- SaveData();
- sphere.Remove();
- }
- break;
- case "list":
- {
- if (Spawns.Count == 0)
- {
- Message(player, "No Spawns Saved");
- return;
- }
- PrintToChat(player, lang.GetMessage("Spawn List", this, player.UserIDString), Spawns.Select(x => x.Number).ToSentence());
- }
- break;
- default:
- {
- Message(player, "Unknown Argument");
- }
- break;
- }
- }
- #endregion
- #region Item Saving
- private class Inventory
- {
- public List<InventoryItem> ContainerBelt = new List<InventoryItem>();
- public List<InventoryItem> ContainerWear = new List<InventoryItem>();
- public List<InventoryItem> ContainerMain = new List<InventoryItem>();
- }
- private class InventoryItem
- {
- public string ItemName;
- public int Amount;
- public ulong SkinID;
- public bool Weapon;
- public List<string> Attachments;
- }
- private Inventory GetInventory(BasePlayer player)
- {
- var inventory = new Inventory();
- foreach (var item in player.inventory.containerBelt.itemList)
- {
- inventory.ContainerBelt.Add(ProcessItem(item));
- }
- foreach (var item in player.inventory.containerWear.itemList)
- {
- inventory.ContainerWear.Add(ProcessItem(item));
- }
- foreach (var item in player.inventory.containerMain.itemList)
- {
- inventory.ContainerMain.Add(ProcessItem(item));
- }
- return inventory;
- }
- private void GiveInventory(BasePlayer player)
- {
- player.inventory.Strip();
- foreach (var item in inventories[player].ContainerBelt)
- {
- var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
- player.inventory.GiveItem(giveItem, player.inventory.containerMain);
- }
- foreach (var item in inventories[player].ContainerWear)
- {
- var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
- player.inventory.GiveItem(giveItem, player.inventory.containerWear);
- }
- foreach (var item in inventories[player].ContainerMain)
- {
- var giveItem = item.Weapon ? BuildWeapon(item.ItemName, item.SkinID, item.Attachments) : BuildItem(item.ItemName, item.Amount, item.SkinID);
- player.inventory.GiveItem(giveItem, player.inventory.containerMain);
- }
- inventories.Remove(player);
- }
- private Item BuildWeapon(string name, ulong skin, List<string> attachments)
- {
- var item = ItemManager.CreateByName(name, 1, skin);
- var weapon = item.GetHeldEntity() as BaseProjectile;
- if (weapon != null)
- {
- weapon.primaryMagazine.contents = weapon.primaryMagazine.capacity;
- }
- if (attachments == null)
- {
- return item;
- }
- foreach (var attachment in attachments)
- {
- BuildItem(attachment, 1).MoveToContainer(item.contents);
- }
- return item;
- }
- private Item BuildItem(string name, int amount, ulong skin = 0)
- {
- if (amount < 1)
- {
- amount = 1;
- }
- var item = ItemManager.CreateByName(name, amount, skin);
- return item;
- }
- private InventoryItem ProcessItem(Item item)
- {
- var inventoryItem = new InventoryItem
- {
- ItemName = item.info.shortname,
- Amount = item.amount,
- SkinID = item.skin,
- Attachments = new List<string>(),
- };
- if (item.info.category.ToString() != "Weapon")
- {
- return inventoryItem;
- }
- var weapon = item.GetHeldEntity() as BaseProjectile;
- if (weapon == null || weapon.primaryMagazine == null)
- {
- return inventoryItem;
- }
- inventoryItem.Weapon = true;
- if (weapon.primaryMagazine.ammoType.shortname == null)
- {
- return inventoryItem;
- }
- if (item.contents == null)
- {
- return inventoryItem;
- }
- foreach (var attachment in item.contents.itemList)
- {
- if (attachment.info.itemid != 0)
- {
- inventoryItem.Attachments.Add(attachment.info.shortname);
- }
- }
- return inventoryItem;
- }
- #endregion
- #region Oxide Hooks
- private void Init()
- {
- PrintWarning("Warning, without Kits this plugin is rendered useless and may cause excessive errors.");
- // DATA
- Instance = this;
- ReadData();
- // CONFIGURATION
- LoadConfig();
- // PERMISSION
- permission.RegisterPermission("deathmatch.admin", this);
- // MESSAGE
- if (Spawns.Count == 0)
- {
- return;
- }
- PrintToChat(lang.GetMessage("Event Message", this));
- timer.Repeat(600f, 0, () =>
- {
- foreach (var player in BasePlayer.activePlayerList)
- {
- if (!players.Contains(player))
- {
- PrintToChat(lang.GetMessage("Event Message", this));
- }
- }
- });
- // SPHERES
- timer.Once(30f, () =>
- {
- PrintWarning("Drawing sphere.");
- var spawns = Spawns.Select(position => position.Location.ToVector3()).ToList();
- DrawSphere(spawns);
- });
- }
- private void OnNewSave()
- {
- if (!Configuration.ResetSpawnsWithNewMap)
- {
- return;
- }
- PrintWarning("Resetting spawns due to new map.");
- Spawns.Clear();
- SaveData();
- }
- private void OnPlayerDisconnected(BasePlayer player)
- {
- if (players.Contains(player))
- {
- Remove(player);
- }
- }
- private void OnPlayerInit(BasePlayer player)
- {
- Message(player, "Event Message");
- }
- private object OnPlayerDie(BasePlayer player, HitInfo hitInfo)
- {
- try
- {
- if (!players.Contains(player))
- {
- return null;
- }
- var attacker = hitInfo.InitiatorPlayer;
- if (attacker == player)
- {
- Respawn(player);
- return false;
- }
- if (attacker != null)
- {
- PrintToChat(player, lang.GetMessage("Victim", this, player.UserIDString), attacker.displayName);
- PrintToChat(attacker, lang.GetMessage("Attacker", this, attacker.UserIDString), player.displayName);
- if (kills.ContainsKey(attacker))
- {
- kills[attacker]++;
- }
- else
- {
- kills.Add(attacker, 1);
- }
- if (kills.Max().Value == kills[attacker])
- {
- foreach (var target in players)
- {
- PrintToChat(target, lang.GetMessage("Most Kills", this, target.UserIDString), attacker.displayName, kills[attacker]);
- }
- }
- if (killStreaks.ContainsKey(attacker))
- {
- killStreaks[attacker]++;
- if (killStreaks[attacker] >= Configuration.KillStreakMin)
- {
- foreach (var target in players)
- {
- PrintToChat(target, lang.GetMessage("Kill Streak", this, target.UserIDString), attacker.displayName, killStreaks[attacker]);
- }
- }
- }
- else
- {
- killStreaks.Add(attacker, 1);
- }
- if (killStreaks.ContainsKey(player))
- {
- killStreaks.Remove(player);
- }
- }
- }
- catch
- {
- }
- Respawn(player);
- return false;
- }
- private object CanBeWounded(BasePlayer player, HitInfo info)
- {
- if (players.Contains(player))
- {
- return false;
- }
- return null;
- }
- private void OnRunPlayerMetabolism(PlayerMetabolism metabolism, BaseCombatEntity entity)
- {
- var player = entity.ToPlayer();
- if (player == null)
- {
- return;
- }
- if (!players.Contains(player))
- {
- return;
- }
- player.metabolism.calories.value = 500f;
- player.metabolism.hydration.value = 250f;
- player.metabolism.comfort.value = player.health < 90f ? 100f : 0f;
- player.metabolism.temperature.value = 30f;
- player.metabolism.bleeding.value = 0f;
- }
- private void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
- {
- if (!players.Contains(entity?.ToPlayer()))
- {
- return;
- }
- if (info.damageTypes.GetMajorityDamageType() != DamageType.Fall)
- {
- return;
- }
- info.damageTypes = new DamageTypeList();
- }
- private void OnLoseCondition(Item item, ref float amount)
- {
- var player = item.GetOwnerPlayer();
- var info = item?.info;
- if (!players.Contains(player))
- {
- return;
- }
- if (info == null)
- {
- return;
- }
- if (item.hasCondition)
- {
- item.RepairCondition(amount);
- }
- }
- /*private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainer, int targetSlot, int numItems)
- {
- var player = item.GetOwnerPlayer();
- if (player == null)
- {
- return null;
- }
- if (players.Contains(player))
- {
- return false;
- }
- return null;
- }*/
- private object OnItemAction(Item item, string action)
- {
- var player = item.GetOwnerPlayer();
- if (player == null)
- {
- return null;
- }
- if (players.Contains(player))
- {
- return false;
- }
- return null;
- }
- private void Unload(BasePlayer player)
- {
- foreach (var target in players)
- {
- Message(target, "Emergency Ending");
- Remove(target, true);
- }
- sphere?.Remove();
- }
- private object OnHelicopterTarget(HelicopterTurret turret, BaseCombatEntity entity)
- {
- var player = entity.ToPlayer();
- if (player == null)
- {
- return null;
- }
- if (players.Contains(player))
- {
- return false;
- }
- return null;
- }
- /*private object CanNetworkTo(BaseNetworkable entity, BaseNetworkable target)
- {
- var player = entity as BasePlayer ?? (entity as HeldEntity)?.GetOwnerPlayer();
- var targetPlayer = target as BasePlayer ?? (target as HeldEntity)?.GetOwnerPlayer();
- if ((players.Contains(player) || players.Contains(targetPlayer)) && (entity is BaseHelicopter || target is BaseHelicopter))
- {
- return false;
- }
- if (player == null || targetPlayer == null || targetPlayer == player)
- {
- return null;
- }
- if (players.Contains(player) && !players.Contains(targetPlayer) || players.Contains(targetPlayer) && !players.Contains(player))
- {
- return false;
- }
- return null;
- }*/
- #endregion
- #region Metabolism
- class Metabolism
- {
- public float Calories, Hydration, Bleed, Health, Wetness, RadiationLevel, RadiationPoison;
- }
- private Metabolism GetMetabolism(BasePlayer player)
- {
- var newMetabolism = new Metabolism
- {
- Calories = player.metabolism.calories.value,
- Hydration = player.metabolism.hydration.value,
- Bleed = player.metabolism.bleeding.value,
- Health = player.health,
- Wetness = player.metabolism.wetness.value,
- RadiationLevel = player.metabolism.radiation_level.value,
- RadiationPoison = player.metabolism.radiation_level.value
- };
- return newMetabolism;
- }
- private void RestoreMetabolism(BasePlayer player)
- {
- player.metabolism.calories.value = metabolism[player].Calories;
- player.metabolism.hydration.value = metabolism[player].Hydration;
- player.metabolism.bleeding.value = metabolism[player].Bleed;
- player.metabolism.wetness.value = metabolism[player].Wetness;
- player.metabolism.radiation_level.value = metabolism[player].RadiationLevel;
- player.metabolism.radiation_poison.value = metabolism[player].RadiationPoison;
- player.health = metabolism[player].Health;
- }
- #endregion
- #region Variables
- [PluginReference]
- private Plugin Kits;
- private List<BasePlayer> players = new List<BasePlayer>();
- private Dictionary<BasePlayer, Vector3> locations = new Dictionary<BasePlayer, Vector3>();
- private Dictionary<BasePlayer, Inventory> inventories = new Dictionary<BasePlayer, Inventory>();
- private Dictionary<BasePlayer, DateTime> usageTimes = new Dictionary<BasePlayer, DateTime>();
- private Dictionary<BasePlayer, Metabolism> metabolism = new Dictionary<BasePlayer, Metabolism>();
- private Dictionary<BasePlayer, int> killStreaks = new Dictionary<BasePlayer, int>();
- private Dictionary<BasePlayer, int> kills = new Dictionary<BasePlayer, int>();
- private List<BasePlayer> outOfBoundsPlayers = new List<BasePlayer>();
- // SPHERES
- private static int spheres;
- private static float radius;
- private float distance;
- private Sphere sphere;
- #endregion
- #region Helpers
- // CONFIGURATION
- private void GetConfig<T>(ref T variable, params string[] path)
- {
- if (path.Length == 0)
- {
- return;
- }
- if (Config.Get(path) == null)
- {
- Config.Set(path.Concat(new object[] { variable }).ToArray());
- PrintWarning($"Added field to config: {string.Join("/", path)}");
- }
- variable = (T)Convert.ChangeType(Config.Get(path), typeof(T));
- }
- // MESSAGES
- protected override void LoadDefaultMessages()
- {
- lang.RegisterMessages(new Dictionary<string, string>
- {
- ["No Permission"] = "Error, you lack permission.",
- ["No Argument"] = "Error, no argument.",
- ["Unknown Argument"] = "Error, unknown argument.",
- ["Not Active"] = "Error, event not active.",
- ["Already Joined"] = "Error, you're already participating in the event.",
- ["Hasn't Joined"] = "Error, you're not currently participating in the event.",
- ["No Spawns"] = "Error, no spawns are saved, try contacting your administrator.",
- ["Spawn Not Found"] = "Error, spawn doesn't exist.",
- ["Spawn Added"] = "Spawn sucessfully added with name <color=#ADD8E6>{0}</color>.",
- ["Spawn Removed"] = "Spawn <color=#ADD8E6>{0}</color> sucessfully removed.",
- ["Spawns Reset"] = "Sucessfully reset all spawns.",
- ["Spawn List"] = "The following spawn(s) are currently saved: <color=#ADD8E6>{0}</color>",
- ["No Spawns Saved"] = "Error, no spawns saved.",
- ["Joined"] = "Sucessfully joined the game, use \"<color=#ADD8E6>/event leave</color>\" to exit.",
- ["Left"] = "Sucessfully left the game.",
- ["All Joined"] = "<color=#ADD8E6>{0}</color> joined the game for a total of <color=#ADD8E6>{1}</color> player(s).",
- ["All Left"] = "<color=#ADD8E6>{0}</color> left the game. <color=#ADD8E6>{1}</color> player(s) left.",
- ["Cooldown"] = "Error, you're doing that too fast. Try again in 5 minutes.",
- ["Emergency Ending"] = "Game ending due to technical difficulties.",
- ["Attacker"] = "You killed <color=#ADD8E6>{0}</color>.",
- ["Victim"] = "<color=#ADD8E6>{0}</color> killed you.",
- ["Kill Streak"] = "<color=#ADD8E6>{0}</color> is on a kill streak with <color=#ADD8E6>{1}</color> kills!",
- ["Kit Block"] = "Error, you may not redeem kits in the arena.",
- ["Event Message"] = "The deathmatch event is currently active, use \"<color=#ADD8E6>/event join</color>\" to participate!",
- ["Arena Full"] = "Error, arena has reached the player cap, try again later.",
- ["Teleport Block"] = "Error, you may not use teleportation, if you'd like to leave simply type \"<color=#ADD8E6>/event leave</color>\" to exit.",
- ["Most Kills"] = "<color=#ADD8E6>{0}</color> is in the lead with <color=#ADD8E6>{1}</color> kills!",
- ["Out of Bounds"] = "Error, you're out of bounds. If you don't enter the arena soon you'll die!"
- }, this);
- }
- private void Message(BasePlayer player, string messageKey)
- {
- PrintToChat(player, lang.GetMessage(messageKey, this, player.UserIDString));
- }
- // TELEPORTATION
- private void Teleport(BasePlayer player, Vector3 position)
- {
- if (player.net?.connection != null)
- {
- player.ClientRPCPlayer(null, player, "StartLoading");
- }
- StartSleeping(player);
- player.MovePosition(position);
- if (player.net?.connection != null)
- {
- player.ClientRPCPlayer(null, player, "ForcePositionTo", position);
- }
- if (player.net?.connection != null)
- {
- player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true);
- }
- player.UpdateNetworkGroup();
- player.SendNetworkUpdateImmediate();
- if (player.net?.connection == null)
- {
- return;
- }
- try
- {
- player.ClearEntityQueue();
- }
- catch
- {
- }
- player.SendFullSnapshot();
- }
- private void StartSleeping(BasePlayer player)
- {
- if (player.IsSleeping())
- {
- return;
- }
- player.SetPlayerFlag(BasePlayer.PlayerFlags.Sleeping, true);
- if (!BasePlayer.sleepingPlayerList.Contains(player))
- {
- BasePlayer.sleepingPlayerList.Add(player);
- }
- player.CancelInvoke("InventoryUpdate");
- }
- // EVENT KIT
- private void GiveKit(BasePlayer player, string kitName)
- {
- player.inventory.Strip();
- Kits?.CallHook("GiveKit", player, kitName);
- }
- // OTHER
- private void Respawn(BasePlayer player)
- {
- var spawn = Spawns.GetRandom((uint)DateTime.UtcNow.Millisecond);
- Teleport(player, spawn.Location.ToVector3());
- player.health = 100;
- player.metabolism.wetness.value = 0f;
- player.metabolism.radiation_level.value = 0f;
- player.metabolism.radiation_poison.value = 0f;
- player.StopWounded();
- GiveKit(player, Configuration.KitName);
- LockInventory(player);
- }
- private void Remove(BasePlayer player, bool unload = false)
- {
- PrintToChat(player, lang.GetMessage("Left", this, player.UserIDString));
- foreach (var target in players)
- {
- if (target != player && !unload)
- {
- PrintToChat(target, lang.GetMessage("All Left", this, target.UserIDString), player.displayName, players.Count - 1);
- }
- }
- if (!unload)
- {
- players.Remove(player);
- }
- Teleport(player, locations[player]);
- locations.Remove(player);
- outOfBoundsPlayers.Remove(player);
- player.inventory.Strip();
- GiveInventory(player);
- RestoreMetabolism(player);
- metabolism.Remove(player);
- killStreaks.Remove(player);
- UnlockInventory(player);
- }
- private void Add(BasePlayer player)
- {
- PrintToChat(player, lang.GetMessage("Joined", this, player.UserIDString));
- foreach (var target in players)
- {
- if (target != player)
- {
- PrintToChat(target, lang.GetMessage("All Joined", this, target.UserIDString), player.displayName, players.Count + 1);
- }
- }
- players.Add(player);
- locations.Add(player, new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z));
- inventories.Add(player, GetInventory(player));
- player.inventory.Strip();
- metabolism.Add(player, GetMetabolism(player));
- Respawn(player);
- }
- private void LockInventory(BasePlayer player)
- {
- player.inventory.containerWear.SetFlag(ItemContainer.Flag.IsLocked, true);
- }
- private void UnlockInventory(BasePlayer player)
- {
- player.inventory.containerWear.SetFlag(ItemContainer.Flag.IsLocked, false);
- }
- // PERMISSION
- private bool HasPermission(BasePlayer player, string perm = "deathmatch.admin") => player.IsAdmin || permission.UserHasPermission(player.UserIDString, perm);
- #endregion
- #region Spheres
- public class Sphere
- {
- private readonly BaseEntity sphereEntity;
- private readonly BaseEntity visSphereEntity;
- public Sphere(Vector3 position)
- {
- sphereEntity = GameManager.server.CreateEntity("assets/prefabs/visualization/sphere.prefab", position);
- sphereEntity.name = $"{spheres++}";
- var sphere = sphereEntity.GetComponent<SphereEntity>();
- sphere.currentRadius = radius * 2 + 30;
- sphere.lerpRadius = sphere.currentRadius;
- sphereEntity.Spawn();
- var trigger = sphereEntity.gameObject.AddComponent<SphereTrigger>();
- trigger.Area = this;
- // VIS SPHERE
- visSphereEntity = GameManager.server.CreateEntity("assets/prefabs/visualization/sphere.prefab", position);
- var visSphere = visSphereEntity.GetComponent<SphereEntity>();
- visSphere.currentRadius = sphere.currentRadius;
- visSphere.lerpRadius = sphere.currentRadius;
- for (var i = 0; i <= 10; i++)
- {
- visSphereEntity.name = $"{i + 5}";
- visSphereEntity.Instantiate();
- }
- }
- public void Remove()
- {
- sphereEntity.Kill();
- visSphereEntity.Kill();
- }
- }
- public class SphereTrigger : TriggerBase
- {
- public Sphere Area;
- internal virtual void Awake()
- {
- gameObject.layer = LayerMask.NameToLayer("Trigger");
- var collider = gameObject.AddComponent<SphereCollider>();
- collider.radius = radius + 15;
- collider.isTrigger = true;
- }
- internal virtual GameObject InterestedInObject(GameObject obj)
- {
- var player = obj.ToBaseEntity() as BasePlayer;
- return !player ? null : obj;
- }
- internal virtual void OnTriggerEnter(Collider collider)
- {
- var go = InterestedInObject(collider.gameObject);
- if (go == null)
- {
- return;
- }
- if (contents == null)
- {
- contents = new HashSet<GameObject>();
- }
- if (contents.Contains(go))
- {
- return;
- }
- contents.Add(go);
- OnEntityEnter(go.ToBaseEntity());
- }
- internal virtual void OnTriggerExit(Collider collider)
- {
- if (collider == null) return;
- var go = InterestedInObject(collider.gameObject);
- if (go == null) return;
- if (contents == null) return;
- contents.Remove(go);
- OnEntityLeave(go.ToBaseEntity());
- }
- internal virtual void OnEntityEnter(BaseEntity ent)
- {
- if (ent == null) return;
- if (entityContents == null) entityContents = new HashSet<BaseEntity>();
- entityContents.Add(ent);
- ent.EnterTrigger(this);
- Instance.CallHook("OnTriggerEnter", this, ent);
- }
- internal virtual void OnEntityLeave(BaseEntity ent)
- {
- if (ent == null) return;
- if (entityContents == null) return;
- entityContents.Remove(ent);
- ent.LeaveTrigger(this);
- Instance.CallHook("OnTriggerLeave", this, ent);
- }
- }
- private void OnTriggerEnter(TriggerBase trigger, BaseEntity entity)
- {
- var area = trigger as SphereTrigger;
- if (area == null)
- {
- return;
- }
- var player = entity as BasePlayer;
- if (player == null)
- {
- return;
- }
- if (outOfBoundsPlayers.Contains(player))
- {
- outOfBoundsPlayers.Remove(player);
- player.metabolism.radiation_poison.value = 0f;
- player.metabolism.radiation_level.value = 0f;
- }
- }
- private void OnTriggerLeave(TriggerBase trigger, BaseEntity entity)
- {
- var area = trigger as SphereTrigger;
- if (area == null)
- {
- return;
- }
- var player = entity as BasePlayer;
- if (player == null)
- {
- return;
- }
- if (!players.Contains(player))
- {
- return;
- }
- outOfBoundsPlayers.Add(player);
- Message(player, "Out of Bounds");
- var outOfBoundsTimer = timer.Repeat(1f, 0, () =>
- {
- if (!outOfBoundsPlayers.Contains(player))
- {
- return;
- }
- player.metabolism.radiation_level.value += UnityEngine.Random.Range(1f, 3f);
- player.metabolism.radiation_poison.value +=UnityEngine.Random.Range(1f, 3f);
- });
- }
- private void DrawSphere(List<Vector3> positionList)
- {
- if (positionList.Count == 1)
- {
- sphere = new Sphere(positionList[0]);
- return;
- }
- var center = new Vector3(0, 0, 0);
- foreach (var position in positionList)
- {
- center += position;
- foreach (Vector3 t in positionList)
- {
- if (Vector3.Distance(position, t) / 2 > radius)
- {
- radius = Vector3.Distance(position, t);
- }
- }
- }
- sphere = new Sphere(center / positionList.Count);
- }
- #endregion
- #region Other
- private object canRedeemKit(BasePlayer player) => players.Contains(player) ? lang.GetMessage("Kit Block", this) : null;
- private object CanTeleport(BasePlayer player) => players.Contains(player) ? lang.GetMessage("Teleport Block", this) : null;
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement