2bb896b51b9a5ba04df320134a923bc0acf110f2
[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
25         Playground playground;
26
27         public InGamePage(TcpJsonConnection connection)
28         {
29             con = connection;
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
39
40             for (int i = 0; i < 3; i++)
41             {
42                 StackLayout horizontalLayout = new StackLayout();
43                 horizontalLayout.Orientation = StackOrientation.Horizontal;
44                 ButtonLayout.Children.Add(horizontalLayout);
45
46                 Button dummy = new Button();
47                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
48                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
49
50                 dummy.Text = "<";
51
52
53                 switch (i)
54                 {
55                     case 0:
56                         horizontalLayout.Children.Add(dummy);
57                         Button up = new Button();
58                         up.Pressed += Up_Pressed;
59                         up.Released += Up_Released;
60                         up.Text = "^";
61                         horizontalLayout.Children.Add(up);
62                         horizontalLayout.Children.Add(dummy);
63                         break;
64
65                     case 1:
66                         Button left = new Button();
67                         left.Text = "<";
68                         left.Pressed += Left_Pressed;
69                         left.Released += Left_Released;
70                         horizontalLayout.Children.Add(left);
71
72                         Button bomb = new Button();
73                         bomb.Pressed += Bomb_Pressed;
74                         bomb.Released += Bomb_Released;
75                         bomb.Text = "*";
76                         horizontalLayout.Children.Add(bomb);
77
78                         Button right = new Button();
79                         right.Pressed += Right_Pressed;
80                         right.Released += Right_Released;
81                         right.Text = ">";
82                         horizontalLayout.Children.Add(right);
83                         break;
84
85                     case 2:
86                         horizontalLayout.Children.Add(dummy);
87                         Button down = new Button();
88                         down.Pressed += Down_Pressed;
89                         down.Released += Down_Released;
90                         down.Text = "V";
91                         horizontalLayout.Children.Add(down);
92                         horizontalLayout.Children.Add(dummy);
93                         break;
94
95
96                 }
97
98             }
99
100             Entry ent = new Entry();
101             ButtonLayout.Children.Add(ent);
102             ent.TextChanged += Ent_TextChanged;
103         }
104         private void Down_Released(object sender, EventArgs e)
105         {
106             con.SendEvent(TcpJsonConnection.InputState.Idle);
107         }
108
109         private void Right_Released(object sender, EventArgs e)
110         {
111             con.SendEvent(TcpJsonConnection.InputState.Idle);
112         }
113
114         private void Bomb_Released(object sender, EventArgs e)
115         {
116             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
117         }
118
119         private void Left_Released(object sender, EventArgs e)
120         {
121             con.SendEvent(TcpJsonConnection.InputState.Idle);
122         }
123
124         private void Up_Released(object sender, EventArgs e)
125         {
126             con.SendEvent(TcpJsonConnection.InputState.Idle);
127         }
128
129         private void MainPage_SizeChanged(object sender, EventArgs e)
130         {
131             CreateInitializePlayground(c_defaultPlaygroundSize);
132         }
133
134         public void CreateInitializePlayground(int size)
135         {
136             fullLayout = new StackLayout();
137             playground = new BombermanPlayground.Playground(size);
138             BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
139             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
140
141             fullLayout.Children.Add(playground);
142             fullLayout.Children.Add(ButtonLayout);
143             ButtonLayout.TranslationY = Width;
144             Content = fullLayout;
145
146         }
147
148         public void UpdatePlayground(ServerPackage pkg)
149         {
150             if (playground == null)
151             {
152                 return;
153             }
154             if (playground.FieldSize != pkg.Field.Count)
155             {
156                 CreateInitializePlayground(pkg.Field.Count);
157             }
158             playground.Update(pkg);
159
160         }
161
162         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
163         {
164             switch (((Entry)sender).Text)
165             {
166                 case "w":
167                     con.SendEvent(TcpJsonConnection.InputState.Up);
168                     break;
169                 case "a":
170                     con.SendEvent(TcpJsonConnection.InputState.Left);
171                     break;
172                 case "s":
173                     con.SendEvent(TcpJsonConnection.InputState.Down);
174                     break;
175                 case "d":
176                     con.SendEvent(TcpJsonConnection.InputState.Right);
177                     break;
178                 case " ":
179                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
180                     new Task(() => { System.Threading.Thread.Sleep(50); con.SendEvent(TcpJsonConnection.InputBomb.Idle); }).Start();
181                     break;
182                 case "X":
183                     con.SendEvent(TcpJsonConnection.InputState.Idle);
184                     break;
185             }
186             ((Entry)sender).Text = "";
187
188
189         }
190
191         private void Bomb_Pressed(object sender, EventArgs e)
192         {
193             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
194         }
195
196         private void Right_Pressed(object sender, EventArgs e)
197         {
198             con.SendEvent(TcpJsonConnection.InputState.Right);
199         }
200
201         private void Left_Pressed(object sender, EventArgs e)
202         {
203             con.SendEvent(TcpJsonConnection.InputState.Left);
204         }
205
206         private void Down_Pressed(object sender, EventArgs e)
207         {
208             con.SendEvent(TcpJsonConnection.InputState.Down);
209         }
210
211
212         private void Up_Pressed(object sender, EventArgs e)
213         {
214             con.SendEvent(TcpJsonConnection.InputState.Up);
215         }
216     }
217 }
218