Extended Client Simulation to Xamarin: Android and UWP works with Server Simulator
[clanbomber-xamarin-client.git] / src / communication / ConnectionTest / ConnectionTest / ConnectionTest / MainPage.xaml.cs
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Xamarin.Forms;
7 using Newtonsoft.Json;
8 using System.Net;
9 using static Bomberman.TcpJsonConnection;
10
11 namespace Bomberman
12 {
13     public partial class MainPage : ContentPage
14     {
15
16         const int c_defaultPlaygroundSize = 13;
17
18         TcpJsonConnection con;
19
20         StackLayout fullLayout;
21         StackLayout ButtonLayout = new StackLayout();
22
23
24         BombermanPlayground.Playground playground;
25
26         public MainPage()
27         {
28             InitializeComponent();
29
30             SizeChanged += MainPage_SizeChanged;
31
32             con = new TcpJsonConnection(ServerPkgReceived);
33
34             //con.Connect(IPAddress.Parse("192.168.0.5"));
35             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
36
37
38             Entry ent = new Entry();
39             ButtonLayout.Children.Add(ent);
40             ent.TextChanged += Ent_TextChanged;
41
42
43             for (int i = 0; i < 3; i++)
44             {
45                 StackLayout horizontalLayout = new StackLayout();
46                 horizontalLayout.Orientation = StackOrientation.Horizontal;
47                 ButtonLayout.Children.Add(horizontalLayout);
48
49                 Button dummy = new Button();
50                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
51                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
52
53                 dummy.Text = "<";
54
55
56                 switch (i)
57                 {
58                     case 0:
59                         horizontalLayout.Children.Add(dummy);
60                         Button up = new Button();
61                         up.Pressed += Up_Pressed;
62                         up.Text = "^";
63                         horizontalLayout.Children.Add(up);
64                         horizontalLayout.Children.Add(dummy);
65                         break;
66
67                     case 1:
68                         Button left = new Button();
69                         left.Text = "<";
70                         left.Pressed += Left_Pressed;
71                         horizontalLayout.Children.Add(left);
72
73                         Button bomb = new Button();
74                         bomb.Pressed += Bomb_Pressed;
75                         bomb.Text = "*";
76                         horizontalLayout.Children.Add(bomb);
77
78                         Button right = new Button();
79                         right.Pressed += Right_Pressed;
80                         right.Text = ">";
81                         horizontalLayout.Children.Add(right);
82                         break;
83
84                     case 2:
85                         horizontalLayout.Children.Add(dummy);
86                         Button down = new Button();
87                         down.Pressed += Down_Pressed;
88                         down.Text = "V";
89                         horizontalLayout.Children.Add(down);
90                         horizontalLayout.Children.Add(dummy);
91                         break;
92
93
94                 }
95
96             }
97         }
98
99         private void MainPage_SizeChanged(object sender, EventArgs e)
100         {
101             CreateInitializePlayground(c_defaultPlaygroundSize);
102         }
103
104         void CreateInitializePlayground(int size)
105         {
106             fullLayout = new StackLayout();
107             playground = new BombermanPlayground.Playground(size);
108             BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
109             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
110
111             fullLayout.Children.Add(ButtonLayout);
112             fullLayout.Children.Add(playground);
113             Content = fullLayout;
114
115         }
116
117         void ServerPkgReceived(ServerPackage pkg)
118         {
119             Device.BeginInvokeOnMainThread(() =>
120             {
121                 if(playground == null)
122                 {
123                     return;
124                 }
125                 if (playground.FieldSize != pkg.Field.Count)
126                 {
127                     CreateInitializePlayground(pkg.Field.Count);
128                 }
129                 playground.Update(pkg);
130
131             });
132         }
133
134
135         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
136         {
137             switch(((Entry)sender).Text)
138             {
139                 case "w":
140                     con.SendEvent(TcpJsonConnection.InputState.Up);
141                     break;
142                 case "a":
143                     con.SendEvent(TcpJsonConnection.InputState.Left);
144                     break;
145                 case "s":
146                     con.SendEvent(TcpJsonConnection.InputState.Down);
147                     break;
148                 case "d":
149                     con.SendEvent(TcpJsonConnection.InputState.Right);
150                     break;
151                 case " ":
152                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
153                     break;
154             }
155             ((Entry)sender).Text = "";
156             con.SendEvent(TcpJsonConnection.InputState.Idle);
157             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
158
159
160         }
161
162         private void Bomb_Pressed(object sender, EventArgs e)
163         {
164             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
165             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
166         }
167
168         private void Right_Pressed(object sender, EventArgs e)
169         {
170             con.SendEvent(TcpJsonConnection.InputState.Right);
171             con.SendEvent(TcpJsonConnection.InputState.Idle);
172         }
173
174         private void Left_Pressed(object sender, EventArgs e)
175         {
176             con.SendEvent(TcpJsonConnection.InputState.Left);
177             con.SendEvent(TcpJsonConnection.InputState.Idle);
178         }
179
180         private void Down_Pressed(object sender, EventArgs e)
181         {
182             con.SendEvent(TcpJsonConnection.InputState.Down);
183             con.SendEvent(TcpJsonConnection.InputState.Idle);
184         }
185
186
187         private void Up_Pressed(object sender, EventArgs e)
188         {
189             con.SendEvent(TcpJsonConnection.InputState.Up);
190             con.SendEvent(TcpJsonConnection.InputState.Idle);
191         }
192     }
193 }