From 540769a8565aacfea2ec9bbdfb161fd2d12172f6 Mon Sep 17 00:00:00 2001 From: JamesOrson Date: Thu, 9 Jul 2020 19:07:48 -0700 Subject: [PATCH] Change region separation standard Regions are now broken up as such: Access Level -> Constructors -> Members -> Member Methods -> Static Members -> Static Methods Disposable Support Destructors --- Common/Behaviors/CameraBehavior.cs | 22 +- Common/Behaviors/CubeBehavior.cs | 67 +- Common/Behaviors/FPSCounterBehavior.cs | 33 +- Common/Behaviors/MoveBehavior.cs | 22 +- Common/Behaviors/PlayerBehavior.cs | 16 +- .../Core/ECS/Components/BehaviorComponent.cs | 36 +- Komodo/Core/ECS/Components/CameraComponent.cs | 66 +- Komodo/Core/ECS/Components/Component.cs | 10 +- .../ECS/Components/Drawable2DComponent.cs | 44 +- .../ECS/Components/Drawable3DComponent.cs | 10 +- .../ECS/Components/DynamicBodyComponent.cs | 16 +- .../ECS/Components/KinematicBodyComponent.cs | 42 +- .../Core/ECS/Components/PhysicsComponent.cs | 40 +- .../Core/ECS/Components/RigidBodyComponent.cs | 86 +-- Komodo/Core/ECS/Components/SoundComponent.cs | 48 +- Komodo/Core/ECS/Components/SpriteComponent.cs | 16 +- .../ECS/Components/StaticBodyComponent.cs | 12 +- Komodo/Core/ECS/Components/TextComponent.cs | 22 +- .../ECS/Components/TriggerBodyComponent.cs | 10 +- Komodo/Core/ECS/Entities/Entity.cs | 120 ++- Komodo/Core/ECS/Systems/BehaviorSystem.cs | 54 +- Komodo/Core/ECS/Systems/CameraSystem.cs | 70 +- Komodo/Core/ECS/Systems/ISystem.cs | 56 +- Komodo/Core/ECS/Systems/PhysicsSystem.cs | 696 +++++++++--------- Komodo/Core/ECS/Systems/Render2DSystem.cs | 48 +- Komodo/Core/ECS/Systems/Render3DSystem.cs | 48 +- Komodo/Core/ECS/Systems/SoundSystem.cs | 85 ++- .../Core/Engine/Graphics/GraphicsManager.cs | 34 +- Komodo/Core/Engine/Graphics/Model.cs | 91 +-- Komodo/Core/Engine/Graphics/Resolution.cs | 78 +- Komodo/Core/Engine/Graphics/Texture.cs | 46 +- Komodo/Core/Engine/Input/InputInfo.cs | 22 +- Komodo/Core/Engine/Input/InputManager.cs | 133 ++-- Komodo/Core/Engine/Input/InputMapper.cs | 80 +- Komodo/Core/Game.cs | 56 +- Komodo/Core/MonoGame.cs | 66 +- Komodo/Core/Physics/Box.cs | 42 +- Komodo/Core/Physics/Collision.cs | 18 +- Komodo/Core/Physics/IPhysicsShape.cs | 12 +- Komodo/Core/Physics/PhysicsMaterial.cs | 54 +- Komodo/Core/Physics/Sphere.cs | 35 +- Komodo/Lib/Compression/Brotli.cs | 43 +- Komodo/Lib/Compression/GZIP.cs | 43 +- Komodo/Lib/Math/Vector2.cs | 179 +++-- Komodo/Lib/Math/Vector3.cs | 202 +++-- Komodo/Lib/Network/Client.cs | 84 +-- Komodo/Lib/Network/Message.cs | 10 +- Komodo/Lib/Network/Transaction.cs | 18 +- Server/TCPServer.cs | 81 +- 49 files changed, 1564 insertions(+), 1658 deletions(-) diff --git a/Common/Behaviors/CameraBehavior.cs b/Common/Behaviors/CameraBehavior.cs index 79937817..e6724f85 100644 --- a/Common/Behaviors/CameraBehavior.cs +++ b/Common/Behaviors/CameraBehavior.cs @@ -8,6 +8,8 @@ namespace Common.Behaviors { public class CameraBehavior : BehaviorComponent { + #region Public + #region Constructors public CameraBehavior(CameraComponent camera, int playerIndex) : base() { @@ -19,27 +21,15 @@ public CameraBehavior(CameraComponent camera, int playerIndex) : base() Camera = camera; PanVelocity = 50f; } - #endregion Constructors + #endregion #region Members - - #region Public Members public CameraComponent Camera { get; set; } public int PlayerIndex { get; set; } public float PanVelocity { get; set; } - #endregion Public Members - - #region Protected Members - #endregion Protected Members - - #region Private Members - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public override void Update(GameTime gameTime) { var cameraLeft = InputManager.GetAction("camera_left", PlayerIndex); @@ -73,8 +63,8 @@ public override void Update(GameTime gameTime) ); Camera.Move(cameraMove); } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Common/Behaviors/CubeBehavior.cs b/Common/Behaviors/CubeBehavior.cs index d64282bd..7f3bde0b 100644 --- a/Common/Behaviors/CubeBehavior.cs +++ b/Common/Behaviors/CubeBehavior.cs @@ -1,5 +1,5 @@ using Komodo.Core.ECS.Components; -using Komodo.Core.Physics; +using Komodo.Core.Physics; using Komodo.Lib.Math; using Color = Microsoft.Xna.Framework.Color; @@ -11,30 +11,21 @@ namespace Common.Behaviors { public class CubeBehavior : BehaviorComponent { + #region Public + #region Constructors public CubeBehavior(string modelPath) : base() { ModelPath = modelPath; } - #endregion Constructors + #endregion #region Members - - #region Public Members public string ModelPath { get; protected set; } public Drawable3DComponent RootComponent { get; protected set; } - #endregion Public Members - - #region Protected Members - private bool _createdBoxes { get; set; } - #endregion Protected Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods - public override void Initialize() { base.Initialize(); @@ -55,31 +46,39 @@ public override void Update(GameTime gameTime) { _createdBoxes = true; var scale = Vector3.One * 20f; - for (int i = 0; i < 10; i++) - { - Parent.AddComponent( - new Drawable3DComponent(RootComponent.ModelData) - { - Position = new Vector3(i + 5, 0f, 0f) * scale.X, - Scale = scale - } - ); + for (int i = 0; i < 10; i++) + { + Parent.AddComponent( + new Drawable3DComponent(RootComponent.ModelData) + { + Position = new Vector3(i + 5, 0f, 0f) * scale.X, + Scale = scale + } + ); } - var material = new PhysicsMaterial("box") - { - Friction = 0.8f + var material = new PhysicsMaterial("box") + { + Friction = 0.8f }; - Parent.AddComponent( - new StaticBodyComponent(new Box(2f, 2f, 2f, 1f)) - { - Material = material, - } - ); + Parent.AddComponent( + new StaticBodyComponent(new Box(2f, 2f, 2f, 1f)) + { + Material = material, + } + ); Parent.Scale = scale; } } - #endregion Public Member Methods + #endregion + + #endregion + + #region Protected + + #region Members + private bool _createdBoxes { get; set; } + #endregion - #endregion Member Methods + #endregion } } diff --git a/Common/Behaviors/FPSCounterBehavior.cs b/Common/Behaviors/FPSCounterBehavior.cs index 3d5e4b64..356040a0 100644 --- a/Common/Behaviors/FPSCounterBehavior.cs +++ b/Common/Behaviors/FPSCounterBehavior.cs @@ -8,33 +8,20 @@ namespace Common.Behaviors { public class FPSCounterBehavior : BehaviorComponent { - #region Constructors - #endregion Constructors + #region Public #region Members - - #region Public Members public TextComponent CounterText { get; protected set; } - #endregion Public Members - - #region Protected Members - #endregion Protected Members - - #region Private Members - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public override void Initialize() { base.Initialize(); - CounterText = new TextComponent("fonts/font", Color.Black, Game.DefaultSpriteShader, "") - { - Position = Komodo.Lib.Math.Vector3.Zero + CounterText = new TextComponent("fonts/font", Color.Black, Game.DefaultSpriteShader, "") + { + Position = Komodo.Lib.Math.Vector3.Zero }; Parent.AddComponent(CounterText); } @@ -42,14 +29,8 @@ public override void Update(GameTime gameTime) { CounterText.Text = $"{Math.Round(Game.FramesPerSecond)} FPS"; } - #endregion Public Member Methods - - #region Protected Member Methods - #endregion Protected Member Methods - - #region Private Member Methods - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Common/Behaviors/MoveBehavior.cs b/Common/Behaviors/MoveBehavior.cs index 834c6519..080ba2a5 100644 --- a/Common/Behaviors/MoveBehavior.cs +++ b/Common/Behaviors/MoveBehavior.cs @@ -9,6 +9,8 @@ namespace Common.Behaviors { public class MoveBehavior : BehaviorComponent { + #region Public + #region Constructors public MoveBehavior(int playerIndex) : base() { @@ -20,28 +22,16 @@ public MoveBehavior(int playerIndex) : base() SprintFactor = 2f; Velocity = 50f; } - #endregion Constructors + #endregion #region Members - - #region Public Members public DynamicBodyComponent Body { get; private set; } public int PlayerIndex { get; set; } public float SprintFactor { get; set; } public float Velocity { get; set; } - #endregion Public Members - - #region Protected Members - #endregion Protected Members - - #region Private Members - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public override void Initialize() { base.Initialize(); @@ -106,8 +96,8 @@ public override void Update(GameTime gameTime) //Body.Move(direction * Velocity); Body.ApplyForce(direction * Velocity); } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Common/Behaviors/PlayerBehavior.cs b/Common/Behaviors/PlayerBehavior.cs index 8287a4d6..054d7649 100644 --- a/Common/Behaviors/PlayerBehavior.cs +++ b/Common/Behaviors/PlayerBehavior.cs @@ -9,24 +9,20 @@ namespace Common.Behaviors { public class PlayerBehavior : BehaviorComponent { + #region Public + #region Constructors public PlayerBehavior(int playerIndex) : base() { PlayerIndex = playerIndex; } - #endregion Constructors + #endregion #region Members - - #region Public Members public int PlayerIndex { get; } - #endregion Public Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public override void Initialize() { base.Initialize(); @@ -41,8 +37,8 @@ public override void Initialize() public override void Update(GameTime gameTime) { } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/BehaviorComponent.cs b/Komodo/Core/ECS/Components/BehaviorComponent.cs index 75180156..d3a715b8 100644 --- a/Komodo/Core/ECS/Components/BehaviorComponent.cs +++ b/Komodo/Core/ECS/Components/BehaviorComponent.cs @@ -2,23 +2,23 @@ namespace Komodo.Core.ECS.Components { - /// - /// Abstract class defining all behaviors. - /// A class derived from BehaviorComponent is the main way scripting is achieved in Komodo. + /// + /// Abstract class defining all behaviors. + /// A class derived from BehaviorComponent is the main way scripting is achieved in Komodo. /// public abstract class BehaviorComponent : Component - { + { + #region Public + #region Constructors - protected BehaviorComponent() : base(true, null) + public BehaviorComponent() : base(true, null) { - } - #endregion Constructors - + } + #endregion + #region Member Methods - - #region Public Member Methods - /// - /// Virtual method initializing a BehaviorComponent. + /// + /// Virtual method initializing a BehaviorComponent. /// public virtual void Initialize() { @@ -26,15 +26,15 @@ public virtual void Initialize() { IsInitialized = true; } - } - - /// - /// Abstract method for updating a BehaviorComponent. + } + + /// + /// Abstract method for updating a BehaviorComponent. /// /// Time passed since last public abstract void Update(GameTime gameTime); - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/CameraComponent.cs b/Komodo/Core/ECS/Components/CameraComponent.cs index 39de04a7..0e0add34 100644 --- a/Komodo/Core/ECS/Components/CameraComponent.cs +++ b/Komodo/Core/ECS/Components/CameraComponent.cs @@ -16,6 +16,8 @@ namespace Komodo.Core.ECS.Components /// public class CameraComponent : Component { + #region Public + #region Constructors public CameraComponent() : base(true, null) { @@ -29,11 +31,9 @@ public CameraComponent() : base(true, null) Rotation = Vector3.Zero; Zoom = 1; } - #endregion Constructors - + #endregion + #region Members - - #region Public Members /// /// Backward direction for current view matrix. /// @@ -289,29 +289,9 @@ public float Zoom ClampZoom(value); } } - #endregion Public Members - - #region Private Members - private float _farPlane { get; set; } - - private float _fieldOfView { get; set; } - - private float _maximumZoom = float.MaxValue; - - private float _minimumZoom { get; set; } - - private float _nearPlane { get; set; } - - private Matrix _viewMatrix { get; set; } - - private float _zoom { get; set; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Checks whether or not a is contained within the of the CameraComponent. /// @@ -435,9 +415,13 @@ public void ZoomOut(float deltaZoom) { ClampZoom(Zoom - deltaZoom); } - #endregion Public Member Methods + #endregion - #region Internal Member Methods + #endregion + + #region Internal + + #region Member Methods /// /// Calculates the by creating a look-at Matrix. /// @@ -463,9 +447,29 @@ internal Matrix CalculateViewMatrix() up.MonoGameVector ); } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private + + #region Members + private float _farPlane { get; set; } + + private float _fieldOfView { get; set; } + + private float _maximumZoom = float.MaxValue; + + private float _minimumZoom { get; set; } + + private float _nearPlane { get; set; } - #region Private Member Methods + private Matrix _viewMatrix { get; set; } + + private float _zoom { get; set; } + #endregion + + #region Member Methods /// /// Clamps between and based on the passed value. /// @@ -480,8 +484,8 @@ private void ClampZoom(float value) MinimumZoom ); } - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/Component.cs b/Komodo/Core/ECS/Components/Component.cs index db49aa2d..acc7386a 100644 --- a/Komodo/Core/ECS/Components/Component.cs +++ b/Komodo/Core/ECS/Components/Component.cs @@ -14,8 +14,10 @@ namespace Komodo.Core.ECS.Components /// public abstract class Component { + #region Public + #region Constructors - protected Component(bool isEnabled = true, Entity parent = null) + public Component(bool isEnabled = true, Entity parent = null) { ID = Guid.NewGuid(); IsEnabled = isEnabled; @@ -24,8 +26,6 @@ protected Component(bool isEnabled = true, Entity parent = null) #endregion #region Members - - #region Public Members /// /// Each Component maintains a reference to the instance. /// @@ -131,8 +131,8 @@ public Vector3 Scale } } } - #endregion Public Members + #endregion - #endregion Members + #endregion } } diff --git a/Komodo/Core/ECS/Components/Drawable2DComponent.cs b/Komodo/Core/ECS/Components/Drawable2DComponent.cs index b23b6222..33d6d825 100644 --- a/Komodo/Core/ECS/Components/Drawable2DComponent.cs +++ b/Komodo/Core/ECS/Components/Drawable2DComponent.cs @@ -1,51 +1,51 @@ using Komodo.Core.ECS.Entities; -using Komodo.Lib.Math; - +using Komodo.Lib.Math; + using Effect = Microsoft.Xna.Framework.Graphics.Effect; namespace Komodo.Core.ECS.Components -{ - /// - /// Abstract class defining all 2D drawable Components. - /// A class derived from Drawable2DComponent will render in a SpriteBatch in a . +{ + /// + /// Abstract class defining all 2D drawable Components. + /// A class derived from Drawable2DComponent will render in a SpriteBatch in a . /// 4 public abstract class Drawable2DComponent : Component { + #region Public + #region Constructors - protected Drawable2DComponent(bool isEnabled = true, Entity parent = null) : base(isEnabled, parent) + public Drawable2DComponent(bool isEnabled = true, Entity parent = null) : base(isEnabled, parent) { } #endregion - + #region Members - - #region Public Members - /// - /// Center point of the rendered Component. + /// + /// Center point of the rendered Component. /// public abstract Vector2 Center { get; } - /// - /// Height of the rendered Component. + /// + /// Height of the rendered Component. /// public abstract float Height { get; } - /// - /// Flags whether or not to draw the Drawable2DComponent as a billboard in 3D space, always facing the relevant . + /// + /// Flags whether or not to draw the Drawable2DComponent as a billboard in 3D space, always facing the relevant . /// public bool IsBillboard { get; set; } - /// - /// Shader to use when rendering the Component. If Shader is null, the will be used. + /// + /// Shader to use when rendering the Component. If Shader is null, the will be used. /// public Effect Shader { get; set; } - /// - /// Width of the rendered Component. + /// + /// Width of the rendered Component. /// public abstract float Width { get; } - #endregion Public Members + #endregion - #endregion Members + #endregion } } diff --git a/Komodo/Core/ECS/Components/Drawable3DComponent.cs b/Komodo/Core/ECS/Components/Drawable3DComponent.cs index f292f3ef..7349b8d0 100644 --- a/Komodo/Core/ECS/Components/Drawable3DComponent.cs +++ b/Komodo/Core/ECS/Components/Drawable3DComponent.cs @@ -10,6 +10,8 @@ namespace Komodo.Core.ECS.Components /// public class Drawable3DComponent : Component { + #region Public + #region Constructors /// /// Creates a Drawable3DComponent with a given . @@ -38,11 +40,9 @@ public Drawable3DComponent(string modelPath) : base(true, null) Texture = null; TexturePath = null; } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Provides a representing the bounds of the model. /// @@ -127,8 +127,8 @@ public float Width return ModelData.Width * Parent.Scale.X; } } - #endregion Public Members + #endregion - #endregion Members + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/DynamicBodyComponent.cs b/Komodo/Core/ECS/Components/DynamicBodyComponent.cs index b45c77e6..cae31f49 100644 --- a/Komodo/Core/ECS/Components/DynamicBodyComponent.cs +++ b/Komodo/Core/ECS/Components/DynamicBodyComponent.cs @@ -11,16 +11,16 @@ namespace Komodo.Core.ECS.Components /// public class DynamicBodyComponent : RigidBodyComponent { + #region Public + #region Constructors public DynamicBodyComponent(IPhysicsShape shape) { Shape = shape; } - #endregion Constructors - + #endregion + #region Member Methods - - #region Public Member Methods /// /// Applies a force vector to the body with no regard for the point of application. /// @@ -36,11 +36,11 @@ public void ApplyForceAtPoint(Vector3 force, Vector3 pointOfApplication) { switch (Shape) { - case Box _: + case Box _: ApplyForce(force); Torque += Vector3.Cross(pointOfApplication, force); break; - case Sphere _: + case Sphere _: throw new NotImplementedException("Sphere not yet implemented"); default: throw new Exception("No shape"); @@ -63,8 +63,8 @@ public override void Initialize() { base.Initialize(); } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/KinematicBodyComponent.cs b/Komodo/Core/ECS/Components/KinematicBodyComponent.cs index 10a01753..c314c67b 100644 --- a/Komodo/Core/ECS/Components/KinematicBodyComponent.cs +++ b/Komodo/Core/ECS/Components/KinematicBodyComponent.cs @@ -11,24 +11,20 @@ namespace Komodo.Core.ECS.Components /// public class KinematicBodyComponent : RigidBodyComponent { + #region Public + #region Constructors public KinematicBodyComponent(IPhysicsShape shape) { Shape = shape; } - #endregion Constructors + #endregion #region Members - - #region Public Members public Vector3 PositionDelta { get; internal set; } - #endregion Public Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Initializes a KinematicBodyComponent. /// @@ -37,23 +33,23 @@ public override void Initialize() base.Initialize(); } - /// - /// Moves the body in the direction of the movement by its magnitude. - /// - /// - /// Movement will later be scaled by . - /// + /// + /// Moves the body in the direction of the movement by its magnitude. + /// + /// + /// Movement will later be scaled by . + /// /// Direction and magnitude of the movement. - public void Move(Vector3 movement) - { - if (PhysicsSystem == null) - { - throw new Exception("Cannot move a KinematicBodyComponent without a registered PhysicsSystem"); - } - PositionDelta += movement; + public void Move(Vector3 movement) + { + if (PhysicsSystem == null) + { + throw new Exception("Cannot move a KinematicBodyComponent without a registered PhysicsSystem"); + } + PositionDelta += movement; } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/PhysicsComponent.cs b/Komodo/Core/ECS/Components/PhysicsComponent.cs index a47aa779..7f35994f 100644 --- a/Komodo/Core/ECS/Components/PhysicsComponent.cs +++ b/Komodo/Core/ECS/Components/PhysicsComponent.cs @@ -1,36 +1,32 @@ -using Komodo.Core.ECS.Systems; -using Komodo.Core.Physics; -using System; +using Komodo.Core.ECS.Systems; +using Komodo.Core.Physics; +using System; using System.Collections.Generic; namespace Komodo.Core.ECS.Components { - /// - /// Abstract class defining all physics components. + /// + /// Abstract class defining all physics components. /// public abstract class PhysicsComponent : Component - { + { + #region Public + #region Constructors - protected PhysicsComponent() : base(true, null) + public PhysicsComponent() : base(true, null) { Collisions = new Dictionary(); - } - #endregion Constructors + } + #endregion - #region Member Methods - - #region Public Member Methods + #region Members public Dictionary Collisions { get; } public PhysicsSystem PhysicsSystem => Parent?.PhysicsSystem; - #endregion Public Member Methods - - #endregion Member Methods - + #endregion + #region Member Methods - - #region Public Member Methods - /// - /// Virtual method initializing a PhysicsComponent. + /// + /// Virtual method initializing a PhysicsComponent. /// public virtual void Initialize() { @@ -39,8 +35,8 @@ public virtual void Initialize() IsInitialized = true; } } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/RigidBodyComponent.cs b/Komodo/Core/ECS/Components/RigidBodyComponent.cs index 822ba117..d20c8c0d 100644 --- a/Komodo/Core/ECS/Components/RigidBodyComponent.cs +++ b/Komodo/Core/ECS/Components/RigidBodyComponent.cs @@ -10,80 +10,71 @@ namespace Komodo.Core.ECS.Components /// public abstract class RigidBodyComponent : PhysicsComponent { + #region Public + #region Constructors - protected RigidBodyComponent() + public RigidBodyComponent() { Material = PhysicsMaterial.GetPhysicsMaterial("default"); } - #endregion Constructors + #endregion #region Members - - #region Public Members - /// - /// Rotational velocity. + /// + /// Rotational velocity. /// public Vector3 AngularVelocity { get; internal set; } - /// - /// Current force applied to the RigidBodyComponent. + /// + /// Current force applied to the RigidBodyComponent. /// public Vector3 Force { get; internal set; } - /// - /// Directional velocity. + /// + /// Directional velocity. /// public Vector3 LinearVelocity { get; internal set; } - /// - /// Material defining the physical parameters of the RigidBodyComponent. + /// + /// Material defining the physical parameters of the RigidBodyComponent. /// public PhysicsMaterial Material { - get - { - return _material; + get + { + return _material; } - set + set { - if (value != null) - { - _material = value; + if (value != null) + { + _material = value; } - } + } } - /// - /// Shape of the RigidBodyComponent. + /// + /// Shape of the RigidBodyComponent. /// public IPhysicsShape Shape { - get - { - return _shape.GetScaledShape(Scale); + get + { + return _shape.GetScaledShape(Scale); } - set - { - _shape = value; + set + { + _shape = value; } } - /// - /// Current torque applied to the RigidBodyComponent. + /// + /// Current torque applied to the RigidBodyComponent. /// public Vector3 Torque { get; internal set; } - #endregion Public Members - - #region Internal Members - internal PhysicsMaterial _material { get; set; } - internal IPhysicsShape _shape { get; set; } - #endregion Internal Members - - #endregion Members - + #endregion + #region Member Methods - - #region Public Member Methods /// /// Initializes a RigidBodyComponent. /// @@ -91,8 +82,17 @@ public override void Initialize() { base.Initialize(); } - #endregion Public Member Methods + #endregion + + #endregion + + #region Internal + + #region Members + internal PhysicsMaterial _material { get; set; } + internal IPhysicsShape _shape { get; set; } + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/SoundComponent.cs b/Komodo/Core/ECS/Components/SoundComponent.cs index 014cd083..2ceef0a7 100644 --- a/Komodo/Core/ECS/Components/SoundComponent.cs +++ b/Komodo/Core/ECS/Components/SoundComponent.cs @@ -13,6 +13,8 @@ namespace Komodo.Core.ECS.Components /// public class SoundComponent : Component { + #region Public + #region Constructors /// /// Creates a SoundComponent with a filepath to a compiled content file. @@ -26,21 +28,9 @@ public SoundComponent(string soundPath) : base(true, null) _instances = new Dictionary(); SoundPath = soundPath; } - #endregion Constructors - - #region Destructor - /// - /// Stops all sound instance found in . - /// - ~SoundComponent() - { - StopAll(); - } - #endregion Destructor + #endregion #region Members - - #region Public Members /// /// Raw sound data loaded from disk. /// @@ -50,17 +40,9 @@ public SoundComponent(string soundPath) : base(true, null) /// Path of the if the SoundComponent was provided a model filepath via . /// public string SoundPath { get; } - #endregion Public Members - - #region Internal Members - internal Dictionary _instances { get; set; } - #endregion Internal Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Changes the intensity of a valid sound instance found in . /// @@ -195,8 +177,26 @@ public void StopAll() Stop(id); } } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion + + #region Internal + + #region Members + internal Dictionary _instances { get; set; } + #endregion + + #endregion + + #region Destructor + /// + /// Stops all sound instance found in . + /// + ~SoundComponent() + { + StopAll(); + } + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/SpriteComponent.cs b/Komodo/Core/ECS/Components/SpriteComponent.cs index c9f9dbd4..7c645d62 100644 --- a/Komodo/Core/ECS/Components/SpriteComponent.cs +++ b/Komodo/Core/ECS/Components/SpriteComponent.cs @@ -5,11 +5,13 @@ namespace Komodo.Core.ECS.Components { - /// - /// Represents any 2D texture to be drawn in a + /// + /// Represents any 2D texture to be drawn in a /// public class SpriteComponent : Drawable2DComponent { + #region Public + #region Constructors /// /// Creates a SpriteComponent with a given . @@ -36,13 +38,11 @@ public SpriteComponent(string texturePath, Effect shader) : base(true, null) Shader = shader; TexturePath = texturePath; } - #endregion Constructors + #endregion #region Members + public override Vector2 Center => Texture != null ? new Vector2(Texture.Width / 2, Texture.Height / 2) : Vector2.Zero; - #region Public Members - public override Vector2 Center => Texture != null ? new Vector2(Texture.Width / 2, Texture.Height / 2) : Vector2.Zero; - /// /// Y dimensional extremity. /// @@ -74,8 +74,8 @@ public override float Width return Texture.Width * Parent.Scale.X; } } - #endregion Public Members + #endregion - #endregion Members + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/StaticBodyComponent.cs b/Komodo/Core/ECS/Components/StaticBodyComponent.cs index 44a4c989..bb124491 100644 --- a/Komodo/Core/ECS/Components/StaticBodyComponent.cs +++ b/Komodo/Core/ECS/Components/StaticBodyComponent.cs @@ -1,4 +1,4 @@ -using Komodo.Core.Physics; +using Komodo.Core.Physics; using GameTime = Microsoft.Xna.Framework.GameTime; namespace Komodo.Core.ECS.Components @@ -8,16 +8,16 @@ namespace Komodo.Core.ECS.Components /// public class StaticBodyComponent : RigidBodyComponent { + #region Public + #region Constructors public StaticBodyComponent(IPhysicsShape shape) { Shape = shape; } - #endregion Constructors + #endregion #region Member Methods - - #region Public Member Methods /// /// Initializes a StaticBodyComponent. /// @@ -25,8 +25,8 @@ public override void Initialize() { base.Initialize(); } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/TextComponent.cs b/Komodo/Core/ECS/Components/TextComponent.cs index 59c43916..a4ffa384 100644 --- a/Komodo/Core/ECS/Components/TextComponent.cs +++ b/Komodo/Core/ECS/Components/TextComponent.cs @@ -12,6 +12,8 @@ namespace Komodo.Core.ECS.Components /// public class TextComponent : Drawable2DComponent { + #region Public + #region Constructors /// /// Creates a TextComponent with a given . @@ -43,11 +45,9 @@ public TextComponent(string fontPath, Color color, Effect shader, string text = Shader = shader; Text = text; } - #endregion Constructors + #endregion #region Members - - #region Public Members public override Vector2 Center => new Vector2(Width / 2, Height / 2); public Color Color { get; set; } @@ -60,8 +60,8 @@ public TextComponent(string fontPath, Color color, Effect shader, string text = /// /// Path of the if the TextComponent was provided a font filepath via . /// - public string FontPath { get; set; } - + public string FontPath { get; set; } + /// /// Y dimensional extremity. /// @@ -78,13 +78,13 @@ public override float Height } } - /// - /// Flags whether or not to draw the TextComponent with the center or the top-left side as origin. + /// + /// Flags whether or not to draw the TextComponent with the center or the top-left side as origin. /// public bool IsCentered { get; set; } - /// - /// Text message to be rendered. + /// + /// Text message to be rendered. /// public string Text { get; set; } @@ -103,8 +103,8 @@ public override float Width return size.X * Scale.X; } } - #endregion Public Members + #endregion - #endregion Members + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Components/TriggerBodyComponent.cs b/Komodo/Core/ECS/Components/TriggerBodyComponent.cs index dade0b99..5e4ed291 100644 --- a/Komodo/Core/ECS/Components/TriggerBodyComponent.cs +++ b/Komodo/Core/ECS/Components/TriggerBodyComponent.cs @@ -10,16 +10,16 @@ namespace Komodo.Core.ECS.Components /// public class TriggerBodyComponent : RigidBodyComponent { + #region Public + #region Constructors public TriggerBodyComponent(IPhysicsShape shape) { Shape = shape; } - #endregion Constructors + #endregion #region Member Methods - - #region Public Member Methods /// /// Initializes a TriggerBodyComponent. /// @@ -27,8 +27,8 @@ public override void Initialize() { base.Initialize(); } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Entities/Entity.cs b/Komodo/Core/ECS/Entities/Entity.cs index 0ef808d7..80d5e839 100644 --- a/Komodo/Core/ECS/Entities/Entity.cs +++ b/Komodo/Core/ECS/Entities/Entity.cs @@ -15,7 +15,9 @@ namespace Komodo.Core.ECS.Entities /// public class Entity { - #region Constructors + #region Public + + #region Constructors /// Reference to current instance. public Entity([NotNull] Game game) { @@ -27,95 +29,89 @@ public Entity([NotNull] Game game) Rotation = Vector3.Zero; Scale = Vector3.One; } - #endregion Constructors + #endregion #region Members - - #region Public Members - /// - /// All tracked objects. + /// + /// All tracked objects. /// public List Components { get; private set; } - /// - /// Reference to current instance. + /// + /// Reference to current instance. /// public Game Game { get; private set; } - /// - /// Unique identifier for the Entity. + /// + /// Unique identifier for the Entity. /// public Guid ID { get; private set; } - /// - /// Whether or not the Entity and all child Components should be drawn or updated. + /// + /// Whether or not the Entity and all child Components should be drawn or updated. /// public bool IsEnabled { get; set; } - /// - /// Defines what should be used to simulate objects. This allows for objects to simulate specific subsets of all Entity objects. + /// + /// Defines what should be used to simulate objects. This allows for objects to simulate specific subsets of all Entity objects. /// public PhysicsSystem PhysicsSystem { get; set; } - /// - /// Position in world space. Used by objects to determine their world space. + /// + /// Position in world space. Used by objects to determine their world space. /// public Vector3 Position { get; set; } - /// - /// Defines what should be used to render objects. This allows for objects to render specific subsets of all Entity objects. + /// + /// Defines what should be used to render objects. This allows for objects to render specific subsets of all Entity objects. /// public Render2DSystem Render2DSystem { get; set; } - /// - /// Defines what should be used to render objects. This allows for objects to render specific subsets of all Entity objects. + /// + /// Defines what should be used to render objects. This allows for objects to render specific subsets of all Entity objects. /// public Render3DSystem Render3DSystem { get; set; } - /// - /// Rotation in world space. + /// + /// Rotation in world space. /// public Vector3 Rotation { get; set; } - /// - /// Rotation in world space as a . + /// + /// Rotation in world space as a . /// public Matrix RotationMatrix => Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z); - /// - /// Rotation in world space as a . + /// + /// Rotation in world space as a . /// public Quaternion RotationQuaternion => Quaternion.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z); - /// - /// Scaling for the entire entity. Scales all child objects. + /// + /// Scaling for the entire entity. Scales all child objects. /// public Vector3 Scale { get; set; } - #endregion Public Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods - /// - /// Adds a to the relevant . will also remove the from any Entity and it was attached to before. - /// - /// to add. + /// + /// Adds a to the relevant . will also remove the from any Entity and it was attached to before. + /// + /// to add. /// Whether or not the was added to this Entity's . Returns false if the already existed. public bool AddComponent([NotNull] Component component) { - if (component == null) - { - return false; + if (component == null) + { + return false; } if (Components == null) { Components = new List(); } - if (Components.Contains(component)) - { - return false; + if (Components.Contains(component)) + { + return false; } if (component.Parent != null) { @@ -154,8 +150,8 @@ public bool AddComponent([NotNull] Component component) } } - /// - /// Removes all objects from the Entity. + /// + /// Removes all objects from the Entity. /// public void ClearComponents() { @@ -165,18 +161,18 @@ public void ClearComponents() RemoveComponent(component); } Components.Clear(); - } - - /// - /// Removes an individual from this Entity's . - /// - /// to remove. + } + + /// + /// Removes an individual from this Entity's . + /// + /// to remove. /// Whether or not the was removed from this Entity's . Will return false if the is not present in . public bool RemoveComponent([NotNull] Component component) { - if (component == null) - { - return false; + if (component == null) + { + return false; } if (Components != null) { @@ -189,13 +185,13 @@ public bool RemoveComponent([NotNull] Component component) } break; case CameraComponent componentToRemove: - if (Render2DSystem.ActiveCamera == componentToRemove) - { - Render2DSystem.ActiveCamera = null; + if (Render2DSystem.ActiveCamera == componentToRemove) + { + Render2DSystem.ActiveCamera = null; } - if (Render3DSystem.ActiveCamera == componentToRemove) - { - Render3DSystem.ActiveCamera = null; + if (Render3DSystem.ActiveCamera == componentToRemove) + { + Render3DSystem.ActiveCamera = null; } if (!Game.CameraSystem.RemoveComponent(componentToRemove)) { @@ -243,8 +239,8 @@ public bool RemoveComponent([NotNull] Component component) } return false; } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/BehaviorSystem.cs b/Komodo/Core/ECS/Systems/BehaviorSystem.cs index f1ab5b68..73f50c45 100644 --- a/Komodo/Core/ECS/Systems/BehaviorSystem.cs +++ b/Komodo/Core/ECS/Systems/BehaviorSystem.cs @@ -13,6 +13,8 @@ namespace Komodo.Core.ECS.Systems /// public class BehaviorSystem : ISystem { + #region Public + #region Constructors /// Reference to current instance. public BehaviorSystem(Game game) @@ -22,11 +24,9 @@ public BehaviorSystem(Game game) Game = game; _uninitializedComponents = new Queue(); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// All tracked objects. /// @@ -46,21 +46,9 @@ public BehaviorSystem(Game game) /// Whether or not the BehaviorSystem has called . /// public bool IsInitialized { get; private set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Adds a to the BehaviorSystem if the is not already present. /// @@ -181,9 +169,13 @@ public bool RemoveEntity(Entity entityToRemove) } return false; } - #endregion Public Member Methods + #endregion + + #endregion + + #region Internal - #region Internal Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -197,11 +189,11 @@ internal bool AddComponent(BehaviorComponent componentToAdd) } var parent = componentToAdd.Parent; if (!Entities.ContainsKey(parent.ID)) - { - return AddEntity(parent); + { + return AddEntity(parent); } - else - { + else + { return AddBehaviorComponent(componentToAdd); } } @@ -234,7 +226,19 @@ internal void UpdateComponents(GameTime gameTime) } } } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion #region Private Member Methods /// @@ -277,8 +281,8 @@ private bool RemoveBehaviorComponent([NotNull] BehaviorComponent componentToRemo { return Components.Remove(componentToRemove); } - #endregion Protected Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/CameraSystem.cs b/Komodo/Core/ECS/Systems/CameraSystem.cs index 263394c1..0a9ad7df 100644 --- a/Komodo/Core/ECS/Systems/CameraSystem.cs +++ b/Komodo/Core/ECS/Systems/CameraSystem.cs @@ -4,8 +4,8 @@ using System.Diagnostics.CodeAnalysis; using System; -using GameTime = Microsoft.Xna.Framework.GameTime; - +using GameTime = Microsoft.Xna.Framework.GameTime; + namespace Komodo.Core.ECS.Systems { /// @@ -13,6 +13,8 @@ namespace Komodo.Core.ECS.Systems /// public class CameraSystem : ISystem { + #region Public + #region Constructors /// Reference to current instance. public CameraSystem(Game game) @@ -22,11 +24,9 @@ public CameraSystem(Game game) Game = game; _uninitializedComponents = new Queue(); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// All tracked objects. /// @@ -46,21 +46,9 @@ public CameraSystem(Game game) /// Whether or not the CameraSystem has called . /// public bool IsInitialized { get; private set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Adds a to the CameraSystem if the is not already present. /// @@ -181,9 +169,13 @@ public bool RemoveEntity(Entity entityToRemove) } return false; } - #endregion Public Member Methods + #endregion + + #endregion - #region Internal Member Methods + #region Internal + + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -197,11 +189,11 @@ internal bool AddComponent(CameraComponent componentToAdd) } var parent = componentToAdd.Parent; if (!Entities.ContainsKey(parent.ID)) - { - return AddEntity(parent); + { + return AddEntity(parent); } - else - { + else + { return AddCameraComponent(componentToAdd); } } @@ -234,9 +226,21 @@ internal void UpdateComponents(GameTime _) } } } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion - #region Private Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -276,14 +280,10 @@ private void InitializeComponents() private bool RemoveCameraComponent([NotNull] CameraComponent componentToRemove) { return Components.Remove(componentToRemove); - } - #endregion Private Member Methods - - #endregion Member Methods - - #region Static Methods + } + #endregion - #region Private Static Methods + #region Static Methods /// /// Performs the update logic on all objects. /// @@ -292,8 +292,8 @@ private static void UpdateComponent([NotNull] CameraComponent component) { component.ViewMatrix = component.CalculateViewMatrix(); } - #endregion Private Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/ISystem.cs b/Komodo/Core/ECS/Systems/ISystem.cs index c419175a..98edc893 100644 --- a/Komodo/Core/ECS/Systems/ISystem.cs +++ b/Komodo/Core/ECS/Systems/ISystem.cs @@ -3,18 +3,17 @@ using System; using System.Collections.Generic; -using GameTime = Microsoft.Xna.Framework.GameTime; - -namespace Komodo.Core.ECS.Systems -{ +using GameTime = Microsoft.Xna.Framework.GameTime; + +namespace Komodo.Core.ECS.Systems +{ /// /// Defines the interface for all classes. - /// - public interface ISystem where T : Component + /// + public interface ISystem where T : Component { #region Members - #region Public Members /// /// All tracked objects. /// @@ -33,57 +32,54 @@ public interface ISystem where T : Component /// /// Whether or not the ISystem has called . /// - public bool IsInitialized { get; } - #endregion Public Members + bool IsInitialized { get; } - #endregion Members + #endregion - #region Member Methods - #region Public Member Methods + #region Member Methods /// /// Adds a to the ISystem if the is not already present. /// /// to add. - /// Whether or not the was added to this Drawable2DSystem's . Returns false if the already existed. - bool AddEntity(Entity entityToAdd); - + /// Whether or not the was added to this Drawable2DSystem's . Returns false if the already existed. + bool AddEntity(Entity entityToAdd); + /// /// Removes all objects from the ISystem. - /// - public void ClearEntities(); - + /// + void ClearEntities(); + /// /// Should update the ISystem as well as all of it's objects. - /// + /// void Initialize(); /// /// Runs any operations needed at the end of the loop. /// - /// Time passed since last . - void PostUpdate(GameTime gameTime); - + /// Time passed since last . + void PostUpdate(GameTime gameTime); + /// /// Runs any operations needed at the beginning of the loop. /// - /// Time passed since last . + /// Time passed since last . void PreUpdate(GameTime gameTime); /// /// Removes a from the ISystem, including all the 's objects. /// /// Unique identifier for the . - /// Whether or not the was removed from this ISystem's . Will return false if the is not present in . - public bool RemoveEntity(Guid entityID); + /// Whether or not the was removed from this ISystem's . Will return false if the is not present in . + bool RemoveEntity(Guid entityID); /// /// Removes a from the ISystem, including all the 's objects. /// /// to remove. - /// Whether or not the was removed from this ISystem's . Will return false if the is not present in . - public bool RemoveEntity(Entity entityToRemove); - #endregion Public Member Methods + /// Whether or not the was removed from this ISystem's . Will return false if the is not present in . + bool RemoveEntity(Entity entityToRemove); - #endregion Member Methods - } + #endregion + } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/PhysicsSystem.cs b/Komodo/Core/ECS/Systems/PhysicsSystem.cs index 4d055325..aab1053e 100644 --- a/Komodo/Core/ECS/Systems/PhysicsSystem.cs +++ b/Komodo/Core/ECS/Systems/PhysicsSystem.cs @@ -1,144 +1,132 @@ -using Komodo.Core.ECS.Components; -using Komodo.Core.ECS.Entities; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System; +using Komodo.Core.ECS.Components; +using Komodo.Core.ECS.Entities; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System; using Komodo.Lib.Math; using System.Linq; using Komodo.Core.Physics; - -using BoundingBox = Microsoft.Xna.Framework.BoundingBox; -using BoundingSphere = Microsoft.Xna.Framework.BoundingSphere; -using ContainmentType = Microsoft.Xna.Framework.ContainmentType; -using GameTime = Microsoft.Xna.Framework.GameTime; + +using BoundingBox = Microsoft.Xna.Framework.BoundingBox; +using BoundingSphere = Microsoft.Xna.Framework.BoundingSphere; +using ContainmentType = Microsoft.Xna.Framework.ContainmentType; +using GameTime = Microsoft.Xna.Framework.GameTime; using MathHelper = Microsoft.Xna.Framework.MathHelper; -namespace Komodo.Core.ECS.Systems -{ - /// - /// Manages all objects. - /// - public class PhysicsSystem : ISystem - { - #region Constructors - /// Reference to current instance. - public PhysicsSystem(Game game) - { - Components = new List(); - Entities = new Dictionary(); - Game = game; - _uninitializedComponents = new Queue(); - } - #endregion Constructors - - #region Members - - #region Public Members - /// - /// All tracked objects. - /// - public List Components { get; private set; } - - /// - /// All tracked objects. - /// - public Dictionary Entities { get; set; } - - /// - /// Reference to current instance. - /// - public Game Game { get; } - - /// - /// Whether or not the PhysicsSystem has called . - /// - public bool IsInitialized { get; private set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members - - #region Member Methods - - #region Public Member Methods - /// - /// Adds a to the PhysicsSystem if the is not already present. - /// - /// to add. - /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. - public bool AddEntity([NotNull] Entity entityToAdd) - { - if (Entities == null) - { - Entities = new Dictionary(); - } - if (Entities.ContainsKey(entityToAdd.ID)) - { - return false; - } - if (entityToAdd.PhysicsSystem != null) - { - entityToAdd.PhysicsSystem.RemoveEntity(entityToAdd.ID); - } - Entities[entityToAdd.ID] = entityToAdd; - foreach (var component in entityToAdd.Components) - { - switch (component) - { - case PhysicsComponent componentToAdd: - AddComponent(componentToAdd); - break; - default: - continue; - } - } - return true; - } - - /// - /// Removes all objects from the PhysicsSystem. - /// - public void ClearEntities() - { - if (Entities != null) - { - foreach (var entityID in Entities.Keys) - { - RemoveEntity(entityID); - } - Entities.Clear(); - } - } - - /// - /// Initializes the PhysicsSystem and all tracked objects. - /// - public void Initialize() - { - if (!IsInitialized) - { - IsInitialized = true; - InitializeComponents(); - } - } - - /// - /// Runs any operations needed at the end of the loop. - /// - /// - /// Will initialize any new uninitialized objects. - /// - /// Time passed since last . - public void PostUpdate(GameTime _) - { - InitializeComponents(); +namespace Komodo.Core.ECS.Systems +{ + /// + /// Manages all objects. + /// + public class PhysicsSystem : ISystem + { + #region Public + + #region Constructors + /// Reference to current instance. + public PhysicsSystem(Game game) + { + Components = new List(); + Entities = new Dictionary(); + Game = game; + _uninitializedComponents = new Queue(); + } + #endregion + + #region Members + /// + /// All tracked objects. + /// + public List Components { get; private set; } + + /// + /// All tracked objects. + /// + public Dictionary Entities { get; set; } + + /// + /// Reference to current instance. + /// + public Game Game { get; } + + /// + /// Whether or not the PhysicsSystem has called . + /// + public bool IsInitialized { get; private set; } + #endregion + + #region Member Methods + /// + /// Adds a to the PhysicsSystem if the is not already present. + /// + /// to add. + /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. + public bool AddEntity([NotNull] Entity entityToAdd) + { + if (Entities == null) + { + Entities = new Dictionary(); + } + if (Entities.ContainsKey(entityToAdd.ID)) + { + return false; + } + if (entityToAdd.PhysicsSystem != null) + { + entityToAdd.PhysicsSystem.RemoveEntity(entityToAdd.ID); + } + Entities[entityToAdd.ID] = entityToAdd; + foreach (var component in entityToAdd.Components) + { + switch (component) + { + case PhysicsComponent componentToAdd: + AddComponent(componentToAdd); + break; + default: + continue; + } + } + return true; + } + + /// + /// Removes all objects from the PhysicsSystem. + /// + public void ClearEntities() + { + if (Entities != null) + { + foreach (var entityID in Entities.Keys) + { + RemoveEntity(entityID); + } + Entities.Clear(); + } + } + + /// + /// Initializes the PhysicsSystem and all tracked objects. + /// + public void Initialize() + { + if (!IsInitialized) + { + IsInitialized = true; + InitializeComponents(); + } + } + + /// + /// Runs any operations needed at the end of the loop. + /// + /// + /// Will initialize any new uninitialized objects. + /// + /// Time passed since last . + public void PostUpdate(GameTime _) + { + InitializeComponents(); foreach (var component in Components) { switch (component) @@ -153,18 +141,18 @@ public void PostUpdate(GameTime _) default: break; } - } - } - - /// - /// Runs any operations needed at the beginning of the loop. - /// - /// - /// Will initialize any new uninitialized objects. - /// - /// Time passed since last . - public void PreUpdate(GameTime _) - { + } + } + + /// + /// Runs any operations needed at the beginning of the loop. + /// + /// + /// Will initialize any new uninitialized objects. + /// + /// Time passed since last . + public void PreUpdate(GameTime _) + { foreach (var component in Components) { switch (component) @@ -172,94 +160,98 @@ public void PreUpdate(GameTime _) default: break; } - } - } - - /// - /// Removes a from the PhysicsSystem, including all the 's objects. - /// - /// Unique identifier for the . - /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . - public bool RemoveEntity(Guid entityID) - { - if (Entities != null && Entities.ContainsKey(entityID)) - { - return RemoveEntity(Entities[entityID]); - } - return false; - } - - /// - /// Removes a from the PhysicsSystem, including all the 's objects. - /// - /// to remove. - /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . - public bool RemoveEntity(Entity entityToRemove) - { - if (Entities != null && Entities.ContainsKey(entityToRemove.ID)) - { - foreach (var component in entityToRemove.Components) - { - switch (component) - { - case PhysicsComponent componentToRemove: - RemoveComponent(componentToRemove); - break; - default: - continue; - } - } - return Entities.Remove(entityToRemove.ID); - } - return false; - } - #endregion Public Member Methods - - #region Internal Member Methods - /// - /// Adds a to relevant . If the is not initialized, it will be queued for initialization. - /// - /// to add. - /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. - internal bool AddComponent(PhysicsComponent componentToAdd) - { - if (!componentToAdd.IsInitialized) - { - _uninitializedComponents.Enqueue(componentToAdd); - } - var parent = componentToAdd.Parent; - if (!Entities.ContainsKey(parent.ID)) + } + } + + /// + /// Removes a from the PhysicsSystem, including all the 's objects. + /// + /// Unique identifier for the . + /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . + public bool RemoveEntity(Guid entityID) + { + if (Entities != null && Entities.ContainsKey(entityID)) + { + return RemoveEntity(Entities[entityID]); + } + return false; + } + + /// + /// Removes a from the PhysicsSystem, including all the 's objects. + /// + /// to remove. + /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . + public bool RemoveEntity(Entity entityToRemove) + { + if (Entities != null && Entities.ContainsKey(entityToRemove.ID)) + { + foreach (var component in entityToRemove.Components) + { + switch (component) + { + case PhysicsComponent componentToRemove: + RemoveComponent(componentToRemove); + break; + default: + continue; + } + } + return Entities.Remove(entityToRemove.ID); + } + return false; + } + #endregion + + #endregion + + #region Internal + + #region Member Methods + /// + /// Adds a to relevant . If the is not initialized, it will be queued for initialization. + /// + /// to add. + /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. + internal bool AddComponent(PhysicsComponent componentToAdd) + { + if (!componentToAdd.IsInitialized) + { + _uninitializedComponents.Enqueue(componentToAdd); + } + var parent = componentToAdd.Parent; + if (!Entities.ContainsKey(parent.ID)) { return AddEntity(parent); - } + } else { - return AddPhysicsComponent(componentToAdd); - } - } - - /// - /// Removes an individual from this PhysicsSystem's . - /// - /// to remove. - /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . - internal bool RemoveComponent(PhysicsComponent componentToRemove) - { - return RemovePhysicsComponent(componentToRemove); - } - - /// - /// Calls on each enabled . - /// - /// Time passed since last . - internal void UpdateComponents(GameTime gameTime) - { - if (Components != null) - { - foreach (var component in Components) - { - if (component.IsEnabled && component.Parent != null && component.Parent.IsEnabled) - { + return AddPhysicsComponent(componentToAdd); + } + } + + /// + /// Removes an individual from this PhysicsSystem's . + /// + /// to remove. + /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . + internal bool RemoveComponent(PhysicsComponent componentToRemove) + { + return RemovePhysicsComponent(componentToRemove); + } + + /// + /// Calls on each enabled . + /// + /// Time passed since last . + internal void UpdateComponents(GameTime gameTime) + { + if (Components != null) + { + foreach (var component in Components) + { + if (component.IsEnabled && component.Parent != null && component.Parent.IsEnabled) + { switch (component) { case KinematicBodyComponent kinematicBody: @@ -270,33 +262,45 @@ internal void UpdateComponents(GameTime gameTime) break; default: break; - } - } - } - HandleCollisions(); - } - } - #endregion Internal Member Methods - - #region Private Member Methods - /// - /// Adds a to relevant . If the is not initialized, it will be queued for initialization. - /// - /// to add. - /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. - private bool AddPhysicsComponent([NotNull] PhysicsComponent componentToAdd) - { - if (Components.Contains(componentToAdd)) - { - return false; - } - Components.Add(componentToAdd); - return true; - } - + } + } + } + HandleCollisions(); + } + } + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion + + #region Member Methods + /// + /// Adds a to relevant . If the is not initialized, it will be queued for initialization. + /// + /// to add. + /// Whether or not the was added to this PhysicsSystem's . Returns false if the already existed. + private bool AddPhysicsComponent([NotNull] PhysicsComponent componentToAdd) + { + if (Components.Contains(componentToAdd)) + { + return false; + } + Components.Add(componentToAdd); + return true; + } + /// /// Corrects all current collisions between and s. - /// + /// private void CorrectCollisions() { var dynamicBodies = Components.Where(body => body is DynamicBodyComponent && body.IsEnabled && body.Parent.IsEnabled).Cast(); @@ -333,11 +337,11 @@ private void CorrectCollisions() collision._isResolved = true; } } - } - + } + /// /// Detects collisions of all enabled s. - /// + /// private void DetectCollisions() { var rigidBodies = Components.Where(body => body is RigidBodyComponent && body.IsEnabled && body.Parent.IsEnabled).Cast(); @@ -404,11 +408,11 @@ private void DetectCollisions() } } } - } - + } + /// /// Detects collisions and corrects them. - /// + /// private void HandleCollisions() { foreach (var component in Components) @@ -417,46 +421,42 @@ private void HandleCollisions() } DetectCollisions(); CorrectCollisions(); - } - - /// - /// Initializes all uninitialized . - /// - private void InitializeComponents() - { - while (_uninitializedComponents.Count > 0) - { - var component = _uninitializedComponents.Dequeue(); - if (!component.IsInitialized) - { - component.IsInitialized = true; - component.Initialize(); - } - } - } - - /// - /// Removes an individual from this PhysicsSystem's . - /// - /// to remove. - /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . - private bool RemovePhysicsComponent([NotNull] PhysicsComponent componentToRemove) - { - return Components.Remove(componentToRemove); } - #endregion Protected Member Methods - #endregion Member Methods + /// + /// Initializes all uninitialized . + /// + private void InitializeComponents() + { + while (_uninitializedComponents.Count > 0) + { + var component = _uninitializedComponents.Dequeue(); + if (!component.IsInitialized) + { + component.IsInitialized = true; + component.Initialize(); + } + } + } - #region Static Methods + /// + /// Removes an individual from this PhysicsSystem's . + /// + /// to remove. + /// Whether or not the was removed from this PhysicsSystem's . Will return false if the is not present in . + private bool RemovePhysicsComponent([NotNull] PhysicsComponent componentToRemove) + { + return Components.Remove(componentToRemove); + } + #endregion - #region Private Static Methods + #region Static Methods /// /// Performs an AABB AABB collision check. /// /// First AABB. /// Second AABB. - /// + /// private static Collision AABBAABB(BoundingBox a, BoundingBox b) { // Minimum Translation Vector @@ -503,41 +503,41 @@ private static Collision AABBAABB(BoundingBox a, BoundingBox b) penetrationDepth = MathF.Sqrt(minimumTranslationVectorDistance) * 1.001f; return new Collision(true, normal, penetrationDepth); - } - - /// - /// Calculates angular velocity for the current frame. - /// - private static void CalculateAngularVelocity(DynamicBodyComponent body, float delta) - { - var angularAcceleration = new Vector3( - body.Torque.X * (1.0f / body.Shape.MomentOfInertia.X), - body.Torque.Y * (1.0f / body.Shape.MomentOfInertia.Y), - body.Torque.Z * (1.0f / body.Shape.MomentOfInertia.Z) - ); - body.AngularVelocity += angularAcceleration * delta; } - /// - /// Calculates linear velocity for the current frame. - /// - private static void CalculateLinearVelocity(DynamicBodyComponent body, float delta) - { - var material = body.Material; - var linearAcceleration = body.Force * (1f / body.Shape.Mass); - body.LinearVelocity += linearAcceleration * delta; - body.LinearVelocity *= 1f - (material.LinearDamping * delta); + /// + /// Calculates angular velocity for the current frame. + /// + private static void CalculateAngularVelocity(DynamicBodyComponent body, float delta) + { + var angularAcceleration = new Vector3( + body.Torque.X * (1.0f / body.Shape.MomentOfInertia.X), + body.Torque.Y * (1.0f / body.Shape.MomentOfInertia.Y), + body.Torque.Z * (1.0f / body.Shape.MomentOfInertia.Z) + ); + body.AngularVelocity += angularAcceleration * delta; + } + + /// + /// Calculates linear velocity for the current frame. + /// + private static void CalculateLinearVelocity(DynamicBodyComponent body, float delta) + { + var material = body.Material; + var linearAcceleration = body.Force * (1f / body.Shape.Mass); + body.LinearVelocity += linearAcceleration * delta; + body.LinearVelocity *= 1f - (material.LinearDamping * delta); if (MathF.Abs(body.LinearVelocity.Length()) <= material.LinearDampingLimit) { body.LinearVelocity = Vector3.Zero; - } - } - + } + } + /// /// Generates a for a with an attached . /// /// Body to create a for. - /// representation of the . + /// representation of the . private static BoundingBox GenerateBoundingBox(RigidBodyComponent body) { var box = body.Shape as Box; @@ -553,49 +553,49 @@ private static BoundingBox GenerateBoundingBox(RigidBodyComponent body) ) + body.WorldPosition; return new BoundingBox(min.MonoGameVector, max.MonoGameVector); - } - + } + /// /// Generates a for a with an attached . /// /// Body to create a for. - /// representation of the . + /// representation of the . private static BoundingSphere GenerateBoundingSphere(RigidBodyComponent body) { var sphere = body.Shape as Sphere; return new BoundingSphere(body.WorldPosition.MonoGameVector, sphere.Radius); - } - + } + /// /// Gets collision data between two es. /// /// First box to check. /// Second box to check. - /// information between the two boxes. + /// information between the two boxes. private static Collision GetCollision(BoundingBox box, BoundingBox otherBox) { return AABBAABB(box, otherBox); - } - + } + /// /// Gets collision data between a and a . /// /// Box to check. /// Sphere to check. - /// information between the box and sphere. + /// information between the box and sphere. private static Collision GetCollision(BoundingBox box, BoundingSphere sphere) { var collision = GetCollision(sphere, box); return new Collision(collision.IsColliding, -collision.Normal, collision.PenetrationDepth); - } - + } + /// /// Gets collision data between two s. /// /// First sphere to check. /// Second sphere to check. - /// information between the two spheres. + /// information between the two spheres. private static Collision GetCollision(BoundingSphere sphere, BoundingSphere otherSphere) { var containment = sphere.Contains(otherSphere); @@ -610,14 +610,14 @@ private static Collision GetCollision(BoundingSphere sphere, BoundingSphere othe penetrationDepth = combinedRadius - distance; } return new Collision(containment != ContainmentType.Disjoint, normal, penetrationDepth); - } - + } + /// /// Gets collision data between a and a . /// /// Sphere to check. /// Box to check. - /// information between the sphere and box. + /// information between the sphere and box. private static Collision GetCollision(BoundingSphere sphere, BoundingBox box) { var containment = sphere.Contains(box); @@ -636,8 +636,8 @@ private static Collision GetCollision(BoundingSphere sphere, BoundingBox box) } return new Collision(containment != ContainmentType.Disjoint, normal, penetrationDepth); - } - + } + private static bool SAT(Vector3 axis, float minA, float maxA, float minB, float maxB, ref Vector3 mtvAxis, ref float mtvDistance) { // Separating Axis Theorem @@ -686,13 +686,13 @@ private static bool SAT(Vector3 axis, float minA, float maxA, float minB, float } return true; - } - + } + /// /// Updates a for the given frame, considering relevant forces and velocities. /// /// to update - /// Time passed since last . + /// Time passed since last . private static void UpdateDynamicBodyComponent(DynamicBodyComponent body, GameTime gameTime) { float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; @@ -701,20 +701,20 @@ private static void UpdateDynamicBodyComponent(DynamicBodyComponent body, GameTi body.Parent.Position += body.LinearVelocity * delta; body.Parent.Rotation += body.AngularVelocity * delta; - } - + } + /// /// Updates a for the given frame, considering relevant movements. /// /// to update - /// Time passed since last . + /// Time passed since last . private static void UpdateKinematicBodyComponent(KinematicBodyComponent body, GameTime gameTime) { float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; body.Parent.Position += body.PositionDelta * delta; - } - #endregion Private Static Methods - - #endregion Static Methods - } + } + #endregion + + #endregion + } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/Render2DSystem.cs b/Komodo/Core/ECS/Systems/Render2DSystem.cs index f4c83a68..54acc97f 100644 --- a/Komodo/Core/ECS/Systems/Render2DSystem.cs +++ b/Komodo/Core/ECS/Systems/Render2DSystem.cs @@ -28,6 +28,8 @@ namespace Komodo.Core.ECS.Systems /// public class Render2DSystem : ISystem { + #region Public + #region Constructors /// Reference to current instance. public Render2DSystem(Game game) @@ -39,11 +41,9 @@ public Render2DSystem(Game game) _uninitializedComponents = new Queue(); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// to be used for rendering all tracked . /// @@ -73,21 +73,9 @@ public Render2DSystem(Game game) /// Texture filtering to use for 2D sprites and fonts. /// public SamplerState TextureFilter { get; set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Adds a to the Drawable2DSystem if the is not already present. /// @@ -211,9 +199,13 @@ public bool RemoveEntity(Entity entityToRemove) } return false; } - #endregion Public Member Methods + #endregion + + #endregion + + #region Internal - #region Internal Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -258,9 +250,21 @@ internal bool RemoveComponent(Drawable2DComponent componentToRemove) { return RemoveDrawable2DComponent(componentToRemove); } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private - #region Private Member Methods + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion + + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -469,8 +473,8 @@ private bool RemoveDrawable2DComponent([NotNull] Drawable2DComponent componentTo { return Components.Remove(componentToRemove); } - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/Render3DSystem.cs b/Komodo/Core/ECS/Systems/Render3DSystem.cs index d4113963..3ada5226 100644 --- a/Komodo/Core/ECS/Systems/Render3DSystem.cs +++ b/Komodo/Core/ECS/Systems/Render3DSystem.cs @@ -22,6 +22,8 @@ namespace Komodo.Core.ECS.Systems /// public class Render3DSystem : ISystem { + #region Public + #region Constructors /// Reference to current instance. public Render3DSystem(Game game) @@ -33,11 +35,9 @@ public Render3DSystem(Game game) _uninitializedComponents = new Queue(); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// to be used for rendering all tracked . /// @@ -67,21 +67,9 @@ public Render3DSystem(Game game) /// Texture filtering to use for 3D textures. /// public SamplerState TextureFilter { get; set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Adds a to the Drawable3DSystem if the is not already present. /// @@ -205,9 +193,13 @@ public bool RemoveEntity(Entity entityToRemove) } return false; } - #endregion Public Member Methods + #endregion + + #endregion + + #region Internal - #region Internal Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -266,9 +258,21 @@ internal bool RemoveComponent(Drawable3DComponent componentToRemove) { return RemoveDrawable3DComponent(componentToRemove); } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private - #region Private Member Methods + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion + + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -359,8 +363,8 @@ private bool RemoveDrawable3DComponent([NotNull] Drawable3DComponent componentTo { return Components.Remove(componentToRemove); } - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/ECS/Systems/SoundSystem.cs b/Komodo/Core/ECS/Systems/SoundSystem.cs index 544a5ebe..852dabf1 100644 --- a/Komodo/Core/ECS/Systems/SoundSystem.cs +++ b/Komodo/Core/ECS/Systems/SoundSystem.cs @@ -17,6 +17,8 @@ namespace Komodo.Core.ECS.Systems /// public class SoundSystem : ISystem { + #region Public + #region Constructors /// Reference to current instance. public SoundSystem(Game game) @@ -26,11 +28,9 @@ public SoundSystem(Game game) Game = game; _uninitializedComponents = new Queue(); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// All tracked objects. /// @@ -50,21 +50,9 @@ public SoundSystem(Game game) /// Whether or not the SoundSystem has called . /// public bool IsInitialized { get; private set; } - #endregion Public Members - - #region Private Members - /// - /// Tracks all potentially uninitialized objects. - /// All objects will be initialized in the , , or methods. - /// - private Queue _uninitializedComponents { get; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Adds a to the SoundSystem if the is not already present. /// @@ -76,9 +64,9 @@ public bool AddEntity([NotNull] Entity entityToAdd) { Entities = new Dictionary(); } - if (Entities.ContainsKey(entityToAdd.ID)) - { - return false; + if (Entities.ContainsKey(entityToAdd.ID)) + { + return false; } Game.SoundSystem.RemoveEntity(entityToAdd.ID); Entities[entityToAdd.ID] = entityToAdd; @@ -185,10 +173,13 @@ public bool RemoveEntity(Entity entityToRemove) } return false; } - #endregion Public Member Methods + #endregion + #endregion + + #region Internal - #region Internal Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -202,11 +193,11 @@ internal bool AddComponent(SoundComponent componentToAdd) } var parent = componentToAdd.Parent; if (!Entities.ContainsKey(parent.ID)) - { - return AddEntity(parent); + { + return AddEntity(parent); } - else - { + else + { return AddSoundComponent(componentToAdd); } } @@ -232,16 +223,28 @@ internal void UpdateComponents(GameTime gameTime) var componentsToUpdate = Components.ToArray(); foreach (var component in componentsToUpdate) { - if (component.IsEnabled && component.Parent != null && component.Parent.IsEnabled) - { + if (component.IsEnabled && component.Parent != null && component.Parent.IsEnabled) + { UpdateComponent(component, gameTime); - } + } } } } - #endregion Internal Member Methods + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Tracks all potentially uninitialized objects. + /// All objects will be initialized in the , , or methods. + /// + private Queue _uninitializedComponents { get; } + #endregion - #region Private Member Methods + #region Member Methods /// /// Adds a to relevant . If the is not initialized, it will be queued for initialization. /// @@ -282,18 +285,14 @@ private void InitializeComponents() private bool RemoveSoundComponent([NotNull] SoundComponent componentToRemove) { return Components.Remove(componentToRemove); - } - #endregion Private Member Methods - - #endregion Member Methods - - #region Static Methods + } + #endregion - #region Private Static Methods - /// - /// Performs the update logic on all objects. - /// - /// to update. + #region Static Methods + /// + /// Performs the update logic on all objects. + /// + /// to update. /// Time passed since last . private static void UpdateComponent(SoundComponent component, GameTime _) { @@ -310,8 +309,8 @@ private static void UpdateComponent(SoundComponent component, GameTime _) component._instances = instances; } - #endregion Private Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Graphics/GraphicsManager.cs b/Komodo/Core/Engine/Graphics/GraphicsManager.cs index deb7060b..12d4108e 100644 --- a/Komodo/Core/Engine/Graphics/GraphicsManager.cs +++ b/Komodo/Core/Engine/Graphics/GraphicsManager.cs @@ -15,6 +15,8 @@ namespace Komodo.Core.Engine.Graphics /// public class GraphicsManager { + #region Public + #region Constructors /// Reference to current instance. public GraphicsManager(Game game) @@ -22,11 +24,9 @@ public GraphicsManager(Game game) Game = game; GraphicsDeviceManager = new GraphicsDeviceManager(_monoGame); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Reference to current instance. /// @@ -118,20 +118,9 @@ public bool VSync } } } - #endregion Public Members - - #region Private Members - /// - /// Accessor for the underlying . - /// - private MonoGame _monoGame => Game?._monoGame; - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Applies pending changes to the . /// @@ -247,8 +236,19 @@ public void ToggleFullscreen(bool shouldApplyChanges = true) GraphicsDeviceManager.ApplyChanges(); } } - #endregion Public Member Methods + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Accessor for the underlying . + /// + private MonoGame _monoGame => Game?._monoGame; + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Graphics/Model.cs b/Komodo/Core/Engine/Graphics/Model.cs index 56fb2ad2..cb3c3311 100644 --- a/Komodo/Core/Engine/Graphics/Model.cs +++ b/Komodo/Core/Engine/Graphics/Model.cs @@ -1,65 +1,66 @@ using Komodo.Lib.Math; using BoundingBox = Microsoft.Xna.Framework.BoundingBox; -using Rectangle = Microsoft.Xna.Framework.Rectangle; - -namespace Komodo.Core.Engine.Graphics -{ +using Rectangle = Microsoft.Xna.Framework.Rectangle; +using MonoGameModel = Microsoft.Xna.Framework.Graphics.Model; + +namespace Komodo.Core.Engine.Graphics +{ /// /// Represents raw 3D model data. - /// - public class Model - { + /// + public class Model + { + #region Public + #region Constructors - /// data loaded in MonoGame's format. - public Model(Microsoft.Xna.Framework.Graphics.Model monoGameModel) - { - MonoGameModel = monoGameModel; - BoundingBox = CalculateBoundingBox(); - - var dimensions = BoundingBox.Max - BoundingBox.Min; - Depth = dimensions.Z; - Height = dimensions.Y; - Width = dimensions.X; - } - #endregion Constructors - - #region Members - - #region Public Members + /// data loaded in MonoGame's format. + public Model(MonoGameModel monoGameModel) + { + MonoGameModel = monoGameModel; + BoundingBox = CalculateBoundingBox(); + + var dimensions = BoundingBox.Max - BoundingBox.Min; + Depth = dimensions.Z; + Height = dimensions.Y; + Width = dimensions.X; + } + #endregion + + #region Members /// /// Z dimensional extremity. - /// - public float Depth { get; private set; } - + /// + public float Depth { get; private set; } + /// /// Y dimensional extremity. - /// - public float Height { get; private set; } - + /// + public float Height { get; private set; } + /// /// Provides a representing the bounds of the model. - /// - public BoundingBox BoundingBox { get; private set; } - + /// + public BoundingBox BoundingBox { get; private set; } + /// /// X dimensional extremity. - /// - public float Width { get; private set; } - + /// + public float Width { get; private set; } + /// /// Raw 3D model data. - /// - public Microsoft.Xna.Framework.Graphics.Model MonoGameModel { get; private set; } - #endregion Public Members + /// + public MonoGameModel MonoGameModel { get; private set; } + #endregion + + #endregion - #endregion Members + #region Private #region Member Methods - - #region Private Member Methods /// /// Calculates the bounding box from the raw model data. - /// + /// private BoundingBox CalculateBoundingBox() { Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue); @@ -87,8 +88,8 @@ private BoundingBox CalculateBoundingBox() return new BoundingBox(min.MonoGameVector, max.MonoGameVector); } - #endregion Private Member Methods + #endregion - #endregion Member Methods - } + #endregion + } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Graphics/Resolution.cs b/Komodo/Core/Engine/Graphics/Resolution.cs index ed8cba72..a49dda28 100644 --- a/Komodo/Core/Engine/Graphics/Resolution.cs +++ b/Komodo/Core/Engine/Graphics/Resolution.cs @@ -1,51 +1,47 @@ using System; -namespace Komodo.Core.Engine.Graphics -{ +namespace Komodo.Core.Engine.Graphics +{ /// /// Represents a screen resolution. - /// - public readonly struct Resolution : IEquatable - { + /// + public readonly struct Resolution : IEquatable + { + #region Public + #region Constructors /// Width of screen. - /// Width of screen. - public Resolution(int width, int height) - { - Width = width; - Height = height; - } - #endregion Constructors - - #region Members - - #region Public Members + /// Width of screen. + public Resolution(int width, int height) + { + Width = width; + Height = height; + } + #endregion + + #region Members /// /// Aspect ratio of the resolution. - /// - public float AspectRatio => Height == 0 ? 0 : Width / (float)Height; - + /// + public float AspectRatio => Height == 0 ? 0 : Width / (float)Height; + /// /// Height of the screen resolution. - /// + /// public int Height { get; } /// /// Width of the screen resolution. - /// + /// public int Width { get; } - #endregion Public Members + #endregion - #endregion Members - - #region Member Methods - - #region Public Member Methods + #region Member Methods /// /// Compares the equality of Resolution and an arbitrary type. /// /// Object to compare against. - /// Whether or not the Object is equivalent. + /// Whether or not the Object is equivalent. public override bool Equals(object obj) { if (obj is Resolution other) @@ -72,27 +68,23 @@ public bool Equals(Resolution other) public override int GetHashCode() { return Width.GetHashCode() + Height.GetHashCode(); - } - #endregion Public Member Methods - - #endregion Member Methods - - #region Static Methods + } + #endregion - #region Public Static Methods - /// Whether or not the Resolutions are equivalent. + #region Static Methods + /// Whether or not the Resolutions are equivalent. public static bool operator ==(Resolution left, Resolution right) { return left.Equals(right); - } + } - /// Whether or not the Resolutions are not equivalent. + /// Whether or not the Resolutions are not equivalent. public static bool operator !=(Resolution left, Resolution right) { return !(left == right); - } - #endregion Public Static Methods + } + #endregion - #endregion Static Methods - } -} + #endregion + } +} diff --git a/Komodo/Core/Engine/Graphics/Texture.cs b/Komodo/Core/Engine/Graphics/Texture.cs index 63482272..23919913 100644 --- a/Komodo/Core/Engine/Graphics/Texture.cs +++ b/Komodo/Core/Engine/Graphics/Texture.cs @@ -10,6 +10,8 @@ namespace Komodo.Core.Engine.Graphics /// public class Texture { + #region Public + #region Constructors /// /// Creates a Texture from data. @@ -65,11 +67,9 @@ public Texture( CreateMonoGameTexture(graphicsManager); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Height of the Texture data. /// @@ -96,9 +96,24 @@ public int Width /// Raw MonoGame . /// public Texture2D MonoGameTexture { get; private set; } - #endregion Public Members + #endregion - #region Private Members + #region Member Methods + /// + /// Accessor for raw data. + /// + /// + public Color[] GetData() + { + return _data; + } + #endregion + + #endregion + + #region Private + + #region Members /// /// Used to store the Texture height from to be used in . /// @@ -113,24 +128,9 @@ public int Width /// Raw data. /// private Color[] _data; - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods - /// - /// Accessor for raw data. - /// - /// - public Color[] GetData() - { - return _data; - } - #endregion Public Member Methods - - #region Private Member Methods /// /// /// @@ -204,8 +204,8 @@ private void SetData(Color[] value) { _data = value; } - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Input/InputInfo.cs b/Komodo/Core/Engine/Input/InputInfo.cs index 98e17934..97c56aa3 100644 --- a/Komodo/Core/Engine/Input/InputInfo.cs +++ b/Komodo/Core/Engine/Input/InputInfo.cs @@ -8,6 +8,8 @@ namespace Komodo.Core.Engine.Input /// public readonly struct InputInfo : IEquatable { + #region Public + #region Constructors /// /// Creates InputInfo for digital inputs. @@ -44,11 +46,9 @@ internal InputInfo(Inputs input, InputState state, Vector2 direction, float stre State = state; Strength = strength; } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Direction of the supplied . /// @@ -68,13 +68,9 @@ internal InputInfo(Inputs input, InputState state, Vector2 direction, float stre /// Strength of the . /// public float Strength { get; } - #endregion Public Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods /// /// Compares the equality of InputInfo and an arbitrary type. /// @@ -107,13 +103,9 @@ public override int GetHashCode() { return Direction.GetHashCode() + Input.GetHashCode() + State.GetHashCode() + Strength.GetHashCode(); } - #endregion Public Member Methods - - #endregion Member Methods + #endregion #region Static Methods - - #region Public Static Methods /// Whether or not the InputInfos are equivalent. public static bool operator ==(InputInfo left, InputInfo right) { @@ -125,8 +117,8 @@ public override int GetHashCode() { return !(left == right); } - #endregion Public Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Input/InputManager.cs b/Komodo/Core/Engine/Input/InputManager.cs index 107c318e..98e86c67 100644 --- a/Komodo/Core/Engine/Input/InputManager.cs +++ b/Komodo/Core/Engine/Input/InputManager.cs @@ -18,65 +18,9 @@ namespace Komodo.Core.Engine.Input /// public static class InputManager { - /// - /// Sets up the input maps and input device states. - /// - static InputManager() - { - _inputMaps = new Dictionary>[4]; - for (int i = 0; i < _inputMaps.Length; i++) - { - _inputMaps[i] = new Dictionary>(); - } - _gamePadStates = new GamePadState[4]; - _previousGamePadStates = new GamePadState[4]; - Update(); - } - - #region Static Members - - #region Private Static Members - /// - /// Current state of all 4 supported gamepads. - /// - private static GamePadState[] _gamePadStates { get; set; } - - /// - /// State of input maps for all 4 supported players. - /// - private static Dictionary>[] _inputMaps { get; } - - /// - /// Current state of 1 supported keyboard. - /// - private static KeyboardState _keyboardState { get; set; } - - /// - /// Current state of 1 supported mouse. - /// - private static MouseState _mouseState { get; set; } - - /// - /// Previous state of all 4 supported gamepads. - /// - private static GamePadState[] _previousGamePadStates { get; set; } - - /// - /// Previous state of 1 supported keyboard. - /// - private static KeyboardState _previousKeyboardState { get; set; } - - /// - /// Previous state of 1 supported mouse. - /// - private static MouseState _previousMouseState { get; set; } - #endregion Private Static Members - - #endregion Static Members + #region Public #region Static Methods - - #region Public Static Methods /// /// Adds an input mapping to the specified player. /// @@ -277,9 +221,13 @@ public static void SetMousePosition(int x, int y) { Mouse.SetPosition(x, y); } - #endregion Public Static Methods + #endregion + + #endregion - #region Internal Static Methods + #region Internal + + #region Static Methods /// /// Updates current and previous input states across all input devices. /// @@ -298,10 +246,50 @@ internal static void Update() _gamePadStates[i] = GamePad.GetState(playerIndex); } } - #endregion Internal Static Methods + #endregion + + #endregion + #region Private + + #region Static Members + /// + /// Current state of all 4 supported gamepads. + /// + private static GamePadState[] _gamePadStates { get; set; } + + /// + /// State of input maps for all 4 supported players. + /// + private static Dictionary>[] _inputMaps { get; } - #region Private Static Methods + /// + /// Current state of 1 supported keyboard. + /// + private static KeyboardState _keyboardState { get; set; } + + /// + /// Current state of 1 supported mouse. + /// + private static MouseState _mouseState { get; set; } + + /// + /// Previous state of all 4 supported gamepads. + /// + private static GamePadState[] _previousGamePadStates { get; set; } + + /// + /// Previous state of 1 supported keyboard. + /// + private static KeyboardState _previousKeyboardState { get; set; } + + /// + /// Previous state of 1 supported mouse. + /// + private static MouseState _previousMouseState { get; set; } + #endregion + + #region Static Methods /// /// Retrives for the given . /// @@ -386,8 +374,25 @@ private static PlayerIndex GetPlayerIndex(int playerIndex) _ => PlayerIndex.One, }; } - #endregion Private Static Methods - - #endregion Static Methods + #endregion + + #endregion + + #region Static Constructor + /// + /// Sets up the input maps and input device states. + /// + static InputManager() + { + _inputMaps = new Dictionary>[4]; + for (int i = 0; i < _inputMaps.Length; i++) + { + _inputMaps[i] = new Dictionary>(); + } + _gamePadStates = new GamePadState[4]; + _previousGamePadStates = new GamePadState[4]; + Update(); + } + #endregion } } \ No newline at end of file diff --git a/Komodo/Core/Engine/Input/InputMapper.cs b/Komodo/Core/Engine/Input/InputMapper.cs index 546d5a49..705c8031 100644 --- a/Komodo/Core/Engine/Input/InputMapper.cs +++ b/Komodo/Core/Engine/Input/InputMapper.cs @@ -1,25 +1,25 @@ -using Buttons = Microsoft.Xna.Framework.Input.Buttons; -using ButtonState = Microsoft.Xna.Framework.Input.ButtonState; -using Keys = Microsoft.Xna.Framework.Input.Keys; -using KeyState = Microsoft.Xna.Framework.Input.KeyState; - -namespace Komodo.Core.Engine.Input -{ +using Buttons = Microsoft.Xna.Framework.Input.Buttons; +using ButtonState = Microsoft.Xna.Framework.Input.ButtonState; +using Keys = Microsoft.Xna.Framework.Input.Keys; +using KeyState = Microsoft.Xna.Framework.Input.KeyState; + +namespace Komodo.Core.Engine.Input +{ /// /// Static class for mapping and MonoGame input identifiers to and from one another. - /// - internal static class InputMapper + /// + internal static class InputMapper { - #region Static Methods + #region Internal - #region Public Static Methods + #region Static Methods /// /// Converts to . /// /// MonoGame button. - /// Converted MonoGame button. - public static Inputs ToInputs(Buttons button) - { + /// Converted MonoGame button. + internal static Inputs ToInputs(Buttons button) + { return button switch { Buttons.A => Inputs.ButtonA, @@ -41,15 +41,15 @@ public static Inputs ToInputs(Buttons button) Buttons.Y => Inputs.ButtonY, _ => Inputs.Undefined, }; - } - + } + /// /// Converts to . /// /// MonoGame key. - /// Converted MonoGame key. - public static Inputs ToInputs(Keys key) - { + /// Converted MonoGame key. + internal static Inputs ToInputs(Keys key) + { return key switch { Keys.A => Inputs.KeyA, @@ -109,45 +109,45 @@ public static Inputs ToInputs(Keys key) Keys.Z => Inputs.KeyZ, _ => Inputs.Undefined, }; - } - + } + /// /// Converts to . /// /// MonoGame button state. - /// Converted MonoGame button state. - public static InputState ToInputState(ButtonState buttonState) - { + /// Converted MonoGame button state. + internal static InputState ToInputState(ButtonState buttonState) + { return buttonState switch { ButtonState.Released => InputState.Up, ButtonState.Pressed => InputState.Down, _ => InputState.Undefined, }; - } - + } + /// /// Converts to . /// /// MonoGame key state. - /// Converted MonoGame key state. - public static InputState ToInputState(KeyState keyState) - { + /// Converted MonoGame key state. + internal static InputState ToInputState(KeyState keyState) + { return keyState switch { KeyState.Up => InputState.Up, KeyState.Down => InputState.Down, _ => InputState.Undefined, }; - } - + } + /// /// Converts to . /// /// Komodo input. - /// Converted Komodo input as . - public static Buttons? ToMonoGameButton(Inputs input) - { + /// Converted Komodo input as . + internal static Buttons? ToMonoGameButton(Inputs input) + { return input switch { Inputs.ButtonA => Buttons.A, @@ -175,9 +175,9 @@ public static InputState ToInputState(KeyState keyState) /// Converts to . /// /// Komodo input. - /// Converted Komodo input as . - public static Keys? ToMonoGameKey(Inputs input) - { + /// Converted Komodo input as . + internal static Keys? ToMonoGameKey(Inputs input) + { return input switch { Inputs.KeyA => Keys.A, @@ -238,8 +238,8 @@ public static InputState ToInputState(KeyState keyState) _ => null, }; } - #endregion Public Static Methods + #endregion - #endregion Static Methods - } + #endregion + } } \ No newline at end of file diff --git a/Komodo/Core/Game.cs b/Komodo/Core/Game.cs index ab8d922d..dca3a00f 100644 --- a/Komodo/Core/Game.cs +++ b/Komodo/Core/Game.cs @@ -20,6 +20,8 @@ namespace Komodo.Core /// public class Game : IDisposable { + #region Public + #region Constructors /// /// Creates the Game instance, instantiating the underlying instance, , and objects. @@ -41,11 +43,9 @@ public Game() Render3DSystems = new List(); SoundSystem = new SoundSystem(this); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Manages all objects. /// @@ -121,32 +121,10 @@ public string Title } } } - #endregion Public Members - - #region Internal Members - /// - /// Provides access to MonoGame APIs. - /// - internal readonly MonoGame _monoGame; - #endregion Internal Members - - #endregion Members - - #region Static Members - - #region Public Static Members - /// - /// Provides access to the content files compiled by the MonoGame Content Pipeline (Releases: https://github.com/MonoGame/MonoGame/releases). - /// - public static ContentManager Content { get; private set; } - #endregion Public Static Members - - #endregion Static Members + #endregion #region Member Methods - #region Public Member Methods - #region Add event handlers /// /// Adds a handler to be executed when game is exiting. @@ -346,7 +324,7 @@ public void AddWindowSizeChangedEventHandler(EventHandler handler) { _monoGame.Window.ClientSizeChanged += handler; } - #endregion Add event handlers + #endregion #region Remove event handlers /// @@ -420,7 +398,7 @@ public void RemoveWindowSizeChangedEventHandler(EventHandler handler) { _monoGame.Window.ClientSizeChanged -= handler; } - #endregion Remove event handlers + #endregion /// /// Creates and begins tracking a new . @@ -584,9 +562,27 @@ public void Update(GameTime gameTime) system.PostUpdate(gameTime); } } - #endregion Public Member Methods + #endregion + + #region Static Members + /// + /// Provides access to the content files compiled by the MonoGame Content Pipeline (Releases: https://github.com/MonoGame/MonoGame/releases). + /// + public static ContentManager Content { get; private set; } + #endregion + + #endregion + + #region Internal + + #region Internal Members + /// + /// Provides access to MonoGame APIs. + /// + internal readonly MonoGame _monoGame; + #endregion - #endregion Member Methods + #endregion #region IDisposable Support /// diff --git a/Komodo/Core/MonoGame.cs b/Komodo/Core/MonoGame.cs index 8d0b0d17..021d42c3 100644 --- a/Komodo/Core/MonoGame.cs +++ b/Komodo/Core/MonoGame.cs @@ -2,40 +2,33 @@ namespace Komodo.Core { - /// - /// Underlying implementation of the MonoGame . + /// + /// Underlying implementation of the MonoGame . /// internal class MonoGame : Microsoft.Xna.Framework.Game { + #region Internal + #region Constructors - /// - /// Defines the root directory of content files. - /// + /// + /// Defines the root directory of content files. + /// /// /// Reference to current instance. - public MonoGame(Game game) + internal MonoGame(Game game) { _game = game; Content.RootDirectory = "Content/MonoGame"; } - #endregion Constructors + #endregion - #region Members + #endregion + + #region Protected - #region Private Members - /// - /// Reference to current instance. - /// - private Game _game { get; } - #endregion Private Members - - #endregion Members - #region Member Methods - - #region Protected Member Methods - /// - /// Passthrough function to . - /// + /// + /// Passthrough function to . + /// /// Time passed since last . protected override void Draw(GameTime gameTime) { @@ -44,8 +37,8 @@ protected override void Draw(GameTime gameTime) base.Draw(gameTime); } - /// - /// Initializes all low-level resources so can be initialized. + /// + /// Initializes all low-level resources so can be initialized. /// protected override void Initialize() { @@ -54,16 +47,16 @@ protected override void Initialize() _game.Initialize(); } - /// - /// Used to load content files before beginning game loop. [Unused] + /// + /// Used to load content files before beginning game loop. [Unused] /// protected override void LoadContent() { } - /// - /// Passthrough function to . - /// + /// + /// Passthrough function to . + /// /// Time passed since last . protected override void Update(GameTime gameTime) { @@ -71,8 +64,19 @@ protected override void Update(GameTime gameTime) base.Update(gameTime); } - #endregion Protected Member Methods + #endregion + + #endregion + + #region Private + + #region Members + /// + /// Reference to current instance. + /// + private Game _game { get; } + #endregion - #endregion Member Methods + #endregion } } diff --git a/Komodo/Core/Physics/Box.cs b/Komodo/Core/Physics/Box.cs index a43eed32..2544758d 100644 --- a/Komodo/Core/Physics/Box.cs +++ b/Komodo/Core/Physics/Box.cs @@ -9,6 +9,8 @@ namespace Komodo.Core.Physics /// public class Box : IPhysicsShape { + #region Public + #region Constructors public Box(float width, float height, float depth, float mass) { @@ -25,11 +27,9 @@ public Box(Vector3 dimensions, float mass) Depth = dimensions.Z; Mass = mass; } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Depth of the Box. /// @@ -113,27 +113,27 @@ public float Width UpdateMomentOfInertia(); } } - #endregion Public Members - - #region Private Members - private float _depth { get; set; } - private float _height { get; set; } - private float _mass { get; set; } - private float _width { get; set; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public IPhysicsShape GetScaledShape(Vector3 scale) { return new Box(Width * scale.X, Height * scale.Y, Depth * scale.Z, Mass); } - #endregion Public Member Methods + #endregion + + #endregion - #region Private Member Methods + #region Private + + #region Members + private float _depth { get; set; } + private float _height { get; set; } + private float _mass { get; set; } + private float _width { get; set; } + #endregion + + #region Member Methods /// /// Updates the moment of intertia. Called whenever relevant fields have been updated. /// @@ -144,8 +144,8 @@ private void UpdateMomentOfInertia() float z = Mass * (Width * Width + Height * Height) / 12f; MomentOfInertia = new Vector3(x, y, z); } - #endregion Private Member Methods - - #endregion Member Methods + #endregion + + #endregion } -} +} \ No newline at end of file diff --git a/Komodo/Core/Physics/Collision.cs b/Komodo/Core/Physics/Collision.cs index 54286db9..be275c37 100644 --- a/Komodo/Core/Physics/Collision.cs +++ b/Komodo/Core/Physics/Collision.cs @@ -9,6 +9,8 @@ namespace Komodo.Core.Physics /// public class Collision { + #region Public + #region Constructors public Collision(bool isColliding, Vector3 normal, float penetrationDepth) { @@ -17,11 +19,9 @@ public Collision(bool isColliding, Vector3 normal, float penetrationDepth) PenetrationDepth = penetrationDepth; _isResolved = false; } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// Correction vector for the collision. Adding this to the affected object's position will separate the collision. /// @@ -41,12 +41,16 @@ public Collision(bool isColliding, Vector3 normal, float penetrationDepth) /// Depth of penetration of the collision. /// public readonly float PenetrationDepth; - #endregion Public Members + #endregion + + #endregion - #region Internal Members + #region Internal + + #region Members internal bool _isResolved { get; set; } - #endregion Internal Members + #endregion - #endregion Members + #endregion } } diff --git a/Komodo/Core/Physics/IPhysicsShape.cs b/Komodo/Core/Physics/IPhysicsShape.cs index 585fee45..dfc0f198 100644 --- a/Komodo/Core/Physics/IPhysicsShape.cs +++ b/Komodo/Core/Physics/IPhysicsShape.cs @@ -8,8 +8,6 @@ namespace Komodo.Core.Physics public interface IPhysicsShape { #region Members - - #region Public Members /// /// Mass of the given shape. /// @@ -18,16 +16,10 @@ public interface IPhysicsShape /// Moment of inertia for the given shape. This is usually a calculated value based on mass, location of vertices, etc. /// Vector3 MomentOfInertia { get; } - #endregion Public Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods IPhysicsShape GetScaledShape(Vector3 scale); - #endregion Public Member Methods - - #endregion Member Methods + #endregion } } diff --git a/Komodo/Core/Physics/PhysicsMaterial.cs b/Komodo/Core/Physics/PhysicsMaterial.cs index f1d4d69a..6151b6fc 100644 --- a/Komodo/Core/Physics/PhysicsMaterial.cs +++ b/Komodo/Core/Physics/PhysicsMaterial.cs @@ -9,6 +9,8 @@ namespace Komodo.Core.Physics /// public class PhysicsMaterial { + #region Public + #region Constructors /// /// Creates a new physics material. Physics material names are unique identifiers. @@ -36,11 +38,9 @@ static PhysicsMaterial() _materials = new Dictionary(); _ = new PhysicsMaterial("default"); } - #endregion Constructors + #endregion #region Members - - #region Public Members /// /// defines how an object's angular veloctity is reduced over time with no interaction from other physics objects. /// @@ -168,30 +168,9 @@ public float Restitution } } } - #endregion Public Members - - #region Internal Members - internal float _angularDamping { get; set; } - internal float _angularDampingLimit { get; set; } - internal float _friction { get; set; } - internal float _linearDamping { get; set; } - internal float _linearDampingLimit { get; set; } - internal float _restitution { get; set; } - #endregion Internal Members - - #endregion Members - - #region Static Members - - #region Private Static Members - private static Dictionary _materials { get; set; } - #endregion Private Static Members - - #endregion Static Members + #endregion #region Static Member Methods - - #region Public Static Member Methods /// /// Provides the PhysicsMaterial instance for the given name. /// @@ -208,8 +187,29 @@ public static PhysicsMaterial GetPhysicsMaterial(string name) return material; } - #endregion Public Static Member Methods + #endregion + + #endregion + + #region Internal + + #region Members + internal float _angularDamping { get; set; } + internal float _angularDampingLimit { get; set; } + internal float _friction { get; set; } + internal float _linearDamping { get; set; } + internal float _linearDampingLimit { get; set; } + internal float _restitution { get; set; } + #endregion + + #endregion + + #region Private + + #region Static Members + private static Dictionary _materials { get; set; } + #endregion - #endregion Static Member Methods + #endregion } } diff --git a/Komodo/Core/Physics/Sphere.cs b/Komodo/Core/Physics/Sphere.cs index 7ba449a2..19079641 100644 --- a/Komodo/Core/Physics/Sphere.cs +++ b/Komodo/Core/Physics/Sphere.cs @@ -7,6 +7,8 @@ namespace Komodo.Core.Physics /// public class Sphere : IPhysicsShape { + #region Public + #region Constructors public Sphere(float radius, float mass) { @@ -14,10 +16,9 @@ public Sphere(float radius, float mass) Mass = mass; } - #endregion Constructors - #region Members + #endregion - #region Public Members + #region Members /// /// Radius of the Sphere. /// @@ -57,25 +58,25 @@ public float Mass /// Moment of inertia for the Sphere /// public Vector3 MomentOfInertia { get; private set; } - #endregion Public Members - - #region Private Members - private float _mass { get; set; } - private float _radius { get; set; } - #endregion Private Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public IPhysicsShape GetScaledShape(Vector3 scale) { return new Sphere(Radius * scale.X, Mass); } - #endregion Public Member Methods + #endregion - #region Private Member Methods + #endregion + + #region Private + + #region Members + private float _mass { get; set; } + private float _radius { get; set; } + #endregion + + #region Member Methods /// /// Updates the moment of intertia. Called whenever relevant fields have been updated. /// @@ -84,8 +85,8 @@ private void UpdateMomentOfInertia() float moment = Mass * Radius * Radius * 0.4f; MomentOfInertia = new Vector3(moment, moment, moment); } - #endregion Private Member Methods + #endregion - #endregion Member Methods + #endregion } } diff --git a/Komodo/Lib/Compression/Brotli.cs b/Komodo/Lib/Compression/Brotli.cs index c0343dcd..186df257 100644 --- a/Komodo/Lib/Compression/Brotli.cs +++ b/Komodo/Lib/Compression/Brotli.cs @@ -6,41 +6,40 @@ namespace Komodo.Lib.Compression { public static class Brotli { + #region Public #region Static Methods - - #region Public Static Methods public static byte[] Compress(string data) => Compress(Encoding.ASCII.GetBytes(data)); public static byte[] Compress(byte[] data) { - using var compressedStream = new MemoryStream(); - using (var decompressedStream = new MemoryStream(data)) - { - using var brotli = new BrotliStream( - compressedStream, - CompressionMode.Compress - ); - decompressedStream.CopyTo(brotli); - } + using var compressedStream = new MemoryStream(); + using (var decompressedStream = new MemoryStream(data)) + { + using var brotli = new BrotliStream( + compressedStream, + CompressionMode.Compress + ); + decompressedStream.CopyTo(brotli); + } return compressedStream.ToArray(); } public static byte[] Decompress(byte[] data) { - using var decompressedStream = new MemoryStream(); - using (var compressedStream = new MemoryStream(data)) - { - using var brotli = new BrotliStream( - compressedStream, - CompressionMode.Decompress - ); - brotli.CopyTo(decompressedStream); - } + using var decompressedStream = new MemoryStream(); + using (var compressedStream = new MemoryStream(data)) + { + using var brotli = new BrotliStream( + compressedStream, + CompressionMode.Decompress + ); + brotli.CopyTo(decompressedStream); + } return decompressedStream.ToArray(); } - #endregion Public Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Compression/GZIP.cs b/Komodo/Lib/Compression/GZIP.cs index 3070ddea..4713e94b 100644 --- a/Komodo/Lib/Compression/GZIP.cs +++ b/Komodo/Lib/Compression/GZIP.cs @@ -6,41 +6,40 @@ namespace Komodo.Lib.Compression { public static class GZIP { + #region Public #region Static Methods - - #region Public Static Methods public static byte[] Compress(string data) => Compress(Encoding.ASCII.GetBytes(data)); public static byte[] Compress(byte[] data) { - using var compressedStream = new MemoryStream(); - using (var decompressedStream = new MemoryStream(data)) - { - using var gzip = new GZipStream( - compressedStream, - CompressionMode.Compress - ); - decompressedStream.CopyTo(gzip); - } + using var compressedStream = new MemoryStream(); + using (var decompressedStream = new MemoryStream(data)) + { + using var gzip = new GZipStream( + compressedStream, + CompressionMode.Compress + ); + decompressedStream.CopyTo(gzip); + } return compressedStream.ToArray(); } public static byte[] Decompress(byte[] data) { - using var decompressedStream = new MemoryStream(); - using (var compressedStream = new MemoryStream(data)) - { - using var gzip = new GZipStream( - compressedStream, - CompressionMode.Decompress - ); - gzip.CopyTo(decompressedStream); - } + using var decompressedStream = new MemoryStream(); + using (var compressedStream = new MemoryStream(data)) + { + using var gzip = new GZipStream( + compressedStream, + CompressionMode.Decompress + ); + gzip.CopyTo(decompressedStream); + } return decompressedStream.ToArray(); } - #endregion Public Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Math/Vector2.cs b/Komodo/Lib/Math/Vector2.cs index 81833e7c..79f6448d 100644 --- a/Komodo/Lib/Math/Vector2.cs +++ b/Komodo/Lib/Math/Vector2.cs @@ -8,6 +8,8 @@ namespace Komodo.Lib.Math { public readonly struct Vector2 : IEquatable { + #region Public + #region Constructors public Vector2(MonoGameVector2 vector) : this(vector.X, vector.Y) { @@ -21,13 +23,11 @@ public Vector2(float x = 0f, float y = 0f) { MonoGameVector = new MonoGameVector2(x, y); } - #endregion Constructors + #endregion #region Members - - #region Public Members public MonoGameVector2 MonoGameVector { get; } - public float X + public float X { get { @@ -35,7 +35,7 @@ public float X } } - public float Y + public float Y { get { @@ -44,7 +44,7 @@ public float Y } #region Swizzling - public Vector2 XY + public Vector2 XY { get { @@ -52,62 +52,51 @@ public Vector2 XY } } - public Vector2 YX + public Vector2 YX { get { return new Vector2(Y, X); } - } - #endregion Swizzling - #endregion Public Members - - #endregion Members - + } + #endregion + + #endregion + + #region Member Methods + public override bool Equals(object obj) + { + if (obj is Vector2 other) + { + return Equals(other); + } + return false; + } + public bool Equals(Vector2 other) + { + return X == other.X && Y == other.Y; + } + public override int GetHashCode() + { + return X.GetHashCode() + Y.GetHashCode(); + } + + public float Length() + { + return MonoGameVector.Length(); + } + #endregion + #region Static Members - - #region Public Static Members public static Vector2 One => new Vector2(1f, 1f); public static Vector2 Down => new Vector2(0f, -1f); public static Vector2 Left => new Vector2(-1f, 0f); public static Vector2 Right => new Vector2(1f, 0f); public static Vector2 Up => new Vector2(0f, 1f); - public static Vector2 Zero => new Vector2(); - #endregion Public Static Members - - #endregion Static Members - - #region Member Methods - - #region Public Member Methods - public override bool Equals(object obj) - { - if (obj is Vector2 other) - { - return Equals(other); - } - return false; - } - public bool Equals(Vector2 other) - { - return X == other.X && Y == other.Y; - } - public override int GetHashCode() - { - return X.GetHashCode() + Y.GetHashCode(); - } - - public float Length() - { - return MonoGameVector.Length(); - } - #endregion Public Member Methods - - #endregion Member Methods - + public static Vector2 Zero => new Vector2(); + #endregion + #region Static Methods - - #region Public Static Methods public static Vector2 operator +(Vector2 vector) => vector; public static Vector2 operator -(Vector2 vector) => new Vector2(-vector.X, -vector.Y); @@ -119,8 +108,8 @@ public static Vector2 Add(Vector2 left, Vector2 right) public static Vector2 operator +(Vector2 left, Vector2 right) { return Add(left, right); - } - + } + public static Vector2 Divide(Vector2 vector, float scale) { var result = MonoGameVector2.Divide(vector.MonoGameVector, scale); @@ -131,14 +120,14 @@ public static Vector2 Divide(Vector2 vector, float scale) return Divide(left, scale); } - public static bool operator ==(Vector2 left, Vector2 right) - { - return left.Equals(right); + public static bool operator ==(Vector2 left, Vector2 right) + { + return left.Equals(right); } - public static bool operator !=(Vector2 left, Vector2 right) - { - return !(left == right); + public static bool operator !=(Vector2 left, Vector2 right) + { + return !(left == right); } public static Vector2 Multiply(Vector2 vector, float scale) @@ -159,14 +148,14 @@ public static Vector2 Subtract(Vector2 left, Vector2 right) public static Vector2 operator -(Vector2 left, Vector2 right) { return Subtract(left, right); - } - + } + public static Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max) { var result = MonoGameVector2.Clamp(value.MonoGameVector, min.MonoGameVector, max.MonoGameVector); return new Vector2(result); - } - + } + public static Vector2 Transform(Vector2 vector, Matrix transform) { return new Vector2( @@ -179,42 +168,42 @@ public static Vector2 Transform(Vector2 vector, Quaternion transform) return new Vector2( MonoGameVector2.Transform(vector.MonoGameVector, transform) ); - } - + } + public static Vector2 Normalize(Vector2 vectorToNormalize) { var normalizedVector = MonoGameVector2.Normalize(vectorToNormalize.MonoGameVector); return new Vector2(normalizedVector); - } - - public static float Distance(Vector2 a, Vector2 b) - { - return MonoGameVector2.Distance(a.MonoGameVector, b.MonoGameVector); - } - - public static float DistanceSquared(Vector2 a, Vector2 b) - { - return MonoGameVector2.DistanceSquared(a.MonoGameVector, b.MonoGameVector); - } - - public static float Dot(Vector2 a, Vector2 b) - { - return MonoGameVector2.Dot(a.MonoGameVector, b.MonoGameVector); - } - - /// - /// Returns the reflection of a vector over a given normal. - /// - /// Vector to reflect over the normal. - /// Normal to reflect the vector over. - /// Reflected vector over the the normal. - public static Vector2 Reflect(Vector2 vectorToReflect, Vector2 normal) - { - var reflection = MonoGameVector2.Reflect(vectorToReflect.MonoGameVector, normal.MonoGameVector); - return new Vector2(reflection); - } - #endregion Public Static Methods - - #endregion Static Methods + } + + public static float Distance(Vector2 a, Vector2 b) + { + return MonoGameVector2.Distance(a.MonoGameVector, b.MonoGameVector); + } + + public static float DistanceSquared(Vector2 a, Vector2 b) + { + return MonoGameVector2.DistanceSquared(a.MonoGameVector, b.MonoGameVector); + } + + public static float Dot(Vector2 a, Vector2 b) + { + return MonoGameVector2.Dot(a.MonoGameVector, b.MonoGameVector); + } + + /// + /// Returns the reflection of a vector over a given normal. + /// + /// Vector to reflect over the normal. + /// Normal to reflect the vector over. + /// Reflected vector over the the normal. + public static Vector2 Reflect(Vector2 vectorToReflect, Vector2 normal) + { + var reflection = MonoGameVector2.Reflect(vectorToReflect.MonoGameVector, normal.MonoGameVector); + return new Vector2(reflection); + } + #endregion + + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Math/Vector3.cs b/Komodo/Lib/Math/Vector3.cs index c68122d5..988d5217 100644 --- a/Komodo/Lib/Math/Vector3.cs +++ b/Komodo/Lib/Math/Vector3.cs @@ -8,6 +8,8 @@ namespace Komodo.Lib.Math { public readonly struct Vector3 : IEquatable { + #region Public + #region Constructors public Vector3(MonoGameVector3 vector) : this(vector.X, vector.Y, vector.Z) { @@ -21,13 +23,11 @@ public Vector3(float x = 0f, float y = 0f, float z = 0f) { MonoGameVector = new MonoGameVector3(x, y, z); } - #endregion Constructors - - #region Members + #endregion #region Public Members public MonoGameVector3 MonoGameVector { get; } - public float X + public float X { get { @@ -35,7 +35,7 @@ public float X } } - public float Y + public float Y { get { @@ -43,7 +43,7 @@ public float Y } } - public float Z + public float Z { get { @@ -52,7 +52,7 @@ public float Z } #region Swizzling - public Vector2 XY + public Vector2 XY { get { @@ -60,7 +60,7 @@ public Vector2 XY } } - public Vector2 XZ + public Vector2 XZ { get { @@ -68,7 +68,7 @@ public Vector2 XZ } } - public Vector2 YX + public Vector2 YX { get { @@ -76,7 +76,7 @@ public Vector2 YX } } - public Vector2 YZ + public Vector2 YZ { get { @@ -84,7 +84,7 @@ public Vector2 YZ } } - public Vector2 ZX + public Vector2 ZX { get { @@ -92,7 +92,7 @@ public Vector2 ZX } } - public Vector2 ZY + public Vector2 ZY { get { @@ -100,7 +100,7 @@ public Vector2 ZY } } - public Vector3 XYZ + public Vector3 XYZ { get { @@ -108,7 +108,7 @@ public Vector3 XYZ } } - public Vector3 XZY + public Vector3 XZY { get { @@ -116,7 +116,7 @@ public Vector3 XZY } } - public Vector3 YXZ + public Vector3 YXZ { get { @@ -124,7 +124,7 @@ public Vector3 YXZ } } - public Vector3 YZX + public Vector3 YZX { get { @@ -132,7 +132,7 @@ public Vector3 YZX } } - public Vector3 ZXY + public Vector3 ZXY { get { @@ -140,73 +140,61 @@ public Vector3 ZXY } } - public Vector3 ZYX + public Vector3 ZYX { get { return new Vector3(Z, Y, X); } - } - #endregion Swizzling - - #endregion Public Members - - #endregion Members - + } + #endregion + + #endregion + + #region Member Methods + public override bool Equals(object obj) + { + if (obj is Vector3 other) + { + return Equals(other); + } + return false; + } + public bool Equals(Vector3 other) + { + return X == other.X && Y == other.Y && Z == other.Z; + } + public override int GetHashCode() + { + return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode(); + } + + public float Length() + { + return MonoGameVector.Length(); + } + #endregion + #region Static Members - - #region Public Static Members public static Vector3 One => new Vector3(1f, 1f, 1f); public static Vector3 Zero => new Vector3(); - public static Vector3 Up => new Vector3(0f, 1f, 0f); - public static Vector3 Down => -Up; - public static Vector3 Right => new Vector3(1f, 0f, 0f); - public static Vector3 Left => -Right; - public static Vector3 Back => new Vector3(0f, 0f, 1f); - public static Vector3 Forward => -Back; - public static Vector3 Max(Vector3 left, Vector3 right) - { - return new Vector3(MonoGameVector3.Max(left.MonoGameVector, right.MonoGameVector)); - } - public static Vector3 Min(Vector3 left, Vector3 right) - { - return new Vector3(MonoGameVector3.Min(left.MonoGameVector, right.MonoGameVector)); - } - #endregion Public Static Members - - #endregion Static Members - - #region Member Methods - - #region Public Member Methods - public override bool Equals(object obj) - { - if (obj is Vector3 other) - { - return Equals(other); - } - return false; - } - public bool Equals(Vector3 other) - { - return X == other.X && Y == other.Y && Z == other.Z; - } - public override int GetHashCode() - { - return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode(); - } - - public float Length() - { - return MonoGameVector.Length(); - } - #endregion Public Member Methods - - #endregion Member Methods - + public static Vector3 Up => new Vector3(0f, 1f, 0f); + public static Vector3 Down => -Up; + public static Vector3 Right => new Vector3(1f, 0f, 0f); + public static Vector3 Left => -Right; + public static Vector3 Back => new Vector3(0f, 0f, 1f); + public static Vector3 Forward => -Back; + public static Vector3 Max(Vector3 left, Vector3 right) + { + return new Vector3(MonoGameVector3.Max(left.MonoGameVector, right.MonoGameVector)); + } + public static Vector3 Min(Vector3 left, Vector3 right) + { + return new Vector3(MonoGameVector3.Min(left.MonoGameVector, right.MonoGameVector)); + } + #endregion + #region Static Methods - - #region Public Static Methods public static Vector3 operator +(Vector3 vector) => vector; public static Vector3 operator -(Vector3 vector) => new Vector3(-vector.X, -vector.Y, -vector.Z); @@ -218,8 +206,8 @@ public static Vector3 Add(Vector3 left, Vector3 right) public static Vector3 operator +(Vector3 left, Vector3 right) { return Add(left, right); - } - + } + public static Vector3 Divide(Vector3 vector, float scale) { var result = MonoGameVector3.Divide(vector.MonoGameVector, scale); @@ -230,14 +218,14 @@ public static Vector3 Divide(Vector3 vector, float scale) return Divide(left, scale); } - public static bool operator ==(Vector3 left, Vector3 right) - { - return left.Equals(right); + public static bool operator ==(Vector3 left, Vector3 right) + { + return left.Equals(right); } - public static bool operator !=(Vector3 left, Vector3 right) - { - return !(left == right); + public static bool operator !=(Vector3 left, Vector3 right) + { + return !(left == right); } public static Vector3 Multiply(Vector3 left, float scale) @@ -296,39 +284,39 @@ public static Vector3 Transform(Vector3 vector, Quaternion transform) ); } - public static Vector3 Cross(Vector3 a, Vector3 b) - { - return new Vector3(MonoGameVector3.Cross(a.MonoGameVector, b.MonoGameVector)); + public static Vector3 Cross(Vector3 a, Vector3 b) + { + return new Vector3(MonoGameVector3.Cross(a.MonoGameVector, b.MonoGameVector)); } - public static float Dot(Vector3 a, Vector3 b) - { - return MonoGameVector3.Dot(a.MonoGameVector, b.MonoGameVector); + public static float Dot(Vector3 a, Vector3 b) + { + return MonoGameVector3.Dot(a.MonoGameVector, b.MonoGameVector); } - public static float Distance(Vector3 a, Vector3 b) - { - return MonoGameVector3.Distance(a.MonoGameVector, b.MonoGameVector); + public static float Distance(Vector3 a, Vector3 b) + { + return MonoGameVector3.Distance(a.MonoGameVector, b.MonoGameVector); } - public static float DistanceSquared(Vector3 a, Vector3 b) - { - return MonoGameVector3.DistanceSquared(a.MonoGameVector, b.MonoGameVector); + public static float DistanceSquared(Vector3 a, Vector3 b) + { + return MonoGameVector3.DistanceSquared(a.MonoGameVector, b.MonoGameVector); } - /// - /// Returns the reflection of a vector over a given normal. - /// - /// Vector to reflect over the normal. - /// Normal to reflect the vector over. - /// Reflected vector over the the normal. - public static Vector3 Reflect(Vector3 vectorToReflect, Vector3 normal) - { - var reflection = MonoGameVector3.Reflect(vectorToReflect.MonoGameVector, normal.MonoGameVector); - return new Vector3(reflection); + /// + /// Returns the reflection of a vector over a given normal. + /// + /// Vector to reflect over the normal. + /// Normal to reflect the vector over. + /// Reflected vector over the the normal. + public static Vector3 Reflect(Vector3 vectorToReflect, Vector3 normal) + { + var reflection = MonoGameVector3.Reflect(vectorToReflect.MonoGameVector, normal.MonoGameVector); + return new Vector3(reflection); } - #endregion Public Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Network/Client.cs b/Komodo/Lib/Network/Client.cs index 0476b328..2fa5c5f6 100644 --- a/Komodo/Lib/Network/Client.cs +++ b/Komodo/Lib/Network/Client.cs @@ -12,6 +12,8 @@ namespace Komodo.Lib.Network { public class Client { + #region Public + #region Constructors public Client(string serverName, int port, SocketAsyncEventArgs socketArgs = null) { @@ -32,25 +34,14 @@ public Client(string serverName, int port, SocketAsyncEventArgs socketArgs = nul _transactions = new ConcurrentDictionary(); _socketArguments = socketArgs ?? new SocketAsyncEventArgs(); } - #endregion Constructors + #endregion #region Members - - #region Public Members public IPAddress IP; public int Port { get; set; } - #endregion Public Members - - #region Protected Members - protected ConcurrentDictionary _transactions { get; set; } - protected SocketAsyncEventArgs _socketArguments { get; set; } - #endregion Protected Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public Transaction Send(T data, string endpoint) => SendAsync(data, endpoint).Result; public Transaction Send(string data, string endpoint) => SendAsync(data, endpoint).Result; public async Task SendAsync(T data, string endpoint) => await SendAsync(JsonSerializer.Serialize(data), endpoint); @@ -61,32 +52,32 @@ public async Task SendAsync(string data, string endpoint) Message response = null; try { - using var clientSocket = new TcpClient(); - request = new Message(data, endpoint) - { - TransactionId = transaction.Id, - }; - - // Send request - var serializedObj = JsonSerializer.Serialize(request); - var socketData = Brotli.Compress(serializedObj); - await clientSocket.ConnectAsync(IP, Port); - var stream = clientSocket.GetStream(); - await stream.WriteAsync(socketData); - - // Read response - var tmp = new byte[1024]; - var rawResponseData = new List(); - int bytesRead = 0; - do - { - bytesRead = await stream.ReadAsync(tmp); - rawResponseData.AddRange(tmp); - } - while (stream.DataAvailable && bytesRead != 0); - var responseData = Brotli.Decompress(rawResponseData.ToArray()); - - var responseString = Encoding.ASCII.GetString(responseData); + using var clientSocket = new TcpClient(); + request = new Message(data, endpoint) + { + TransactionId = transaction.Id, + }; + + // Send request + var serializedObj = JsonSerializer.Serialize(request); + var socketData = Brotli.Compress(serializedObj); + await clientSocket.ConnectAsync(IP, Port); + var stream = clientSocket.GetStream(); + await stream.WriteAsync(socketData); + + // Read response + var tmp = new byte[1024]; + var rawResponseData = new List(); + int bytesRead = 0; + do + { + bytesRead = await stream.ReadAsync(tmp); + rawResponseData.AddRange(tmp); + } + while (stream.DataAvailable && bytesRead != 0); + var responseData = Brotli.Decompress(rawResponseData.ToArray()); + + var responseString = Encoding.ASCII.GetString(responseData); response = JsonSerializer.Deserialize(responseString); } catch (Exception ex) @@ -102,16 +93,17 @@ public async Task SendAsync(string data, string endpoint) _transactions[transaction.Id] = transaction; return transaction; } - #endregion Public Member Methods + #endregion - #endregion Member Methods + #endregion - #region Static Methods + #region Protected - #region Protected Static Methods - - #endregion Protected Static Methods + #region Members + protected ConcurrentDictionary _transactions { get; set; } + protected SocketAsyncEventArgs _socketArguments { get; set; } + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Network/Message.cs b/Komodo/Lib/Network/Message.cs index 56f82d8d..1708693f 100644 --- a/Komodo/Lib/Network/Message.cs +++ b/Komodo/Lib/Network/Message.cs @@ -4,6 +4,8 @@ namespace Komodo.Lib.Network { public class Message { + #region Public + #region Constructors public Message() { @@ -19,17 +21,15 @@ public Message(string data, string endpoint) Id = Guid.NewGuid(); TransactionId = Guid.Empty; } - #endregion Constructors + #endregion #region Members - - #region Public Members public string Data { get; set; } public string Endpoint { get; set; } public Guid Id { get; set; } public Guid TransactionId { get; set; } - #endregion Public Members + #endregion - #endregion Members + #endregion } } \ No newline at end of file diff --git a/Komodo/Lib/Network/Transaction.cs b/Komodo/Lib/Network/Transaction.cs index 5d1a2c1d..034986b7 100644 --- a/Komodo/Lib/Network/Transaction.cs +++ b/Komodo/Lib/Network/Transaction.cs @@ -4,16 +4,16 @@ namespace Komodo.Lib.Network { public sealed class Transaction { + #region Public + #region Constructors public Transaction() { Id = Guid.NewGuid(); } - #endregion Constructors + #endregion #region Members - - #region Public Members public readonly Guid Id; public Message Request { @@ -45,13 +45,17 @@ public Message Response } } } - #endregion Public Members + #endregion + + #endregion - #region Private Members + #region Private + + #region Members private Message _request; private Message _response; - #endregion Private Members + #endregion - #endregion Members + #endregion } } \ No newline at end of file diff --git a/Server/TCPServer.cs b/Server/TCPServer.cs index 5299e71a..fb45067c 100644 --- a/Server/TCPServer.cs +++ b/Server/TCPServer.cs @@ -14,29 +14,21 @@ namespace Server { public class TCPServer { + #region Public + #region Constructors public TCPServer(int port) { Port = port; _routes = new ConcurrentDictionary>(); } - #endregion Constructors + #endregion #region Members - - #region Public Members public int Port { get; set; } - #endregion Public Members - - #region Protected Members - public ConcurrentDictionary> _routes; - #endregion Protected Members - - #endregion Members + #endregion #region Member Methods - - #region Public Member Methods public void RegisterAction([NotNull] string endpoint, [NotNull] Action messageHandler) { _routes[endpoint] = messageHandler; @@ -68,36 +60,44 @@ public async Task RunAsync() cancellation.Cancel(); } } - #endregion Public Member Methods + #endregion + + #endregion + + #region Protected + + #region Members + public ConcurrentDictionary> _routes; + #endregion - #region Protected Member Methods + #region Member Methods protected async void HandleConnection([NotNull] TcpClient client) { await Task.Yield(); using (client) { Console.WriteLine("Connection accepted"); - using var networkStream = client.GetStream(); - try - { - var tmp = new byte[1024]; - var rawData = new List(); - int bytesRead = 0; - do - { - bytesRead = networkStream.Read(tmp); - rawData.AddRange(tmp); - } while (networkStream.DataAvailable && bytesRead != 0); - - - var data = Brotli.Decompress(rawData.ToArray()); - var message = System.Text.Json.JsonSerializer.Deserialize(Encoding.ASCII.GetString(data)); - Route(message); - networkStream.Write(Brotli.Compress(data)); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); + using var networkStream = client.GetStream(); + try + { + var tmp = new byte[1024]; + var rawData = new List(); + int bytesRead = 0; + do + { + bytesRead = networkStream.Read(tmp); + rawData.AddRange(tmp); + } while (networkStream.DataAvailable && bytesRead != 0); + + + var data = Brotli.Decompress(rawData.ToArray()); + var message = System.Text.Json.JsonSerializer.Deserialize(Encoding.ASCII.GetString(data)); + Route(message); + networkStream.Write(Brotli.Compress(data)); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); } } } @@ -115,15 +115,8 @@ protected void Route([NotNull] Message message) } } - #endregion Protected Member Methods - - #endregion Member Methods - - #region Static Methods - - #region Protected Static Methods - #endregion Protected Static Methods + #endregion - #endregion Static Methods + #endregion } } \ No newline at end of file