2bdc35d6db334c52bbb57876cf7e564fe31cdf4a
[clanbomber-xamarin-client.git] / src / visualization / Playground / HelloMovingImage / HelloMovingImage / Playground.cs
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Bomberman;
5 using Xamarin.Forms;
6 using static Bomberman.TcpJsonConnection;
7
8 namespace BombermanPlayground
9 {
10     class Playground : Xamarin.Forms.AbsoluteLayout
11     {
12
13         class PlaygroundException : Exception {
14             public PlaygroundException(string str) : base(str) { }
15         }
16
17
18         private readonly int fieldSize; // Lets leave this readonly
19         public int FieldSize { get { return fieldSize; } }
20
21
22         private BombermanBlock[,] playgroungBlocks;
23         private List<PlayerBlock> players = new List<PlayerBlock>();
24         private List<BombBlock> bombs = new List<BombBlock>();
25         private List<FlameBlock> flames = new List<FlameBlock>();
26
27         public Playground(int fieldSize)
28         {
29             this.fieldSize = fieldSize;
30             playgroungBlocks = new BombermanBlock[fieldSize, fieldSize];
31         }
32
33         public void Update(ServerPackage pkg)
34         {
35             if(pkg.Field.Count != fieldSize)
36             {
37                 throw new PlaygroundException("Error: Fieldsize in Playground does not match Package fieldsize");
38             }
39
40             for (int i = 0; i < pkg.Field.Count; i++)
41             {
42                 if (pkg.Field[i].Count != fieldSize)
43                 {
44                     throw new PlaygroundException("Error: Fieldsize in Playground does not match Package fieldsize");
45                 }
46
47                 for (int j = 0; j < pkg.Field[i].Count; j++)
48                 {
49                     BombermanBlock block = null;
50                     switch(pkg.Field[i][j])
51                     {
52                         case ServerPackage.FieldTypes.EmptyField:
53                             block = null;
54                             break;
55                         case ServerPackage.FieldTypes.DynamicOrDestroyableBlock:
56                             block = new DestroyableBlock(i,j);
57                             break;
58                         case ServerPackage.FieldTypes.ItemOrMoreBombs:
59                             block = new ItemBlock(i, j);
60                             break;
61                         case ServerPackage.FieldTypes.StaticOrUndestroyableBlock:
62                             block = new WallBlock(i, j);
63                             break;
64                     }
65
66                     ChangePlaygroundElement(i, j, block);
67
68                 }
69             }
70
71
72             for (int i = 0; i < pkg.Players.Count; i++)
73             {
74                 if(i< players.Count)
75                 {
76                     players[i].MoveTo(pkg.Players[i].X, pkg.Players[i].Y);
77                 }
78                 else
79                 {
80                     players.Add(new PlayerBlock(pkg.Players[i].X, pkg.Players[i].Y, i));
81                     if(i >= players.Count)
82                     {
83                         throw new PlaygroundException("Error: Populating Playground with Players");
84                     }
85                     Children.Add(players[i]);
86                 }
87             }
88
89
90             // Remove old bombs..
91             foreach (BombBlock bomb in bombs)
92             {
93                 Children.Remove(bomb);
94             }
95             bombs.Clear();
96
97             // Add new ones
98             for (int i = 0; i < pkg.Bombs.Count; i++)
99             {
100                 var bomb = new BombBlock(pkg.Bombs[i].X, pkg.Bombs[i].Y);
101                 Children.Add(bomb);
102                 bombs.Add(bomb);
103             }
104
105             // Remove old flames..
106             foreach (FlameBlock flame in flames)
107             {
108                 Children.Remove(flame);
109             }
110             flames.Clear();
111
112             // Add new ones
113             for (int i = 0; i < pkg.Flames.Count; i++)
114             {
115                 var flame = new FlameBlock(pkg.Flames[i].X, pkg.Flames[i].Y);
116                 Children.Add(flame);
117                 flames.Add(flame);
118             }
119
120
121         }
122
123
124         /// <summary>
125         /// Change Element to the new object, but only if it has different type
126         /// </summary>
127         /// <param name="posX"></param>
128         /// <param name="posY"></param>
129         /// <param name="newBlock"></param>
130         private void ChangePlaygroundElement(int posX, int posY, BombermanBlock newBlock)
131         {
132             if (playgroungBlocks[posX, posY] != null)
133             {
134                 if (newBlock == null || playgroungBlocks[posX, posY].GetType() != newBlock.GetType())
135                 {
136                     Children.Remove(playgroungBlocks[posX, posY]);
137
138                     if (newBlock != null)
139                     {
140                         playgroungBlocks[posX, posY] = newBlock;
141                         Children.Add(newBlock);
142                     }
143                 }
144             }
145             else
146             {
147                 if (newBlock != null)
148                 {
149                     playgroungBlocks[posX, posY] = newBlock;
150                     Children.Add(newBlock);
151                 }
152             }
153
154         }
155     }
156
157
158     class BombermanBlock : Xamarin.Forms.Image
159     {
160         public static double BlockSize { get; set; } = 1.0;
161
162         public BombermanBlock(double posX, double posY)
163         {
164             Source = ImageSource.FromResource("HelloMovingImage.Images.Unknown.png");
165
166             WidthRequest = BlockSize + 1;
167             TranslationX = BlockSize * posX + BlockSize / 2;
168             HeightRequest = BlockSize + 1;
169             TranslationY = BlockSize * posY + BlockSize / 2;
170         }
171
172         public void MoveTo(double posX, double posY)
173         {
174             this.TranslateTo(BlockSize * posX + BlockSize / 2, BlockSize * posY + BlockSize / 2);
175         }
176
177     }
178
179     class WallBlock : BombermanBlock
180     {
181         public WallBlock(double posX, double posY) : base(posX, posY)
182         {
183             Source = ImageSource.FromResource("HelloMovingImage.Images.Wall.png");
184         }
185     }
186
187     class BombBlock : BombermanBlock
188     {
189         public BombBlock(double posX, double posY) : base(posX, posY)
190         {
191             Source = ImageSource.FromResource("HelloMovingImage.Images.Bomb.png");
192         }
193     }
194
195     class FlameBlock : BombermanBlock
196     {
197         public FlameBlock(double posX, double posY) : base(posX, posY)
198         {
199             Source = ImageSource.FromResource("HelloMovingImage.Images.Flame.png");
200         }
201     }
202
203     class PlayerBlock : BombermanBlock
204     {
205         public PlayerBlock(double posX, double posY, int playerId) : base(posX, posY)
206         {
207             const int c_NumOfAvailablePictures = 4;
208
209             switch ((playerId / c_NumOfAvailablePictures) % 5)
210             {
211                 case 0:
212                     Rotation = 0;
213                     break;
214                 case 1:
215                     Rotation = 45;
216                     break;
217                 case 2:
218                     Rotation = -45;
219                     break;
220                 case 3:
221                     Rotation = 90;
222                     break;
223                 case 4:
224                     Rotation = -90;
225                     break;
226             }
227
228
229             switch (playerId % c_NumOfAvailablePictures)
230             {
231                 case 0:
232                     Source = ImageSource.FromResource("HelloMovingImage.Images.Player1.png");
233                     break;
234
235                 case 1:
236                     Source = ImageSource.FromResource("HelloMovingImage.Images.Player2.png");
237                     break;
238
239                 case 2:
240                     Source = ImageSource.FromResource("HelloMovingImage.Images.Player3.png");
241                     break;
242
243                 case 3:
244                     Source = ImageSource.FromResource("HelloMovingImage.Images.Player4.png");
245                     break;
246             }
247         }
248     }
249
250     class ItemBlock : BombermanBlock
251     {
252         public ItemBlock(double posX, double posY) : base(posX, posY)
253         {
254             //Source = ImageSource.FromResource("HelloMovingImage.Images.Unknwon.png");
255         }
256     }
257
258     class DestroyableBlock : BombermanBlock
259     {
260         public DestroyableBlock(double posX, double posY) : base(posX, posY)
261         {
262             //Source = ImageSource.FromResource("HelloMovingImage.Images.Unknwon.png");
263         }
264     }
265
266 }