source: 2010/24/ramipasa/ALTKTileEngine/Peli.cs @ 895

Revision 895, 22.6 KB checked in by ramipasa, 13 years ago (diff)

Korjattu useita bugeja kartan sekä omistunvärin piirtämiseen liittyen sekä lisätty logiikka ja grafiikat omistusvärin piirtämiselle.

Line 
1using System;                        // Tämän tarpeellisuus pitäisi olla itsestäänselvyys
2using Jypeli;                        // Jypeli
3using Jypeli.ScreenObjects;          // Jypeli.ScreenObjects
4using Jypeli.Assets;                 // Jypeli.Assets
5using System.Collections.Generic;    // Jypelin tutoriaali
6using System.IO;                     // Streamreader vaatii tämän
7using System.Windows.Forms;          // jotta voin näyttää msgboxeja
8using ALTKTechs;                     // Avaruuslaatikon moottorikomponentit
9using ALTKUnits;
10using ALTKCiv;
11using ALTKPromotions;
12using ALTKDiff;
13using ALTKAIBehaviour;
14using BaseRules;                     // Avaruuslaatikon sääntötiedosto, helpottaa modien tekemistä
15
16namespace ALTKTileEngine
17{
18    public class Peli : Game
19    {
20        bool gamestarted = false;
21        int currDiffLevel; // jotta tätä voidaan käyttää missä tahansa
22
23        int[,] TerrainMap; // perusmaastokartta
24        int[,] DTerrainMap; // erikoismaastokartta
25        int[,] OwnershipMap; // ruudukon omistuskartta (kenen pelaajan hallinnassa on mikäkin ruutu)
26
27        string Textpath;
28        string OwnershipColorPath;
29
30        int AmountofPlayers;
31       
32        // !!!!!!!!!!!!!!!!!!!!!!!!!!!
33        // Pitänee tehdä vielä yksi ALTKSUnit-namespace ALTKCoreen.
34        // Tämä sisältäisi tiedot yksiköiden omistajasta, yksikön perusyksiköstä ym. sekä myös karttakoordinaatit
35        // (karttakoordinaatit säilyttää myös UnitMap, josta on helppo tarkastaa esim. liikuttaessa yksiköitä, onko ruudussa jo esim. vihollisyksiköitä.
36       
37        int kartanLeveys;
38        int kartanKorkeus;
39
40        // perusmaastotyypit
41        const int Aavikko = 2, Tundra = 3, Vuoristo = 1, Lehtimetsa = 0;
42
43        // erikoismaastotyypit
44        const int Havumetsa = 2, Tyhja = 0, Kuusimetsa = 3;
45
46        // Pelissä on kaksi eri tasoa, toisella perusmaasto ja toisella tarkempaa tietoa (esim. onko ruutu metsää ym.)
47        GameObject[,] tile;
48        GameObject[,] DTile;
49        GameObject[,] TileOwnership;
50        //Vector tileVector;
51
52        Vector CamSpeedEast = new Vector(20.0, .0);
53        Vector CamSpeedWest = new Vector(-20.0, .0);
54        Vector CamSpeedNorth = new Vector(.0, 20.0);
55        Vector CamSpeedSouth = new Vector(.0, -20.0);
56
57        // Käytän ennemmin enemmän muistia kuin prosessoria, joten lasken nämä jo täällä
58        int MouseScreenWidth;
59        int MouseScreenHeight;
60        int NegMouseScreenWidth;
61        int NegMouseScreenHeight;
62        protected override void Begin()
63        {
64            //SetWindowSize(true);
65            Textpath = Application.StartupPath + "\\Content\\Textures\\";
66            OwnershipColorPath = Textpath + "Ownership\\";
67            InitRules();
68            InitStartMenu();
69            //InitControls();
70            //InitMap();
71        }
72        void InitStartMenu()
73        {
74            Mouse.Enabled = true;
75            Mouse.IsCursorVisible = true;
76
77            GameObject TrainingButton = new GameObject(250, 30, Shapes.Rectangle);
78            GameObject EasyButton = new GameObject(250, 30, Shapes.Rectangle);
79            GameObject NormalButton = new GameObject(250, 30, Shapes.Rectangle);
80            GameObject HardButton = new GameObject(250, 30, Shapes.Rectangle);
81            GameObject VHardButton = new GameObject(250, 30, Shapes.Rectangle);
82            GameObject NightmareButton = new GameObject(250, 30, Shapes.Rectangle);
83            GameObject InsanityButton = new GameObject(250, 30, Shapes.Rectangle);
84
85            for (int a = 0; a <= 6; a++)
86            {
87                if (a == 0) { SetDiffButton(TrainingButton, 0); }
88                if (a == 1) { SetDiffButton(EasyButton, 1); }
89                if (a == 2) { SetDiffButton(NormalButton, 2); }
90                if (a == 3) { SetDiffButton(HardButton, 3); }
91                if (a == 4) { SetDiffButton(VHardButton, 4); }
92                if (a == 5) { SetDiffButton(NightmareButton, 5); }
93                if (a == 6) { SetDiffButton(InsanityButton, 6); }
94            }
95            Mouse.ListenOn(TrainingButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 0);
96            Mouse.ListenOn(EasyButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 1);
97            Mouse.ListenOn(NormalButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 2);
98            Mouse.ListenOn(HardButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 3);
99            Mouse.ListenOn(VHardButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 4);
100            Mouse.ListenOn(NightmareButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 5);
101            Mouse.ListenOn(InsanityButton, MouseButton.Left, Jypeli.ButtonState.Down, SelectDifficulty, "", 6);
102        }
103
104        void SetDiffButton(GameObject diffbutton, int difflevel)
105        {
106            diffbutton.X = -250;
107            diffbutton.Y = ((difflevel * -50) + 400);
108            if (difflevel == 0) { diffbutton.Image = LoadImage(Textpath + "training"); }
109            else if (difflevel == 1) { diffbutton.Image = LoadImage(Textpath + "easy"); }
110            else if (difflevel == 2) { diffbutton.Image = LoadImage(Textpath + "normal"); }
111            else if (difflevel == 3) { diffbutton.Image = LoadImage(Textpath + "hard"); }
112            else if (difflevel == 4) { diffbutton.Image = LoadImage(Textpath + "vhard"); }
113            else if (difflevel == 5) { diffbutton.Image = LoadImage(Textpath + "nightmare"); }
114            else if (difflevel == 6) { diffbutton.Image = LoadImage(Textpath + "insanity"); }
115            Add(diffbutton);
116            //return diffbutton;
117        }
118
119        void SelectDifficulty(int difflevel)
120        {
121            ClearAll();
122            currDiffLevel = new int();
123            currDiffLevel = difflevel;
124            InitControls();
125            InitMap();
126            gamestarted = true;
127        }
128       
129        # region Kartan luominen
130
131        void InitMap()
132        {
133            AmountofPlayers = new int();
134            Int32 i;
135            Int32 j;
136            string mLine;
137            TextReader bReader;
138            bReader = new StreamReader("Content\\Maps\\1\\base.txt");
139            //try
140            //{
141
142            // Yleensä kartan kaksi ensimmäistä riviä ovat kommentteja
143            bReader.ReadLine();
144            bReader.ReadLine();
145
146            kartanLeveys = Convert.ToInt32(bReader.ReadLine().Substring(9, 2));
147            kartanKorkeus = Convert.ToInt32(bReader.ReadLine().Substring(10, 2));
148
149            Int32 mapWidthJ = (kartanLeveys * 48) / 2;
150            Int32 mapHeightJ = (kartanKorkeus * 48) / 2;
151
152            bReader.ReadLine();
153            // Ladataan TerrainMapit muistiin myöhempää käyttöä varten
154
155            TerrainMap = new int[kartanKorkeus, kartanLeveys];
156            DTerrainMap = new int[kartanKorkeus, kartanLeveys];
157            OwnershipMap = new int[kartanKorkeus, kartanLeveys];
158            InitTerrain(0);
159            InitTerrain(1);
160            InitTerrain(2);
161
162            tile = new GameObject[kartanKorkeus, kartanLeveys];
163            DTile = new GameObject[kartanKorkeus, kartanLeveys];
164            TileOwnership = new GameObject[kartanKorkeus, kartanLeveys];
165            InitTerrainGraphics(0);
166            InitTerrainGraphics(1);
167            InitTerrainGraphics(2);
168
169            // Koodi käännetty RGE:n VB.netistä
170            // Asettaa ja piirtää pohjamaaston
171
172            for (i = 0; i <= (kartanKorkeus - 1); i++)
173            {
174                mLine = bReader.ReadLine();
175                for (j = 0; j <= (kartanLeveys - 1); j++)
176                {
177                    // MessageBox.Show(mLine + " " + j + " " + i);
178                    tile[i, j].Shape = Shapes.Rectangle;
179                    tile[i, j].X = (j * 48) - mapWidthJ;
180                    tile[i, j].Y = (i * 48) - mapHeightJ;
181                    TerrainMap[i, j] = getTerrainType(mLine.Substring(j, 1), 0, i, j);
182                    tile[i, j].Image = LoadImage(getTexture_fromTerrainType(TerrainMap[i, j], 0, i, j));
183                    Add(tile[i, j]);
184                    // MessageBox.Show(Convert.ToString(TerrainMap[i, j]));
185                }
186            }
187
188            // Luetaan karttatiedoston rivivälit
189            bReader.ReadLine();
190            bReader.ReadLine();
191
192            // Rami 16. 6. 2010: Omistuskartta pitää sittenkin säätää ennen erikoismaastoa, sillä
193            // kaupungit ja omistusrinki piirretään erikoismaaston asettamiskoodin kutsusta
194            for (i = 0; i <= (kartanKorkeus - 1); i++)
195            {
196                for (j = 0; j <= (kartanLeveys - 1); j++)
197                {
198                    // MessageBox.Show(mLine + " " + j + " " + i);
199                    TileOwnership[i, j].Shape = Shapes.Rectangle;
200                    TileOwnership[i, j].X = (j * 48) - mapWidthJ;
201                    TileOwnership[i, j].Y = (i * 48) - mapHeightJ;
202                    //TileOwnership[i, j].Image = LoadImage(Textpath + "nothing"); poistettu optimisoinnin takia
203                    //Add(TileOwnership[i, j]); eihän näitä tarvitse vielä lisätä, optimisointia
204                    //MessageBox.Show(TileOwnership[i, j].X + "x" + TileOwnership[i, j].Y + " " + DTile[i, j].X + "x" + DTile[i, j].Y + " Params: " + i + " " + j);
205                    // MessageBox.Show(Convert.ToString(TerrainMap[i, j]));
206                }
207            }
208
209            // Asettaa ja piirtää erikoismaaston
210
211            for (i = 0; i <= (kartanKorkeus - 1); i++)
212            {
213                mLine = bReader.ReadLine();
214                for (j = 0; j <= (kartanLeveys - 1); j++)
215                {
216                    // MessageBox.Show(mLine + " " + j + " " + i);
217                    DTile[i, j].Shape = Shapes.Rectangle;
218                    DTile[i, j].X = (j * 48) - mapWidthJ;
219                    DTile[i, j].Y = (i * 48) - mapHeightJ;
220                    DTerrainMap[i, j] = getTerrainType(mLine.Substring(j, 1), 1, i, j);
221                    DTile[i, j].Image = LoadImage(getTexture_fromTerrainType(DTerrainMap[i, j], 1, i, j));
222                    Add(DTile[i, j]);
223                    if (getTexture_fromTerrainType(DTerrainMap[i, j], 1, i, j).Contains("city"))
224                    {
225                        DrawOwnership(i, j);
226                    }
227                    //MessageBox.Show(DTile[i, j].X + "x" + DTile[i, j].Y);
228                    // MessageBox.Show(Convert.ToString(TerrainMap[i, j]));
229                }
230            }
231            //catch
232            //{
233            //    MessageBox.Show("Map error!");
234            //    bReader.Close();
235            //    Exit();
236            //}
237            bReader.Close();
238            //for (int playernumber = 0; playernumber <= 30; playernumber++)
239            //{
240            //    Rules.pCivs[playernumber].Color = setAIOwnershipColor(playernumber);
241            //}
242            Rules.pCivs[0].HumanPlayer = true;
243            Rules.pCivs[0].Color = "red";
244            Level.BackgroundColor = Color.Black;
245        }
246
247        private void InitTerrain(int tlayer)
248        {
249            for (int a = 0; a <= (kartanKorkeus - 1); a++)
250            {
251                for (int b = 0; b <= (kartanLeveys - 1); b++)
252                {
253                    GetTerrainLayer(tlayer)[a, b] = new int();
254                }
255            }
256        }
257
258        int[,] GetTerrainLayer(int tlayer)
259        {
260            if (tlayer == 0) { return TerrainMap; }
261            if (tlayer == 1) { return DTerrainMap; }
262            if (tlayer == 2) { return OwnershipMap; }
263            return TerrainMap;
264        }
265
266        private void InitTerrainGraphics(int tlayer)
267        {
268            for (int a = 0; a <= (kartanKorkeus - 1); a++)
269            {
270                for (int b = 0; b <= (kartanLeveys - 1); b++)
271                {
272                    GetGraphicsLayer(tlayer)[a, b] = new PhysicsObject(48.0, 48.0);
273                }
274            }
275        }
276
277        GameObject[,] GetGraphicsLayer(int tlayer)
278        {
279            if (tlayer == 0) {return tile; }
280            if (tlayer == 1) {return DTile; }
281            if (tlayer == 2) { return TileOwnership; }
282            return tile;
283        }
284
285        int getTerrainType(string ch, int terrainlayer, int i, int j)
286        {
287            if (terrainlayer == 0)
288            {
289                /*/ Maastotyypit
290                 * 0 = lehtimetsää
291                 * 1 = vuoristoa
292                 * 2 = aavikkoa
293                 * 3 = tundraa
294                /*/
295                if (ch == " ") { return Lehtimetsa; }
296                if (ch == "V") { return Vuoristo; }
297                if (ch == "A") { return Aavikko; }
298                if (ch == "T") { return Tundra; }
299            }
300            else if (terrainlayer == 1)
301            {
302                /*/ Erikoismaastotyypit
303                 * 0 = ei mitään
304                 * 1 = vuoristoa
305                 * 2 = metsää
306                /*/
307                if (ch == " ") { return Tyhja; }
308                if (ch == "M") { return Havumetsa; }
309                if (ch == "V") { return Vuoristo; }
310                if (ch == "K") { return Kuusimetsa; }
311                try
312                {
313                    //MessageBox.Show(TileOwnership[i, j].X + "x" + TileOwnership[i, j].Y + " " + DTile[i, j].X + "x" + DTile[i, j].Y + " Params: " + i + " " + j);
314                    int Playerstartloc;
315                    Playerstartloc = Convert.ToInt32(ch);
316                    AmountofPlayers = AmountofPlayers + 1;
317                    AddStartingCity(Playerstartloc, i, j);
318                    return (Playerstartloc + 10);
319                }
320                catch
321                {
322                }
323                return Tyhja;
324            }
325            return 0;
326        }
327
328        private void DrawOwnership(int i, int j)
329        {
330            TileOwnership[i, j].Image = LoadImage(OwnershipColorPath + Rules.pCivs[OwnershipMap[i, j]].Color);
331            Add(TileOwnership[i, j]);
332            //MessageBox.Show(TileOwnership[i, j].X + "x" + TileOwnership[i, j].Y + " " + DTile[i, j].X + "x" + DTile[i, j].Y + " Params: " + i + " " + j);
333        }
334
335        private void AddStartingCity(int playernumber, int i, int j)
336        {
337            //MessageBox.Show(TileOwnership[i, j].X + "x" + TileOwnership[i, j].Y + " " + DTile[i, j].X + "x" + DTile[i, j].Y + " Params: " + i + " " + j);
338            Rules.pCivs[playernumber].Production = Rules.pCivs[playernumber].Production + 10;
339            Rules.pCivs[playernumber].NumberofCities = Rules.pCivs[playernumber].NumberofCities + 1;
340            Rules.pCivs[playernumber].CityCoords[Rules.pCivs[playernumber].NumberofCities, 0] = i;
341            Rules.pCivs[playernumber].CityCoords[Rules.pCivs[playernumber].NumberofCities, 1] = j;
342            if (Rules.pCivs[playernumber].HumanPlayer == false)
343            {
344            Rules.pCivs[playernumber].Color = setAIOwnershipColor(playernumber);
345            Rules.pCivs[playernumber].Enabled = true;
346            }
347            OwnershipMap[i, j] = playernumber;
348            // 16. 6. 2010: Kutsutaanpa DrawOwnership sittenkin vasta kaupungin piirtämisen jälkeen (ks. InitMap ja DTilen asettaminen)
349            //DrawOwnership(i, j);
350
351            //DTile[i, j].Image = LoadImage("\\Content\\Textures\\cityancient");
352        }
353
354        public string setAIOwnershipColor(int playernumber)
355        {
356            int c;
357            int b;
358            bool ColorInUse = false;
359            for (b = 0; b <= 20; b++)
360            {
361                ColorInUse = false;
362                for (c = 0; c <= 30; c++)
363                {
364                    if (Rules.pCivs[c].Color == getOwnershipColor_fromInt(b))
365                    {
366                        ColorInUse = true;
367                    }
368                }
369                if (ColorInUse == false)
370                {
371                    return getOwnershipColor_fromInt(b);
372                }
373            }
374            return getOwnershipColor_fromInt(0);
375        }
376
377        public string getOwnershipColor_fromInt(Int32 color)
378        {
379            if (color == 0) { return "aqua"; }
380            if (color == 1) { return "beige"; }
381            if (color == 2) { return "limegreen"; }
382            if (color == 3) { return "orange"; }
383            if (color == 4) { return "black"; }
384            if (color == 5) { return "red"; }
385            if (color == 6) { return "rustred"; }
386            if (color == 7) { return "yellow"; }
387            if (color == 8) { return "blue"; }
388            if (color == 9) { return "darkblue"; }
389            if (color == 10) { return "darkgreen"; }
390            if (color == 11) { return "lightblue"; }
391            if (color == 12) { return "purple"; }
392            if (color == 13) { return "darkpurple"; }
393            if (color == 14) { return "lightpurple"; }
394            if (color == 15) { return "gold"; }
395            if (color == 16) { return "silver"; }
396            if (color == 17) { return "pink"; }
397            if (color == 18) { return "teal"; }
398            if (color == 19) { return "greenyellow"; }
399            if (color == 20) { return "darkorange"; }
400            if (color == 21) { return "white"; }
401            if (color == 22) { return "darkgray"; }
402            return "aqua";
403        }
404
405        string getTexture_fromTerrainType(int terraintype, int terrainlayer, int i, int j)
406        {
407            if (terrainlayer == 0)
408            {
409                if (terraintype == Lehtimetsa) { return Application.StartupPath + "\\Content\\Textures\\grassland"; }
410                if (terraintype == Vuoristo) { return Application.StartupPath + "\\Content\\Textures\\mountain"; }
411                if (terraintype == Aavikko) { return Application.StartupPath + "\\Content\\Textures\\desert"; }
412                if (terraintype == Tundra) { return Application.StartupPath + "\\Content\\Textures\\tundra"; }
413            }
414            else if (terrainlayer == 1)
415            {
416                if (terraintype == Tyhja) { return Application.StartupPath + "\\Content\\Textures\\nothing"; }
417                if (terraintype == Vuoristo) { return Application.StartupPath + "\\Content\\Textures\\mountain"; }
418                if (terraintype == Havumetsa) { return Application.StartupPath + "\\Content\\Textures\\forest"; }
419                if (terraintype == Kuusimetsa) { return Application.StartupPath + "\\Content\\Textures\\spruceforest"; }
420                // Jos terraintype on 10 tai enemmän, ruudussa on pelaajan kaupunki (ks. getTerrainType)
421                if (terraintype >= 10)
422                {
423                    if (TerrainMap[i, j] == Tundra)
424                    {
425                        return Application.StartupPath + "\\Content\\Textures\\cityancientTundra";
426                        //DrawOwnership(i, j);
427                    }
428                    else
429                    {
430                        return Application.StartupPath + "\\Content\\Textures\\cityancient";
431                    }
432                }
433                return Application.StartupPath + "\\Content\\Textures\\nothing";
434            }
435            return Application.StartupPath + "\\Content\\Textures\\grassland.jpg";
436        }
437        # endregion
438
439        private void InitRules()
440        {
441            // Käytetään valmiiksi tehtyjä DLL-tiedostoja
442            BaseRules.Rules.InitTechs();
443            BaseRules.Rules.InitUnits();
444            BaseRules.Rules.InitDifficulty();
445            BaseRules.Rules.InitCivs();
446        }
447
448        protected override void Update(Time time)
449        {
450            base.Update(time);
451            // Mouse.PositionOnScreen voisi olla vain positiivinen, eikä ruudun pitäisi jakautua keskeltä kahtia
452            // negatiivisiin ja positiivisiin kokonaislukuihin
453            if (Mouse.PositionOnScreen.X > MouseScreenWidth) { MoveCamera(0); }
454            else if (Mouse.PositionOnScreen.X < NegMouseScreenWidth) { MoveCamera(2); }
455            else if (Mouse.PositionOnScreen.Y > MouseScreenHeight) { MoveCamera(1); }
456            else if (Mouse.PositionOnScreen.Y < NegMouseScreenHeight) { MoveCamera(3); }
457        }
458
459        private void InitControls()
460        {
461            Keyboard.Listen(Key.Right, Jypeli.ButtonState.Down, MoveCamera, null, 0);
462            Keyboard.Listen(Key.Left, Jypeli.ButtonState.Down, MoveCamera, null, 2);
463            Keyboard.Listen(Key.Up, Jypeli.ButtonState.Down, MoveCamera, null, 1);
464            Keyboard.Listen(Key.Down, Jypeli.ButtonState.Down, MoveCamera, null, 3);
465            Keyboard.Listen(Key.Tab, Jypeli.ButtonState.Down, ActivateDebug, null, 0);
466
467            MouseScreenWidth = new int();
468            MouseScreenWidth = Convert.ToInt32(Peli.Screen.Width / 2) - 5;
469            MouseScreenHeight = new int();
470            MouseScreenHeight = Convert.ToInt32(Peli.Screen.Height / 2) - 5;
471            NegMouseScreenWidth = new int();
472            NegMouseScreenWidth = Convert.ToInt32(Peli.Screen.Width / -2) + 5;
473            NegMouseScreenHeight = new int();
474            NegMouseScreenHeight = Convert.ToInt32(Peli.Screen.Height / -2) + 20;
475        }
476
477        private void ActivateDebug(int asd)
478        {
479            //Add(MessageDisplay);
480            MessageDisplay.Add("DEBUG MODE ACTIVATED");
481            Keyboard.Listen(Key.A, Jypeli.ButtonState.Down, ShowColors, null);
482        }
483
484        private void ShowColors()
485        {
486            for (int a = 0; a <= (AmountofPlayers - 1); a++)
487            {
488                MessageDisplay.Add("Pelaaja: " + a + "; " + Rules.pCivs[a].Color);
489            }
490        }
491
492        private void Mouse_Move()
493        {
494            // MessageBox.Show(Mouse.PositionOnScreen.X);
495        }
496
497        private void MoveCamera(Int32 dir)
498        {
499            if (gamestarted == false) { return; }
500            if ((dir == 0)  && (Camera.Position.X < (tile[kartanKorkeus - 1, kartanLeveys - 1].X) - (Peli.Screen.Width / 2)))
501            {
502                Camera.Move(CamSpeedEast);
503            }
504            if ((dir == 1) && (Camera.Position.Y < (tile[kartanKorkeus - 1, kartanLeveys - 1].Y) - ((Peli.Screen.Height / 2) - 12)))
505            {
506                Camera.Move(CamSpeedNorth);
507            }
508            if ((dir == 2) && (Camera.Position.X > (tile[0, 0].X) + (Peli.Screen.Width / 2)))
509            {
510                Camera.Move(CamSpeedWest);
511            }
512            if ((dir == 3) && (Camera.Position.Y > (tile[0, 0].Y) + (Peli.Screen.Height / 2)))
513            {
514                Camera.Move(CamSpeedSouth);
515            }
516        }
517    }
518}
Note: See TracBrowser for help on using the repository browser.