centered position of player controls
[clanbomber-xamarin-client.git] / src / full_system / ConnectionTest / ConnectionTest / ConnectionTest / InGamePage.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 using BombermanPlayground;
11
12 namespace Bomberman
13 {
14     public class InGamePage : ContentPage
15     {
16         private TcpJsonConnection con;
17
18         const int c_defaultPlaygroundSize = 13;
19
20         //TcpJsonConnection con;
21
22         StackLayout fullLayout;
23         StackLayout ButtonLayout = new StackLayout();
24         Label labelLives;
25
26         Playground playground;
27         Grid ButtonGrid;
28
29         public InGamePage(TcpJsonConnection connection)
30         {
31             con = connection;
32             SizeChanged += MainPage_SizeChanged;
33
34             //con = new TcpJsonConnection(ServerPkgReceived);
35
36             //con.Connect(IPAddress.Parse("192.168.0.5"));
37             //con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
38
39             ButtonGrid = new Grid();
40
41             ButtonGrid.ColumnDefinitions = new ColumnDefinitionCollection()
42             {
43                 new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)},
44                 new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)},
45                 new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)},
46                 new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)},
47                 new ColumnDefinition(){Width = new GridLength(1, GridUnitType.Star)},
48
49             };
50
51             labelLives = new Label
52             {
53                 Text = "Lives: 3",
54                 FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
55                 VerticalOptions = LayoutOptions.CenterAndExpand,
56                 HorizontalOptions = LayoutOptions.CenterAndExpand
57             };
58
59             ButtonGrid.Children.Add(labelLives, 0, 0);
60
61             for (int i = 0; i < 3; i++)
62             {
63                 StackLayout horizontalLayout = new StackLayout();
64                 horizontalLayout.Orientation = StackOrientation.Horizontal;
65                 ButtonLayout.Children.Add(horizontalLayout);
66
67                 Button dummy = new Button();
68                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
69                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
70
71                 dummy.Text = "<";
72
73
74                 switch (i)
75                 {
76                     case 0:
77                         horizontalLayout.Children.Add(dummy);
78                         Button up = new Button();
79                         up.Pressed += Up_Pressed;
80                         up.Released += Up_Released;
81                         up.Text = "^";
82                         horizontalLayout.Children.Add(up);
83                         horizontalLayout.Children.Add(dummy);
84                         ButtonGrid.Children.Add(up, 2, 0);
85                         break;
86
87                     case 1:
88                         Button left = new Button();
89                         left.Text = "<";
90                         left.Pressed += Left_Pressed;
91                         left.Released += Left_Released;
92                         horizontalLayout.Children.Add(left);
93                         ButtonGrid.Children.Add(left, 1, 1);
94
95                         Button bomb = new Button();
96                         bomb.Pressed += Bomb_Pressed;
97                         bomb.Released += Bomb_Released;
98                         bomb.Text = "*";
99                         horizontalLayout.Children.Add(bomb);
100                         ButtonGrid.Children.Add(bomb, 2, 1);
101
102                         Button right = new Button();
103                         right.Pressed += Right_Pressed;
104                         right.Released += Right_Released;
105                         right.Text = ">";
106                         horizontalLayout.Children.Add(right);
107                         ButtonGrid.Children.Add(right, 3, 1);
108                         break;
109
110                     case 2:
111                         horizontalLayout.Children.Add(dummy);
112                         Button down = new Button();
113                         down.Pressed += Down_Pressed;
114                         down.Released += Down_Released;
115                         down.Text = "V";
116                         horizontalLayout.Children.Add(down);
117                         horizontalLayout.Children.Add(dummy);
118                         ButtonGrid.Children.Add(down, 2, 2);
119                         break;
120
121
122                 }
123
124             }
125
126             Entry ent = new Entry();
127             ButtonLayout.Children.Add(ent);
128             ent.TextChanged += Ent_TextChanged;
129         }
130         private void Down_Released(object sender, EventArgs e)
131         {
132             con.SendEvent(TcpJsonConnection.InputState.Idle);
133         }
134
135         private void Right_Released(object sender, EventArgs e)
136         {
137             con.SendEvent(TcpJsonConnection.InputState.Idle);
138         }
139
140         private void Bomb_Released(object sender, EventArgs e)
141         {
142             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
143         }
144
145         private void Left_Released(object sender, EventArgs e)
146         {
147             con.SendEvent(TcpJsonConnection.InputState.Idle);
148         }
149
150         private void Up_Released(object sender, EventArgs e)
151         {
152             con.SendEvent(TcpJsonConnection.InputState.Idle);
153         }
154
155         private void MainPage_SizeChanged(object sender, EventArgs e)
156         {
157             CreateInitializePlayground(c_defaultPlaygroundSize);
158         }
159
160         public void CreateInitializePlayground(int size)
161         {
162             fullLayout = new StackLayout();
163             playground = new BombermanPlayground.Playground(size);
164             BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
165             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
166
167             ButtonGrid.TranslationY = Width;
168             fullLayout.Children.Add(playground);
169             //fullLayout.Children.Add(ButtonLayout);
170             //ButtonLayout.TranslationY = Width;
171             //fullLayout.Children.Add(labelLives);
172             fullLayout.Children.Add(ButtonGrid);
173             Content = fullLayout;
174
175         }
176
177         public void UpdateLableLives(ServerPackage pkg)
178         {
179             for (int i = 0; i < pkg.Players.Count; i++)
180             {
181                 if (pkg.Id == pkg.Players[i].Id)
182                 {
183                     labelLives.Text = "Lives: " + pkg.Players[i].Life;
184                     break;
185                 }
186             }
187         }
188
189
190         public void UpdatePlayground(ServerPackage pkg)
191         {
192             if (playground == null)
193             {
194                 return;
195             }
196             if (playground.FieldSize != pkg.Field.Count)
197             {
198                 CreateInitializePlayground(pkg.Field.Count);
199             }
200             playground.Update(pkg);
201
202         }
203
204         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
205         {
206             switch (((Entry)sender).Text)
207             {
208                 case "w":
209                     con.SendEvent(TcpJsonConnection.InputState.Up);
210                     break;
211                 case "a":
212                     con.SendEvent(TcpJsonConnection.InputState.Left);
213                     break;
214                 case "s":
215                     con.SendEvent(TcpJsonConnection.InputState.Down);
216                     break;
217                 case "d":
218                     con.SendEvent(TcpJsonConnection.InputState.Right);
219                     break;
220                 case " ":
221                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
222                     new Task(() => { System.Threading.Thread.Sleep(50); con.SendEvent(TcpJsonConnection.InputBomb.Idle); }).Start();
223                     break;
224                 case "X":
225                     con.SendEvent(TcpJsonConnection.InputState.Idle);
226                     break;
227             }
228             ((Entry)sender).Text = "";
229
230
231         }
232
233         private void Bomb_Pressed(object sender, EventArgs e)
234         {
235             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
236         }
237
238         private void Right_Pressed(object sender, EventArgs e)
239         {
240             con.SendEvent(TcpJsonConnection.InputState.Right);
241         }
242
243         private void Left_Pressed(object sender, EventArgs e)
244         {
245             con.SendEvent(TcpJsonConnection.InputState.Left);
246         }
247
248         private void Down_Pressed(object sender, EventArgs e)
249         {
250             con.SendEvent(TcpJsonConnection.InputState.Down);
251         }
252
253
254         private void Up_Pressed(object sender, EventArgs e)
255         {
256             con.SendEvent(TcpJsonConnection.InputState.Up);
257         }
258     }
259 }
260