This commit is contained in:
Sofia 2017-05-09 00:14:30 +03:00
commit 7134c406f3
26 changed files with 555 additions and 487 deletions

View File

@ -1,49 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A syncable component that all characters have. Controls the character's subsystems.
/// </summary>
public class Character : SyncBase {
/// <summary>
/// How fast the player should move in Unity's spatial units per second.
/// </summary>
public float MovementSpeed = 5.0f;
/// <summary>
/// The character controller, used to move the character. Handles collisions.
/// </summary>
public CharacterController CharacterController;
private Vector3 MovementDirection = new Vector3();
/// <summary>
/// Moves the character in the given direction.
/// </summary>
/// <param name="Direction">Movement direction.</param>
public void Move(Vector3 Direction) {
if (!Direction.Equals(MovementDirection)) {
MovementDirection = Direction.normalized;
}
}
/// <summary>
/// Stops the player from moving.
/// </summary>
public void Stop() {
if (Moving()) {
MovementDirection = new Vector3();
}
}
/// <summary>
/// Whether the player is moving or not.
/// </summary>
public bool Moving() {
return MovementDirection.sqrMagnitude != 0;
}
private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
}
}

View File

@ -4,26 +4,32 @@ using System.Text.RegularExpressions;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
/// <summary> 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 /// <summary>
/// <see cref="Term"/>. /// Controls an input and an output to implement a console interface to call
/// </summary> /// arbitrary commands which can be set from anywhere in the program through
public class DebugConsole : MonoBehaviour { /// <see cref="Term"/>.
/// </summary>
public class DebugConsole : MonoBehaviour {
private static readonly Regex CommandPartRegex = new Regex("([^ \"]+ )|(\"[^\"]+\")"); private static readonly Regex CommandPartRegex = new Regex("([^ \"]+ )|(\"[^\"]+\")");
/// <summary> /// <summary>
/// The parent of the <see cref="InputField"/> and <see cref="TextField"/>. /// The parent of the <see cref="InputField"/> and <see cref="TextField"/>.
/// </summary> /// </summary>
public GameObject Panel; public GameObject Panel;
/// <summary> /// <summary>
/// The input for the console. /// The input for the console.
/// </summary> /// </summary>
public InputField InputField; public InputField InputField;
/// <summary> /// <summary>
/// The output for the console. /// The output for the console.
/// </summary> /// </summary>
public Text TextField; public Text TextField;
/// <summary> /// <summary>
/// Controls the visibility of the console. For how this is controlled in-game, see <see cref="Update"/>. /// Controls the visibility of the console. For how this is controlled in-game, see <see cref="Update"/>.
/// </summary> /// </summary>
@ -151,4 +157,5 @@ public class DebugConsole : MonoBehaviour {
} }
Rect.offsetMin = OffsetMin; Rect.offsetMin = OffsetMin;
} }
}
} }

View File

@ -1,14 +1,18 @@
using System.Collections.Generic; using System.Collections.Generic;
/// <summary> namespace Cyber.Console {
/// Class that defines an action that commands in the
/// <see cref="DebugConsole"/> might use. /// <summary>
/// </summary> /// Class that defines an action that commands in the
public class DebugConsoleAction { /// <see cref="DebugConsole"/> might use.
/// </summary>
public class DebugConsoleAction {
/// <summary> /// <summary>
/// A delegate for all of the actions that commands do. /// A delegate for all of the actions that commands do.
/// </summary> /// </summary>
public delegate void Action(List<string> command); public delegate void Action(List<string> command);
/// <summary> /// <summary>
/// A description that will be shown when using the "help (command)" command in the console. /// A description that will be shown when using the "help (command)" command in the console.
/// </summary> /// </summary>
@ -33,4 +37,5 @@ public class DebugConsoleAction {
public void Call(List<string> command) { public void Call(List<string> command) {
ActionFunc(command); ActionFunc(command);
} }
}
} }

View File

@ -2,10 +2,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary> namespace Cyber.Console {
/// A class that has static functions for printing text in the DebugConsole.
/// </summary> /// <summary>
public class Term { /// A class that has static functions for printing text in the DebugConsole.
/// </summary>
public class Term {
private static DebugConsole Console; private static DebugConsole Console;
/// <summary> /// <summary>
@ -63,4 +66,5 @@ public class Term {
Console.AddCommand(command, description, action); Console.AddCommand(command, description, action);
} }
} }
}
} }

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: ed8f719cfc1ca409787d1cbcb89e6fca
folderAsset: yes
timeCreated: 1494276190
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cyber.Entities;
using Cyber.Console;
namespace Cyber.Controls {
/// <summary>
/// Controls the player character. Shouldn't exist on the server, and only one
/// should exist per client (the character that client is controlling).
/// </summary>
public class PlayerController : MonoBehaviour {
/// <summary>
/// The character this controller should control.
/// </summary>
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();
}
}
}
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: c0ce6f05a0f994c00be318d1738f7749
folderAsset: yes
timeCreated: 1494276297
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,52 @@
using UnityEngine;
namespace Cyber.Entities {
/// <summary>
/// A syncable component that all characters have. Controls the character's subsystems.
/// </summary>
public class Character : SyncBase {
/// <summary>
/// How fast the player should move in Unity's spatial units per second.
/// </summary>
public float MovementSpeed = 5.0f;
/// <summary>
/// The character controller, used to move the character. Handles collisions.
/// </summary>
public CharacterController CharacterController;
private Vector3 MovementDirection = new Vector3();
/// <summary>
/// Moves the character in the given direction.
/// </summary>
/// <param name="Direction">Movement direction.</param>
public void Move(Vector3 Direction) {
if (!Direction.Equals(MovementDirection)) {
MovementDirection = Direction.normalized;
}
}
/// <summary>
/// Stops the player from moving.
/// </summary>
public void Stop() {
if (Moving()) {
MovementDirection = new Vector3();
}
}
/// <summary>
/// Whether the player is moving or not.
/// </summary>
public bool Moving() {
return MovementDirection.sqrMagnitude != 0;
}
private void FixedUpdate() {
CharacterController.Move(MovementDirection * MovementSpeed * Time.fixedDeltaTime);
}
}
}

View File

@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cyber.Entities {
/// <summary>
/// Types of entities. Every entity type has its own prefab, see
/// <see cref="Spawner"/>.
/// </summary>
public enum EntityType {
/// <summary>
/// Player character.
/// </summary>
PC,
/// <summary>
/// Non player character.
/// </summary>
NPC
}
}

View File

@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cyber.Console;
namespace Cyber.Entities {
/// <summary>
/// A utility class to spawn entities into the world based on
/// their <see cref="EntityType"/> and .
/// </summary>
public class Spawner : MonoBehaviour {
/// <summary>
/// The <see cref="SyncDB"/> this <see cref="Spawner"/> should be using to
/// set entities' IDs.
/// </summary>
public SyncDB SyncDB;
/// <summary>
/// The <see cref="EntityType.PC"/> prefab.
/// </summary>
public GameObject PCEntityPrefab;
/// <summary>
/// The <see cref="EntityType.NPC"/> prefab.
/// </summary>
public GameObject NPCEntityPrefab;
/// <summary>
/// Spawns an entity and returns that entity.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="position">Position.</param>
/// <param name="ids">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 <see cref="SyncDB.GetEntityIDs"/>. To create new
/// ids, leave as the default (null).</param>
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)));
}
}
}
}

View File

@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cyber.Entities {
/// <summary>
/// A base class for all syncable components. An instance of
/// <see cref="SyncDB"/> will contain all of the game's synced components.
/// </summary>
public class SyncBase : MonoBehaviour {
/// <summary>
/// The ID this syncable component can be found with from its parent
/// <see cref="SyncDB"/>.
/// </summary>
public uint ID;
}
}

View File

@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
namespace Cyber.Entities {
/// <summary>
/// A database of the game's all syncable components. Syncable components are
/// the instances of the subclasses of <see cref="SyncBase"/>.
/// </summary>
public class SyncDB : MonoBehaviour {
private static readonly Type[] SyncableClasses = new Type[] {
typeof(Character)
};
private uint IDCounter = 0;
private Dictionary<uint, SyncBase> Database = new Dictionary<uint, SyncBase>();
/// <summary>
/// Add an entity to the database with the given IDs.
/// </summary>
/// <param name="gameObject">Game object.</param>
/// <param name="ids">The IDs. Should be from <see cref="GetEntityIDs"/> or
/// <see cref="GetNewEntityIDs"/>, since the order is important.</param>
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;
}
}
}
/// <summary>
/// Makes an ordered list of the given gameobject's syncable components'
/// IDs.
/// </summary>
/// <returns>The IDs.</returns>
/// <param name="gameObject">Game object.</param>
/// <param name="newIDs">Whether or not new IDs are created.
/// <see cref="GetNewEntityIDs"/> is a shorthand for this function with
/// this parameter set to true.</param>
public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) {
List<uint> IDs = new List<uint>();
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;
}
/// <summary>
/// Creates an ordered list of the given gameobject's syncable components'
/// IDs. See <see cref="GetEntityIDs"/> for more information.
/// </summary>
/// <returns>The new IDs.</returns>
/// <param name="gameObject">Game object.</param>
public uint[] GetNewEntityIDs(GameObject gameObject) {
return GetEntityIDs(gameObject, true);
}
/// <summary>
/// Get a synced component by its ID.
/// </summary>
/// <param name="id">The ID.</param>
public SyncBase Get(uint id) {
return Database[id];
}
/// <summary>
/// Creates a new ID which isn't in use yet.
/// </summary>
/// <returns>A new, free ID.</returns>
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;
}
}
}

View File

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Types of entities. Every entity type has its own prefab, see
/// <see cref="Spawner"/>.
/// </summary>
public enum EntityType {
/// <summary>
/// Player character.
/// </summary>
PC,
/// <summary>
/// Non player character.
/// </summary>
NPC
}

View File

@ -1,4 +1,5 @@
using Cyber.Networking.Messages; using Cyber.Console;
using Cyber.Networking.Messages;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;

View File

@ -3,6 +3,7 @@ using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using Cyber.Networking.Clientside; using Cyber.Networking.Clientside;
using Cyber.Networking.Serverside; using Cyber.Networking.Serverside;
using Cyber.Console;
namespace Cyber.Networking { namespace Cyber.Networking {

View File

@ -1,4 +1,5 @@
using Cyber.Networking.Messages; using Cyber.Console;
using Cyber.Networking.Messages;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;

View File

@ -1,29 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Controls the player character. Shouldn't exist on the server, and only one
/// should exist per client (the character that client is controlling).
/// </summary>
public class PlayerController : MonoBehaviour {
/// <summary>
/// The character this controller should control.
/// </summary>
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();
}
}
}

View File

@ -1,62 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A utility class to spawn entities into the world based on
/// their <see cref="EntityType"/> and .
/// </summary>
public class Spawner : MonoBehaviour {
/// <summary>
/// The <see cref="SyncDB"/> this <see cref="Spawner"/> should be using to
/// set entities' IDs.
/// </summary>
public SyncDB SyncDB;
/// <summary>
/// The <see cref="EntityType.PC"/> prefab.
/// </summary>
public GameObject PCEntityPrefab;
/// <summary>
/// The <see cref="EntityType.NPC"/> prefab.
/// </summary>
public GameObject NPCEntityPrefab;
/// <summary>
/// Spawns an entity and returns that entity.
/// </summary>
/// <param name="type">Type.</param>
/// <param name="position">Position.</param>
/// <param name="ids">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 <see cref="SyncDB.GetEntityIDs"/>. To create new
/// ids, leave as the default (null).</param>
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)));
}
}
}

View File

@ -1,15 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A base class for all syncable components. An instance of
/// <see cref="SyncDB"/> will contain all of the game's synced components.
/// </summary>
public class SyncBase : MonoBehaviour {
/// <summary>
/// The ID this syncable component can be found with from its parent
/// <see cref="SyncDB"/>.
/// </summary>
public uint ID;
}

View File

@ -1,103 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
/// <summary>
/// A database of the game's all syncable components. Syncable components are
/// the instances of the subclasses of <see cref="SyncBase"/>.
/// </summary>
public class SyncDB : MonoBehaviour {
private static readonly Type[] SyncableClasses = new Type[]{
typeof(Character)
};
private uint IDCounter = 0;
private Dictionary<uint, SyncBase> Database = new Dictionary<uint, SyncBase>();
/// <summary>
/// Add an entity to the database with the given IDs.
/// </summary>
/// <param name="gameObject">Game object.</param>
/// <param name="ids">The IDs. Should be from <see cref="GetEntityIDs"/> or
/// <see cref="GetNewEntityIDs"/>, since the order is important.</param>
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;
}
}
}
/// <summary>
/// Makes an ordered list of the given gameobject's syncable components'
/// IDs.
/// </summary>
/// <returns>The IDs.</returns>
/// <param name="gameObject">Game object.</param>
/// <param name="newIDs">Whether or not new IDs are created.
/// <see cref="GetNewEntityIDs"/> is a shorthand for this function with
/// this parameter set to true.</param>
public uint[] GetEntityIDs(GameObject gameObject, bool newIDs = false) {
List<uint> IDs = new List<uint>();
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;
}
/// <summary>
/// Creates an ordered list of the given gameobject's syncable components'
/// IDs. See <see cref="GetEntityIDs"/> for more information.
/// </summary>
/// <returns>The new IDs.</returns>
/// <param name="gameObject">Game object.</param>
public uint[] GetNewEntityIDs(GameObject gameObject) {
return GetEntityIDs(gameObject, true);
}
/// <summary>
/// Get a synced component by its ID.
/// </summary>
/// <param name="id">The ID.</param>
public SyncBase Get(uint id) {
return Database[id];
}
/// <summary>
/// Creates a new ID which isn't in use yet.
/// </summary>
/// <returns>A new, free ID.</returns>
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;
}
}