Removed offset for player position calculation as server does this for now
[clanbomber-xamarin-client.git] / src / communication / ConnectionTest / ConnectionTest / ConnectionTest / MainPage.xaml.cs
index a2eb64a..2cdf227 100644 (file)
@@ -6,29 +6,37 @@ using System.Threading.Tasks;
 using Xamarin.Forms;
 using Newtonsoft.Json;
 using System.Net;
+using static Bomberman.TcpJsonConnection;
 
-namespace ConnectionTest
+namespace Bomberman
 {
     public partial class MainPage : ContentPage
     {
 
+        const int c_defaultPlaygroundSize = 13;
 
         TcpJsonConnection con;
 
+        StackLayout fullLayout;
+        StackLayout ButtonLayout = new StackLayout();
+
+
+        BombermanPlayground.Playground playground;
+
         public MainPage()
         {
             InitializeComponent();
 
-            con = new TcpJsonConnection((field) => { });
+            SizeChanged += MainPage_SizeChanged;
 
-            //con.Connect(IPAddress.Parse("fe80::c0a:44f1:63e7:bfb8%4"));
-            con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]);    //localhost
+            con = new TcpJsonConnection(ServerPkgReceived);
 
+            //con.Connect(IPAddress.Parse("10.29.17.60"));
+            con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
 
-            StackLayout verticalLayout = new StackLayout();
 
             Entry ent = new Entry();
-            verticalLayout.Children.Add(ent);
+            ButtonLayout.Children.Add(ent);
             ent.TextChanged += Ent_TextChanged;
 
 
@@ -36,20 +44,22 @@ namespace ConnectionTest
             {
                 StackLayout horizontalLayout = new StackLayout();
                 horizontalLayout.Orientation = StackOrientation.Horizontal;
-                verticalLayout.Children.Add(horizontalLayout);
+                ButtonLayout.Children.Add(horizontalLayout);
 
                 Button dummy = new Button();
                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
 
                 dummy.Text = "<";
-                
 
-                switch (i) {
+
+                switch (i)
+                {
                     case 0:
                         horizontalLayout.Children.Add(dummy);
                         Button up = new Button();
                         up.Pressed += Up_Pressed;
+                        up.Released += Up_Released;
                         up.Text = "^";
                         horizontalLayout.Children.Add(up);
                         horizontalLayout.Children.Add(dummy);
@@ -59,16 +69,19 @@ namespace ConnectionTest
                         Button left = new Button();
                         left.Text = "<";
                         left.Pressed += Left_Pressed;
+                        left.Released += Left_Released;
                         horizontalLayout.Children.Add(left);
 
                         Button bomb = new Button();
                         bomb.Pressed += Bomb_Pressed;
+                        bomb.Released += Bomb_Released;
                         bomb.Text = "*";
                         horizontalLayout.Children.Add(bomb);
 
                         Button right = new Button();
                         right.Pressed += Right_Pressed;
-                        right.Text = ">";                    
+                        right.Released += Right_Released;
+                        right.Text = ">";
                         horizontalLayout.Children.Add(right);
                         break;
 
@@ -76,6 +89,7 @@ namespace ConnectionTest
                         horizontalLayout.Children.Add(dummy);
                         Button down = new Button();
                         down.Pressed += Down_Pressed;
+                        down.Released += Down_Released;
                         down.Text = "V";
                         horizontalLayout.Children.Add(down);
                         horizontalLayout.Children.Add(dummy);
@@ -84,16 +98,70 @@ namespace ConnectionTest
 
                 }
 
+            }
+        }
 
+        private void Down_Released(object sender, EventArgs e)
+        {
+            con.SendEvent(TcpJsonConnection.InputState.Idle);
+        }
 
+        private void Right_Released(object sender, EventArgs e)
+        {
+            con.SendEvent(TcpJsonConnection.InputState.Idle);
+        }
 
-            }
+        private void Bomb_Released(object sender, EventArgs e)
+        {
+            con.SendEvent(TcpJsonConnection.InputBomb.Idle);
+        }
+
+        private void Left_Released(object sender, EventArgs e)
+        {
+            con.SendEvent(TcpJsonConnection.InputState.Idle);
+        }
+
+        private void Up_Released(object sender, EventArgs e)
+        {
+            con.SendEvent(TcpJsonConnection.InputState.Idle);
+        }
 
-            Content = verticalLayout;
-            
+        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(ButtonLayout);
+            fullLayout.Children.Add(playground);
+            Content = fullLayout;
 
         }
 
+        void ServerPkgReceived(ServerPackage pkg)
+        {
+            Device.BeginInvokeOnMainThread(() =>
+            {
+                if(playground == null)
+                {
+                    return;
+                }
+                if (playground.FieldSize != pkg.Field.Count)
+                {
+                    CreateInitializePlayground(pkg.Field.Count);
+                }
+                playground.Update(pkg);
+
+            });
+        }
+
+
         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
         {
             switch(((Entry)sender).Text)
@@ -124,32 +192,27 @@ namespace ConnectionTest
         private void Bomb_Pressed(object sender, EventArgs e)
         {
             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
-            con.SendEvent(TcpJsonConnection.InputBomb.Idle);
         }
 
         private void Right_Pressed(object sender, EventArgs e)
         {
             con.SendEvent(TcpJsonConnection.InputState.Right);
-            con.SendEvent(TcpJsonConnection.InputState.Idle);
         }
 
         private void Left_Pressed(object sender, EventArgs e)
         {
             con.SendEvent(TcpJsonConnection.InputState.Left);
-            con.SendEvent(TcpJsonConnection.InputState.Idle);
         }
 
         private void Down_Pressed(object sender, EventArgs e)
         {
             con.SendEvent(TcpJsonConnection.InputState.Down);
-            con.SendEvent(TcpJsonConnection.InputState.Idle);
         }
 
 
         private void Up_Pressed(object sender, EventArgs e)
         {
             con.SendEvent(TcpJsonConnection.InputState.Up);
-            con.SendEvent(TcpJsonConnection.InputState.Idle);
         }
     }
 }