forked from kemot90/GameClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cs
177 lines (168 loc) · 5.56 KB
/
Map.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using Commands;
namespace RPGClient
{
public class City
{
private uint id;
private string name;
private uint accessLevel;
private uint leftCoordinate;
private uint topCoordinate;
private string icon;
public City(uint _id, string _name, uint _accessLevel, uint _leftCoordinate, uint _topCoordinate, string _icon)
{
id = _id;
name = _name;
accessLevel = _accessLevel;
leftCoordinate = _leftCoordinate;
topCoordinate = _topCoordinate;
icon = _icon;
}
public uint Id
{
get
{
return id;
}
}
public string Name
{
get
{
return name;
}
}
public uint AccessLevel
{
get
{
return accessLevel;
}
}
public uint LeftCoordinate
{
get
{
return leftCoordinate;
}
}
public uint TopCoordinate
{
get
{
return topCoordinate;
}
}
public string Icon
{
get
{
return icon;
}
}
}
public class Map
{
private List<Button> cityButtons = new List<Button>();
private List<City> cityData = new List<City>();
private Socket socket;
public Map(TcpClient clientSocket)
{
socket = clientSocket.Client;
uint citiesNumber = 0;
string[] citiesData;
Command request = new Command();
request.Request(ClientCmd.GET_CITIES);
citiesData = request.Apply(socket, true);
//obliczenie liczby miast
citiesNumber = uint.Parse(citiesData[1]);
for (int i = 2; i < citiesData.Length; i += 6)
{
City city = new City(
uint.Parse(citiesData[i]), //id
citiesData[i + 1], //name
uint.Parse(citiesData[i + 2]), //accessLevel
uint.Parse(citiesData[i + 3]), //leftCoordinate
uint.Parse(citiesData[i + 4]), //topCoordinate
citiesData[i + 5] //icon
);
cityData.Add(city);
}
//tworzenie buttonów na podstawie danych z bazki
foreach (City city in cityData)
{
//najpierw sam button
Button btn = new Button();
//potem stackpanel przechowujący jego zawartość
StackPanel btnContent = new StackPanel();
//obrazek, który będzie jego zawartością
Image btnImage = new Image();
//i jego źródło
btnImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/" + city.Icon + ".png"));
btnImage.Width = 44;
btnImage.Height = 60;
//oraz textblock, który będzie wyświetlał nazwę miasta
TextBlock btnText = new TextBlock();
//przypisanie do texblocka nazwy miasta
btnText.Text = city.Name;
//szerokość stała na 50 px
btnText.Width = 50;
//nazwa kontrolki aby można było dynamicznie zmieniać kolor tekstu w buttonie
btnText.Name = "city" + city.Id + "Name";
//wyśrodkowanie tekstu
btnText.TextAlignment = TextAlignment.Center;
//ustawienie stackpanelu na 40 szerokości i 75 wysokości (obrazek wtedy sam się zeskaluje)
btnContent.Width = 50;
btnContent.Height = 75;
btnContent.Margin = new Thickness(3, 3, 3, 3);
//wyśrodkowanie zawartości w pionie do środka
btnContent.VerticalAlignment = VerticalAlignment.Center;
//dodanie obrazka i texblocka z nazwą miasta do stackpanelu
btnContent.Children.Add(btnImage);
btnContent.Children.Add(btnText);
//nazwanie buttona miasta
btn.Name = "city" + city.Id;
//jego szerokość na 55 px
btn.Width = 55;
//cały content wyśrodkowany
btn.HorizontalAlignment = HorizontalAlignment.Center;
//przypisanie stackpanelu jako zawartości buttona
btn.Content = btnContent;
//dodanie tagu z identyfikatorem miasta
btn.Tag = city.Id;
//określenie położenia na mapie
Canvas.SetLeft(btn, city.LeftCoordinate);
Canvas.SetTop(btn, city.TopCoordinate);
//dodanie do listy buttonów mapy
cityButtons.Add(btn);
}
}
public List<Button> CityButtons
{
get
{
return cityButtons;
}
}
public List<City> CityData
{
get
{
return cityData;
}
}
}
}