// TODO: make redundant /// <summary> /// Loads MAP file geometry into a mesh and returns a gameobject /// </summary> public static GameObject LoadMap(string filePath, ResourceLifespan lifespan) { GameObject map = UnityEngine.Object.Instantiate(ResourceManager.Load <GameObject>(filePath, lifespan)); map.SetActive(true); return(map); }
public T Load <T>(string path, ResourceLifespan span = ResourceLifespan.Global) { if (CheckCache(path, out Resource <T> res)) { return(res.Data); } if (typeof(T) == typeof(SpriteSheet)) { string jsonText = _content.Load <String>(path); JSONNode node = JSON.Parse(jsonText); SpriteSheet sheet = SpriteSheetLoader.LoadFromJSON(node, _content); Resource <SpriteSheet> resource = new Resource <SpriteSheet>(sheet, span); _resources[path] = resource; } else if (typeof(T) == typeof(TileMap)) { string jsonText = _content.Load <String>(path); JSONNode node = JSON.Parse(jsonText); TileMap map = TileMapLoader.LoadFromJSON(node, _content, this, span); Resource <TileMap> resource = new Resource <TileMap>(map, span); _resources[path] = resource; } else { Resource <T> resource = new Resource <T>(_content.Load <T>(path), span); _resources[path] = resource; } return(((Resource <T>)_resources[path]).Data); }
// TODO: make redundant /// <summary> /// Loads a Torii object (3D model with animations) and returns a gameobject /// </summary> public static GameObject LoadObject(string filePath, bool collisionMesh, ResourceLifespan lifespan) { GameObject g = UnityEngine.Object.Instantiate(ResourceManager.Load <GameObject>(filePath, lifespan)); TOBJ t = g.GetComponent <ToriiObject>().ToriiObj; Renderer[] renderers = g.GetComponentsInChildren <Renderer>(); foreach (Renderer r in renderers) { Material m = r.material; // load texture if (!string.IsNullOrEmpty(t.ObjectTexture)) { if (Path.GetFileNameWithoutExtension(t.ObjectTexture).Contains("[")) { // part of a texture set m.shader = Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/TransparentSet" : "LSD/TransparentSet"); string texNameWithoutExtension = Path.GetFileNameWithoutExtension(t.ObjectTexture); string baseTexName = texNameWithoutExtension.Substring(1); string pathToTextureDir = Path.GetDirectoryName(t.ObjectTexture); m.SetTexture("_MainTexA", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "A" + baseTexName) + ".png", lifespan)); m.SetTexture("_MainTexB", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "B" + baseTexName) + ".png", lifespan)); m.SetTexture("_MainTexC", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "C" + baseTexName) + ".png", lifespan)); m.SetTexture("_MainTexD", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "D" + baseTexName) + ".png", lifespan)); } else { m.shader = Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/Transparent" : "Transparent/Diffuse"); m.SetTexture("_MainTex", ResourceManager.Load <Texture2D>(t.ObjectTexture, lifespan)); } } } // load animations if (t.NumberOfAnimations > 0) { ToriiObjectAnimator animator = g.AddComponent <ToriiObjectAnimator>(); foreach (Transform child in g.transform) { animator.Objects.Add(child.gameObject); } animator.ToriiObject = t; } if (collisionMesh) { foreach (Transform child in g.transform) { child.gameObject.AddComponent <MeshCollider>(); } } g.transform.localScale = new Vector3(-g.transform.localScale.x, g.transform.localScale.y, g.transform.localScale.z); g.SetActive(true); return(g); }
// TODO: make redundant /// <summary> /// Loads a Torii map and returns a gameobject with entities in the torii map as child elements /// </summary> public static GameObject LoadToriiMap(string filePath, ResourceLifespan lifespan, out TMAP tmap) { // TODO: handle missing files tmap = ResourceManager.Load <TMAP>(filePath, lifespan); GameObject tmapObject = new GameObject(tmap.Header.Name); foreach (ENTITY e in tmap.Content.Entities) { GameObject entityObject = EntityInstantiator.Instantiate(e); entityObject.transform.SetParent(tmapObject.transform); } return(tmapObject); }
public static void ClearLifespan(ResourceLifespan lifespan) { string[] keys = _resources.Keys.ToArray(); foreach (string resource in keys) { if (_resources[resource].Lifespan == lifespan) { string extension = Path.GetExtension(resource); // if there's a gameobject, destroy it if (extension == ".tobj" || extension == ".map") { UnityEngine.Object.Destroy(((GenericResource <GameObject>)_resources[resource]).Resource); } _resources.Remove(resource); } } }
public void ClearLifespan(ResourceLifespan span) { List <string> toRemove = new List <string>(); foreach (var res in _resources) { if (res.Value.Lifespan == span) { toRemove.Add(res.Key); } } foreach (string key in toRemove) { _resources.Remove(key); } }
public Resource(ResourceLifespan lifespan = ResourceLifespan.Global) : base(lifespan) { // intentionally empty }
public Resource(T data, ResourceLifespan lifespan = ResourceLifespan.Global) : base(lifespan) { Data = data; }
public GenericResource(ResourceLifespan lifespan) { Lifespan = lifespan; }
public static TileMap LoadFromJSON(JSONNode json, ContentManager content, ResourceManager manager, ResourceLifespan span) { JSONNode tilesets = json["tilesets"]; List <SpriteSheet> sheets = new List <SpriteSheet>(); int[] sheetLastIndices = new int[tilesets.Count]; int i = 0; foreach (JSONNode tileset in tilesets) { string source = tileset["source"]; if (source.StartsWith("../")) { source = source.Substring(3); } string path = Path.GetDirectoryName(source); string filename = Path.GetFileNameWithoutExtension(source); SpriteSheet sheet = manager.Load <SpriteSheet>(Path.Combine(path, filename), span); sheets.Add(sheet); sheetLastIndices[i] = sheet.NumberOfSprites + sheetLastIndices.Sum(); i++; } int width = json["width"]; int height = json["height"]; TileMap tilemap = new TileMap(width, height); foreach (SpriteSheet sheet in sheets) { tilemap.AddSheet(sheet); } JSONNode layers = json["layers"]; List <TileLayer> tileLayers = new List <TileLayer>(); foreach (JSONNode layer in layers) { JSONNode layerProperties = layer["properties"]; float depth = layerProperties["depth"]; TileLayer tileLayer = new TileLayer(layer["name"], width, height, depth); JSONNode layerData = layer["data"]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int tileID = layer["data"][y * width + x]; int sheetID = calculateSheetIndex(tileID, sheetLastIndices); int tileSheetTileID = sheetID > 0 ? tileID - sheetLastIndices[sheetID - 1] : tileID; bool isSolid = sheets[sheetID].Properties[(tileSheetTileID - 1).ToString()]["solid"]; tileLayer[x, y] = new Tile(tileSheetTileID, sheetID, isSolid); } } tilemap.AddLayer(tileLayer); } return(tilemap); }
// TODO: handle missing files public static DataType Load <DataType>(string filePath, ResourceLifespan lifespan = ResourceLifespan.GLOBAL, bool absolutePath = false) { string normalizedPath = IOUtil.NormalizePath(absolutePath ? filePath : IOUtil.PathCombine(Application.streamingAssetsPath, filePath)); // if the cache contains the resource, return it if (_resources.ContainsKey(normalizedPath)) { return(((GenericResource <DataType>)_resources[normalizedPath]).Resource); } string fileExt = Path.GetExtension(normalizedPath); bool fileNotFound = false; switch (fileExt) { case ".png": { GenericResource <Texture2D> resource = new GenericResource <Texture2D>(); resource.Resource = IOUtil.LoadPNG(normalizedPath); if (resource.Resource == null) { fileNotFound = true; break; } resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".json": { GenericResource <JSONClass> resource = new GenericResource <JSONClass>(); resource.Resource = IOUtil.ReadJSONFromDisk(normalizedPath); if (resource.Resource == null) { fileNotFound = true; break; } resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".map": { GenericResource <GameObject> resource = new GenericResource <GameObject>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } resource.Resource = MapReader.LoadMap(normalizedPath, IOUtil.PathCombine(Application.streamingAssetsPath, "textures", "wad"), Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/DiffuseSetNoAffine" : "LSD/DiffuseSet"), Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/TransparentSetNoAffine" : "LSD/TransparentSet")); resource.Resource.transform.SetParent(_resourceManagerGameObject.transform); resource.Resource.SetActive(false); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".tmap": { GenericResource <TMAP> resource = new GenericResource <TMAP>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } resource.Resource = ToriiMapReader.ReadFromFile(normalizedPath); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } case ".tobj": { GenericResource <GameObject> resource = new GenericResource <GameObject>(); if (!File.Exists(normalizedPath)) { Debug.LogError("Could not load resource " + normalizedPath); fileNotFound = true; break; } TOBJ tobj = new TOBJ(); ToriiObjectReader.Read(normalizedPath, ref tobj); resource.Resource = OBJReader.ReadOBJString(tobj.ObjectFile); resource.Resource.AddComponent <ToriiObject>(); ToriiObject toriiObjScript = resource.Resource.GetComponent <ToriiObject>(); toriiObjScript.ToriiObj = tobj; resource.Resource.transform.SetParent(_resourceManagerGameObject.transform); resource.Resource.SetActive(false); resource.Lifespan = lifespan; _resources.Add(normalizedPath, resource); break; } default: { throw new ResourceLoadException("Did not recognize file type: " + normalizedPath); } } if (fileNotFound) { throw new ResourceLoadException("Could not find resource!"); } return(((GenericResource <DataType>)_resources[normalizedPath]).Resource); }