diff --git a/Assets/Scripts/Character.cs b/Assets/Scripts/Character.cs deleted file mode 100644 index b593c6c..0000000 --- a/Assets/Scripts/Character.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// A syncable component that all characters have. Controls the character's subsystems. -/// -public class Character : SyncBase { - /// - /// How fast the player should move in Unity's spatial units per second. - /// - public float MovementSpeed = 5.0f; - /// - /// The character controller, used to move the character. Handles collisions. - /// - public CharacterController CharacterController; - - private Vector3 MovementDirection = new Vector3(); - - /// - /// Moves the character in the given direction. - /// - /// Movement direction. - public void Move(Vector3 Direction) { - if (!Direction.Equals(MovementDirection)) { - MovementDirection = Direction.normalized; - } - } - - /// - /// Stops the player from moving. - /// - public void Stop() { - if (Moving()) { - MovementDirection = new Vector3(); - } - } - - /// - /// Whether the player is moving or not. - /// - public bool Moving() { - return MovementDirection.sqrMagnitude != 0; - } - - private void FixedUpdate() { - CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime); - } -} diff --git a/Assets/Scripts/Console/DebugConsole.cs b/Assets/Scripts/Console/DebugConsole.cs index f0a5b4f..a0433b2 100644 --- a/Assets/Scripts/Console/DebugConsole.cs +++ b/Assets/Scripts/Console/DebugConsole.cs @@ -4,151 +4,158 @@ using System.Text.RegularExpressions; using UnityEngine; using UnityEngine.UI; -/// -/// Controls an input and an output to implement a console interface to call -/// arbitrary commands which can be set from anywhere in the program through -/// . -/// -public class DebugConsole : MonoBehaviour { - private static readonly Regex CommandPartRegex = new Regex("([^ \"]+ )|(\"[^\"]+\")"); +namespace Cyber.Console { + + /// + /// Controls an input and an output to implement a console interface to call + /// arbitrary commands which can be set from anywhere in the program through + /// . + /// + public class DebugConsole : MonoBehaviour { + + private static readonly Regex CommandPartRegex = new Regex("([^ \"]+ )|(\"[^\"]+\")"); - /// - /// The parent of the and . - /// - public GameObject Panel; - /// - /// The input for the console. - /// - public InputField InputField; - /// - /// The output for the console. - /// - public Text TextField; - /// - /// Controls the visibility of the console. For how this is controlled in-game, see . - /// - public bool Visible = false; + /// + /// The parent of the and . + /// + public GameObject Panel; - private Dictionary Actions = new Dictionary(); + /// + /// The input for the console. + /// + public InputField InputField; - /// - /// Creates a new , and sets the 's singleton. - /// - /// - public DebugConsole() { - Term.SetDebugConsole(this); - } + /// + /// The output for the console. + /// + public Text TextField; - /// - /// Tries to call the command in the . - /// - public void CallCommand() { - if (InputField.text.Length == 0) { - return; + /// + /// Controls the visibility of the console. For how this is controlled in-game, see . + /// + public bool Visible = false; + + private Dictionary Actions = new Dictionary(); + + /// + /// Creates a new , and sets the 's singleton. + /// + /// + public DebugConsole() { + Term.SetDebugConsole(this); } - Println(InputField.text); - List Arguments = new List(); - MatchCollection Matches = CommandPartRegex.Matches(InputField.text + " "); - for (int i = 0; i < Matches.Count; i++) { - Arguments.Add(Matches[i].Value.Replace('"', ' ').Trim()); - } - foreach (string Action in Actions.Keys) { - string[] Parts = Action.Split(' '); - if (Arguments.Count == Parts.Length && Arguments[0].Equals(Parts[0])) { - Arguments.RemoveAt(0); - Actions[Action].Call(Arguments); - break; + + /// + /// Tries to call the command in the . + /// + public void CallCommand() { + if (InputField.text.Length == 0) { + return; } - } - InputField.text = ""; - InputField.ActivateInputField(); - } - - /// - /// Adds a command to be used in the console. - /// - /// The command template that should be used. - /// eg. "print (text)" or "add (number) (number)". - /// Description. - /// Action. - public void AddCommand(string command, string description, DebugConsoleAction.Action action) { - string PrettyDescription = command; - foreach (string Line in description.Split('\n')) { - PrettyDescription += "\n " + Line; - } - Actions[command] = new DebugConsoleAction(PrettyDescription, action); - } - - /// - /// Prints text into the DebugConsole and adds a newline. - /// - /// Text. - public void Println(string text) { - Print(text + "\n"); - } - - // TODO: Handle removing history when it gets very long. Very long console logs might cause problems when displaying new prints. - /// - /// Prints text into the Console. - /// - /// Text. - public void Print(string text) { - TextField.text += text; - } - - private void Start() { - AddCommand("help", "Lists all commands.", (args) => { - Println("Commands:"); - foreach (string Action in Actions.Keys) { - Println("- " + Action); + Println(InputField.text); + List Arguments = new List(); + MatchCollection Matches = CommandPartRegex.Matches(InputField.text + " "); + for (int i = 0; i < Matches.Count; i++) { + Arguments.Add(Matches[i].Value.Replace('"', ' ').Trim()); } - }); - - AddCommand("help (command)", "Describes the given command.", (args) => { - // Check complete versions of the names (so you can do eg. help "help (command)") - foreach (string Action in Actions.Keys) { - if (Action.Equals(args[0])) { - Println(Actions[Action].Description); - return; - } - } - // Check just names foreach (string Action in Actions.Keys) { string[] Parts = Action.Split(' '); - if (Parts[0].Equals(args[0])) { - Println(Actions[Action].Description); - return; + if (Arguments.Count == Parts.Length && Arguments[0].Equals(Parts[0])) { + Arguments.RemoveAt(0); + Actions[Action].Call(Arguments); + break; } } - Println("That command doesn't exist."); - }); - - AddCommand("print (text)", "Prints the given text.", (args) => { - Println(args[0]); - }); - } - - private void Update() { - if (Input.GetButtonDown("Console Toggle")) { - Visible = !Visible; + InputField.text = ""; + InputField.ActivateInputField(); } - RectTransform Rect = Panel.GetComponent(); - Vector2 OffsetMin = Rect.offsetMin; - if (Visible) { - if (OffsetMin.y > 1) { - OffsetMin.y = Mathf.Lerp(OffsetMin.y, 0, 5f * Time.deltaTime); - } - if (!InputField.isFocused) { - InputField.ActivateInputField(); - } - } else { - if (1000 - OffsetMin.y > 1) { - OffsetMin.y = Mathf.Lerp(OffsetMin.y, 1000, 1f * Time.deltaTime); - } - if (InputField.isFocused) { - InputField.DeactivateInputField(); + + /// + /// Adds a command to be used in the console. + /// + /// The command template that should be used. + /// eg. "print (text)" or "add (number) (number)". + /// Description. + /// Action. + public void AddCommand(string command, string description, DebugConsoleAction.Action action) { + string PrettyDescription = command; + foreach (string Line in description.Split('\n')) { + PrettyDescription += "\n " + Line; } + Actions[command] = new DebugConsoleAction(PrettyDescription, action); + } + + /// + /// Prints text into the DebugConsole and adds a newline. + /// + /// Text. + public void Println(string text) { + Print(text + "\n"); + } + + // TODO: Handle removing history when it gets very long. Very long console logs might cause problems when displaying new prints. + /// + /// Prints text into the Console. + /// + /// Text. + public void Print(string text) { + TextField.text += text; + } + + private void Start() { + AddCommand("help", "Lists all commands.", (args) => { + Println("Commands:"); + foreach (string Action in Actions.Keys) { + Println("- " + Action); + } + }); + + AddCommand("help (command)", "Describes the given command.", (args) => { + // Check complete versions of the names (so you can do eg. help "help (command)") + foreach (string Action in Actions.Keys) { + if (Action.Equals(args[0])) { + Println(Actions[Action].Description); + return; + } + } + // Check just names + foreach (string Action in Actions.Keys) { + string[] Parts = Action.Split(' '); + if (Parts[0].Equals(args[0])) { + Println(Actions[Action].Description); + return; + } + } + Println("That command doesn't exist."); + }); + + AddCommand("print (text)", "Prints the given text.", (args) => { + Println(args[0]); + }); + } + + private void Update() { + if (Input.GetButtonDown("Console Toggle")) { + Visible = !Visible; + } + RectTransform Rect = Panel.GetComponent(); + Vector2 OffsetMin = Rect.offsetMin; + if (Visible) { + if (OffsetMin.y > 1) { + OffsetMin.y = Mathf.Lerp(OffsetMin.y, 0, 5f * Time.deltaTime); + } + if (!InputField.isFocused) { + InputField.ActivateInputField(); + } + } else { + if (1000 - OffsetMin.y > 1) { + OffsetMin.y = Mathf.Lerp(OffsetMin.y, 1000, 1f * Time.deltaTime); + } + if (InputField.isFocused) { + InputField.DeactivateInputField(); + } + } + Rect.offsetMin = OffsetMin; } - Rect.offsetMin = OffsetMin; } } diff --git a/Assets/Scripts/Console/DebugConsoleAction.cs b/Assets/Scripts/Console/DebugConsoleAction.cs index 08e5716..f2e55f6 100644 --- a/Assets/Scripts/Console/DebugConsoleAction.cs +++ b/Assets/Scripts/Console/DebugConsoleAction.cs @@ -1,36 +1,41 @@ using System.Collections.Generic; -/// -/// Class that defines an action that commands in the -/// might use. -/// -public class DebugConsoleAction { +namespace Cyber.Console { + /// - /// A delegate for all of the actions that commands do. + /// Class that defines an action that commands in the + /// might use. /// - public delegate void Action(List command); - /// - /// A description that will be shown when using the "help (command)" command in the console. - /// - public readonly string Description; + public class DebugConsoleAction { + + /// + /// A delegate for all of the actions that commands do. + /// + public delegate void Action(List command); - private Action ActionFunc; + /// + /// A description that will be shown when using the "help (command)" command in the console. + /// + public readonly string Description; - /// - /// Initializes a new instance of the class. - /// - /// Description. - /// Action func. - public DebugConsoleAction(string description, Action actionFunc) { - this.Description = description; - this.ActionFunc = actionFunc; - } + private Action ActionFunc; - /// - /// Executes the . - /// - /// Command. - public void Call(List command) { - ActionFunc(command); + /// + /// Initializes a new instance of the class. + /// + /// Description. + /// Action func. + public DebugConsoleAction(string description, Action actionFunc) { + this.Description = description; + this.ActionFunc = actionFunc; + } + + /// + /// Executes the . + /// + /// Command. + public void Call(List command) { + ActionFunc(command); + } } } diff --git a/Assets/Scripts/Console/Term.cs b/Assets/Scripts/Console/Term.cs index e4b3f98..6d9195a 100644 --- a/Assets/Scripts/Console/Term.cs +++ b/Assets/Scripts/Console/Term.cs @@ -2,65 +2,69 @@ using System.Collections.Generic; using UnityEngine; -/// -/// A class that has static functions for printing text in the DebugConsole. -/// -public class Term { - private static DebugConsole Console; - +namespace Cyber.Console { + /// - /// Sets the singleton that will be used in other static functions. + /// A class that has static functions for printing text in the DebugConsole. /// - /// Console. - public static void SetDebugConsole(DebugConsole console) { - Console = console; - } + public class Term { + + private static DebugConsole Console; - /// - /// Returns whether or not the DebugConsole is currently on the screen, ready to be used. - /// - /// true if is visible; otherwise, false. - public static bool IsVisible() { - if (Console == null) { - return false; - } else { - return Console.Visible; + /// + /// Sets the singleton that will be used in other static functions. + /// + /// Console. + public static void SetDebugConsole(DebugConsole console) { + Console = console; } - } - /// - /// See . - /// - /// Text. - public static void Println(string text) { - if (Console == null) { - Debug.Log(text); - } else { - Console.Println(text); + /// + /// Returns whether or not the DebugConsole is currently on the screen, ready to be used. + /// + /// true if is visible; otherwise, false. + public static bool IsVisible() { + if (Console == null) { + return false; + } else { + return Console.Visible; + } } - } - /// - /// See . - /// - /// Text. - public static void Print(string text) { - if (Console == null) { - Debug.Log(text); - } else { - Console.Print(text); + /// + /// See . + /// + /// Text. + public static void Println(string text) { + if (Console == null) { + Debug.Log(text); + } else { + Console.Println(text); + } } - } - /// - /// See - /// - /// Command. - /// Description. - /// Action. - public static void AddCommand(string command, string description, DebugConsoleAction.Action action) { - if (Console != null) { - Console.AddCommand(command, description, action); + /// + /// See . + /// + /// Text. + public static void Print(string text) { + if (Console == null) { + Debug.Log(text); + } else { + Console.Print(text); + } + } + + /// + /// See + /// + /// Command. + /// Description. + /// Action. + public static void AddCommand(string command, string description, DebugConsoleAction.Action action) { + if (Console != null) { + Console.AddCommand(command, description, action); + } } } } diff --git a/Assets/Scripts/Controls.meta b/Assets/Scripts/Controls.meta new file mode 100644 index 0000000..d8f0b4a --- /dev/null +++ b/Assets/Scripts/Controls.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: ed8f719cfc1ca409787d1cbcb89e6fca +folderAsset: yes +timeCreated: 1494276190 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs new file mode 100644 index 0000000..ec991c5 --- /dev/null +++ b/Assets/Scripts/Controls/PlayerController.cs @@ -0,0 +1,35 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Cyber.Entities; +using Cyber.Console; + +namespace Cyber.Controls { + + /// + /// Controls the player character. Shouldn't exist on the server, and only one + /// should exist per client (the character that client is controlling). + /// + public class PlayerController : MonoBehaviour { + + /// + /// The character this controller should control. + /// + public Character Character; + + private void Update() { + if (!Term.IsVisible()) { + // Handle inputs + Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); + if (Move.sqrMagnitude != 0) { + Character.Move(transform.TransformDirection(Move)); + } else if (Character.Moving()) { + Character.Stop(); + } + } else if (Character.Moving()) { + // The debug console is open, stop the player. + Character.Stop(); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs.meta b/Assets/Scripts/Controls/PlayerController.cs.meta similarity index 100% rename from Assets/Scripts/PlayerController.cs.meta rename to Assets/Scripts/Controls/PlayerController.cs.meta diff --git a/Assets/Scripts/Entities.meta b/Assets/Scripts/Entities.meta new file mode 100644 index 0000000..32d1867 --- /dev/null +++ b/Assets/Scripts/Entities.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: c0ce6f05a0f994c00be318d1738f7749 +folderAsset: yes +timeCreated: 1494276297 +licenseType: Free +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Entities/Character.cs b/Assets/Scripts/Entities/Character.cs new file mode 100644 index 0000000..1fc2dba --- /dev/null +++ b/Assets/Scripts/Entities/Character.cs @@ -0,0 +1,54 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Entities { + + /// + /// A syncable component that all characters have. Controls the character's subsystems. + /// + public class Character : SyncBase { + + /// + /// How fast the player should move in Unity's spatial units per second. + /// + public float MovementSpeed = 5.0f; + + /// + /// The character controller, used to move the character. Handles collisions. + /// + public CharacterController CharacterController; + + private Vector3 MovementDirection = new Vector3(); + + /// + /// Moves the character in the given direction. + /// + /// Movement direction. + public void Move(Vector3 Direction) { + if (!Direction.Equals(MovementDirection)) { + MovementDirection = Direction.normalized; + } + } + + /// + /// Stops the player from moving. + /// + public void Stop() { + if (Moving()) { + MovementDirection = new Vector3(); + } + } + + /// + /// Whether the player is moving or not. + /// + public bool Moving() { + return MovementDirection.sqrMagnitude != 0; + } + + private void FixedUpdate() { + CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime); + } + } +} diff --git a/Assets/Scripts/Character.cs.meta b/Assets/Scripts/Entities/Character.cs.meta similarity index 100% rename from Assets/Scripts/Character.cs.meta rename to Assets/Scripts/Entities/Character.cs.meta diff --git a/Assets/Scripts/Entities/EntityType.cs b/Assets/Scripts/Entities/EntityType.cs new file mode 100644 index 0000000..11afe79 --- /dev/null +++ b/Assets/Scripts/Entities/EntityType.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Entities { + + /// + /// Types of entities. Every entity type has its own prefab, see + /// . + /// + public enum EntityType { + + /// + /// Player character. + /// + PC, + + /// + /// Non player character. + /// + NPC + + } +} \ No newline at end of file diff --git a/Assets/Scripts/EntityType.cs.meta b/Assets/Scripts/Entities/EntityType.cs.meta similarity index 100% rename from Assets/Scripts/EntityType.cs.meta rename to Assets/Scripts/Entities/EntityType.cs.meta diff --git a/Assets/Scripts/Entities/Spawner.cs b/Assets/Scripts/Entities/Spawner.cs new file mode 100644 index 0000000..fffa743 --- /dev/null +++ b/Assets/Scripts/Entities/Spawner.cs @@ -0,0 +1,69 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Cyber.Console; + +namespace Cyber.Entities { + + /// + /// A utility class to spawn entities into the world based on + /// their and . + /// + public class Spawner : MonoBehaviour { + + /// + /// The this should be using to + /// set entities' IDs. + /// + public SyncDB SyncDB; + + /// + /// The prefab. + /// + public GameObject PCEntityPrefab; + + /// + /// The prefab. + /// + public GameObject NPCEntityPrefab; + + /// + /// Spawns an entity and returns that entity. + /// + /// Type. + /// Position. + /// The ids of the entity's synced components. Should be + /// set if they exist already (eg. the server has sent them over). These + /// ids should be from . To create new + /// ids, leave as the default (null). + public GameObject Spawn(EntityType type, Vector3 position, uint[] ids = null) { + GameObject Spawned = null; + switch (type) { + case EntityType.PC: + Spawned = Instantiate(PCEntityPrefab, position, new Quaternion(), transform); + break; + case EntityType.NPC: + Spawned = Instantiate(NPCEntityPrefab, position, new Quaternion(), transform); + break; + } + if (Spawned != null) { + if (ids != null) { + SyncDB.AddEntity(Spawned, ids); + } else { + SyncDB.AddEntity(Spawned, SyncDB.GetNewEntityIDs(Spawned)); + } + } + return Spawned; + } + + private void Start() { + Spawn(EntityType.PC, new Vector3()); + } + + private void Update() { + if (Input.GetButtonDown("Jump") && !Term.IsVisible()) { + Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f))); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Spawner.cs.meta b/Assets/Scripts/Entities/Spawner.cs.meta similarity index 100% rename from Assets/Scripts/Spawner.cs.meta rename to Assets/Scripts/Entities/Spawner.cs.meta diff --git a/Assets/Scripts/Entities/SyncBase.cs b/Assets/Scripts/Entities/SyncBase.cs new file mode 100644 index 0000000..b541091 --- /dev/null +++ b/Assets/Scripts/Entities/SyncBase.cs @@ -0,0 +1,19 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Entities { + + /// + /// A base class for all syncable components. An instance of + /// will contain all of the game's synced components. + /// + public class SyncBase : MonoBehaviour { + + /// + /// The ID this syncable component can be found with from its parent + /// . + /// + public uint ID; + } +} \ No newline at end of file diff --git a/Assets/Scripts/SyncBase.cs.meta b/Assets/Scripts/Entities/SyncBase.cs.meta similarity index 100% rename from Assets/Scripts/SyncBase.cs.meta rename to Assets/Scripts/Entities/SyncBase.cs.meta diff --git a/Assets/Scripts/Entities/SyncDB.cs b/Assets/Scripts/Entities/SyncDB.cs new file mode 100644 index 0000000..9685026 --- /dev/null +++ b/Assets/Scripts/Entities/SyncDB.cs @@ -0,0 +1,108 @@ +using System.Collections; +using System.Collections.Generic; +using System; +using UnityEngine; + +namespace Cyber.Entities { + + /// + /// A database of the game's all syncable components. Syncable components are + /// the instances of the subclasses of . + /// + public class SyncDB : MonoBehaviour { + + private static readonly Type[] SyncableClasses = new Type[] { + typeof(Character) + }; + + private uint IDCounter = 0; + private Dictionary Database = new Dictionary(); + + /// + /// Add an entity to the database with the given IDs. + /// + /// Game object. + /// The IDs. Should be from or + /// , since the order is important. + public void AddEntity(GameObject gameObject, uint[] ids) { + int Index = 0; + for (int i = 0; i < SyncableClasses.Length; i++) { + SyncBase Syncable = (SyncBase)gameObject.GetComponent(SyncableClasses[i]); + if (Syncable != null) { + Syncable.ID = ids[Index]; + Database[ids[Index++]] = Syncable; + } + } + } + + /// + /// Makes an ordered list of the given gameobject's syncable components' + /// IDs. + /// + /// The IDs. + /// Game object. + /// Whether or not new IDs are created. + /// is a shorthand for this function with + /// this parameter set to true. + public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) { + List IDs = new List(); + for (int i = 0; i < SyncableClasses.Length; i++) { + SyncBase Syncable = (SyncBase)gameObject.GetComponent(SyncableClasses[i]); + if (Syncable != null) { + if (newIDs) { + Syncable.ID = CreateID(); + } + IDs.Add(Syncable.ID); + } + } + uint[] IDArray = new uint[IDs.Count]; + for (int i = 0; i < IDs.Count; i++) { + IDArray[i] = IDs[i]; + } + return IDArray; + } + + /// + /// Creates an ordered list of the given gameobject's syncable components' + /// IDs. See for more information. + /// + /// The new IDs. + /// Game object. + public uint[] GetNewEntityIDs(GameObject gameObject) { + return GetEntityIDs(gameObject, true); + } + + /// + /// Get a synced component by its ID. + /// + /// The ID. + public SyncBase Get(uint id) { + return Database[id]; + } + + /// + /// Creates a new ID which isn't in use yet. + /// + /// A new, free ID. + public uint CreateID() { + uint ID; + try { + ID = IDCounter++; + } catch (OverflowException) { + ID = 0; + IDCounter = 1; + } + while (Database.ContainsKey(ID) && ID < uint.MaxValue) { + ID++; + if (ID < uint.MaxValue - 1) + IDCounter = ID + 1; + } + if (Database.ContainsKey(ID)) { + // Somehow we've managed to fill up the whole database. + // I can't even imagine why or how. + Debug.LogError("!!!WARNING!!! The SyncDB is full. Update the game to use longs instead of uints. !!!WARNING!!!"); + } + return ID; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/SyncDB.cs.meta b/Assets/Scripts/Entities/SyncDB.cs.meta similarity index 100% rename from Assets/Scripts/SyncDB.cs.meta rename to Assets/Scripts/Entities/SyncDB.cs.meta diff --git a/Assets/Scripts/EntityType.cs b/Assets/Scripts/EntityType.cs deleted file mode 100644 index 068a31a..0000000 --- a/Assets/Scripts/EntityType.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// Types of entities. Every entity type has its own prefab, see -/// . -/// -public enum EntityType { - /// - /// Player character. - /// - PC, - /// - /// Non player character. - /// - NPC -} diff --git a/Assets/Scripts/Networking/Client.cs b/Assets/Scripts/Networking/Client.cs index 8201b4b..7c44c06 100644 --- a/Assets/Scripts/Networking/Client.cs +++ b/Assets/Scripts/Networking/Client.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; +using Cyber.Console; /// /// Client-class used to connecting to a server and communicating to it. diff --git a/Assets/Scripts/Networking/NetworkEstablisher.cs b/Assets/Scripts/Networking/NetworkEstablisher.cs index bc39ebb..042b207 100644 --- a/Assets/Scripts/Networking/NetworkEstablisher.cs +++ b/Assets/Scripts/Networking/NetworkEstablisher.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; +using Cyber.Console; /// /// This class is used pretty much anywhere in order to make the "first step" of networking. diff --git a/Assets/Scripts/Networking/Server.cs b/Assets/Scripts/Networking/Server.cs index cf3d7a5..18b4eec 100644 --- a/Assets/Scripts/Networking/Server.cs +++ b/Assets/Scripts/Networking/Server.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; +using Cyber.Console; /// /// Server-class used to host a server and communicate to clients. diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs deleted file mode 100644 index 1127e33..0000000 --- a/Assets/Scripts/PlayerController.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// Controls the player character. Shouldn't exist on the server, and only one -/// should exist per client (the character that client is controlling). -/// -public class PlayerController : MonoBehaviour { - /// - /// The character this controller should control. - /// - public Character Character; - - private void Update() { - if (!Term.IsVisible()) { - // Handle inputs - Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); - if (Move.sqrMagnitude != 0) { - Character.Move(transform.TransformDirection(Move)); - } else if (Character.Moving()) { - Character.Stop(); - } - } else if (Character.Moving()) { - // The debug console is open, stop the player. - Character.Stop(); - } - } -} diff --git a/Assets/Scripts/Spawner.cs b/Assets/Scripts/Spawner.cs deleted file mode 100644 index 154cd6a..0000000 --- a/Assets/Scripts/Spawner.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// A utility class to spawn entities into the world based on -/// their and . -/// -public class Spawner : MonoBehaviour { - /// - /// The this should be using to - /// set entities' IDs. - /// - public SyncDB SyncDB; - /// - /// The prefab. - /// - public GameObject PCEntityPrefab; - /// - /// The prefab. - /// - public GameObject NPCEntityPrefab; - - /// - /// Spawns an entity and returns that entity. - /// - /// Type. - /// Position. - /// The ids of the entity's synced components. Should be - /// set if they exist already (eg. the server has sent them over). These - /// ids should be from . To create new - /// ids, leave as the default (null). - public GameObject Spawn(EntityType type, Vector3 position, uint[] ids = null) { - GameObject Spawned = null; - switch (type) { - case EntityType.PC: - Spawned = Instantiate(PCEntityPrefab, position, new Quaternion(), transform); - break; - case EntityType.NPC: - Spawned = Instantiate(NPCEntityPrefab, position, new Quaternion(), transform); - break; - } - if (Spawned != null) { - if (ids != null) { - SyncDB.AddEntity(Spawned, ids); - } else { - SyncDB.AddEntity(Spawned, SyncDB.GetNewEntityIDs(Spawned)); - } - } - return Spawned; - } - - private void Start() { - Spawn(EntityType.PC, new Vector3()); - } - - private void Update() { - if (Input.GetButtonDown("Jump") && !Term.IsVisible()) { - Spawn(EntityType.NPC, new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-2f, 2f))); - } - } -} diff --git a/Assets/Scripts/SyncBase.cs b/Assets/Scripts/SyncBase.cs deleted file mode 100644 index b18bea7..0000000 --- a/Assets/Scripts/SyncBase.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// A base class for all syncable components. An instance of -/// will contain all of the game's synced components. -/// -public class SyncBase : MonoBehaviour { - /// - /// The ID this syncable component can be found with from its parent - /// . - /// - public uint ID; -} diff --git a/Assets/Scripts/SyncDB.cs b/Assets/Scripts/SyncDB.cs deleted file mode 100644 index 8223878..0000000 --- a/Assets/Scripts/SyncDB.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System; -using UnityEngine; - -/// -/// A database of the game's all syncable components. Syncable components are -/// the instances of the subclasses of . -/// -public class SyncDB : MonoBehaviour { - private static readonly Type[] SyncableClasses = new Type[]{ - typeof(Character) - }; - - private uint IDCounter = 0; - private Dictionary Database = new Dictionary(); - - /// - /// Add an entity to the database with the given IDs. - /// - /// Game object. - /// The IDs. Should be from or - /// , since the order is important. - public void AddEntity(GameObject gameObject, uint[] ids) { - int Index = 0; - for (int i = 0; i < SyncableClasses.Length; i++) { - SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); - if (Syncable != null) { - Syncable.ID = ids[Index]; - Database[ids[Index++]] = Syncable; - } - } - } - - /// - /// Makes an ordered list of the given gameobject's syncable components' - /// IDs. - /// - /// The IDs. - /// Game object. - /// Whether or not new IDs are created. - /// is a shorthand for this function with - /// this parameter set to true. - public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) { - List IDs = new List(); - for (int i = 0; i < SyncableClasses.Length; i++) { - SyncBase Syncable = (SyncBase) gameObject.GetComponent(SyncableClasses[i]); - if (Syncable != null) { - if (newIDs) { - Syncable.ID = CreateID(); - } - IDs.Add(Syncable.ID); - } - } - uint[] IDArray = new uint[IDs.Count]; - for (int i = 0; i < IDs.Count; i++) { - IDArray[i] = IDs[i]; - } - return IDArray; - } - - /// - /// Creates an ordered list of the given gameobject's syncable components' - /// IDs. See for more information. - /// - /// The new IDs. - /// Game object. - public uint[] GetNewEntityIDs(GameObject gameObject) { - return GetEntityIDs(gameObject, true); - } - - /// - /// Get a synced component by its ID. - /// - /// The ID. - public SyncBase Get(uint id) { - return Database[id]; - } - - /// - /// Creates a new ID which isn't in use yet. - /// - /// A new, free ID. - public uint CreateID() { - uint ID; - try { - ID = IDCounter++; - } catch (OverflowException) { - ID = 0; - IDCounter = 1; - } - while (Database.ContainsKey(ID) && ID < uint.MaxValue) { - ID++; - if (ID < uint.MaxValue - 1) IDCounter = ID + 1; - } - if (Database.ContainsKey(ID)) { - // Somehow we've managed to fill up the whole database. - // I can't even imagine why or how. - Debug.LogError("!!!WARNING!!! The SyncDB is full. Update the game to use longs instead of uints. !!!WARNING!!!"); - } - return ID; - } -}