From fa38fc074aef5df1967cc46dfde9556c2ebca641 Mon Sep 17 00:00:00 2001 From: MrShutCo Date: Mon, 9 Jan 2017 21:52:11 -0500 Subject: [PATCH 1/9] Added basic Entity-component system -Added CharacterComponent (handles view and anim, maybe change name?) -Added HeroController (Use arrow keys to move entity around) --- Roguicka/Components/CharacterComponent.cs | 46 +++++++++++++++++ Roguicka/Components/Entity.cs | 34 +++++++++++++ Roguicka/Components/HeroController.cs | 15 ++++++ Roguicka/Components/IComponent.cs | 12 +++++ Roguicka/Engines/ControllerEngine.cs | 38 ++++++++++++++ Roguicka/Engines/RenderingEngine.cs | 5 -- Roguicka/MapConsole.cs | 61 +++++++++++++++++++++-- Roguicka/Program.cs | 11 ++-- Roguicka/Roguicka.csproj | 35 ++++++++++++- Roguicka/packages.config | 3 ++ 10 files changed, 246 insertions(+), 14 deletions(-) create mode 100644 Roguicka/Components/CharacterComponent.cs create mode 100644 Roguicka/Components/Entity.cs create mode 100644 Roguicka/Components/HeroController.cs create mode 100644 Roguicka/Components/IComponent.cs create mode 100644 Roguicka/Engines/ControllerEngine.cs delete mode 100644 Roguicka/Engines/RenderingEngine.cs diff --git a/Roguicka/Components/CharacterComponent.cs b/Roguicka/Components/CharacterComponent.cs new file mode 100644 index 0000000..84f649f --- /dev/null +++ b/Roguicka/Components/CharacterComponent.cs @@ -0,0 +1,46 @@ +using Microsoft.Xna.Framework; +using SadConsole; +using SadConsole.Game; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Roguicka.Components { + public class CharacterComponent: IComponent { + + public GameObject gameObject; + + public int Symbol { get; } + public Color ForeColor { get; } + public Color BackColor { get; } + + + + public CharacterComponent(Color fore, Color back, int sym, int x, int y) { + ForeColor = fore; + BackColor = back; + Symbol = sym; + gameObject = new GameObject(Engine.DefaultFont); + var animation = new SadConsole.Consoles.AnimatedTextSurface("default",1,1,Engine.DefaultFont); + var frame = animation.CreateFrame(); + frame[0].GlyphIndex = sym; + frame[0].Foreground = fore; + frame[0].Background = back; + gameObject.Animation = animation; + gameObject.Position = new Point(x, y); + gameObject.Update(); + } + + public void Draw(int X, int Y) { + gameObject.Position = new Point(X, Y); + gameObject.Render(); + } + + public void Update() { + gameObject.Update(); + } + + } +} diff --git a/Roguicka/Components/Entity.cs b/Roguicka/Components/Entity.cs new file mode 100644 index 0000000..3d9dce0 --- /dev/null +++ b/Roguicka/Components/Entity.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Roguicka.Components { + public class Entity { + + public int X { get; set; } + public int Y { get; set; } + + private List Components; + + public Entity() { + Components = new List(); + } + + public Entity Add(IComponent component) { + Components.Add(component); + return this; + } + + public bool Has() where T : IComponent { + return this.GetComponent() != null; + } + + + public T GetComponent() where T : IComponent { + return (T)Components.Single(x => x.GetType() == typeof(T)); + } + + } +} diff --git a/Roguicka/Components/HeroController.cs b/Roguicka/Components/HeroController.cs new file mode 100644 index 0000000..4490f0e --- /dev/null +++ b/Roguicka/Components/HeroController.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework.Input; +using SadConsole.Input; + +namespace Roguicka.Components { + public class HeroController : IComponent { + + public HeroController() { + } + } +} diff --git a/Roguicka/Components/IComponent.cs b/Roguicka/Components/IComponent.cs new file mode 100644 index 0000000..dbbeb2a --- /dev/null +++ b/Roguicka/Components/IComponent.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Roguicka.Components { + //Empty.... hm... + public interface IComponent { + + } +} diff --git a/Roguicka/Engines/ControllerEngine.cs b/Roguicka/Engines/ControllerEngine.cs new file mode 100644 index 0000000..d694e30 --- /dev/null +++ b/Roguicka/Engines/ControllerEngine.cs @@ -0,0 +1,38 @@ +using Microsoft.Xna.Framework.Input; +using Roguicka.Components; +using SadConsole.Input; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Roguicka.Engines { + public class ControllerEngine { + + public ControllerEngine() { + + } + + public void Update(KeyboardInfo info, Entity e) { + + if (e.Has()) { + if (info.KeysPressed.Contains(AsciiKey.Get(Keys.Down))) { + e.Y += 1; + } + else if (info.KeysPressed.Contains(AsciiKey.Get(Keys.Up))) { + e.Y -= 1; + } + + if (info.KeysPressed.Contains(AsciiKey.Get(Keys.Right))) { + e.X += 1; + } + else if (info.KeysPressed.Contains(AsciiKey.Get(Keys.Left))) { + e.X -= 1; + } + } + + } + + } +} diff --git a/Roguicka/Engines/RenderingEngine.cs b/Roguicka/Engines/RenderingEngine.cs deleted file mode 100644 index 7ee9b7f..0000000 --- a/Roguicka/Engines/RenderingEngine.cs +++ /dev/null @@ -1,5 +0,0 @@ -namespace Roguicka.Engines { - public class RenderingEngine { - - } -} diff --git a/Roguicka/MapConsole.cs b/Roguicka/MapConsole.cs index cdb3c0d..552c0e8 100644 --- a/Roguicka/MapConsole.cs +++ b/Roguicka/MapConsole.cs @@ -1,16 +1,67 @@ -using Roguicka.Maps; +using Microsoft.Xna.Framework; +using Roguicka.Components; +using Roguicka.Engines; +using Roguicka.Maps; +using SadConsole; +using SadConsole.Game; +using SadConsole.Input; +using System.Collections.Generic; namespace Roguicka { public class MapConsole : SadConsole.Consoles.Console { private RoguickaMap _map; + ControllerEngine _controlEngine; + + List _objects; + public MapConsole(RoguickaMap map, int width, int height, int viewH, int viewW) : base(width,height) { _map = map; - SadConsole.Shapes.Box box = SadConsole.Shapes.Box.GetDefaultBox(); - box.Width = width; - box.Height = height; - box.Draw(this); + TextSurface.RenderArea = new Rectangle(0, 0, viewW, viewH); + + + Engine.Keyboard.RepeatDelay = 0.07f; + Engine.Keyboard.InitialRepeatDelay = 0.1f; + + _objects = new List(); + _controlEngine = new ControllerEngine(); + + Entity hero = new Entity() + .Add(new CharacterComponent(Color.Aqua, Color.Black, 100, 5, 5)) + .Add(new HeroController()); + + _objects.Add(hero); + + } + + public override bool ProcessKeyboard(KeyboardInfo info) { + + foreach(Entity e in _objects) { + _controlEngine.Update(info, e); + } + + return false; + } + + public override void Render() { + + foreach (Entity e in _objects) { + if (e.Has()) { + e.GetComponent().Draw(e.X,e.Y); + } + } + + + base.Render(); + } + public override void Update() { + foreach (Entity e in _objects) { + if (e.Has()) { + e.GetComponent().Update(); + } + } + base.Update(); } } diff --git a/Roguicka/Program.cs b/Roguicka/Program.cs index 7589ef4..b3b7356 100644 --- a/Roguicka/Program.cs +++ b/Roguicka/Program.cs @@ -5,6 +5,9 @@ using Color = Microsoft.Xna.Framework.ColorAnsi; using Roguicka.Maps; using RogueSharp.MapCreation; using RogueSharp; +using Roguicka.Components; +using SadConsole.Game; +using Roguicka.Engines; namespace Roguicka { @@ -33,7 +36,6 @@ namespace Roguicka private static void Engine_EngineUpdated(object sender, EventArgs e) { - } private static void Engine_EngineStart(object sender, EventArgs e) @@ -41,11 +43,14 @@ namespace Roguicka _map = new RoguickaMap(Map.Create(new CaveMapCreationStrategy(_width, _height, 45, 4, 3))); Engine.ConsoleRenderStack.Clear(); - Engine.ActiveConsole = null; - var c = new MapConsole(_map, _width, _height,30,80); + var c = new MapConsole(_map, _width, _height, 30, 80); + + Engine.ActiveConsole = c; + Engine.ConsoleRenderStack.Add(c); + CellAppearance[,] mapData = new CellAppearance[_width,_height]; diff --git a/Roguicka/Roguicka.csproj b/Roguicka/Roguicka.csproj index e4a636f..bc069fb 100644 --- a/Roguicka/Roguicka.csproj +++ b/Roguicka/Roguicka.csproj @@ -12,6 +12,8 @@ Roguicka v4.5 512 + + AnyCPU @@ -49,6 +51,26 @@ packages\SadConsole.Core.MonoGameGL.5.2.0\lib\net45\SadConsole.Core.OpenGL.dll + + packages\SadConsole.GameHelpers.MonoGameGL.4.5.0\lib\net45\SadConsole.GameHelpers.OpenGL.dll + True + + + packages\Graphnode.SFML.Net.2.3\lib\sfmlnet-audio-2.dll + True + + + packages\Graphnode.SFML.Net.2.3\lib\sfmlnet-graphics-2.dll + True + + + packages\Graphnode.SFML.Net.2.3\lib\sfmlnet-system-2.dll + True + + + packages\Graphnode.SFML.Net.2.3\lib\sfmlnet-window-2.dll + True + @@ -73,7 +95,11 @@ - + + + + + @@ -83,6 +109,13 @@ + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + - \ No newline at end of file + diff --git a/Roguicka/packages.config b/Roguicka/packages.config index bb3f40f..97b0606 100644 --- a/Roguicka/packages.config +++ b/Roguicka/packages.config @@ -1,9 +1,7 @@  - - \ No newline at end of file -- GitLab From 3421f916bbe2c9f9b1febd7951e8dc2c36df923f Mon Sep 17 00:00:00 2001 From: Alec Date: Wed, 18 Jan 2017 22:12:01 -0600 Subject: [PATCH 9/9] Update to RenderComponent and Related changes Updated Character Component to RenderComponent. Will store position, visibility, animations, and other related items there. Updated related Console. --- ...haracterComponent.cs => RenderComponent.cs} | 12 ++++++++---- Roguicka/Consoles/MapConsole.cs | 18 ++++++++++-------- Roguicka/Roguicka.csproj | 4 ++-- 3 files changed, 20 insertions(+), 14 deletions(-) rename Roguicka/Components/{CharacterComponent.cs => RenderComponent.cs} (73%) diff --git a/Roguicka/Components/CharacterComponent.cs b/Roguicka/Components/RenderComponent.cs similarity index 73% rename from Roguicka/Components/CharacterComponent.cs rename to Roguicka/Components/RenderComponent.cs index 6fe26a6..553fe04 100644 --- a/Roguicka/Components/CharacterComponent.cs +++ b/Roguicka/Components/RenderComponent.cs @@ -3,19 +3,21 @@ using SadConsole; using SadConsole.Game; namespace Roguicka.Components { - public class CharacterComponent: IComponent { + public class RenderComponent: IComponent { public GameObject gameObject; public int Symbol { get; } public Color ForeColor { get; } public Color BackColor { get; } + private bool Visible { get; } - public CharacterComponent(Color fore, Color back, int sym) { + public RenderComponent(Color fore, Color back, int sym, bool visibility = true) { ForeColor = fore; BackColor = back; Symbol = sym; gameObject = new GameObject(Engine.DefaultFont); + Visible = visibility; var animation = new SadConsole.Consoles.AnimatedTextSurface("default",1,1,Engine.DefaultFont); var frame = animation.CreateFrame(); frame[0].GlyphIndex = sym; @@ -26,8 +28,10 @@ namespace Roguicka.Components { gameObject.Update(); } - public void Draw(int X, int Y) { - gameObject.Position = new Point(X, Y); + public void Draw(int X, int Y) + { + if (!Visible) return; + gameObject.Position = new Point(X, Y); gameObject.Render(); } diff --git a/Roguicka/Consoles/MapConsole.cs b/Roguicka/Consoles/MapConsole.cs index bbfc479..569877d 100644 --- a/Roguicka/Consoles/MapConsole.cs +++ b/Roguicka/Consoles/MapConsole.cs @@ -29,7 +29,7 @@ namespace Roguicka { var y = _map.GetRandomCell(); Entity hero = new Entity(x.X,x.Y) - .Add(new CharacterComponent(Color.Aqua, Color.Black, 1)) + .Add(new RenderComponent(Color.Aqua, Color.Black, 1)) .Add(new HeroController()); TextSurface.RenderArea = new Rectangle(hero.X - (TextSurface.RenderArea.Width / 2), @@ -37,7 +37,7 @@ namespace Roguicka { TextSurface.RenderArea.Width, TextSurface.RenderArea.Height); Entity hero2 = new Entity(x.X,x.Y+1) - .Add(new CharacterComponent(Color.Red, Color.Black, 128)); + .Add(new RenderComponent(Color.Red, Color.Black, 128)); _objects.Add(hero); @@ -63,10 +63,12 @@ namespace Roguicka { foreach (var entity in _objects) { - if (!entity.Has()) continue; - if (textSurface.RenderArea.Contains(entity.X, entity.Y)) { - entity.GetComponent().gameObject.RenderOffset = this.Position - TextSurface.RenderArea.Location; - entity.GetComponent().Draw(entity.X, entity.Y); + if (!entity.Has()) continue; + if (textSurface.RenderArea.Contains(entity.X, entity.Y)) + { + var entityRenderComponent = entity.GetComponent(); + entityRenderComponent.gameObject.RenderOffset = this.Position - TextSurface.RenderArea.Location; + entityRenderComponent.Draw(entity.X, entity.Y); } } @@ -75,8 +77,8 @@ namespace Roguicka { } public override void Update() { foreach (var entity in _objects) { - if (entity.Has()) { - entity.GetComponent().Update(); + if (entity.Has()) { + entity.GetComponent().Update(); } } diff --git a/Roguicka/Roguicka.csproj b/Roguicka/Roguicka.csproj index 49c36ef..44a5df6 100644 --- a/Roguicka/Roguicka.csproj +++ b/Roguicka/Roguicka.csproj @@ -91,7 +91,7 @@ - + @@ -111,4 +111,4 @@ --> - + \ No newline at end of file -- GitLab