|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using System;
-
- using Unity.Mathematics;
- using RobocraftX.Common;
-
- using GamecraftModdingAPI.Players;
-
- namespace GamecraftModdingAPI
- {
- /// <summary>
- /// An in-game player character. Any Leo you see is a player.
- /// </summary>
- public class Player
- {
- // static functionality
- private static PlayerEngine playerEngine = new PlayerEngine();
-
- /// <summary>
- /// Checks if the specified player exists.
- /// </summary>
- /// <returns>Whether the player exists.</returns>
- /// <param name="player">Player type.</param>
- public static bool Exists(PlayerType player)
- {
- switch (player)
- {
- case PlayerType.Remote:
- return playerEngine.GetRemotePlayer() != uint.MaxValue;
- case PlayerType.Local:
- return playerEngine.GetLocalPlayer() != uint.MaxValue;
- }
- return false;
- }
-
- /// <summary>
- /// Checks if the specified player exists.
- /// </summary>
- /// <returns>Whether the player exists.</returns>
- /// <param name="player">The player's unique identifier.</param>
- public static bool Exists(uint player)
- {
- return playerEngine.ExistsById(player);
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
- /// </summary>
- /// <param name="id">The player's unique identifier.</param>
- public Player(uint id)
- {
- this.Id = id;
- if (!Exists(id))
- {
- throw new PlayerNotFoundException($"No player with id {id} exists");
- }
- this.Type = playerEngine.GetLocalPlayer() == id ? PlayerType.Local : PlayerType.Remote;
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="T:GamecraftModdingAPI.Player"/> class.
- /// </summary>
- /// <param name="player">The player type. Chooses the first available player matching the criteria.</param>
- public Player(PlayerType player)
- {
- uint localId = playerEngine.GetLocalPlayer();
- switch (player)
- {
- case PlayerType.Local:
- this.Id = playerEngine.GetLocalPlayer();
- break;
- case PlayerType.Remote:
- this.Id = playerEngine.GetRemotePlayer();
- break;
- }
- if (this.Id == uint.MaxValue)
- {
- throw new PlayerNotFoundException($"No player of {player} type exists");
- }
- this.Type = player;
- }
-
- // object fields & properties
-
- /// <summary>
- /// The player's type.
- /// The player type is always relative to the current client, not the game host.
- /// </summary>
- /// <value>The enumerated player type.</value>
- public PlayerType Type { get; }
-
- /// <summary>
- /// The player's unique identifier.
- /// </summary>
- /// <value>The identifier.</value>
- public uint Id { get; private set; }
-
- /// <summary>
- /// The player's current position.
- /// </summary>
- /// <value>The position.</value>
- public float3 Position
- {
- get
- {
- return playerEngine.GetLocation(Id);
- }
-
- set
- {
- playerEngine.SetLocation(Id, value, false);
- }
- }
-
- /// <summary>
- /// The player's current rotation.
- /// </summary>
- /// <value>The rotation.</value>
- public quaternion Rotation
- {
- get
- {
- return playerEngine.GetRotation(Id);
- }
-
- set
- {
- playerEngine.SetRotation(Id, value);
- }
- }
-
- /// <summary>
- /// The player's current velocity.
- /// </summary>
- /// <value>The velocity.</value>
- public float3 Velocity
- {
- get
- {
- return playerEngine.GetLinearVelocity(Id);
- }
-
- set
- {
- playerEngine.SetLinearVelocity(Id, value);
- }
- }
-
- /// <summary>
- /// The player's current angular velocity.
- /// </summary>
- /// <value>The angular velocity.</value>
- public float3 AngularVelocity
- {
- get
- {
- return playerEngine.GetAngularVelocity(Id);
- }
-
- set
- {
- playerEngine.SetAngularVelocity(Id, value);
- }
- }
-
- /// <summary>
- /// The player's mass.
- /// </summary>
- /// <value>The mass.</value>
- public float Mass
- {
- get
- {
- return 1f / playerEngine.GetMass(Id).InverseMass;
- }
-
- // FIXME: Setting mass doesn't do anything
- /*set
- {
- playerEngine.SetInverseMass(Id, 1f / value);
- }*/
- }
-
- private float _ping = -1f;
-
- /// <summary>
- /// The player's latest network ping time.
- /// </summary>
- /// <value>The ping (s).</value>
- public float Ping
- {
- get
- {
- float? temp = playerEngine.GetLastPingTime(Id, Type);
- if (temp.HasValue)
- {
- _ping = temp.Value;
- }
- return _ping;
- }
- }
-
- // object methods
-
- /// <summary>
- /// Teleport the player to the specified coordinates.
- /// </summary>
- /// <param name="x">The x coordinate.</param>
- /// <param name="y">The y coordinate.</param>
- /// <param name="z">The z coordinate.</param>
- /// <param name="relative">If set to <c>true</c> teleport relative to the player's current position.</param>
- /// <param name="exitSeat">If set to <c>true</c> exit any seat the player is in.</param>
- public void Teleport(float x, float y, float z, bool relative = true, bool exitSeat = true)
- {
- float3 location = new float3(x, y, z);
- if (relative)
- {
- location += playerEngine.GetLocation(Id);
- }
- playerEngine.SetLocation(Id, location, exitSeat: exitSeat);
- }
-
- /// <summary>
- /// Returns the block the player is currently looking at.
- /// </summary>
- /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
- /// <returns>The block or null if not found</returns>
- public Block GetBlockLookedAt(float maxDistance = -1f)
- {
- var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
- return egid.HasValue && egid.Value.groupID == CommonExclusiveGroups.OWNED_BLOCKS_GROUP
- ? new Block(egid.Value)
- : null;
- }
-
- /// <summary>
- /// Returns the rigid body the player is currently looking at during simulation.
- /// </summary>
- /// <param name="maxDistance">The maximum distance from the player (default is the player's building reach)</param>
- /// <returns>The block or null if not found</returns>
- public SimBody GetSimBodyLookedAt(float maxDistance = -1f)
- {
- var egid = playerEngine.GetThingLookedAt(Id, maxDistance);
- return egid.HasValue && egid.Value.groupID == CommonExclusiveGroups.SIMULATION_BODIES_GROUP
- ? new SimBody(egid.Value)
- : null;
- }
-
- // internal methods
-
- public static void Init()
- {
- Utility.GameEngineManager.AddGameEngine(playerEngine);
- }
- }
- }
|