Skip to content

Commit

Permalink
make BGUnitRailSceneObj the first proper SceneObject and do some refa…
Browse files Browse the repository at this point in the history
…ctoring
  • Loading branch information
jupahe64 committed Nov 21, 2023
1 parent 824d22f commit 8da9e18
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 645 deletions.
219 changes: 92 additions & 127 deletions Fushigi/course/CourseUnit.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
using Fushigi.Byml;
using Fushigi.ui.SceneObjects.bgunit;
using Fushigi.util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using static Fushigi.course.CourseUnit;
using Vector3 = System.Numerics.Vector3;

namespace Fushigi.course
Expand All @@ -35,10 +26,8 @@ public enum SkinDivision

public CourseUnit()
{
this.Walls = new List<Wall>();
this.mBeltRails = new List<BGUnitRailSceneObj>();
this.mModelType = 0;
this.mSkinDivision = 0;
this.mModelType = ModelType.Solid;
this.mSkinDivision = SkinDivision.FieldA;
}

public CourseUnit(BymlHashTable tbl)
Expand All @@ -48,139 +37,111 @@ public CourseUnit(BymlHashTable tbl)

if (tbl.ContainsKey("BeltRails"))
{
BymlArrayNode belts = tbl["BeltRails"] as BymlArrayNode;
BymlArrayNode belts = (BymlArrayNode)tbl["BeltRails"];

foreach (BymlHashTable beltsTbl in belts.Array)
{
Rail belt = new Rail();
belt.IsClosed = BymlUtil.GetNodeData<bool>(beltsTbl["IsClosed"]);
belt.mPoints = new();

BymlArrayNode beltsArr = beltsTbl["Points"] as BymlArrayNode;

foreach (BymlHashTable pointsTbl in beltsArr.Array)
{
belt.mPoints.Add(BymlUtil.GetVector3FromArray((BymlArrayNode)pointsTbl["Translate"]));
}
this.mBeltRails.Add(new BGUnitRailSceneObj(this, belt));
mBeltRails.Add(LoadRailNode(beltsTbl));
}
}

if (tbl.ContainsKey("Walls"))
{
BymlArrayNode wallsNode = tbl["Walls"] as BymlArrayNode;
BymlArrayNode wallsNode = (BymlArrayNode)tbl["Walls"];
this.Walls = new List<Wall>();

BGUnitRailSceneObj LoadRail(BymlHashTable railDict, bool isInternal = false)
{
BymlArrayNode pointsArr = railDict["Points"] as BymlArrayNode;

Rail rail = new();
rail.IsClosed = BymlUtil.GetNodeData<bool>(railDict["IsClosed"]);
rail.mPoints = new List<System.Numerics.Vector3?>();
rail.IsInternal = isInternal;

foreach (BymlHashTable pointsTbl in pointsArr.Array)
{
rail.mPoints.Add(BymlUtil.GetVector3FromArray((BymlArrayNode)pointsTbl["Translate"]));
}

return new BGUnitRailSceneObj(this, rail);
}

foreach (BymlHashTable wallsTbl in wallsNode.Array)
{
Wall wall = new Wall(this);
this.Walls.Add(wall);

if (wallsTbl.ContainsKey("ExternalRail"))
wall.ExternalRail = LoadRail(wallsTbl["ExternalRail"] as BymlHashTable);
wall.ExternalRail = LoadRailNode((BymlHashTable)wallsTbl["ExternalRail"]);
if (wallsTbl.ContainsKey("InternalRails"))
{
var railList = wallsTbl["InternalRails"] as BymlArrayNode;
var railList = (BymlArrayNode)wallsTbl["InternalRails"];
foreach (BymlHashTable rail in railList.Array)
wall.InternalRails.Add(LoadRail(rail, true));
wall.InternalRails.Add(LoadRailNode(rail, true));
}
}
}

GenerateTileSubUnits();
}

public BymlHashTable BuildNode()
private BGUnitRail LoadRailNode(BymlHashTable railDict, bool isInternal = false)
{
BymlHashTable table = new();
table.AddNode(BymlNodeId.Int, BymlUtil.CreateNode<int>("ModelType", (int)mModelType), "ModelType");
table.AddNode(BymlNodeId.Int, BymlUtil.CreateNode<int>("SkinDivision", (int)mSkinDivision), "SkinDivision");
BymlArrayNode pointsArr = (BymlArrayNode)railDict["Points"];

BymlArrayNode beltsArray = new((uint)mBeltRails.Count);
BGUnitRail rail = new(this)
{
IsClosed = BymlUtil.GetNodeData<bool>(railDict["IsClosed"]),
IsInternal = isInternal
};

foreach(var belt in mBeltRails)
foreach (BymlHashTable pointsTbl in pointsArr.Array)
{
var rail = belt.Save();
var position = BymlUtil.GetVector3FromArray((BymlArrayNode)pointsTbl["Translate"]);
rail.Points.Add(new BGUnitRail.RailPoint(rail, position));
}

BymlHashTable beltNode = new();
beltNode.AddNode(BymlNodeId.Bool, BymlUtil.CreateNode<bool>("IsClosed", belt.IsClosed), "IsClosed");
return rail;
}

BymlArrayNode pointsArr = new((uint)rail.mPoints.Count);
private BymlHashTable BuildRailNode(BGUnitRail rail)
{
BymlHashTable railNode = new();
railNode.AddNode(BymlNodeId.Bool, BymlUtil.CreateNode<bool>(rail.IsClosed), "IsClosed");

foreach (System.Numerics.Vector3 point in rail.mPoints)
{
BymlHashTable pointTbl = new();
BymlArrayNode translateNode = new(3);
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("X", point.X));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("Y", point.Y));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("Z", point.Z));

//beltNode.AddNode(BymlNodeId.Array, translateNode, "Translate");
pointTbl.AddNode(BymlNodeId.Array, translateNode, "Translate");
pointsArr.AddNodeToArray(pointTbl);
}
BymlArrayNode pointsArrayNode = new();

beltNode.AddNode(BymlNodeId.Array, pointsArr, "Points");
beltsArray.AddNodeToArray(beltNode);
foreach (BGUnitRail.RailPoint point in rail.Points)
{
var position = point.Position;
BymlHashTable pointDict = new();
BymlArrayNode translateNode = new(3);
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>(position.X));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>(position.Y));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>(position.Z));

pointDict.AddNode(BymlNodeId.Array, translateNode, "Translate");
pointsArrayNode.AddNodeToArray(pointDict);
}

table.AddNode(BymlNodeId.Array, beltsArray, "BeltRails");
railNode.AddNode(BymlNodeId.Array, pointsArrayNode, "Points");

BymlArrayNode wallsArray = new((uint)this.Walls.Count);
return railNode;
}

foreach (Wall wall in this.Walls)
{
BymlHashTable SaveRail(Rail rail)
{
BymlHashTable railNode = new();
railNode.AddNode(BymlNodeId.Bool, BymlUtil.CreateNode<bool>("IsClosed", rail.IsClosed), "IsClosed");
public BymlHashTable BuildNode()
{
BymlHashTable table = new();
table.AddNode(BymlNodeId.Int, BymlUtil.CreateNode<int>((int)mModelType), "ModelType");
table.AddNode(BymlNodeId.Int, BymlUtil.CreateNode<int>((int)mSkinDivision), "SkinDivision");

BymlArrayNode pointsArrayNode = new();
BymlArrayNode beltsArray = new((uint)mBeltRails.Count);

foreach (System.Numerics.Vector3 point in rail.mPoints)
{
BymlHashTable pointDict = new();
BymlArrayNode translateNode = new(3);
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("X", point.X));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("Y", point.Y));
translateNode.AddNodeToArray(BymlUtil.CreateNode<float>("Z", point.Z));

pointDict.AddNode(BymlNodeId.Array, translateNode, "Translate");
pointsArrayNode.AddNodeToArray(pointDict);
}
foreach (var belt in mBeltRails)
{
beltsArray.AddNodeToArray(BuildRailNode(belt));
}

railNode.AddNode(BymlNodeId.Array, pointsArrayNode, "Points");
table.AddNode(BymlNodeId.Array, beltsArray, "BeltRails");

return railNode;
}
BymlArrayNode wallsArray = new((uint)this.Walls.Count);

foreach (Wall wall in this.Walls)
{
BymlHashTable wallNode = new();
if (wall.InternalRails.Count > 0)
{
BymlArrayNode internaiRailListNode = new BymlArrayNode();
wallNode.AddNode(BymlNodeId.Array, internaiRailListNode, "InternalRails");
BymlArrayNode internalRailListNode = new BymlArrayNode();
wallNode.AddNode(BymlNodeId.Array, internalRailListNode, "InternalRails");

foreach (var rail in wall.InternalRails)
internaiRailListNode.AddNodeToArray(SaveRail(rail.Save()));
internalRailListNode.AddNodeToArray(BuildRailNode(rail));
}
wallNode.AddNode(BymlNodeId.Hash, SaveRail(wall.ExternalRail.Save()), "ExternalRail");
wallNode.AddNode(BymlNodeId.Hash, BuildRailNode(wall.ExternalRail), "ExternalRail");
wallsArray.AddNodeToArray(wallNode);
}

Expand All @@ -193,11 +154,11 @@ public void GenerateTileSubUnits()
{
mTileSubUnits.Clear();

if(mModelType == ModelType.Bridge)
if (mModelType == ModelType.Bridge)
{
foreach (var rail in mBeltRails)
{
mTileSubUnits.Add(TileSubUnits.CreateFromRails(rail, Array.Empty<BGUnitRailSceneObj>(),
mTileSubUnits.Add(TileSubUnits.CreateFromRails(rail, Array.Empty<BGUnitRail>(),
isBridgeModel: true));
}
}
Expand All @@ -211,42 +172,46 @@ public void GenerateTileSubUnits()
}
}

public struct Rail
{
public bool IsClosed;
public List<Vector3?> mPoints;
public bool IsInternal;

public Rail()
{
IsInternal = false;
IsClosed = true;
mPoints = new List<Vector3?>();
}
}

public ModelType mModelType;
public SkinDivision mSkinDivision;

//Editor render objects
internal List<Wall> Walls = new List<Wall>();
internal List<BGUnitRailSceneObj> mBeltRails = new List<BGUnitRailSceneObj>();
internal List<Wall> Walls = [];
internal List<BGUnitRail> mBeltRails = [];

internal List<TileSubUnits> mTileSubUnits = new List<TileSubUnits>();
internal List<TileSubUnits> mTileSubUnits = [];

//Editor toggle
public bool Visible = true;
}

public class Wall
{
internal BGUnitRailSceneObj ExternalRail;
internal List<BGUnitRailSceneObj> InternalRails = new List<BGUnitRailSceneObj>();
internal BGUnitRail ExternalRail;
internal List<BGUnitRail> InternalRails = [];

internal Wall(CourseUnit unit)
{
ExternalRail = new BGUnitRailSceneObj(unit, new CourseUnit.Rail());
ExternalRail = new BGUnitRail(unit);
}
}

public class BGUnitRail(CourseUnit unit)
{
public readonly CourseUnit mCourseUnit = unit;

public List<RailPoint> Points = [];

public bool IsClosed = false;

public bool IsInternal = false;

public class RailPoint(BGUnitRail rail, Vector3 position)
{
public readonly BGUnitRail mRail = rail;
public Vector3 Position = position;
}


}

public class TileSubUnits
Expand All @@ -263,7 +228,7 @@ public enum SlopeType
public readonly InfiniteTileMap mTileMap = new();
public readonly List<(int x, int y, int width, int height, SlopeType type)> mSlopes = [];

internal static TileSubUnits CreateFromRails(BGUnitRailSceneObj mainRail, IReadOnlyList<BGUnitRailSceneObj> internalRails, bool isBridgeModel)
internal static TileSubUnits CreateFromRails(BGUnitRail mainRail, IReadOnlyList<BGUnitRail> internalRails, bool isBridgeModel)
{
TileSubUnits component = new();

Expand Down Expand Up @@ -356,22 +321,22 @@ internal static TileSubUnits CreateFromRails(BGUnitRailSceneObj mainRail, IReadO
int tileX = Math.Min((int)triP0.X, (int)triP1.X);
int tileY = Math.Min((int)triP0.Y, (int)triP1.Y);



if(slope == 0.0f)

if (slope == 0.0f)
{
bridgeTiles.Add((tileX, tileY - 1));
continue;
}

blockedTiles.Add((tileX, tileY));
if(isBridgeModel)
blockedTiles.Add((tileX, tileY));
if (isBridgeModel)
bridgeTiles.Add((tileX, tileY - 1));

if (slopeWidth == 2)
{
blockedTiles.Add((tileX + 1, tileY));
if (isBridgeModel)
if (isBridgeModel)
bridgeTiles.Add((tileX + 1, tileY - 1));
}

Expand Down Expand Up @@ -423,7 +388,7 @@ internal static TileSubUnits CreateFromRails(BGUnitRailSceneObj mainRail, IReadO
return component;
}

foreach (var (x,y) in bridgeTiles)
foreach (var (x, y) in bridgeTiles)
{
component.mTileMap.AddTile(x, y);
}
Expand Down
Loading

0 comments on commit 8da9e18

Please sign in to comment.