rimworld-animation-studio/Assets/Scripts/AnimationComponents/AlienRaceDef.cs
2022-09-24 02:17:40 -05:00

210 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace RimWorldAnimationStudio
{
public class AlienRaceDef
{
public string defName;
public bool isHumanoid = true;
public float scale = 1f;
public List<MultiDirectionalGraphic> bodyTypeGraphics = new List<MultiDirectionalGraphic>();
public MultiDirectionalGraphic headGraphics = new MultiDirectionalGraphic();
public AlienRaceDef()
{
}
public AlienRaceDef(string defName)
{
this.defName = defName;
}
public Sprite GetHeadGraphic(CardinalDirection facing)
{
if (HasValidHeadGraphicPath(facing) == false)
{ return null; }
switch (facing)
{
case CardinalDirection.North: return headGraphics.northGraphic.sprite;
case CardinalDirection.East: return headGraphics.eastGraphic.sprite;
case CardinalDirection.South: return headGraphics.southGraphic.sprite;
default: return headGraphics.eastGraphic.sprite;
}
}
public Sprite GetBodyTypeGraphic(CardinalDirection facing, string bodyType = "None")
{
if (HasValidBodyTypeGraphicPath(facing, bodyType) == false)
{ return null; }
MultiDirectionalGraphic bodyTypeGraphic = bodyTypeGraphics.FirstOrDefault(x => x.bodyType == bodyType);
if (bodyTypeGraphic == null)
{
bodyTypeGraphic = new MultiDirectionalGraphic(bodyType);
bodyTypeGraphics.Add(bodyTypeGraphic);
}
switch (facing)
{
case CardinalDirection.North: return bodyTypeGraphic.northGraphic.sprite;
case CardinalDirection.East: return bodyTypeGraphic.eastGraphic.sprite;
case CardinalDirection.South: return bodyTypeGraphic.southGraphic.sprite;
default: return bodyTypeGraphic.eastGraphic.sprite;
}
}
public string GetHeadGraphicPath(CardinalDirection facing)
{
if (HasValidHeadGraphicPath(facing) == false)
{ return "Invalid file path"; }
switch (facing)
{
case CardinalDirection.North: return headGraphics.northGraphic.path;
case CardinalDirection.East: return headGraphics.eastGraphic.path;
case CardinalDirection.South: return headGraphics.southGraphic.path;
default: return headGraphics.eastGraphic.path;
}
}
public string GetBodyTypeGraphicPath(CardinalDirection facing, string bodyType = "None")
{
if (HasValidBodyTypeGraphicPath(facing, bodyType) == false)
{ return "Invalid file path"; }
MultiDirectionalGraphic bodyTypeGraphic = bodyTypeGraphics.FirstOrDefault(x => x.bodyType == bodyType);
if (bodyTypeGraphic == null)
{
bodyTypeGraphic = new MultiDirectionalGraphic(bodyType);
bodyTypeGraphics.Add(bodyTypeGraphic);
}
switch (facing)
{
case CardinalDirection.North: return bodyTypeGraphic.northGraphic.path;
case CardinalDirection.East: return bodyTypeGraphic.eastGraphic.path;
case CardinalDirection.South: return bodyTypeGraphic.southGraphic.path;
default: return bodyTypeGraphic.eastGraphic.path;
}
}
public void SetHeadGraphicPath(string path, CardinalDirection facing)
{
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png")
{ path = "Invalid file path"; }
switch (facing)
{
case CardinalDirection.North:
headGraphics.northGraphic.path = path;
headGraphics.northGraphic.sprite = LoadSprite(path); break;
case CardinalDirection.East:
headGraphics.eastGraphic.path = path;
headGraphics.eastGraphic.sprite = LoadSprite(path); break;
case CardinalDirection.South:
headGraphics.southGraphic.path = path;
headGraphics.southGraphic.sprite = LoadSprite(path); break;
default:
headGraphics.eastGraphic.path = path;
headGraphics.eastGraphic.sprite = LoadSprite(path); break;
}
}
public void SetBodyTypeGraphicPath(string path, CardinalDirection facing, string bodyType = "None")
{
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png")
{ path = "Invalid file path"; }
MultiDirectionalGraphic bodyTypeGraphic = bodyTypeGraphics.FirstOrDefault(x => x.bodyType == bodyType);
if (bodyTypeGraphic == null)
{
bodyTypeGraphic = new MultiDirectionalGraphic(bodyType);
bodyTypeGraphics.Add(bodyTypeGraphic);
}
switch (facing)
{
case CardinalDirection.North:
bodyTypeGraphic.northGraphic.path = path;
bodyTypeGraphic.northGraphic.sprite = LoadSprite(path); break;
case CardinalDirection.East:
bodyTypeGraphic.eastGraphic.path = path;
bodyTypeGraphic.eastGraphic.sprite = LoadSprite(path); break;
case CardinalDirection.South:
bodyTypeGraphic.southGraphic.path = path;
bodyTypeGraphic.southGraphic.sprite = LoadSprite(path); break;
default:
bodyTypeGraphic.eastGraphic.path = path;
bodyTypeGraphic.eastGraphic.sprite = LoadSprite(path); break;
}
}
public bool HasValidHeadGraphicPath(CardinalDirection facing)
{
string path;
switch (facing)
{
case CardinalDirection.North: path = headGraphics.northGraphic.path; break;
case CardinalDirection.East: path = headGraphics.eastGraphic.path; break;
case CardinalDirection.South: path = headGraphics.southGraphic.path; break;
default: path = headGraphics.eastGraphic.path; break;
}
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png") return false;
return true;
}
public bool HasValidBodyTypeGraphicPath(CardinalDirection facing, string bodyType = "None")
{
MultiDirectionalGraphic bodyTypeGraphic = bodyTypeGraphics.FirstOrDefault(x => x.bodyType == bodyType);
if (bodyTypeGraphic == null)
{
bodyTypeGraphic = new MultiDirectionalGraphic(bodyType);
bodyTypeGraphics.Add(bodyTypeGraphic);
}
string path;
switch (facing)
{
case CardinalDirection.North: path = bodyTypeGraphic.northGraphic.path; break;
case CardinalDirection.East: path = bodyTypeGraphic.eastGraphic.path; break;
case CardinalDirection.South: path = bodyTypeGraphic.southGraphic.path; break;
default: path = bodyTypeGraphic.eastGraphic.path; break;
}
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png") return false;
return true;
}
public Sprite LoadSprite(string path)
{
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png") return null;
byte[] pngBytes = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(pngBytes);
Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 85.0f);
return sprite;
}
}
}