Example #1
0
    private void Start()
    {
        panel    = GameObject.Find("Main/Canvas/Game-Over Menu").GetComponent <RectTransform>();
        GameOver = false;
        x        = GetComponent <loadlevel>();
        //once = true;
        no_of_objs = 0;
        objs       = GameObject.FindGameObjectsWithTag("EnemyA");

        foreach (GameObject obj in objs)
        {
            no_of_objs++;
        }
        //Debug.Log(no_of_objs);
    }
Example #2
0
    //When the object awakens, we assign the static variable if its a new instance and
    void Awake()
    {
        //destroy the already existing instance, if any
        if (instance)
        {
            Destroy(gameObject);
            hide();                                                             //call hide function to hide the 'loading texture'
            return;
        }

        instance = this;
        gameObject.AddComponent <GUITexture>().enabled = false;                 //disable the texture on start of the scene
        GetComponent <GUITexture>().texture            = texture;               //assign the texture
        transform.localScale += new Vector3(-0.1F, -0.8f, 0);
        transform.position    = new Vector3(0.45f, 0.18f, 1f);                  //position the texture to the center of the screen
        DontDestroyOnLoad(this);                                                //make this object persistent between scenes
    }
    //FILE SHIT

    //REAL FILE SHIT

    void LoadLevel(string fileName)
    {
        Debug.Log("Loading...");
        //creates blank file if level not found
        if (!File.Exists(Application.persistentDataPath + "/" + fileName))
        {
            Debug.Log("Level was blank, creating new level");
            byte[] blankLevel = new byte[fileSize];
            for (int i = 0; i < LEVEL_SIZE * LEVEL_SIZE * TILE_LAYERS; i++)
            {
                blankLevel[SETTINGS_BLOCK_SIZE + (i * TILE_SIZE) + 0] = 0;
                blankLevel[SETTINGS_BLOCK_SIZE + (i * TILE_SIZE) + 1] = 0;
                blankLevel[SETTINGS_BLOCK_SIZE + (i * TILE_SIZE) + 2] = 31;
            }
            File.WriteAllBytes(Application.persistentDataPath + "/" + fileName, blankLevel);
            Debug.Log("New level created.");
        }
        //ok now we know it exists, so...
        workingLevel = File.ReadAllBytes(Application.persistentDataPath + "/" + fileName);
        Debug.Log("Level file found. Loading level file...");
        //actual loading
        Debug.Log("Loading settings.");
        byte ub1 = workingLevel[0];
        byte ub2 = workingLevel[1];
        byte db1 = workingLevel[2];
        byte db2 = workingLevel[3];
        byte lb1 = workingLevel[4];
        byte lb2 = workingLevel[5];
        byte rb1 = workingLevel[6];
        byte rb2 = workingLevel[7];

        workingSettings.up = ub1 << 8 | ub2;
        Debug.Log(workingSettings.up);
        workingSettings.down = db1 << 8 | db2;
        Debug.Log(workingSettings.down);
        workingSettings.left = lb1 << 8 | lb2;
        Debug.Log(workingSettings.left);
        workingSettings.right = rb1 << 8 | rb2;
        Debug.Log(workingSettings.right);
        Debug.Log("Loading tiles.");
        for (int l = 0; l < TILE_LAYERS; l++)
        {
            Debug.Log("Loading layer " + l);
            for (int y = 0; y < LEVEL_SIZE; y++)
            {
                for (int x = 0; x < LEVEL_SIZE; x++)
                {
                    //getting the bytes of the tile
                    int  tilepos = SETTINGS_BLOCK_SIZE + ((y * LEVEL_SIZE + x) * TILE_SIZE) + (LEVEL_SIZE * LEVEL_SIZE * TILE_SIZE * l);
                    byte setb1   = workingLevel[tilepos + 0];
                    byte setb2   = workingLevel[tilepos + 1];
                    byte subb1   = workingLevel[tilepos + 2];
                    if (subb1 != 31)
                    {
                        Debug.Log("Loaded a tile at " + l + "," + x + "," + y + " (" + tilepos + ")");
                    }
                    //if (x == 0) { Debug.Log(subb1); }
                    int setcombined = setb1 << 8 | setb2;
                    if (subb1 != 31)
                    {
                        GameObject n = new GameObject();
                        n.name = "" + l + "_" + x + "_" + y;
                        SpriteRenderer sr = n.AddComponent <SpriteRenderer>();
                        //Debug.Log ((int)workingLevel[SETTINGS_BLOCK_SIZE + (i*TILE_SIZE)]);
                        sr.sprite                 = SpriteArray[(int)subb1];
                        n.transform.parent        = RendHolder;
                        n.transform.localPosition = new Vector3(x, y, l);
                        if (l == 1)
                        {
                            n.AddComponent <BoxCollider2D>();
                        }
                    }
                }
            }
        }
        Debug.Log("Level loaded.");
        Debug.Log("Checking and spawning adjacent levels... ");
        if (workingSettings.up != 0)
        {
            string        fileNameFragment = workingSettings.up.ToString("X4");
            DirectoryInfo searchIn         = new DirectoryInfo(Application.persistentDataPath);
            FileInfo[]    fileToSpawn      = searchIn.GetFiles("*" + fileNameFragment + "*_*");
            if (fileToSpawn.Length != 0)
            {
                string toLoad = fileToSpawn[0].Name;
                Debug.Log("Level " + toLoad + " found above. Spawning...");
                GameObject n  = new GameObject();
                loadlevel  ll = n.AddComponent <loadlevel>();
                //Debug.Log ((int)workingLevel[SETTINGS_BLOCK_SIZE + (i*TILE_SIZE)]);
                ll.levelToLoad       = toLoad;
                ll.tempTileset       = tempTileset;
                ll.RendHolder        = ll.transform;
                n.transform.position = new Vector3(this.transform.position.x + 000, this.transform.position.y + LEVEL_SIZE, 0);
            }
            else
            {
                Debug.Log("Level not found at " + fileNameFragment);
            }
        }
        if (workingSettings.down != 0)
        {
            string        fileNameFragment = workingSettings.down.ToString("X4");
            DirectoryInfo searchIn         = new DirectoryInfo(Application.persistentDataPath);
            FileInfo[]    fileToSpawn      = searchIn.GetFiles("*" + fileNameFragment + "*_*");
            if (fileToSpawn.Length != 0)
            {
                string toLoad = fileToSpawn[0].Name;
                Debug.Log("Level " + toLoad + " found below. Spawning...");
                GameObject n  = new GameObject();
                loadlevel  ll = n.AddComponent <loadlevel>();
                //Debug.Log ((int)workingLevel[SETTINGS_BLOCK_SIZE + (i*TILE_SIZE)]);
                ll.levelToLoad       = toLoad;
                ll.tempTileset       = tempTileset;
                ll.RendHolder        = ll.transform;
                n.transform.position = new Vector3(this.transform.position.x + 000, this.transform.position.y - LEVEL_SIZE, 0);
            }
            else
            {
                Debug.Log("Level not found at " + fileNameFragment);
            }
        }
        if (workingSettings.left != 0)
        {
            string        fileNameFragment = workingSettings.left.ToString("X4");
            DirectoryInfo searchIn         = new DirectoryInfo(Application.persistentDataPath);
            FileInfo[]    fileToSpawn      = searchIn.GetFiles("*" + fileNameFragment + "*_*");
            if (fileToSpawn.Length != 0)
            {
                string toLoad = fileToSpawn[0].Name;
                Debug.Log("Level " + toLoad + " found left. Spawning...");
                GameObject n  = new GameObject();
                loadlevel  ll = n.AddComponent <loadlevel>();
                //Debug.Log ((int)workingLevel[SETTINGS_BLOCK_SIZE + (i*TILE_SIZE)]);
                ll.levelToLoad       = toLoad;
                ll.tempTileset       = tempTileset;
                ll.RendHolder        = ll.transform;
                n.transform.position = new Vector3(this.transform.position.x - LEVEL_SIZE, this.transform.position.y + 000, 0);
            }
            else
            {
                Debug.Log("Level not found at " + fileNameFragment);
            }
        }
        if (workingSettings.right != 0)
        {
            string        fileNameFragment = workingSettings.right.ToString("X4");
            DirectoryInfo searchIn         = new DirectoryInfo(Application.persistentDataPath);
            FileInfo[]    fileToSpawn      = searchIn.GetFiles("*" + fileNameFragment + "*_*");
            if (fileToSpawn.Length != 0)
            {
                string toLoad = fileToSpawn[0].Name;
                Debug.Log("Level " + toLoad + " found right. Spawning...");
                GameObject n  = new GameObject();
                loadlevel  ll = n.AddComponent <loadlevel>();
                //Debug.Log ((int)workingLevel[SETTINGS_BLOCK_SIZE + (i*TILE_SIZE)]);
                ll.levelToLoad       = toLoad;
                ll.tempTileset       = tempTileset;
                ll.RendHolder        = ll.transform;
                n.transform.position = new Vector3(this.transform.position.x + LEVEL_SIZE, this.transform.position.y + 000, 0);
            }
            else
            {
                Debug.Log("Level not found at " + fileNameFragment);
            }
        }
    }