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