using System; using System.IO; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace blue_dish_te { internal class Svet { public int width = 0; public int height = 0; public int startX = 0; public int startY = 0; public char[][] map; private Matrix[,] mapMatrix; private Device device; //ktere steny se budou u 3D kosticky kreslit private int[,] wallDirection; //rendery private Stena stena = null; private Strop strop = null; private Podlaha podlaha = null; public Svet(Device device) { ReInitialize(device); } public void ReInitialize(Device dev) { //vytvoreni objektu po resetu Device stena = new Stena(dev); strop = new Strop(dev); podlaha = new Podlaha(dev); device = dev; } public bool LoadMap(string filename) { try { String sTmp; String[] asTmp; //otevreme soubor StreamReader reader = File.OpenText(filename); //nacteme prvni radku sTmp = reader.ReadLine(); //rozparsneme na velikosti mapy asTmp = sTmp.Split('x'); width = int.Parse(asTmp[0]); height = int.Parse(asTmp[1]); //vytvorime pole map = new char[height][]; mapMatrix = new Matrix[height,width]; wallDirection = new int[height,width]; //nacteme mapu ze souboru for (int line = 0; line < height; line++) { map[line] = reader.ReadLine().ToCharArray(); } //uzavreme soubor reader.Close(); // Nyni projedeme celou mapu a porovedeme: // * hledani pocatku hrace // * urceni, ktere steny u 3D kosticky se budou kreslit // * nastaveni matice sveta na mape for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) { //urceni, ktere steny u 3D kosticky se budou kreslit if (map[y][x] == '*' || (map[y][x] >= 'a' && map[y][x] <= 'n')) { if (x > 0) if (map[y][x - 1] != '*' && (map[y][x - 1] < 'a' || map[y][x - 1] > 'n')) wallDirection[y, x] |= 1; if (x < width - 1) if (map[y][x + 1] != '*' && (map[y][x + 1] < 'a' || map[y][x + 1] > 'n')) wallDirection[y, x] |= 4; if (y > 0) if (map[y - 1][x] != '*' && (map[y - 1][x] < 'a' || map[y - 1][x] > 'n')) wallDirection[y, x] |= 2; if (y < height - 1) if (map[y + 1][x] != '*' && (map[y + 1][x] < 'a' || map[y + 1][x] > 'n')) wallDirection[y, x] |= 8; } //hledani pocatku hrace if (map[y][x] == 'S') { startX = y; startY = x; } //nastaveni matice sveta na mape mapMatrix[y, x] = Matrix.Identity * Matrix.Translation(y * 2f, 0f, x * 2f); } return true; } catch(IOException e) { MessageBox.Show("Nepovedlo se nacist mapu."); Console.WriteLine(e); return false; } } public void Render() { //renderujeme mapu for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { //pokud se jedna o zed device.Transform.World = mapMatrix[y, x]; if (map[y][x] == '*' || (map[y][x] >= 'a' && map[y][x] <= 'n')) { //typ steny stena.type = map[y][x]; stena.wallDirection = wallDirection[y, x]; stena.Render(); } // pokud to neni stena renderujeme strop a podlahu if (map[y][x] != '*' && (map[y][x] < 'a' || map[y][x] > 'n')) { //typ stropu strop.type = map[y][x]; strop.Render(); //typ podlahy podlaha.type = map[y][x]; podlaha.Render(); } } } } } }