Example #1
0
        // ---------------------------------------------------- //
        // generate game world based on the path to a text file //

        private void generate_game_world(string _data_file_path)
        {
            const int  CELL_SIZE = 64;
            const char CELL_WALL = 'W';
            const char CELL_TANK = 'T';

            var physics_nodes = new List <PhysicsNode>();

            try
            {
                StreamReader file = new StreamReader(_data_file_path);
                string       line;

                int world_width;

                line = file.ReadLine();
                if (line != null)
                {
                    world_width = line.Length;                      //The width of the world is the length of the first line
                }
                else
                {
                    Console.WriteLine("Error: the first line of the file is empty");
                    return;
                }

                int current_y = 0;
                while (line != null)
                {
                    for (int current_x = 0; current_x < line.Length; ++current_x)
                    {
                        char    current_cell = line[current_x];
                        Vector2 current_pos  = new Vector2(current_x * CELL_SIZE + CELL_SIZE / 2, current_y * CELL_SIZE + CELL_SIZE / 2);

                        if (current_cell == CELL_WALL)
                        {
                            PhysicsNode node = new PhysicsNode(root);
                            node.set_texture(Graphics.texture_wall);
                            node.set_position(current_pos);

                            //Add to collision manager
                            physics_nodes.Add(node);
                        }
                        if (current_cell == CELL_TANK)
                        {
                            var tank = new Tank(root);
                            tank.set_position(current_pos);
                            Console.WriteLine("\n\nTank was created at: \n" + current_pos);
                            //Add to collision manager
                            physics_nodes.Add(tank);
                        }
                    }

                    ++current_y;
                    line = file.ReadLine();
                }

                //add nodes to collision_manager
                foreach (PhysicsNode node in physics_nodes)
                {
                    collision_manager.add_node(node);
                }

                file.Close();
                file.Dispose();
            } catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }