Commits

glav  committed 8625b84

Created a basic visualisation engine for showing player movements

  • Participants
  • Parent commits 347f73a

Comments (0)

Files changed (11)

File EcoDev.Core/Common/Maps/MapPosition.cs

 			zPosition = originalPosition.zPosition;
 		}
 
+		public MapPosition Clone()
+		{
+			return new MapPosition(this);
+		}
+
 		public override bool Equals(object obj)
 		{
 			var pos = obj as MapPosition;

File EcoDev.Visualisation.Basic/EcoDev.Visualisation.Basic.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>EcoDev.Visualisation.Basic</RootNamespace>
+    <AssemblyName>EcoDev.Visualisation.Basic</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="VisualisationEngine.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\EcoDev.Core\EcoDev.Core.csproj">
+      <Project>{5C5DEBB7-904C-4955-80BC-30C19EA63EE2}</Project>
+      <Name>EcoDev.Core</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\EcoDev\EcoDev.Engine.csproj">
+      <Project>{5388D079-6789-4BEB-97E7-2872B9BB3BC4}</Project>
+      <Name>EcoDev.Engine</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

File EcoDev.Visualisation.Basic/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("EcoDev.Visualisation.Basic")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("EcoDev.Visualisation.Basic")]
+[assembly: AssemblyCopyright("Copyright ©  2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("95abf610-df86-4fe6-bf91-8bdaef28cd32")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

File EcoDev.Visualisation.Basic/VisualisationEngine.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using EcoDev.Engine.MapEngine;
+using EcoDev.Engine.Entities;
+using System.Threading.Tasks;
+using System.Threading;
+using EcoDev.Engine.WorldEngine;
+using EcoDev.Core.Common.BuildingBlocks;
+
+namespace EcoDev.Visualisation.Basic
+{
+	public class VisualisationEngine
+	{
+		private Dictionary<Guid, LivingEntityWithQualities> _currentPlayers = new Dictionary<Guid, LivingEntityWithQualities>();
+		public Dictionary<Guid,LivingEntityWithQualities> CurrentPlayers { get { return _currentPlayers; } }
+		private CancellationTokenSource _tokenSource = new CancellationTokenSource();
+		private EcoWorld _world;
+		ConsoleColor _originalConsoleColor;
+		ConsoleColor _edgeColor = ConsoleColor.Black;
+		ConsoleColor _playerColor = ConsoleColor.Yellow;
+		ConsoleColor _solidBlockColor = ConsoleColor.DarkRed;
+		ConsoleColor _entranceColor = ConsoleColor.Green;
+		ConsoleColor _exitColor = ConsoleColor.Magenta;
+
+		public VisualisationEngine(EcoWorld world)
+		{
+			_world = world;
+		}
+		
+		public void StartMapRendering()
+		{
+			Task.Factory.StartNew(() => 
+			{
+				Console.Clear();
+
+				while (!_tokenSource.IsCancellationRequested)
+				{
+					DrawMap();
+				}
+			}, _tokenSource.Token);
+		}
+
+		public void StopMapRendering()
+		{
+			_tokenSource.Cancel();
+		}
+
+		private void DrawMap()
+		{
+			_originalConsoleColor = Console.ForegroundColor;
+			Console.CursorLeft = 0;
+			Console.CursorTop = 0;
+
+			for (int zPos = 0; zPos < _world.WorldMap.DepthInUnits; zPos++)
+			{
+				Console.ForegroundColor = ConsoleColor.White;
+				Console.Write("{1}Level: {0}{1}{1}", zPos, Environment.NewLine);
+
+				// Draw the boundary
+				for (int xPos = -1; xPos <= _world.WorldMap.WidthInUnits; xPos++)
+				{
+					Console.ForegroundColor = _edgeColor;
+					Console.Write("_");
+				}
+				Console.Write("{0}", Environment.NewLine);
+
+				//do y position in reverse so the text rendering starts at the top and works down the graph
+				for (int yPos = _world.WorldMap.HeightInUnits - 1; yPos >= 0; yPos--)
+				{
+					Console.ForegroundColor = _edgeColor;
+					Console.Write("|");
+					for (int xPos = 0; xPos < _world.WorldMap.WidthInUnits; xPos++)
+					{
+						var textBlock = _world.WorldMap.Get(xPos, yPos, zPos);
+						if (IsPlayerAtThisPosition(xPos,yPos,zPos))
+						{
+							Console.ForegroundColor = _playerColor;
+							Console.Write("*");
+						}
+						else
+						{
+							if (textBlock is MapExitBlock)
+							{
+								Console.ForegroundColor = _exitColor;
+							}
+							else if (textBlock is MapEntranceBlock)
+							{
+								Console.ForegroundColor = _entranceColor;
+							}
+							else
+							{
+								Console.ForegroundColor = _solidBlockColor;
+							}
+
+							Console.Write(textBlock != null ? textBlock.ToString() : " ");
+						}
+						
+					}
+					Console.ForegroundColor = _edgeColor;
+					Console.Write("|{0}", Environment.NewLine);
+				}
+
+				// Draw the boundary
+				for (int xPos = -1; xPos <= _world.WorldMap.WidthInUnits; xPos++)
+				{
+					Console.ForegroundColor = _edgeColor;
+					Console.Write("_");
+				}
+				Console.Write("{0}", Environment.NewLine);
+			}
+
+			Console.ForegroundColor = _originalConsoleColor;
+		}
+
+		private bool IsPlayerAtThisPosition(int xPos, int yPos, int zPos)
+		{
+			var players = _world.Inhabitants.ToList();
+			for (int cnt = 0; cnt < players.Count; cnt++ )
+			{
+				var entity = players[cnt];
+				if (entity.PositionInMap.xPosition == xPos && entity.PositionInMap.yPosition == yPos && entity.PositionInMap.zPosition == zPos)
+				{
+					return true;
+				}
+			}
+
+			return false;
+		}
+	}
+}
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEcoWorldHost", "TestEcoWorldHost\TestEcoWorldHost.csproj", "{9FE49555-8EAD-4212-B60D-C44C44BC1FAE}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EcoDev.Visualisation.Basic", "EcoDev.Visualisation.Basic\EcoDev.Visualisation.Basic.csproj", "{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}"
+EndProject
 Global
 	GlobalSection(TestCaseManagementSettings) = postSolution
 		CategoryFile = EcoDev.vsmdi
 		{9FE49555-8EAD-4212-B60D-C44C44BC1FAE}.Release|Mixed Platforms.Build.0 = Release|x86
 		{9FE49555-8EAD-4212-B60D-C44C44BC1FAE}.Release|x86.ActiveCfg = Release|x86
 		{9FE49555-8EAD-4212-B60D-C44C44BC1FAE}.Release|x86.Build.0 = Release|x86
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Release|Any CPU.Build.0 = Release|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+		{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}.Release|x86.ActiveCfg = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

File EcoDev/EcoDev.Engine.csproj

     <Compile Include="WorldEngine\DebugInfoEventArgs.cs" />
     <Compile Include="WorldEngine\EcoWorld.cs" />
     <Compile Include="WorldEngine\EntityExitEventArgs.cs" />
+    <Compile Include="WorldEngine\InhabitantActionEventArgs.cs" />
     <Compile Include="WorldEngine\InhabitantPositionEngine.cs" />
     <Compile Include="WorldEngine\MovementActionResponse.cs" />
   </ItemGroup>

File EcoDev/WorldEngine/EcoWorld.cs

 		List<LivingEntityWithQualities> _inhabitants = new List<LivingEntityWithQualities>();
 		string _worldName;
 		Task _worldTask = null;
-		const int MIN_MILLISECONDS_TO_CYCLE_THROUGH_PLAYER_ACTIONS = 3000;
+		const int MIN_MILLISECONDS_TO_CYCLE_THROUGH_PLAYER_ACTIONS = 1000;
 		public InhabitantPositionEngine _positionEngine = new InhabitantPositionEngine();
 		static object _debugLock = new object();
 		bool _enableDebug = false;
 
 		public event EventHandler<DebugInfoEventArgs> DebugInformation;
 		public event EventHandler<EntityExitEventArgs> EntityExited;
+		public event EventHandler<InhabitantActionEventArgs> InhabitantPerformedAction;
 
 		public EcoWorld(string worldName, Map worldMap, LivingEntityWithQualities[] inhabitants, bool enableDebug)
 		{
 			}
 		}
 
+		protected void FireInhabitantPerformedActionEvent(ActionToPerform actionPerformed, MovementDirection directionMoved, MapPosition positionInMap)
+		{
+			if (InhabitantPerformedAction != null)
+			{
+				Task.Factory.StartNew(() =>
+				{
+					InhabitantPerformedAction(this, new InhabitantActionEventArgs(actionPerformed, directionMoved, positionInMap));
+				});
+			}
+		}
+
 		private string FormatDebugInformation(string debugInfo)
 		{
 			string fullInfo = string.Format("[{0} - {1}] {2}{3}", DateTime.Now.ToShortDateString(), DateTime.Now.ToString("hh:mm:ss"), debugInfo, Environment.NewLine);
 			if (result.ErrorException == null)
 			{
 				ActOnEntityActionResult(entity, result.ActionResult);
+				FireInhabitantPerformedActionEvent(result.ActionResult.DecidedAction, result.ActionResult.DirectionToMove, entity.PositionInMap.Clone());
 			}
 			else
 			{

File EcoDev/WorldEngine/InhabitantActionEventArgs.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using EcoDev.Core.Common;
+using EcoDev.Core.Common.Maps;
+using EcoDev.Core.Common.Actions;
+
+namespace EcoDev.Engine.WorldEngine
+{
+	public class InhabitantActionEventArgs : EventArgs
+	{
+		public InhabitantActionEventArgs(ActionToPerform actionPerformed, MovementDirection directionMoved, MapPosition positionInMap)
+		{
+			ActionPerformed = actionPerformed;
+			DirectionMoved = directionMoved;
+			PositionInMap = positionInMap;
+		}
+		public ActionToPerform ActionPerformed { get; set; }
+		public MovementDirection DirectionMoved { get; set; }
+		public MapPosition PositionInMap { get; set; }
+	}
+}

File TestEcoWorldHost/Program.cs

 using EcoDev.Core.Common.BuildingBlocks;
 using System.IO;
 using System.Timers;
+using EcoDev.Visualisation.Basic;
 
 namespace TestEcoWorldHost
 {
 	class Program
 	{
-		static EcoWorld _world;
+		static EcoDev.Engine.WorldEngine.EcoWorld _world;
 		static Timer _playerTimer = new Timer();
+		static VisualisationEngine _visualiser;
 
 		static void Main(string[] args)
 		{
 				File.Delete("WorldDebugInfo.log");
 			}
 			//_world = CreateWorld();
-			_world = CreateWorld2();
+			//_world = CreateWorld2();
+			_world = CreateWorld3();
+
+			_visualiser = new VisualisationEngine(_world);
 
 			_world.DebugInformation += new EventHandler<DebugInfoEventArgs>(world_DebugInformation);
 			_world.EntityExited += new EventHandler<EntityExitEventArgs>(world_EntityExited);
+			_world.InhabitantPerformedAction += new EventHandler<InhabitantActionEventArgs>(_world_InhabitantPerformedAction);
+			
 			var player = CreatePlayer();
 
 			WriteDebuggingInfo(_world);
 			_world.StartWorld();
 
 			Console.WriteLine("World Started. Hit ENTER to stop");
+			_visualiser.StartMapRendering();
 			Console.ReadLine();
 
+			_visualiser.StopMapRendering();
 			_world.DestroyWorld();
 		}
 
+		static void _world_InhabitantPerformedAction(object sender, InhabitantActionEventArgs e)
+		{
+			//throw new NotImplementedException();
+		}
+
 		static void playerTimer_Elapsed(object sender, ElapsedEventArgs e)
 		{
 			_playerTimer.Stop();
 
 		static void world_DebugInformation(object sender, DebugInfoEventArgs e)
 		{
-			Console.Write(e.DebugInformation);
-			using (var file = File.Open("WorldDebugInfo.log", FileMode.Append))
-			{
-				var data = ASCIIEncoding.ASCII.GetBytes(e.DebugInformation);
-				file.Write(data, 0, data.Length);
-			}
+			return;
+
+			//Console.Write(e.DebugInformation);
+			//using (var file = File.Open("WorldDebugInfo.log", FileMode.Append))
+			//{
+			//    var data = ASCIIEncoding.ASCII.GetBytes(e.DebugInformation);
+			//    file.Write(data, 0, data.Length);
+			//}
 		}
 
-		private static void WriteDebuggingInfo(EcoWorld world)
+		private static void WriteDebuggingInfo(EcoDev.Engine.WorldEngine.EcoWorld world)
 		{
 			System.IO.File.WriteAllText(".\\WorldMapDebug.txt", world.WorldMap.ToString());
 		}
 			return player;
 		}
 
-		private static EcoWorld CreateWorld()
+		private static EcoDev.Engine.WorldEngine.EcoWorld CreateWorld()
 		{
 			Map map = new Map(10, 10, 1);
 			
 
 			map.InitialiseMap();
 
-			var world = new EcoWorld("TestWorld", map, null,true);
+			var world = new EcoDev.Engine.WorldEngine.EcoWorld("TestWorld", map, null, true);
 			return world;
 		}
-		private static EcoWorld CreateWorld2()
+		private static EcoDev.Engine.WorldEngine.EcoWorld CreateWorld2()
 		{
 			Map map = new Map(10, 10, 1);
 
 
 			map.InitialiseMap();
 
-			var world = new EcoWorld("TestWorld", map, null,true);
+			var world = new EcoDev.Engine.WorldEngine.EcoWorld("TestWorld", map, null, true);
+			return world;
+		}
+		private static EcoDev.Engine.WorldEngine.EcoWorld CreateWorld3()
+		{
+			Map map = new Map(20, 20, 1);
+
+			// setup entry and exit points
+			map.Set(0, 0, 0, new MapEntranceBlock());
+			map.Set(19, 19, 0, new MapExitBlock());
+
+			// Setup some barrier blocks
+			map.Set(3, 4, 0, new SolidBlock());
+			map.Set(6, 7, 0, new SolidBlock());
+			map.Set(3, 0, 0, new SolidBlock());
+			map.Set(3, 1, 0, new SolidBlock());
+			map.Set(3, 2, 0, new SolidBlock());
+			map.Set(2, 2, 0, new SolidBlock());
+			map.Set(5, 9, 0, new SolidBlock());
+			map.Set(5, 19, 0, new SolidBlock());
+			map.Set(14, 19, 0, new SolidBlock());
+			map.Set(14, 18, 0, new SolidBlock());
+
+			map.InitialiseMap();
+
+			var world = new EcoDev.Engine.WorldEngine.EcoWorld("TestWorld", map, null, true);
 			return world;
 		}
 	}

File TestEcoWorldHost/TestEcoWorldHost.csproj

     <RootNamespace>TestEcoWorldHost</RootNamespace>
     <AssemblyName>TestEcoWorldHost</AssemblyName>
     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
-    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+    <TargetFrameworkProfile>
+    </TargetFrameworkProfile>
     <FileAlignment>512</FileAlignment>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
       <Project>{5C5DEBB7-904C-4955-80BC-30C19EA63EE2}</Project>
       <Name>EcoDev.Core</Name>
     </ProjectReference>
+    <ProjectReference Include="..\EcoDev.Visualisation.Basic\EcoDev.Visualisation.Basic.csproj">
+      <Project>{8D28A37E-A41E-4A42-9580-BB7DC0B3A537}</Project>
+      <Name>EcoDev.Visualisation.Basic</Name>
+    </ProjectReference>
     <ProjectReference Include="..\EcoDev\EcoDev.Engine.csproj">
       <Project>{5388D079-6789-4BEB-97E7-2872B9BB3BC4}</Project>
       <Name>EcoDev.Engine</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="app.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

File TestEcoWorldHost/app.config

+<?xml version="1.0"?>
+<configuration>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>