public GameObject LoadModule(int level, int number)
    {
        //create the parent module object for all loaded game objects to parent from.
        //parent is returned for easy transform positioning
        GameObject moduleParent = new GameObject("Module Parent. Level: " + level.ToString() + " Number: " + number.ToString());

        moduleParent.transform.position = Vector3.zero;

        //create file string
        string file;

        //set application path
        if (Directory.Exists(Application.dataPath + "/Modules/"))
        {
            //set file path
            file = Application.dataPath + "/Modules/" + "Level_" + level.ToString("D2") + "_Module_" + number.ToString("D2") + ".mod";

            //check if file exists
            if (!File.Exists(file))
            {
                Debug.Log("Module file does not exist in directory: " + level + " " + number);
                return(null);
            }
        }
        else
        {
            Debug.Log("Module directory does not exist!");
            return(null);
        }

        //procede if module exists
        using (Stream s = File.OpenRead(file))
        {
            //create reader
            using (BinaryReader r = new BinaryReader(s))
            {
                //verify file is of correct format
                string head = new string(r.ReadChars(4));
                if (!head.Equals(Constants.MODULE_FILE_HEADER))
                {
                    Debug.Log("File not of correct format in reading module for level generation.");
                    return(null);
                }

                //get number of game objects
                int numberOfGameObjects = r.ReadInt32();

                //loop through the file and create each game object, add to grid, and move it
                for (int i = 0; i < numberOfGameObjects; i++)
                {
                    //get type
                    int typeInt = r.ReadInt32();
                    Constants.ObjectIDs type = (Constants.ObjectIDs)typeInt;

                    //check if flipped
                    bool flipped = r.ReadBoolean();

                    //get position
                    int x = r.ReadInt32();
                    int y = r.ReadInt32();

                    //create spawn game object reference
                    GameObject spawnObject = null;// = new GameObject();

                    //create object
                    switch (type)
                    {
                    case Constants.ObjectIDs.None:
                        break;

                    //utilities
                    case Constants.ObjectIDs.LevelStartPoint:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.LevelStartPoint), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.LevelEndPoint:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.LevelEndPoint), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    //environment - blocks
                    case Constants.ObjectIDs.DirtBlock:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.DirtBlock), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.DirtBlockGrass:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockGrass), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.DirtBlockSloped:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockSloped), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.DirtBlockSlopedGrass:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockSlopedGrass), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.StoneBlock:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.StoneBlock), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.StoneBlockSloped:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockSloped), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.StoneBlockConcreteTop:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockConcreteTop), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.StoneBlockSlopedConcreteTop:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockSlopedConcreteTop), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    //environment - buildings
                    case Constants.ObjectIDs.HangarClose:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.HangarClose), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.HangarMiddle:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.HangarMiddle), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.HangarFar:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.HangarFar), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Tower:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Tower), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    //environment - weather
                    case Constants.ObjectIDs.WeatherHazard1:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard1), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.WeatherHazard2:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard2), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.WeatherHazard3:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard3), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    //environment - other
                    case Constants.ObjectIDs.Bird:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Bird), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    //allies


                    //enemies
                    case Constants.ObjectIDs.MotherShip:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Mothership), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Zepplin:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Zepplin), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Tank:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Tank), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Soldier:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Soldier), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Jeep:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Jeep), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    case Constants.ObjectIDs.Bomber:
                        spawnObject = Instantiate(ResourceManager.Instance.GetPrefab(Prefabs.Bomber), new Vector3(x, y, 0f), Quaternion.identity);
                        break;

                    default:
                        Debug.Log("Invalid object being loaded");
                        spawnObject = new GameObject("Invalid object loaded");
                        break;
                    }

                    //set object properties
                    if (flipped)
                    {
                        spawnObject.transform.Rotate(new Vector3(0, 180, 0));
                    }

                    //set parent
                    spawnObject.transform.SetParent(null);
                    spawnObject.transform.SetParent(moduleParent.transform);
                }
            }
        }

        return(moduleParent);
    }
Exemple #2
0
    public void LoadModule(string moduleName)
    {
        //set file path
        //Debug.Log(moduleName);
        string file = Application.dataPath + "/Modules/" + moduleName;

        //load the module if the file exists
        if (File.Exists(file))
        {
            //prep grid for new module
            ClearGrid();

            //open stream
            using (Stream s = File.OpenRead(file))
            {
                //create reader
                using (BinaryReader r = new BinaryReader(s))
                {
                    //verify file is of correct format
                    string head = new string(r.ReadChars(4));
                    if (!head.Equals(Constants.MODULE_FILE_HEADER))
                    {
                        Debug.Log("File not of correct format");
                        return;
                    }

                    //get number of game objects
                    int numberOfGameObjects = r.ReadInt32();

                    //loop through the file and create each game object, add to grid, and move it
                    for (int i = 0; i < numberOfGameObjects; i++)
                    {
                        //get type
                        int typeInt = r.ReadInt32();
                        Constants.ObjectIDs type = (Constants.ObjectIDs)typeInt;

                        //check if flipped
                        bool flipped = r.ReadBoolean();

                        //get position
                        int x = r.ReadInt32();
                        int y = r.ReadInt32();

                        //prep cell
                        GridPoints[x, y].IsFlipped = flipped;

                        //create object
                        switch (type)
                        {
                        case Constants.ObjectIDs.None:
                            break;

                        //utilities
                        case Constants.ObjectIDs.LevelStartPoint:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.LevelStartPoint);
                            break;

                        case Constants.ObjectIDs.LevelEndPoint:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.LevelEndPoint);
                            break;

                        //environment - blocks
                        case Constants.ObjectIDs.DirtBlock:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.DirtBlock);
                            break;

                        case Constants.ObjectIDs.DirtBlockGrass:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockGrass);
                            break;

                        case Constants.ObjectIDs.DirtBlockSloped:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockSloped);
                            break;

                        case Constants.ObjectIDs.DirtBlockSlopedGrass:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.DirtBlockSlopedGrass);
                            break;

                        case Constants.ObjectIDs.StoneBlock:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.StoneBlock);
                            break;

                        case Constants.ObjectIDs.StoneBlockSloped:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockSloped);
                            break;

                        case Constants.ObjectIDs.StoneBlockConcreteTop:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockConcreteTop);
                            break;

                        case Constants.ObjectIDs.StoneBlockSlopedConcreteTop:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.StoneBlockSlopedConcreteTop);
                            break;

                        //environment - buildings
                        case Constants.ObjectIDs.HangarClose:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.HangarClose);
                            break;

                        case Constants.ObjectIDs.HangarMiddle:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.HangarMiddle);
                            break;

                        case Constants.ObjectIDs.HangarFar:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.HangarFar);
                            break;

                        case Constants.ObjectIDs.Tower:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Tower);
                            break;

                        //environment - weather
                        case Constants.ObjectIDs.WeatherHazard1:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard1);
                            break;

                        case Constants.ObjectIDs.WeatherHazard2:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard2);
                            break;

                        case Constants.ObjectIDs.WeatherHazard3:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.WeatherHazard3);
                            break;

                        //environment - other
                        case Constants.ObjectIDs.Bird:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Bird);
                            break;

                        //allies


                        //enemies
                        case Constants.ObjectIDs.MotherShip:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Mothership);
                            break;

                        case Constants.ObjectIDs.Zepplin:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Zepplin);
                            break;

                        case Constants.ObjectIDs.Tank:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Tank);
                            break;

                        case Constants.ObjectIDs.Soldier:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Soldier);
                            break;

                        case Constants.ObjectIDs.Jeep:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Jeep);
                            break;

                        case Constants.ObjectIDs.Bomber:
                            GridPoints[x, y].CellObject = ResourceManager.Instance.GetPrefab(Prefabs.Bomber);
                            break;

                        default:
                            break;
                        }

                        //add game object to list
                        if (GridPoints[x, y].CellObject)
                        {
                            gameObjects.Add(GridPoints[x, y].CellObject);
                        }
                    }
                }
            }
        }
        else
        {
            Debug.Log("LoadModule: File does not exist");
        }
    }