Skip to content

Commit

Permalink
OLD-REPO: Fix method for initializing components
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesaorson committed Mar 22, 2020
1 parent bf8ea25 commit da04c14
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 272 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -2,3 +2,6 @@

# IDE1006: Naming Styles
dotnet_diagnostic.IDE1006.severity = none

# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = suggestion
Expand Up @@ -6,24 +6,19 @@

namespace Common.Behaviors
{
public class ModelStartupBehavior : BehaviorComponent
public class CubeBehavior : BehaviorComponent
{
#region Constructors
public ModelStartupBehavior() : base()
public CubeBehavior(string modelPath) : base()
{
ModelPath = modelPath;
}
#endregion Constructors

#region Members

#region Public Members
public bool IsInitialized
{
get
{
return this._isInitialized;
}
}
public string ModelPath { get; protected set; }
#endregion Public Members

#region Protected Members
Expand All @@ -44,12 +39,15 @@ public override void Update(GameTime gameTime)
{
_isInitialized = true;
var loadedTexture = KomodoGame.Content.Load<Texture2D>("player/idle/player_idle_01");
Parent.AddComponent(
new ModelComponent("models/cube")
{
Texture = new KomodoTexture(loadedTexture)
}
);
if (ModelPath != null)
{
Parent.AddComponent(
new ModelComponent(ModelPath)
{
Texture = new KomodoTexture(loadedTexture)
}
);
}
Parent.Scale = new KomodoVector3(20f, 20f, 20f);
}
Parent.Rotation += new KomodoVector3(0f, 0f, 0.001f);
Expand Down
19 changes: 12 additions & 7 deletions Common/Behaviors/FPSCounterBehavior.cs
Expand Up @@ -8,20 +8,15 @@ namespace Common.Behaviors
public class FPSCounterBehavior : BehaviorComponent
{
#region Constructors
public FPSCounterBehavior(TextComponent textComponent) : base()
{
_counterText = textComponent;
_counterText.Position = KomodoVector3.Zero;
}
#endregion Constructors

#region Members

#region Public Members
public TextComponent CounterText { get; protected set; }
#endregion Public Members

#region Protected Members
public TextComponent _counterText { get; }
#endregion Protected Members

#region Private Members
Expand All @@ -32,9 +27,19 @@ public FPSCounterBehavior(TextComponent textComponent) : base()
#region Member Methods

#region Public Member Methods
public override void Initialize()
{
base.Initialize();

CounterText = new TextComponent("fonts/font", Color.Black, Game.DefaultSpriteShader, "")
{
Position = KomodoVector3.Zero
};
Parent.AddComponent(CounterText);
}
public override void Update(GameTime gameTime)
{
_counterText.Text = $"{Math.Round(Game.FramesPerSecond)} FPS";
CounterText.Text = $"{Math.Round(Game.FramesPerSecond)} FPS";
}
#endregion Public Member Methods

Expand Down
@@ -1,24 +1,22 @@
using Komodo.Core;
using Komodo.Core.ECS.Components;
using Microsoft.Xna.Framework;

namespace Common.Behaviors
{
public class FPSCounterStartupBehavior : BehaviorComponent
public class PlayerBehavior : BehaviorComponent
{
#region Constructors
public FPSCounterStartupBehavior() : base()
public PlayerBehavior(int playerIndex) : base()
{
PlayerIndex = playerIndex;
}
#endregion Constructors

#region Members

#region Public Members
public bool IsInitialized
{
get;
private set;
}
public int PlayerIndex { get; }
#endregion Public Members

#region Protected Members
Expand All @@ -32,15 +30,25 @@ public bool IsInitialized
#region Member Methods

#region Public Member Methods
public override void Initialize()
{
base.Initialize();

Parent.AddComponent(new SpriteComponent("player/idle/player_idle_01", Game.DefaultSpriteShader));
if (PlayerIndex == 0)
{
Parent.AddComponent(new MoveBehavior(PlayerIndex));
}

Parent.AddComponent(
new TextComponent("fonts/font", Color.Black, Game.DefaultSpriteShader, $"Test {PlayerIndex}")
{
Position = new KomodoVector3(0f, 20f, 0)
}
);
}
public override void Update(GameTime gameTime)
{
if (!IsInitialized)
{
IsInitialized = true;
var textComponent = new TextComponent("fonts/font", Color.Black, Game.DefaultSpriteShader, "");
Parent.AddComponent(textComponent);
Parent.AddComponent(new FPSCounterBehavior(textComponent));
}
}
#endregion Public Member Methods

Expand Down
71 changes: 0 additions & 71 deletions Common/Behaviors/RootStartupBehavior.cs

This file was deleted.

18 changes: 4 additions & 14 deletions DesktopGL/Startup.cs
Expand Up @@ -23,12 +23,12 @@ static void Main()
{
Position = new KomodoVector3(0f, 0f, 100f),
};
player1Entity.AddComponent(new RootStartupBehavior(0));
player1Entity.AddComponent(new PlayerBehavior(0));
var player2Entity = new Entity(Game.ActiveScene)
{
Position = new KomodoVector3(0f, 0f, 100f),
};
player2Entity.AddComponent(new ModelStartupBehavior());
player2Entity.AddComponent(new CubeBehavior("models/cube"));

var camera = new CameraComponent()
{
Expand All @@ -45,7 +45,7 @@ static void Main()
var orthographicScene = new Scene(Game);
Game.ActiveScene.Children.Add(orthographicScene);
var counterEntity = new Entity(orthographicScene);
counterEntity.AddComponent(new FPSCounterStartupBehavior());
counterEntity.AddComponent(new FPSCounterBehavior());

var cameraEntity = new Entity(orthographicScene)
{
Expand All @@ -60,17 +60,7 @@ static void Main()
cameraEntity.AddComponent(camera);
camera.SetActive();

var startupEntities = new List<Entity>
{
player1Entity,
player2Entity,
counterEntity,
cameraEntity,
};

// Startup entities will be attached to the active scene once the monogame initialization has occurred.
// This allows the default shader to be created only after the graphics device has been initialized.
Game.Run(startupEntities);
Game.Run();
}
}

Expand Down
10 changes: 9 additions & 1 deletion Komodo/Core/ECS/Components/Component.cs
Expand Up @@ -8,7 +8,7 @@ namespace Komodo.Core.ECS.Components
public abstract class Component : ISerializable<Component>
{
#region Constructors
public Component(bool isEnabled = true, Entity parent = null)
protected Component(bool isEnabled = true, Entity parent = null)
{
IsEnabled = isEnabled;
Parent = parent;
Expand All @@ -19,6 +19,7 @@ public Component(bool isEnabled = true, Entity parent = null)

#region Public Members
public bool IsEnabled { get; set; }
public bool IsInitialized { get; private set; }
[JsonIgnore]
public Entity Parent { get; set; }
public KomodoVector3 Position { get; set; }
Expand Down Expand Up @@ -78,6 +79,13 @@ public virtual void Draw(SpriteBatch spriteBatch)
public virtual void Draw()
{
}
public virtual void Initialize()
{
if (!IsInitialized)
{
IsInitialized = true;
}
}
public abstract SerializedObject Serialize();
public virtual void Update(GameTime gameTime)
{
Expand Down
8 changes: 5 additions & 3 deletions Komodo/Core/ECS/Entities/Entity.cs
Expand Up @@ -203,9 +203,11 @@ public bool RemoveComponent(Component componentToRemove)

public SerializedObject Serialize()
{
var serializedObject = new SerializedObject();
serializedObject.Type = this.GetType().ToString();

var serializedObject = new SerializedObject
{
Type = this.GetType().ToString()
};

var components = new List<SerializedObject>();
foreach (var component in Components)
{
Expand Down

0 comments on commit da04c14

Please sign in to comment.