Ejemplo n.º 1
0
    public void LoadLevel()
    {
        Hashtable  file = null;                                       //Get an empty hashtable
        FileStream fs   = new FileStream("level.dat", FileMode.Open); //Open a filestream

        try
        {
            BinaryFormatter formatter = new BinaryFormatter(); //Make a new binary formatter
            file = (Hashtable)formatter.Deserialize(fs);       //Deserialize the hash table
        }
        catch (Exception e)
        {
            Console.WriteLine("Deserialization failed: " + e.Message);
        }

        List <string> list = new List <string>(); //Get a new list of strings

        foreach (DictionaryEntry obj in file)
        {
            list.Add(obj.Key.ToString() + "." + obj.Value.ToString()); //Put the deserialized file in the list
        }

        list.Sort();                                               //Sort the list (very important!)
        int             z     = 0;                                 //Set a counter for the list line
        List <GridNode> nodes = GameWorld.FindByType <GridNode>(); //Get a list of all the gridnodes

        while (z < list.Count - 1)
        {
            /*0*/ string typeString = getHash(list[z++], 2);              //Get the type string
            /*1*/ float  x          = float.Parse(getHash(list[z++], 2)); //Get the x
            /*2*/ float  y          = float.Parse(getHash(list[z++], 2)); //Get the y

            switch (typeString)
            {
            case "GridNode":                                                               //If the type was a GridNode
                /*3*/ int          tex   = int.Parse(getHash(list[z++], 2));               //Add the texture
                /*4*/ Camera.Plane plane = (Camera.Plane) int.Parse(getHash(list[z++], 2));
                for (int i = 0; i < nodes.Count; ++i)                                      //Find the corresponding node in the grid
                {
                    if (nodes[i].Position == new Vector2(x, y) && nodes[i].plane == plane) //If it matches the position AND plane
                    {
                        nodes[i].texture = tex;                                            //Set the texture from loadfile
                    }
                }
                break;
            }
        }
        fs.Close(); //Close the filestream
    }
Ejemplo n.º 2
0
 public GridNode(Camera.Plane plane, Vector2 position, int texture) : base()
 {
     offset             = Vector2.Zero;
     neighbours         = new List <GridNode>();
     extendedNeighbours = new List <GridNode>();
     this.texture       = texture;
     this.position      = position;
     this.plane         = plane;
     Hval       = 0;
     Gval       = 0;
     Fval       = 0;
     Dval       = 0;
     pathParent = this;
     beacon     = false;
     congestion = 0;
     available  = true;
 }
Ejemplo n.º 3
0
    public GridPlane(Camera.Plane planeType) : base()
    {
        spawnGrid      = new List <GridNode>();
        this.planeType = planeType;
        //Initialize the grid with the size of the level
        grid = new GridNode[LEVEL_SIZE.X, LEVEL_SIZE.Y];
        //Fill the grid with GridItems
        for (int y = 0; y < LEVEL_SIZE.Y; ++y)
        {
            for (int x = 0; x < LEVEL_SIZE.X; ++x) //Make sure the grid gets build up from the bottom
            {
                if (y % 2 == 0)                    //All odd rows will be shifted half a node in order to lock together
                {
                    grid[x, y] = new GridNode(planeType, new Vector2(NODE_SIZE.X * x, NODE_SIZE.Y / 2 * y), 0);
                }
                else
                {
                    grid[x, y] = new GridNode(planeType, new Vector2(NODE_SIZE.X * x + NODE_SIZE.X / 2, NODE_SIZE.Y / 2 * y), 0);
                }
                Add(grid[x, y]);
            }
        }
        //SET THE NEIGHBOURS
        for (int x = 0; x < LEVEL_SIZE.X; ++x)
        {
            for (int y = 0; y < LEVEL_SIZE.Y; ++y)
            {
                if (y % 2 == 0) //Even rows (4 cases)
                {
                    if (x > 0 && y > 0)
                    {
                        grid[x, y].neighbours.Add(grid[x - 1, y - 1]);
                    }                                                                      //TopLeft
                    if (y > 0)
                    {
                        grid[x, y].neighbours.Add(grid[x, y - 1]);
                    }                                                         //TopRight
                    if (y < LEVEL_SIZE.Y - 1)
                    {
                        grid[x, y].neighbours.Add(grid[x, y + 1]);
                    }                                                                       //BottomRight
                    if (x > 0 && y < LEVEL_SIZE.Y - 1)
                    {
                        grid[x, y].neighbours.Add(grid[x - 1, y + 1]);
                    }                                                                                     //BottomLeft
                }
                else //Odd rows (4 cases)
                {
                    if (y > 0)
                    {
                        grid[x, y].neighbours.Add(grid[x, y - 1]);
                    }                                                         //TopLeft
                    if (x < LEVEL_SIZE.X - 1 && y > 0)
                    {
                        grid[x, y].neighbours.Add(grid[x + 1, y - 1]);
                    }                                                                                     //TopRight
                    if (x < LEVEL_SIZE.X - 1 && y < LEVEL_SIZE.Y - 1)
                    {
                        grid[x, y].neighbours.Add(grid[x + 1, y + 1]);
                    }                                                                                                    //BottomRight
                    if (y < LEVEL_SIZE.Y - 1)
                    {
                        grid[x, y].neighbours.Add(grid[x, y + 1]);
                    }                                                                        //BottomLeft
                }
            }
        }

        for (int x = 0; x < LEVEL_SIZE.X; ++x) //set the extended neighbours
        {
            for (int y = 0; y < LEVEL_SIZE.Y; ++y)
            {
                if (x > 0)
                {
                    grid[x, y].extendedNeighbours.Add(grid[x - 1, y]);
                }
                if (x < LEVEL_SIZE.X - 1)
                {
                    grid[x, y].extendedNeighbours.Add(grid[x + 1, y]);
                }
                if (y > 1)
                {
                    grid[x, y].extendedNeighbours.Add(grid[x, y - 2]);
                }
                if (y < LEVEL_SIZE.Y - 2)
                {
                    grid[x, y].extendedNeighbours.Add(grid[x, y + 2]);
                }
            }
        }
        //Calculate the Heuristic for the pathfinding algorithm
        foreach (GridNode node in grid)
        {
            node.Hval       = (int)(Math.Abs(node.Position.X - LEVEL_CENTER.X) + Math.Abs(node.Position.Y - LEVEL_CENTER.Y)) / 64; //Calculate the Heuristic
            node.pathParent = node;                                                                                                //Reset the parent
        }

        //ADD LEVEL OBJECTS IN THE CAMERA CLASS.
        //ADDING THEM HERE WILL ADD THEM TO EVERY PLANE AL THE SAME
        Add(particleControl = new ParticleController());
        WishnusArmy.WishnusArmy.startSorting = true;
        SortingThread.AddRequest(this);
    }