Files
CalamityModPublic/Schematics/SchematicLoader.cs
T
DominicKarma42 a8b15f290e Another load of Arsenal things. See description.
Updated Draedon's logbook GUI to be a bit more advanced.

Added Dubious Plating and Mysterious Circuitry. Currently not used in recipes yet.

Added Gauss Pistol

Several misc changes/fixes pertaining to Arsenal content.

Added a BaseTurret tile entity and Draedon Turret which derives from this.

Added a Draedon Energy field tile entity.

Renamed the item generator internally. Its name used to use "Dreadon" instead of "Draedon".

 Updated worldgen schematics to use the field generator and turret.

Added a hologram tile entity that can be interacted with like a typical town NPC. Placeholder text is used currently.
2020-06-21 03:07:51 -05:00

77 lines
3.4 KiB
C#

using CalamityMod.Schematics.AutoGeneratedContent;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod.Schematics
{
public static class SchematicLoader
{
#region Fields
internal static Dictionary<string, Tile[,]> TileMaps = new Dictionary<string, Tile[,]>();
internal static Dictionary<string, PilePlacementFunction> PilePlacementMaps = new Dictionary<string, PilePlacementFunction>();
public delegate void PilePlacementFunction(int x, int y, Rectangle placeInArea);
#endregion
#region Loading/Unloading
internal static void LoadEverything()
{
TileMaps = new Dictionary<string, Tile[,]>
{
// Texture loading does not require a .png at the end of the path string.
["Workshop"] = LoadSchematicCodeAndTexture(typeof(WorkshopSchematic), "Schematics/AutoGeneratedContent/WorkshopSchematic"),
["Plague Research Facility"] = LoadSchematicCodeAndTexture(typeof(ResearchFacilitySchematic), "Schematics/AutoGeneratedContent/ResearchFacilitySchematic")
};
}
internal static void UnloadEverything()
{
TileMaps = null;
PilePlacementMaps = null;
}
#endregion
#region Texture and Code to Tile
internal static MethodInfo SeekSchematicLoaderInType(Type toSearch)
{
// Checks if the method has a GeneratedCode attribute with a specific tool.
// This should always be the case as it is added by default with the auto-generated code.
bool isSuitableMethod(MethodInfo method)
{
GeneratedCodeAttribute generatedCodeAttribute = method.GetCustomAttribute(typeof(GeneratedCodeAttribute)) as GeneratedCodeAttribute;
if (generatedCodeAttribute == null)
return false;
return generatedCodeAttribute.Tool.ToLower() == "screencapturetoschematic";
}
MethodInfo[] appropriateMethodsFound = toSearch.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
MethodInfo match = appropriateMethodsFound.First(isSuitableMethod);
if (match == null)
throw new NullReferenceException($"Could not load any methods from {toSearch.FullName} that are appropriately marked to be used for schematic loading.");
return match;
}
internal static Tile[,] LoadSchematicCodeAndTexture(Type schematicType, string texturePath)
{
ColorTileMap map = (ColorTileMap)SeekSchematicLoaderInType(schematicType).Invoke(null, null);
Texture2D schemanticTexture = ModContent.GetTexture($"CalamityMod/{texturePath}");
Tile[,] tiles = new Tile[schemanticTexture.Width, schemanticTexture.Height];
Color[,] colors = schemanticTexture.GetColorsFromTexture();
for (int x = 0; x < schemanticTexture.Width; x++)
{
for (int y = 0; y < schemanticTexture.Height; y++)
{
tiles[x, y] = map[colors[x, y]].SpecifiedTile;
}
}
return tiles;
}
#endregion
}
}