Extended Client Simulation to Xamarin: Android and UWP works with Server Simulator
authormile4712 <Lehner.Michael43@gmail.com>
Mon, 10 Dec 2018 22:00:40 +0000 (23:00 +0100)
committermile4712 <Lehner.Michael43@gmail.com>
Mon, 10 Dec 2018 22:00:40 +0000 (23:00 +0100)
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.Android/ConnectionTest.Android.csproj
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.Android/Properties/AndroidManifest.xml
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP.csproj
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP.csproj.user [new file with mode: 0644]
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP_TemporaryKey.pfx [new file with mode: 0644]
src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/Package.appxmanifest
src/communication/ConnectionTest/ConnectionTest/ConnectionTest/ConnectionTest.csproj
src/communication/ConnectionTest/ConnectionTest/ConnectionTest/MainPage.xaml.cs
src/communication/ConnectionTest/ServerSimulator/Program.cs
src/visualization/Playground/HelloMovingImage/HelloMovingImage/Playground.cs

index b09b561..af1f3d3 100644 (file)
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <AndroidLinkMode>None</AndroidLinkMode>
+    <AotAssemblies>false</AotAssemblies>
+    <EnableLLVM>false</EnableLLVM>
+    <BundleAssemblies>false</BundleAssemblies>
+    <AndroidSupportedAbis />
+    <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
index 791bb28..dd7f0b4 100644 (file)
@@ -7,5 +7,6 @@
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
+       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <application android:label="ConnectionTest.Android"></application>
 </manifest>
\ No newline at end of file
index d5802d8..4dab9bd 100644 (file)
@@ -22,7 +22,7 @@
     <AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
     <AppxBundle>Always</AppxBundle>
     <AppxBundlePlatforms>x86|x64|arm</AppxBundlePlatforms>
-    <PackageCertificateThumbprint>B488641D238096E70E16111DE2F59FA0AFBEC1E4</PackageCertificateThumbprint>
+    <PackageCertificateThumbprint>458CCC855E61C17BACFD118D592F724C96F6832F</PackageCertificateThumbprint>
     <AppInstallerUpdateFrequency>1</AppInstallerUpdateFrequency>
     <AppInstallerCheckForUpdateFrequency>OnApplicationRun</AppInstallerCheckForUpdateFrequency>
   </PropertyGroup>
       <Name>ConnectionTest</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="ConnectionTest.UWP_TemporaryKey.pfx" />
+  </ItemGroup>
   <PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
     <VisualStudioVersion>14.0</VisualStudioVersion>
   </PropertyGroup>
diff --git a/src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP.csproj.user b/src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP.csproj.user
new file mode 100644 (file)
index 0000000..f92aef0
--- /dev/null
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
+    <CreateCleanLayout>true</CreateCleanLayout>
+    <RemoteDebugEnabled>true</RemoteDebugEnabled>
+    <RemoteDebugMachine>localhost</RemoteDebugMachine>
+  </PropertyGroup>
+  <PropertyGroup>
+    <AppxPackageIsForStore>False</AppxPackageIsForStore>
+    <AppxShowAllApps>False</AppxShowAllApps>
+    <AppxBuildConfigurationSelection>x86|x64|arm</AppxBuildConfigurationSelection>
+    <PackageOptionalProjectsInIdeBuilds>False</PackageOptionalProjectsInIdeBuilds>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
+    <DeviceId>30F105C9-681E-420b-A277-7C086EAD8A4E</DeviceId>
+    <UseEmulator>true</UseEmulator>
+  </PropertyGroup>
+</Project>
\ No newline at end of file
diff --git a/src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP_TemporaryKey.pfx b/src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP_TemporaryKey.pfx
new file mode 100644 (file)
index 0000000..0310ffd
Binary files /dev/null and b/src/communication/ConnectionTest/ConnectionTest/ConnectionTest.UWP/ConnectionTest.UWP_TemporaryKey.pfx differ
index af45c8b..0d9cd02 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
-  <Identity Name="86f72843-d865-4b0a-b4e8-0e0eab64a758" Publisher="CN=0a82030a-6916-4e4d-846d-e88d76b918b5" Version="1.0.4.0" />
+  <Identity Name="86f72843-d865-4b0a-b4e8-0e0eab64a758" Publisher="CN=0a82030a-6916-4e4d-846d-e88d76b918b5" Version="1.0.8.0" />
   <mp:PhoneIdentity PhoneProductId="ec0cc741-fd3e-485c-81be-68815c480690" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
   <Properties>
     <DisplayName>ConnectionTest.UWP</DisplayName>
@@ -29,5 +29,7 @@
   </Applications>
   <Capabilities>
     <Capability Name="internetClient" />
+    <Capability Name="internetClientServer" />
+    <Capability Name="privateNetworkClientServer" />
   </Capabilities>
 </Package>
\ No newline at end of file
index 0aa5dd9..1bcad56 100644 (file)
   </ItemGroup>
 
   <ItemGroup>
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\4Players.png" Link="Images\4Players.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Bomb.png" Link="Images\Bomb.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Flame.png" Link="Images\Flame.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Player1.png" Link="Images\Player1.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Player2.png" Link="Images\Player2.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Player3.png" Link="Images\Player3.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Player4.png" Link="Images\Player4.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Unknown.png" Link="Images\Unknown.png" />
+    <EmbeddedResource Include="..\..\..\..\visualization\Playground\HelloMovingImage\HelloMovingImage\Images\Wall.png" Link="Images\Wall.png" />
+  </ItemGroup>
+
+  <ItemGroup>
     <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
     <PackageReference Include="Xamarin.Forms" Version="3.3.0.912540" />
   </ItemGroup>
+
+  <ItemGroup>
+    <Folder Include="Images\" />
+  </ItemGroup>
 </Project>
\ No newline at end of file
index da8535c..7dc03f8 100644 (file)
@@ -31,16 +31,9 @@ namespace Bomberman
 
             con = new TcpJsonConnection(ServerPkgReceived);
 
-            //con.Connect(IPAddress.Parse("fe80::6520:ac61:a4a1:827a%20"));
+            //con.Connect(IPAddress.Parse("192.168.0.5"));
             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
 
-        }
-
-        private void MainPage_SizeChanged(object sender, EventArgs e)
-        {
-
-
-
 
             Entry ent = new Entry();
             ButtonLayout.Children.Add(ent);
@@ -100,23 +93,23 @@ namespace Bomberman
 
                 }
 
-                CreateInitializePlayground(c_defaultPlaygroundSize);
-
-
             }
+        }
 
-
+        private void MainPage_SizeChanged(object sender, EventArgs e)
+        {
+            CreateInitializePlayground(c_defaultPlaygroundSize);
         }
 
         void CreateInitializePlayground(int size)
         {
             fullLayout = new StackLayout();
             playground = new BombermanPlayground.Playground(size);
+            BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
 
-
-            fullLayout.Children.Add(playground);
             fullLayout.Children.Add(ButtonLayout);
+            fullLayout.Children.Add(playground);
             Content = fullLayout;
 
         }
@@ -125,6 +118,10 @@ namespace Bomberman
         {
             Device.BeginInvokeOnMainThread(() =>
             {
+                if(playground == null)
+                {
+                    return;
+                }
                 if (playground.FieldSize != pkg.Field.Count)
                 {
                     CreateInitializePlayground(pkg.Field.Count);
index ef5fabd..ffc40ba 100644 (file)
@@ -41,7 +41,7 @@ namespace Bomberman
             public GameSimulation() : base((field) => { }) { }
 
 
-            const int FieldSize = 20;
+            const int FieldSize = 13;
             const double MoveLengthPerEvent = 1.0;  // We do not hold the state of players (like the server) in simulation, so we will only move when we get the event to do that..
 
             //Run Simulation in this class.
@@ -185,6 +185,12 @@ namespace Bomberman
                     {
                         bombXPos.Add(playerXPos[ClientId]);
                         bombYPos.Add(playerYPos[ClientId]);
+
+                        if(bombXPos.Count >5)
+                        {
+                            bombXPos.RemoveAt(0);
+                            bombYPos.RemoveAt(0);
+                        }
                     }
                 }
             }
@@ -196,7 +202,7 @@ namespace Bomberman
         /// Socket stuff below
         //////////////////////////////////////////>
 
-        const int c_ClientUpdateInterval = 500 /*ms*/;
+        const int c_ClientUpdateInterval = 50 /*ms*/;
 
 
         bool Running = true;
index 2bdc35d..2be8bb6 100644 (file)
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using System.Threading.Tasks;
 using Bomberman;
 using Xamarin.Forms;
 using static Bomberman.TcpJsonConnection;
@@ -10,6 +11,9 @@ namespace BombermanPlayground
     class Playground : Xamarin.Forms.AbsoluteLayout
     {
 
+        public static string c_ImageNamespace = "HelloMovingImage";
+
+
         class PlaygroundException : Exception {
             public PlaygroundException(string str) : base(str) { }
         }
@@ -37,6 +41,9 @@ namespace BombermanPlayground
                 throw new PlaygroundException("Error: Fieldsize in Playground does not match Package fieldsize");
             }
 
+            //---------------------------------------------------------------------------------------------
+            //----------------------------------Update Field-----------------------------------------------
+
             for (int i = 0; i < pkg.Field.Count; i++)
             {
                 if (pkg.Field[i].Count != fieldSize)
@@ -68,6 +75,9 @@ namespace BombermanPlayground
                 }
             }
 
+            //---------------------------------------------------------------------------------------------
+            //----------------------------------Update Players---------------------------------------------
+
 
             for (int i = 0; i < pkg.Players.Count; i++)
             {
@@ -86,40 +96,105 @@ namespace BombermanPlayground
                 }
             }
 
+            //---------------------------------------------------------------------------------------------
+            const double c_maxKeepOldElementDistance = 0.1;
+            //---------------------------------------------------------------------------------------------
+            //----------------------------------Update Bombs-----------------------------------------------
+
+
 
-            // Remove old bombs..
-            foreach (BombBlock bomb in bombs)
+            List<BombBlock> newBombs = new List<BombBlock>();
+            List<BombBlock> unchangedBombs = new List<BombBlock>();
+            List<BombBlock> removeOldBombs = new List<BombBlock>();
+
+            for (int i = 0; i < pkg.Bombs.Count; i++)
             {
-                Children.Remove(bomb);
+                newBombs.Add(new BombBlock(pkg.Bombs[i].X, pkg.Bombs[i].Y));
             }
-            bombs.Clear();
 
-            // Add new ones
-            for (int i = 0; i < pkg.Bombs.Count; i++)
+            foreach (var oldBomb in bombs)
+            {
+                bool IsOldBombStillHere = false;
+                foreach (var newBomb in newBombs)
+                {
+                    if (newBomb.TranslationX < oldBomb.TranslationX + c_maxKeepOldElementDistance && newBomb.TranslationX > oldBomb.TranslationX - c_maxKeepOldElementDistance &&
+                        newBomb.TranslationY < oldBomb.TranslationY + c_maxKeepOldElementDistance && newBomb.TranslationY > oldBomb.TranslationY - c_maxKeepOldElementDistance)
+                    {
+                        IsOldBombStillHere = true;
+                        unchangedBombs.Add(newBomb);
+                    }
+
+                }
+                if (!IsOldBombStillHere)
+                {
+                    removeOldBombs.Add(oldBomb);
+                }
+            }
+
+            foreach (var newBomb in newBombs)
             {
-                var bomb = new BombBlock(pkg.Bombs[i].X, pkg.Bombs[i].Y);
-                Children.Add(bomb);
-                bombs.Add(bomb);
+                if (!unchangedBombs.Contains(newBomb))
+                {
+                    bombs.Add(newBomb);
+                    Children.Add(newBomb);
+                }
             }
 
-            // Remove old flames..
-            foreach (FlameBlock flame in flames)
+            foreach (var oldBomb in removeOldBombs)
             {
-                Children.Remove(flame);
+                bombs.Remove(oldBomb);
+                Children.Remove(oldBomb);
             }
-            flames.Clear();
 
-            // Add new ones
+
+            //---------------------------------------------------------------------------------------------
+            //----------------------------------Update Flames-----------------------------------------------
+
+            List<FlameBlock> newFlames = new List<FlameBlock>();
+            List<FlameBlock> unchangedFlames = new List<FlameBlock>();
+            List<FlameBlock> removeOldFlames = new List<FlameBlock>();
+
             for (int i = 0; i < pkg.Flames.Count; i++)
             {
-                var flame = new FlameBlock(pkg.Flames[i].X, pkg.Flames[i].Y);
-                Children.Add(flame);
-                flames.Add(flame);
+                newFlames.Add(new FlameBlock(pkg.Flames[i].X, pkg.Flames[i].Y));
             }
 
+            foreach (var oldFlame in flames)
+            {
+                bool IsOldFlameStillHere = false;
+                foreach (var newFlame in newFlames)
+                {
+                    if (newFlame.TranslationX < oldFlame.TranslationX + c_maxKeepOldElementDistance && newFlame.TranslationX > oldFlame.TranslationX - c_maxKeepOldElementDistance &&
+                        newFlame.TranslationY < oldFlame.TranslationY + c_maxKeepOldElementDistance && newFlame.TranslationY > oldFlame.TranslationY - c_maxKeepOldElementDistance)
+                    {
+                        IsOldFlameStillHere = true;
+                        unchangedFlames.Add(newFlame);
+                    }
 
-        }
+                }
+                if (!IsOldFlameStillHere)
+                {
+                    removeOldFlames.Add(oldFlame);
+                }
+            }
 
+            foreach (var newFlame in newFlames)
+            {
+                if (!unchangedFlames.Contains(newFlame))
+                {
+                    flames.Add(newFlame);
+                    Children.Add(newFlame);
+                }
+            }
+
+            foreach (var oldFlame in removeOldFlames)
+            {
+                flames.Remove(oldFlame);
+                Children.Remove(oldFlame);
+            }
+
+
+        }
 
         /// <summary>
         /// Change Element to the new object, but only if it has different type
@@ -161,7 +236,7 @@ namespace BombermanPlayground
 
         public BombermanBlock(double posX, double posY)
         {
-            Source = ImageSource.FromResource("HelloMovingImage.Images.Unknown.png");
+            Source = ImageSource.FromResource(Playground.c_ImageNamespace + ".Images.Unknown.png");
 
             WidthRequest = BlockSize + 1;
             TranslationX = BlockSize * posX + BlockSize / 2;
@@ -180,7 +255,7 @@ namespace BombermanPlayground
     {
         public WallBlock(double posX, double posY) : base(posX, posY)
         {
-            Source = ImageSource.FromResource("HelloMovingImage.Images.Wall.png");
+            Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Wall.png");
         }
     }
 
@@ -188,7 +263,7 @@ namespace BombermanPlayground
     {
         public BombBlock(double posX, double posY) : base(posX, posY)
         {
-            Source = ImageSource.FromResource("HelloMovingImage.Images.Bomb.png");
+            Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Bomb.png");
         }
     }
 
@@ -196,7 +271,7 @@ namespace BombermanPlayground
     {
         public FlameBlock(double posX, double posY) : base(posX, posY)
         {
-            Source = ImageSource.FromResource("HelloMovingImage.Images.Flame.png");
+            Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Flame.png");
         }
     }
 
@@ -229,19 +304,19 @@ namespace BombermanPlayground
             switch (playerId % c_NumOfAvailablePictures)
             {
                 case 0:
-                    Source = ImageSource.FromResource("HelloMovingImage.Images.Player1.png");
+                    Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Player1.png");
                     break;
 
                 case 1:
-                    Source = ImageSource.FromResource("HelloMovingImage.Images.Player2.png");
+                    Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Player2.png");
                     break;
 
                 case 2:
-                    Source = ImageSource.FromResource("HelloMovingImage.Images.Player3.png");
+                    Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Player3.png");
                     break;
 
                 case 3:
-                    Source = ImageSource.FromResource("HelloMovingImage.Images.Player4.png");
+                    Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Player4.png");
                     break;
             }
         }
@@ -251,7 +326,7 @@ namespace BombermanPlayground
     {
         public ItemBlock(double posX, double posY) : base(posX, posY)
         {
-            //Source = ImageSource.FromResource("HelloMovingImage.Images.Unknwon.png");
+            //Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Unknwon.png");
         }
     }
 
@@ -259,7 +334,7 @@ namespace BombermanPlayground
     {
         public DestroyableBlock(double posX, double posY) : base(posX, posY)
         {
-            //Source = ImageSource.FromResource("HelloMovingImage.Images.Unknwon.png");
+            //Source = ImageSource.FromResource(Playground.c_ImageNamespace+".Images.Unknwon.png");
         }
     }