1 | using System; // Tämän tarpeellisuus pitäisi olla itsestäänselvyys |
---|
2 | using Jypeli; // Jypeli |
---|
3 | using Jypeli.ScreenObjects; // Jypeli.ScreenObjects |
---|
4 | using Jypeli.Assets; // Jypeli.Assets |
---|
5 | using System.Collections.Generic; // Jypelin tutoriaali |
---|
6 | using System.IO; // Streamreader vaatii tämän |
---|
7 | using System.Windows.Forms; // jotta voin näyttää msgboxeja |
---|
8 | |
---|
9 | namespace ALTKTileEngine |
---|
10 | { |
---|
11 | public class Peli : Game |
---|
12 | { |
---|
13 | //int[,] MapObjectCoords; |
---|
14 | int[,] TerrainMap; |
---|
15 | int[,] DTerrainMap; |
---|
16 | |
---|
17 | int kartanLeveys; |
---|
18 | int kartanKorkeus; |
---|
19 | |
---|
20 | // perusmaastotyypit |
---|
21 | const int Aavikko = 2, Tundra = 3, Vuoristo = 1, Lehtimetsa = 0; |
---|
22 | |
---|
23 | // erikoismaastotyypit |
---|
24 | const int Havumetsa = 2, Tyhja = 0; |
---|
25 | |
---|
26 | // Pelissä on kaksi eri tasoa, toisella perusmaasto ja toisella tarkempaa tietoa (esim. onko ruutu metsää ym.) |
---|
27 | PhysicsObject[,] tile; |
---|
28 | PhysicsObject[,] DTile; |
---|
29 | //Vector tileVector; |
---|
30 | |
---|
31 | protected override void Begin() |
---|
32 | { |
---|
33 | LuoKentta(); |
---|
34 | } |
---|
35 | # region Kartan luominen |
---|
36 | void LuoKentta() |
---|
37 | { |
---|
38 | //tileVector = new Vector(); |
---|
39 | Int32 num; |
---|
40 | Int32 i; |
---|
41 | Int32 j; |
---|
42 | Int32 a; |
---|
43 | Int32 b; |
---|
44 | string mLine; |
---|
45 | //string chars; |
---|
46 | num = RandomGen.NextInt(1, 6); |
---|
47 | //chars = " VAT"; |
---|
48 | TextReader bReader; |
---|
49 | bReader = new StreamReader("Content\\Maps\\1\\base.txt"); |
---|
50 | |
---|
51 | //try |
---|
52 | //{ |
---|
53 | |
---|
54 | // Yleensä kartan kaksi ensimmäistä riviä ovat kommentteja |
---|
55 | bReader.ReadLine(); |
---|
56 | bReader.ReadLine(); |
---|
57 | |
---|
58 | kartanLeveys = Convert.ToInt32(bReader.ReadLine().Substring(9, 2)); |
---|
59 | kartanKorkeus = Convert.ToInt32(bReader.ReadLine().Substring(10, 2)); |
---|
60 | |
---|
61 | Int32 mapWidthJ = (kartanLeveys * 48) / 2; |
---|
62 | Int32 mapHeightJ = (kartanKorkeus * 48) / 2; |
---|
63 | |
---|
64 | bReader.ReadLine(); |
---|
65 | // Ladataan TerrainMapit muistiin |
---|
66 | |
---|
67 | TerrainMap = new int[kartanKorkeus, kartanLeveys]; |
---|
68 | for (a = 0; a <= (kartanKorkeus - 1); a++) |
---|
69 | { |
---|
70 | for (b = 0; b <= (kartanLeveys - 1); b++) |
---|
71 | { |
---|
72 | TerrainMap[a, b] = new int(); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | DTerrainMap = new int[kartanKorkeus, kartanLeveys]; |
---|
77 | for (a = 0; a <= (kartanKorkeus - 1); a++) |
---|
78 | { |
---|
79 | for (b = 0; b <= (kartanKorkeus - 1); b++) |
---|
80 | { |
---|
81 | DTerrainMap[a, b] = new int(); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // Ladataan ruudut muistiin |
---|
86 | |
---|
87 | tile = new PhysicsObject[kartanKorkeus, kartanLeveys]; |
---|
88 | for (a = 0; a <= (kartanKorkeus - 1); a++) |
---|
89 | { |
---|
90 | for (b = 0; b <= (kartanLeveys - 1); b++) |
---|
91 | { |
---|
92 | tile[a, b] = new PhysicsObject(48.0, 48.0); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | DTile = new PhysicsObject[kartanKorkeus, kartanLeveys]; |
---|
97 | for (a = 0; a <= (kartanKorkeus - 1); a++) |
---|
98 | { |
---|
99 | for (b = 0; b <= (kartanLeveys - 1); b++) |
---|
100 | { |
---|
101 | DTile[a, b] = new PhysicsObject(48.0, 48.0); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | // Koodi käännetty RGE:n VB.netistä |
---|
106 | // Asettaa ja piirtää pohjamaaston |
---|
107 | |
---|
108 | for (i = 0; i <= (kartanKorkeus - 1); i++) |
---|
109 | { |
---|
110 | mLine = bReader.ReadLine(); |
---|
111 | for (j = 0; j <= (kartanLeveys - 1); j++) |
---|
112 | { |
---|
113 | // MessageBox.Show(mLine + " " + j + " " + i); |
---|
114 | TerrainMap[i, j] = getTerrainType(mLine.Substring(j, 1)); |
---|
115 | //tileVector.X = j * 48; |
---|
116 | //tileVector.Y = i * 48; |
---|
117 | tile[i, j].Shape = Shapes.Rectangle; |
---|
118 | tile[i, j].X = (j * 48) - mapWidthJ; |
---|
119 | tile[i, j].Y = (i * 48) - mapHeightJ; |
---|
120 | tile[i, j].Restitution = 0.0; |
---|
121 | tile[i, j].Image = LoadImage(getTexture_fromTerrainType(TerrainMap[i, j])); |
---|
122 | // tile.Position = tileVector; |
---|
123 | Add(tile[i, j]); |
---|
124 | // MessageBox.Show(Convert.ToString(TerrainMap[i, j])); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | // Luetaan karttatiedoston rivivälit |
---|
129 | bReader.ReadLine(); |
---|
130 | bReader.ReadLine(); |
---|
131 | |
---|
132 | // Asettaa ja piirtää erikoismaaston |
---|
133 | |
---|
134 | for (i = 0; i <= (kartanKorkeus - 1); i++) |
---|
135 | { |
---|
136 | mLine = bReader.ReadLine(); |
---|
137 | for (j = 0; j <= (kartanLeveys - 1); j++) |
---|
138 | { |
---|
139 | // MessageBox.Show(mLine + " " + j + " " + i); |
---|
140 | DTerrainMap[i, j] = getDetailTerrainType(mLine.Substring(j, 1)); |
---|
141 | //DTileVector.X = j * 48; |
---|
142 | //DTileVector.Y = i * 48; |
---|
143 | DTile[i, j].Shape = Shapes.Rectangle; |
---|
144 | DTile[i, j].X = (j * 48) - mapWidthJ; |
---|
145 | DTile[i, j].Y = (i * 48) - mapHeightJ; |
---|
146 | DTile[i, j].Restitution = 0.0; |
---|
147 | DTile[i, j].Image = LoadImage(getTexture_fromDetailTerrainType(DTerrainMap[i, j])); |
---|
148 | // DTile.Position = DTileVector; |
---|
149 | Add(DTile[i, j]); |
---|
150 | // MessageBox.Show(Convert.ToString(TerrainMap[i, j])); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | //catch |
---|
156 | //{ |
---|
157 | // MessageBox.Show("Map error!"); |
---|
158 | // bReader.Close(); |
---|
159 | // Exit(); |
---|
160 | //} |
---|
161 | bReader.Close(); |
---|
162 | Level.BackgroundColor = Color.Black; |
---|
163 | //MapObjectCoords = new int[kartanLeveys, kartanKorkeus]; |
---|
164 | // var merkit = new Dictionary<char, ObjectCreator>(); |
---|
165 | // merkit['='] = LuoPalikka; |
---|
166 | // merkit['*'] = LuoTahti; |
---|
167 | |
---|
168 | // char[,] ruudut = Tiles.ReadFromFile("Content\\Maps\\1\\base.txt"); |
---|
169 | //Tiles.Insert(this, ruudut, merkit, kartanLeveys, kartanKorkeus); |
---|
170 | } |
---|
171 | # endregion |
---|
172 | int getTerrainType(string ch) |
---|
173 | { |
---|
174 | /*/ Maastotyypit |
---|
175 | * 0 = lehtimetsää |
---|
176 | * 1 = vuoristoa |
---|
177 | * 2 = aavikkoa |
---|
178 | * 3 = tundraa |
---|
179 | /*/ |
---|
180 | if (ch == " ") { return Lehtimetsa; } |
---|
181 | if (ch == "V") { return Vuoristo; } |
---|
182 | if (ch == "A") { return Aavikko; } |
---|
183 | if (ch == "T") { return Tundra; } |
---|
184 | return 0; |
---|
185 | } |
---|
186 | |
---|
187 | int getDetailTerrainType(string ch) |
---|
188 | { |
---|
189 | /*/ Erikoismaastotyypit |
---|
190 | * 0 = ei mitään |
---|
191 | * 1 = vuoristoa |
---|
192 | * 2 = metsää |
---|
193 | /*/ |
---|
194 | if (ch == " ") { return Tyhja; } |
---|
195 | if (ch == "M") { return Havumetsa; } |
---|
196 | if (ch == "V") { return Vuoristo; } |
---|
197 | return Tyhja; |
---|
198 | } |
---|
199 | |
---|
200 | string getTexture_fromTerrainType(int terraintype) |
---|
201 | { |
---|
202 | if (terraintype == Lehtimetsa) { return Application.StartupPath + "\\Content\\Textures\\grassland"; } |
---|
203 | if (terraintype == Vuoristo) { return Application.StartupPath + "\\Content\\Textures\\mountain"; } |
---|
204 | if (terraintype == Aavikko) { return Application.StartupPath + "\\Content\\Textures\\desert"; } |
---|
205 | if (terraintype == Tundra) { return Application.StartupPath + "\\Content\\Textures\\water"; } |
---|
206 | return Application.StartupPath + "\\Content\\Textures\\grassland.jpg"; |
---|
207 | } |
---|
208 | |
---|
209 | string getTexture_fromDetailTerrainType(int dtType) |
---|
210 | { |
---|
211 | if (dtType == Tyhja) { return Application.StartupPath + "\\Content\\Textures\\nothing"; } |
---|
212 | if (dtType == Vuoristo) { return Application.StartupPath + "\\Content\\Textures\\mountain"; } |
---|
213 | if (dtType == Havumetsa) { return Application.StartupPath + "\\Content\\Textures\\forest"; } |
---|
214 | return Application.StartupPath + "\\Content\\Textures\\nothing"; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | //bReader.ReadLine(); |
---|
219 | |
---|
220 | //chars = ".a"; |
---|
221 | |
---|
222 | //for (i = 0; i <= 19; i++) |
---|
223 | //{ |
---|
224 | // Log("LoadBattle: ShadowField; i = " + i + "; luetaan rivi", false); |
---|
225 | // mLine = bReader.ReadLine; |
---|
226 | // for (j = 0; j <= 19; j++) |
---|
227 | // { |
---|
228 | // ShadowField(j, i) = Strings.InStr(chars, Strings.Mid(mLine, j + 1, 1)) - 1; |
---|
229 | // } |
---|
230 | //} |
---|
231 | //bReader.ReadLine(); |
---|
232 | |
---|
233 | //chars = ".rasp+k"; |
---|
234 | //string a = null; |
---|
235 | //for (i = 0; i <= 19; i++) |
---|
236 | //{ |
---|
237 | // Log("LoadBattle: DetailTerrainField; i = " + i + "; luetaan rivi", false); |
---|
238 | // mLine = bReader.ReadLine; |
---|
239 | // for (j = 0; j <= 19; j++) |
---|
240 | // { |
---|
241 | // DetailTerrainField(j, i) = Strings.InStr(chars, Strings.Mid(mLine, j + 1, 1)) - 1; |
---|
242 | // a = Strings.InStr(chars, Strings.Mid(mLine, j + 1, 1)) - 1; |
---|
243 | // } |
---|
244 | //} |
---|
245 | |
---|
246 | //PhysicsObject LuoPalikka() |
---|
247 | //{ |
---|
248 | // PhysicsObject palikka = PhysicsObject.CreateStaticObject(50.0, 50.0); |
---|
249 | // palikka.Shape = Shapes.Circle; |
---|
250 | // palikka.Color = Color.LimeGreen; |
---|
251 | // this.Add(palikka); |
---|
252 | // return palikka; |
---|
253 | //} |
---|
254 | |
---|
255 | //PhysicsObject LuoTahti() |
---|
256 | //{ |
---|
257 | // PhysicsObject tahti = PhysicsObject.CreateStaticObject(25.0, 25.0); |
---|
258 | // tahti.Shape = Shapes.Rectangle; |
---|
259 | // tahti.Color = Color.LightGray; |
---|
260 | // this.Add(tahti); |
---|
261 | // return tahti; |
---|
262 | //} |
---|
263 | |
---|
264 | } |
---|