Removed duplicate Files
[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("fe80::6520:ac61:a4a1:827a%20"));
35             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
36
37         }
38
39         private void MainPage_SizeChanged(object sender, EventArgs e)
40         {
41
42
43
44
45             Entry ent = new Entry();
46             ButtonLayout.Children.Add(ent);
47             ent.TextChanged += Ent_TextChanged;
48
49
50             for (int i = 0; i < 3; i++)
51             {
52                 StackLayout horizontalLayout = new StackLayout();
53                 horizontalLayout.Orientation = StackOrientation.Horizontal;
54                 ButtonLayout.Children.Add(horizontalLayout);
55
56                 Button dummy = new Button();
57                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
58                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
59
60                 dummy.Text = "<";
61
62
63                 switch (i)
64                 {
65                     case 0:
66                         horizontalLayout.Children.Add(dummy);
67                         Button up = new Button();
68                         up.Pressed += Up_Pressed;
69                         up.Text = "^";
70                         horizontalLayout.Children.Add(up);
71                         horizontalLayout.Children.Add(dummy);
72                         break;
73
74                     case 1:
75                         Button left = new Button();
76                         left.Text = "<";
77                         left.Pressed += Left_Pressed;
78                         horizontalLayout.Children.Add(left);
79
80                         Button bomb = new Button();
81                         bomb.Pressed += Bomb_Pressed;
82                         bomb.Text = "*";
83                         horizontalLayout.Children.Add(bomb);
84
85                         Button right = new Button();
86                         right.Pressed += Right_Pressed;
87                         right.Text = ">";
88                         horizontalLayout.Children.Add(right);
89                         break;
90
91                     case 2:
92                         horizontalLayout.Children.Add(dummy);
93                         Button down = new Button();
94                         down.Pressed += Down_Pressed;
95                         down.Text = "V";
96                         horizontalLayout.Children.Add(down);
97                         horizontalLayout.Children.Add(dummy);
98                         break;
99
100
101                 }
102
103                 CreateInitializePlayground(c_defaultPlaygroundSize);
104
105
106             }
107
108
109         }
110
111         void CreateInitializePlayground(int size)
112         {
113             fullLayout = new StackLayout();
114             playground = new BombermanPlayground.Playground(size);
115             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
116
117
118             fullLayout.Children.Add(playground);
119             fullLayout.Children.Add(ButtonLayout);
120             Content = fullLayout;
121
122         }
123
124         void ServerPkgReceived(ServerPackage pkg)
125         {
126             Device.BeginInvokeOnMainThread(() =>
127             {
128                 if (playground.FieldSize != pkg.Field.Count)
129                 {
130                     CreateInitializePlayground(pkg.Field.Count);
131                 }
132                 playground.Update(pkg);
133
134             });
135         }
136
137
138         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
139         {
140             switch(((Entry)sender).Text)
141             {
142                 case "w":
143                     con.SendEvent(TcpJsonConnection.InputState.Up);
144                     break;
145                 case "a":
146                     con.SendEvent(TcpJsonConnection.InputState.Left);
147                     break;
148                 case "s":
149                     con.SendEvent(TcpJsonConnection.InputState.Down);
150                     break;
151                 case "d":
152                     con.SendEvent(TcpJsonConnection.InputState.Right);
153                     break;
154                 case " ":
155                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
156                     break;
157             }
158             ((Entry)sender).Text = "";
159             con.SendEvent(TcpJsonConnection.InputState.Idle);
160             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
161
162
163         }
164
165         private void Bomb_Pressed(object sender, EventArgs e)
166         {
167             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
168             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
169         }
170
171         private void Right_Pressed(object sender, EventArgs e)
172         {
173             con.SendEvent(TcpJsonConnection.InputState.Right);
174             con.SendEvent(TcpJsonConnection.InputState.Idle);
175         }
176
177         private void Left_Pressed(object sender, EventArgs e)
178         {
179             con.SendEvent(TcpJsonConnection.InputState.Left);
180             con.SendEvent(TcpJsonConnection.InputState.Idle);
181         }
182
183         private void Down_Pressed(object sender, EventArgs e)
184         {
185             con.SendEvent(TcpJsonConnection.InputState.Down);
186             con.SendEvent(TcpJsonConnection.InputState.Idle);
187         }
188
189
190         private void Up_Pressed(object sender, EventArgs e)
191         {
192             con.SendEvent(TcpJsonConnection.InputState.Up);
193             con.SendEvent(TcpJsonConnection.InputState.Idle);
194         }
195     }
196 }