BattleBit-Community-Server-API/BattleBitAPI/Server/Player.cs

315 lines
9.0 KiB
C#
Raw Normal View History

2023-07-30 12:31:06 -04:00
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using System.Net;
2023-07-27 20:24:00 -04:00
using System.Numerics;
namespace BattleBitAPI
{
public class Player<TPlayer> where TPlayer : Player<TPlayer>
2023-07-27 20:24:00 -04:00
{
2023-08-14 05:43:58 -04:00
private Internal mInternal;
2023-07-30 12:31:06 -04:00
2023-08-14 05:43:58 -04:00
// ---- Variables ----
public ulong SteamID => mInternal.SteamID;
public string Name => mInternal.Name;
public IPAddress IP => mInternal.IP;
public GameServer<TPlayer> GameServer => mInternal.GameServer;
2023-08-14 09:21:30 -04:00
public GameRole Role
{
get => mInternal.Role;
set
{
if (value == mInternal.Role)
return;
SetNewRole(value);
}
}
public Team Team
{
get => mInternal.Team;
set
{
if (mInternal.Team != value)
ChangeTeam(value);
}
}
2023-08-20 03:39:00 -04:00
public Squads SquadName
2023-08-14 09:21:30 -04:00
{
2023-08-20 03:39:00 -04:00
get => mInternal.SquadName;
2023-08-14 09:21:30 -04:00
set
{
2023-08-20 03:39:00 -04:00
if (value == mInternal.SquadName)
2023-08-14 09:21:30 -04:00
return;
if (value == Squads.NoSquad)
KickFromSquad();
else
JoinSquad(value);
}
}
2023-08-20 03:39:00 -04:00
public Squad<TPlayer> Squad
{
get => GameServer.GetSquad(mInternal.Team, mInternal.SquadName);
set
{
if (value == Squad)
return;
if (value == null)
KickFromSquad();
else
{
if(value.Team != this.Team)
ChangeTeam(value.Team);
JoinSquad(value.Name);
}
}
}
public bool InSquad => mInternal.SquadName != Squads.NoSquad;
2023-08-14 05:43:58 -04:00
public int PingMs => mInternal.PingMs;
2023-08-17 11:39:20 -04:00
public float HP
{
get => mInternal.HP;
set
{
if (mInternal.HP > 0)
{
float v = value;
if (v <= 0)
v = 0.1f;
else if (v > 100f)
v = 100f;
SetHP(v);
}
}
}
2023-08-14 05:43:58 -04:00
public bool IsAlive => mInternal.HP >= 0f;
public bool IsUp => mInternal.HP > 0f;
public bool IsDown => mInternal.HP == 0f;
public bool IsDead => mInternal.HP == -1f;
2023-08-14 09:21:30 -04:00
public Vector3 Position
{
get => mInternal.Position;
set => Teleport(value);
}
public PlayerStand StandingState => mInternal.Standing;
public LeaningSide LeaningState => mInternal.Leaning;
2023-08-14 05:43:58 -04:00
public LoadoutIndex CurrentLoadoutIndex => mInternal.CurrentLoadoutIndex;
public bool InVehicle => mInternal.InVehicle;
public bool IsBleeding => mInternal.IsBleeding;
public PlayerLoadout CurrentLoadout => mInternal.CurrentLoadout;
public PlayerWearings CurrentWearings => mInternal.CurrentWearings;
2023-08-14 10:13:31 -04:00
public PlayerModifications<TPlayer> Modifications => mInternal.Modifications;
2023-08-14 05:43:58 -04:00
// ---- Events ----
public virtual void OnCreated()
{
}
2023-08-14 05:43:58 -04:00
public virtual async Task OnConnected()
{
}
public virtual async Task OnSpawned()
{
2023-08-14 09:21:30 -04:00
}
public virtual async Task OnDowned()
{
}
public virtual async Task OnGivenUp()
{
}
public virtual async Task OnRevivedByAnotherPlayer()
{
}
public virtual async Task OnRevivedAnotherPlayer()
{
}
public virtual async Task OnDied()
{
2023-08-14 05:43:58 -04:00
}
public virtual async Task OnChangedTeam()
{
}
public virtual async Task OnChangedRole(GameRole newRole)
{
}
2023-08-20 03:39:00 -04:00
public virtual async Task OnJoinedSquad(Squad<TPlayer> newSquad)
2023-08-14 05:43:58 -04:00
{
}
2023-08-20 03:39:00 -04:00
public virtual async Task OnLeftSquad(Squad<TPlayer> oldSquad)
2023-08-14 05:43:58 -04:00
{
}
public virtual async Task OnDisconnected()
2023-07-30 12:31:06 -04:00
{
}
2023-07-27 20:24:00 -04:00
2023-08-14 05:43:58 -04:00
// ---- Functions ----
2023-07-27 20:24:00 -04:00
public void Kick(string reason = "")
{
2023-08-14 10:13:31 -04:00
GameServer.Kick(this, reason);
2023-07-27 20:24:00 -04:00
}
public void Kill()
{
2023-08-14 10:13:31 -04:00
GameServer.Kill(this);
2023-07-27 20:24:00 -04:00
}
public void ChangeTeam()
{
2023-08-14 10:13:31 -04:00
GameServer.ChangeTeam(this);
2023-07-27 20:24:00 -04:00
}
2023-08-14 05:43:58 -04:00
public void ChangeTeam(Team team)
{
2023-08-14 10:13:31 -04:00
GameServer.ChangeTeam(this, team);
2023-08-14 05:43:58 -04:00
}
2023-07-27 20:24:00 -04:00
public void KickFromSquad()
{
2023-08-14 10:13:31 -04:00
GameServer.KickFromSquad(this);
2023-07-27 20:24:00 -04:00
}
2023-08-14 09:21:30 -04:00
public void JoinSquad(Squads targetSquad)
{
2023-08-14 10:13:31 -04:00
GameServer.JoinSquad(this, targetSquad);
2023-08-14 09:21:30 -04:00
}
2023-07-27 20:24:00 -04:00
public void DisbandTheSquad()
{
2023-08-14 10:13:31 -04:00
GameServer.DisbandPlayerCurrentSquad(this);
2023-07-27 20:24:00 -04:00
}
public void PromoteToSquadLeader()
{
2023-08-14 10:13:31 -04:00
GameServer.PromoteSquadLeader(this);
2023-07-27 20:24:00 -04:00
}
public void WarnPlayer(string msg)
{
2023-08-14 10:13:31 -04:00
GameServer.WarnPlayer(this, msg);
2023-07-27 20:24:00 -04:00
}
public void Message(string msg)
{
2023-08-14 10:13:31 -04:00
GameServer.MessageToPlayer(this, msg);
2023-07-27 20:24:00 -04:00
}
2023-08-14 05:43:58 -04:00
public void Message(string msg, float fadeoutTime)
{
2023-08-14 10:13:31 -04:00
GameServer.MessageToPlayer(this, msg, fadeoutTime);
2023-08-14 05:43:58 -04:00
}
2023-07-30 12:31:06 -04:00
public void SetNewRole(GameRole role)
{
2023-08-14 10:13:31 -04:00
GameServer.SetRoleTo(this, role);
2023-07-30 12:31:06 -04:00
}
2023-07-27 20:24:00 -04:00
public void Teleport(Vector3 target)
{
}
public void SpawnPlayer(PlayerLoadout loadout, PlayerWearings wearings, Vector3 position, Vector3 lookDirection, PlayerStand stand, float spawnProtection)
{
GameServer.SpawnPlayer(this, loadout, wearings, position, lookDirection, stand, spawnProtection);
}
public void SetHP(float newHP)
{
GameServer.SetHP(this, newHP);
}
public void GiveDamage(float damage)
{
GameServer.GiveDamage(this, damage);
}
public void Heal(float hp)
{
GameServer.Heal(this, hp);
}
2023-08-14 05:43:58 -04:00
public void SetPrimaryWeapon(WeaponItem item, int extraMagazines, bool clear = false)
{
GameServer.SetPrimaryWeapon(this, item, extraMagazines, clear);
}
public void SetSecondaryWeapon(WeaponItem item, int extraMagazines, bool clear = false)
{
GameServer.SetSecondaryWeapon(this, item, extraMagazines, clear);
}
public void SetFirstAidGadget(string item, int extra, bool clear = false)
{
GameServer.SetFirstAid(this, item, extra, clear);
}
public void SetLightGadget(string item, int extra, bool clear = false)
{
GameServer.SetLightGadget(this, item, extra, clear);
}
public void SetHeavyGadget(string item, int extra, bool clear = false)
{
GameServer.SetHeavyGadget(this, item, extra, clear);
}
public void SetThrowable(string item, int extra, bool clear = false)
{
GameServer.SetThrowable(this, item, extra, clear);
}
2023-08-14 05:43:58 -04:00
// ---- Static ----
2023-08-17 18:43:23 -04:00
public static void SetInstance(TPlayer player, Player<TPlayer>.Internal @internal)
2023-08-14 05:43:58 -04:00
{
player.mInternal = @internal;
}
// ---- Overrides ----
2023-07-27 20:24:00 -04:00
public override string ToString()
{
2023-08-14 10:13:31 -04:00
return Name + " (" + SteamID + ")";
2023-07-27 20:24:00 -04:00
}
2023-08-14 05:43:58 -04:00
// ---- Internal ----
public class Internal
{
public ulong SteamID;
public string Name;
public IPAddress IP;
public GameServer<TPlayer> GameServer;
public GameRole Role;
public Team Team;
2023-08-20 03:39:00 -04:00
public Squads SquadName;
2023-08-14 05:43:58 -04:00
public int PingMs = 999;
public bool IsAlive;
public float HP;
public Vector3 Position;
public PlayerStand Standing;
public LeaningSide Leaning;
public LoadoutIndex CurrentLoadoutIndex;
public bool InVehicle;
public bool IsBleeding;
public PlayerLoadout CurrentLoadout;
public PlayerWearings CurrentWearings;
2023-08-17 11:39:20 -04:00
public PlayerModifications<TPlayer>.mPlayerModifications _Modifications;
2023-08-14 10:13:31 -04:00
public PlayerModifications<TPlayer> Modifications;
public Internal()
{
2023-08-17 11:39:20 -04:00
this._Modifications = new PlayerModifications<TPlayer>.mPlayerModifications();
2023-08-14 10:13:31 -04:00
this.Modifications = new PlayerModifications<TPlayer>(this);
}
2023-08-14 05:43:58 -04:00
public void OnDie()
{
2023-08-14 10:13:31 -04:00
IsAlive = false;
HP = -1f;
Position = default;
Standing = PlayerStand.Standing;
Leaning = LeaningSide.None;
CurrentLoadoutIndex = LoadoutIndex.Primary;
InVehicle = false;
IsBleeding = false;
CurrentLoadout = new PlayerLoadout();
CurrentWearings = new PlayerWearings();
}
}
2023-07-27 20:24:00 -04:00
}
}