/// <summary> /// Instantiate base RMB block by DFBlock data. /// </summary> /// <param name="blockData">Block data.</param> /// <returns>Block GameObject.</returns> public static GameObject CreateBaseGameObject(ref DFBlock blockData, DaggerfallRMBBlock cloneFrom = null) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } // Create gameobject GameObject go; string name = string.Format("DaggerfallBlock [{0}]", blockData.Name); if (cloneFrom != null) { go = GameObjectHelper.InstantiatePrefab(cloneFrom.gameObject, name, null, Vector3.zero); } else { go = new GameObject(name); go.AddComponent <DaggerfallRMBBlock>(); } // Setup combiner ModelCombiner combiner = null; if (dfUnity.Option_CombineRMB) { combiner = new ModelCombiner(); } // Lists to receive any doors found in this block List <StaticDoor> modelDoors; List <StaticDoor> propDoors; // Add models and static props GameObject modelsNode = new GameObject("Models"); modelsNode.transform.parent = go.transform; AddModels(dfUnity, ref blockData, out modelDoors, combiner, modelsNode.transform); AddProps(dfUnity, ref blockData, out propDoors, combiner, modelsNode.transform); // Add doors List <StaticDoor> allDoors = new List <StaticDoor>(); if (modelDoors.Count > 0) { allDoors.AddRange(modelDoors); } if (propDoors.Count > 0) { allDoors.AddRange(propDoors); } if (allDoors.Count > 0) { AddStaticDoors(allDoors.ToArray(), go); } // Apply combiner if (combiner != null) { if (combiner.VertexCount > 0) { combiner.Apply(); GameObjectHelper.CreateCombinedMeshGameObject( combiner, "CombinedModels", modelsNode.transform, dfUnity.Option_SetStaticFlags); } } return(go); }
/// <summary> /// Instantiate base RMB block by DFBlock data. /// </summary> /// <param name="blockData">Block data.</param> /// <param name="layoutX">X coordinate in parent map layout.</param> /// /// <param name="layoutY">Y coordinate in parent map layout.</param> /// <param name="cloneFrom">Prefab to clone from.</param> /// <returns>Block GameObject.</returns> public static GameObject CreateBaseGameObject(ref DFBlock blockData, int layoutX, int layoutY, DaggerfallRMBBlock cloneFrom = null) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } // Create gameobject GameObject go; string name = string.Format("DaggerfallBlock [{0}]", blockData.Name); if (cloneFrom != null) { go = GameObjectHelper.InstantiatePrefab(cloneFrom.gameObject, name, null, Vector3.zero); } else { go = new GameObject(name); } // Setup combiner ModelCombiner combiner = null; if (dfUnity.Option_CombineRMB) { combiner = new ModelCombiner(); } // Lists to receive any doors found in this block List <StaticDoor> modelDoors; List <StaticDoor> propDoors; List <StaticBuilding> modelBuildings; // Add models and static props GameObject modelsNode = new GameObject("Models"); modelsNode.transform.parent = go.transform; AddModels(dfUnity, layoutX, layoutY, ref blockData, out modelDoors, out modelBuildings, combiner, modelsNode.transform); AddProps(dfUnity, ref blockData, out propDoors, combiner, modelsNode.transform); // Combine list of doors found in models and props List <StaticDoor> allDoors = new List <StaticDoor>(); if (modelDoors.Count > 0) { allDoors.AddRange(modelDoors); } if (propDoors.Count > 0) { allDoors.AddRange(propDoors); } // Assign building key to each door for (int i = 0; i < allDoors.Count; i++) { StaticDoor door = allDoors[i]; door.buildingKey = BuildingDirectory.MakeBuildingKey((byte)layoutX, (byte)layoutY, (byte)door.recordIndex); allDoors[i] = door; } // Assign building key to each building for (int i = 0; i < modelBuildings.Count; i++) { StaticBuilding building = modelBuildings[i]; building.buildingKey = BuildingDirectory.MakeBuildingKey((byte)layoutX, (byte)layoutY, (byte)building.recordIndex); modelBuildings[i] = building; } // Add static doors component if (allDoors.Count > 0) { AddStaticDoors(allDoors.ToArray(), go); } // Add static buildings component if (modelBuildings.Count > 0) { AddStaticBuildings(modelBuildings.ToArray(), go); } // Apply combiner if (combiner != null) { if (combiner.VertexCount > 0) { combiner.Apply(); GameObjectHelper.CreateCombinedMeshGameObject( combiner, "CombinedModels", modelsNode.transform, dfUnity.Option_SetStaticFlags); } } return(go); }
/// <summary> /// Creates a new RDB GameObject and performs block layout. /// Can pass information about dungeon for texture swaps and random enemies. /// </summary> /// <param name="dfUnity">DaggerfallUnity singleton. Required for content readers and settings.</param> /// <param name="blockName">Name of RDB block to build.</param> /// <param name="isStartingBlock">True if this is the starting block. Controls exit doors.</param> /// <param name="textureTable">Dungeon texture table.</param> /// <param name="dungeonType">Type of dungeon for random encounter tables.</param> /// <param name="seed">Seed for random encounters.</param> /// <returns>GameObject.</returns> public static GameObject CreateGameObject( string blockName, bool isStartingBlock, int[] textureTable = null, DFRegion.DungeonTypes dungeonType = DFRegion.DungeonTypes.HumanStronghold, int seed = 0) { // Validate if (string.IsNullOrEmpty(blockName)) { return(null); } if (!blockName.ToUpper().EndsWith(".RDB")) { return(null); } DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } // Create gameobject GameObject go = new GameObject(string.Format("DaggerfallBlock [Name={0}]", blockName)); DaggerfallRDBBlock dfBlock = go.AddComponent <DaggerfallRDBBlock>(); // Start new layout RDBLayout layout = new RDBLayout(blockName); layout.isStartingBlock = isStartingBlock; layout.textureTable = textureTable; layout.dungeonType = dungeonType; layout.staticModelsNode = new GameObject("Static Models"); layout.actionModelsNode = new GameObject("Action Models"); layout.doorsNode = new GameObject("Doors"); layout.flatsNode = new GameObject("Flats"); layout.lightsNode = new GameObject("Lights"); layout.enemiesNode = new GameObject("Enemies"); // Parent child game objects layout.staticModelsNode.transform.parent = go.transform; layout.actionModelsNode.transform.parent = go.transform; layout.doorsNode.transform.parent = go.transform; layout.flatsNode.transform.parent = go.transform; layout.lightsNode.transform.parent = go.transform; layout.enemiesNode.transform.parent = go.transform; // List to receive any exit doors found List <StaticDoor> allDoors = new List <StaticDoor>(); // Seed random generator UnityEngine.Random.seed = seed; // Iterate object groups layout.groupIndex = 0; DFBlock blockData = dfUnity.ContentReader.BlockFileReader.GetBlock(blockName); foreach (DFBlock.RdbObjectRoot group in blockData.RdbBlock.ObjectRootList) { // Skip empty object groups if (null == group.RdbObjects) { layout.groupIndex++; continue; } // Iterate objects in this group List <StaticDoor> modelDoors; foreach (DFBlock.RdbObject obj in group.RdbObjects) { // Handle by object type switch (obj.Type) { case DFBlock.RdbResourceTypes.Model: layout.AddRDBModel(obj, out modelDoors, layout.staticModelsNode.transform); if (modelDoors.Count > 0) { allDoors.AddRange(modelDoors); } break; case DFBlock.RdbResourceTypes.Flat: layout.AddRDBFlat(obj, layout.flatsNode.transform); break; case DFBlock.RdbResourceTypes.Light: layout.AddRDBLight(obj, layout.lightsNode.transform); break; default: break; } } // Increment group index layout.groupIndex++; } // Link action nodes layout.LinkActionNodes(); // Combine meshes if (dfUnity.Option_CombineRDB) { layout.combiner.Apply(); GameObject cgo = GameObjectHelper.CreateCombinedMeshGameObject(layout.combiner, "CombinedMeshes", layout.staticModelsNode.transform, dfUnity.Option_SetStaticFlags); cgo.GetComponent <DaggerfallMesh>().SetDungeonTextures(textureTable); } // Fix enemy standing positions for this block // Some enemies are floating in air or sunk into ground // Can only adjust this after geometry instantiated layout.FixEnemyStanding(go); // Store start markers in block dfBlock.SetStartMarkers(layout.startMarkers.ToArray()); // Add doors if (allDoors.Count > 0) { layout.AddDoors(allDoors.ToArray(), go); } return(go); }
/// <summary> /// Instantiate base RDB block by DFBlock data. /// </summary> /// <param name="blockData">Block data.</param> /// <param name="textureTable">Optional texture table for dungeon.</param> /// <param name="allowExitDoors">Add exit doors to block.</param> /// <param name="cloneFrom">Clone and build on a prefab object template.</param> /// <returns>Block GameObject.</returns> public static GameObject CreateBaseGameObject( ref DFBlock blockData, int[] textureTable = null, bool allowExitDoors = true, DaggerfallRDBBlock cloneFrom = null) { DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } // Use default texture table if one not specified if (textureTable == null) { textureTable = StaticTextureTables.DefaultTextureTable; } // Create gameobject GameObject go; string name = string.Format("DaggerfallBlock [{0}]", blockData.Name); if (cloneFrom != null) { go = GameObjectHelper.InstantiatePrefab(cloneFrom.gameObject, name, null, Vector3.zero); } else { go = new GameObject(name); go.AddComponent <DaggerfallRDBBlock>(); } // Setup combiner ModelCombiner combiner = null; if (dfUnity.Option_CombineRDB) { combiner = new ModelCombiner(); } // Add parent node GameObject modelsNode = new GameObject("Models"); GameObject actionModelsNode = new GameObject("Action Models"); modelsNode.transform.parent = go.transform; actionModelsNode.transform.parent = go.transform; // Add models List <StaticDoor> exitDoors; AddModels( dfUnity, ref blockData, textureTable, allowExitDoors, out exitDoors, combiner, modelsNode.transform, actionModelsNode.transform); // Apply combiner if (combiner != null) { if (combiner.VertexCount > 0) { combiner.Apply(); GameObject cgo = GameObjectHelper.CreateCombinedMeshGameObject( combiner, "CombinedModels", modelsNode.transform, dfUnity.Option_SetStaticFlags); cgo.GetComponent <DaggerfallMesh>().SetDungeonTextures(textureTable); } } // Add exit doors if (exitDoors.Count > 0) { DaggerfallStaticDoors c = go.AddComponent <DaggerfallStaticDoors>(); c.Doors = exitDoors.ToArray(); } return(go); }
public static GameObject CreateGameObject( DaggerfallUnity dfUnity, ref DFBlock blockData, bool disableGround = false, DaggerfallBillboardBatch natureBatch = null) { // Create gameobject GameObject go = new GameObject(string.Format("DaggerfallBlock [Name={0}]", blockData.Name)); //go.AddComponent<DaggerfallBlock>(); // Setup combiner ModelCombiner combiner = null; if (dfUnity.Option_CombineRMB) { combiner = new ModelCombiner(); } // Lists to receive any doors found in this block List <StaticDoor> modelDoors; List <StaticDoor> propDoors; // Add models and props GameObject modelsNode = new GameObject("Models"); modelsNode.transform.parent = go.transform; AddModels(dfUnity, ref blockData, out modelDoors, combiner, modelsNode.transform); AddProps(dfUnity, ref blockData, out propDoors, combiner, modelsNode.transform); // Add doors List <StaticDoor> allDoors = new List <StaticDoor>(); if (modelDoors.Count > 0) { allDoors.AddRange(modelDoors); } if (propDoors.Count > 0) { allDoors.AddRange(propDoors); } if (allDoors.Count > 0) { AddDoors(allDoors.ToArray(), go); } // Add block flats GameObject flatsNode = new GameObject("Flats"); flatsNode.transform.parent = go.transform; AddBlockFlats(dfUnity, ref blockData, flatsNode.transform); // Add nature flats if (natureBatch == null) { AddNatureFlats(dfUnity, ref blockData, flatsNode.transform); } else { AddNatureFlatsToBatch(natureBatch, ref blockData); } // Add ground plane if (dfUnity.Option_SimpleGroundPlane && !disableGround) { AddSimpleGroundPlane(dfUnity, ref blockData, go.transform); } // Apply combiner if (combiner != null) { if (combiner.VertexCount > 0) { combiner.Apply(); GameObjectHelper.CreateCombinedMeshGameObject( combiner, "CombinedModels", modelsNode.transform, dfUnity.Option_SetStaticFlags); } } return(go); }