8000 Feature/train/rail component utility by toropippi · Pull Request #527 · moorestech/moorestech · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature/train/rail component utility #527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Game.Block.Blocks.TrainRail;
using Game.Block.Interface;
using Game.Train.RailGraph;

namespace Game.Block.Factory.BlockTemplate.Utility
{
public static class RailComponentFactory
{
/// <summary>
/// 指定数のRailComponentを作成し、必要に応じて自動的に接続します。
/// </summary>
public static RailComponent[] CreateRailComponents(int count, BlockPositionInfo positionInfo)
{
var positions = RailComponentUtility.CalculateRailComponentPositions(positionInfo);
var components = new RailComponent[count];

for (int i = 0; i < count; i++)
{
var componentId = new RailComponentID(positionInfo.OriginalPos, i);
components[i] = new RailComponent(positions[i], positionInfo.BlockDirection, componentId);
}

// stationの前と後ろにそれぞれrailComponentがある、自動で接続する
if (count == 2)
{
components[0].ConnectRailComponent(components[1], true, true);
}

return components;
}

/// <summary>
/// RailSaverComponentを生成します。
/// </summary>
public static RailSaverComponent CreateRailSaverComponent(RailComponent[] railComponents)
{
return new RailSaverComponent(railComponents);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using Game.Block.Blocks.TrainRail;
using Game.Block.Interface;
using Game.Block.Interface.Extension;
using Game.Context;
using Game.Train.RailGraph;
using Newtonsoft.Json;
using UnityEngine;

namespace Game.Block.Factory.BlockTemplate.Utility
{
public static class RailComponentUtility
{

static public RailComponent[] RestoreRailComponents(Dictionary<string, string> componentStates,BlockPositionInfo positionInfo)
{
// JSON形式の保存データを取得・復元
string railSaverJson = componentStates[typeof(RailSaverComponent).FullName];
var saverData = JsonConvert.DeserializeObject<RailSaverData> 8000 (railSaverJson);

int count = saverData.Values.Count;
var railComponents = new RailComponent[count];
var railComponentPositions = CalculateRailComponentPositions(positionInfo);

// 各RailComponentを生成
for (int i = 0; i < count; i++)
{
var componentInfo = saverData.Values[i];
railComponents[i] = new RailComponent(railComponentPositions[i], positionInfo.BlockDirection, componentInfo.MyID);
// ベジェ曲線の強度を設定
railComponents[i].UpdateControlPointStrength(componentInfo.BezierStrength);
}

// 接続情報の復元 (Front/Back)
for (int i = 0; i < count; i++)
{
var componentInfo = saverData.Values[i];
var currentComponent = railComponents[i];

// FrontNodeへの接続情報を復元
foreach (var destinationConnection in componentInfo.ConnectMyFrontTo)
{
EstablishConnection(currentComponent, destinationConnection, isFrontSideOfComponent: true);
}
// BackNodeへの接続情報を復元
foreach (var destinationConnection in componentInfo.ConnectMyBackTo)
{
EstablishConnection(currentComponent, destinationConnection, isFrontSideOfComponent: false);
}
}
return railComponents;
}

static public Vector3[] CalculateRailComponentPositions(BlockPositionInfo positionInfo)
{
var blockDirection = positionInfo.BlockDirection;
Vector3 baseOriginPosition = blockDirection.GetBlockBaseOriginPos(positionInfo);
var coordinateConverter = blockDirection.GetCoordinateConvertAction();
Vector3Int blockSize = positionInfo.BlockSize;
Vector3 corner0 = coordinateConverter(new Vector3Int(0, 0, 0));
Vector3 corner1 = coordinateConverter(new Vector3Int(0, 0, blockSize.z - 1));
Vector3 corner2 = coordinateConverter(new Vector3Int(-1, 0, 0));
Vector3 corner3 = coordinateConverter(new Vector3Int(-1, 0, blockSize.z - 1));
Vector3 corner4 = coordinateConverter(new Vector3Int(blockSize.x - 1, 0, 0));
Vector3 corner5 = coordinateConverter(new Vector3Int(blockSize.x - 1, 0, blockSize.z - 1));
Vector3 corner6 = coordinateConverter(new Vector3Int(blockSize.x, 0, 0));
Vector3 corner7 = coordinateConverter(new Vector3Int(blockSize.x, 0, blockSize.z - 1));
Vector3[] componentPositions = new Vector3[2];
componentPositions[0] = (corner0 + corner1 + corner2 + corner3) * 0.25f + baseOriginPosition + new Vector3(0.5f, 0.5f, 0.5f);
componentPositions[1] = (corner4 + corner5 + corner6 + corner7) * 0.25f + baseOriginPosition + new Vector3(0.5f, 0.5f, 0.5f);
return componentPositions;
}

static public void EstablishConnection(RailComponent sourceComponent, ConnectionDestination destinationConnection, bool isFrontSideOfComponent)
{
var destinationComponentId = destinationConnection.DestinationID;
var useFrontSideOfTarget = destinationConnection.IsFront;

var destinationPosition = destinationComponentId.Position;
var componentIndex = destinationComponentId.ID;

// 対象ブロックをワールドから取得
var targetBlock = ServerContext.WorldBlockDatastore.GetBlock(destinationPosition);
if (targetBlock == null) return;

// 対象ブロックがRailSaverComponentを持っているか確認
if (!targetBlock.TryGetComponent<RailSaverComponent>(out var targetRailSaver))
return;

// RailComponents配列から対象のRailComponentを取得
if (componentIndex < 0 || componentIndex >= targetRailSaver.RailComponents.Length)
return;

var targetComponent = targetRailSaver.RailComponents[componentIndex];

// 接続を実施 (既に接続済みの場合、距離が上書きされるだけ)
sourceComponent.ConnectRailComponent(targetComponent, isFrontSideOfComponent, useFrontSideOfTarget);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Game.Block.Interface;
using Game.Train.RailGraph;
using Game.Train.Utility;
using Game.Block.Blocks.TrainRail;
using Game.Block.Interface.Component;
using System;
using Mooresmaster.Model.BlocksModule;

namespace Game.Block.Factory.BlockTemplate.Utility
{
public static class StationComponentFactory
{
/// <summary>
/// StationまたはCargoPlatformコンポーネントを作成し、RailComponentを接続します。
/// </summary>
public static T CreateAndConnectStationComponent<T>(
BlockMasterElement masterElement,
BlockPositionInfo positionInfo,
RailComponent[] railComponents)
where T : IBlockComponent
{
T component = CreateStationComponent<T>(masterElement.BlockParam);

ConnectStationComponents(positionInfo, railComponents[1], railComponents[0]);

return component;
}

/// <summary>
/// T型のStation関連コンポーネントを生成します。
/// </summary>
private static T CreateStationComponent<T>(IBlockParam blockParam) where T : IBlockComponent
{
switch (blockParam)
{
case TrainCargoPlatformBlockParam cargoParam when typeof(T) == typeof(CargoplatformComponent):
return (T)(IBlockComponent)new CargoplatformComponent(cargoParam.PlatformDistance, cargoParam.InputSlotCount, cargoParam.OutputSlotCount);

case TrainStationBlockParam stationParam when typeof(T) == typeof(StationComponent):
return (T)(IBlockComponent)new StationComponent(stationParam.StationDistance, "test", 1);

default:
throw new ArgumentException($"Unsupported blockParam type: {blockParam.GetType()} for component type {typeof(T)}");
}
}

/// <summary>
/// StationのRailComponent接続処理(既存の処理を共通化したもの)
/// stationをつなげて設置した場合に自動でrailComponentを接続するための処理
/// </summary>
private static void ConnectStationComponents(BlockPositionInfo positionInfo, RailComponent frontComponent, RailComponent backComponent)
{
var (frontPos, hasFrontConnection) = StationConnectionChecker.IsStationConnectedToFront(positionInfo);
if (hasFrontConnection)
{
RailComponentUtility.EstablishConnection(frontComponent, new ConnectionDestination(new RailComponentID(frontPos, 0), true), true);
}

var (backPos, hasBackConnection) = StationConnectionChecker.IsStationConnectedToBack(positionInfo);
if (hasBackConnection)
{
RailComponentUtility.EstablishConnection(backComponent, new ConnectionDestination(new RailComponentID(backPos, 1), false), false);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using System.Collections.Generic;
using Game.Block.Blocks;
using Game.Block.Blocks.Chest;
using Game.Block.Blocks.Service;
using Game.Block.Interface;
using Game.Block.Interface.Component;
using Mooresmaster.Model.BlocksModule;
using Game.Block.Blocks.TrainRail;
using Game.Train.RailGraph;
using Game.Train.Utility;
using Newtonsoft.Json;
using Game.Context;
using Game.Block.Interface.Extension;
using UnityEngine;
using Game.Block.Factory.BlockTemplate.Utility;


namespace Game.Block.Factory.BlockTemplate
{
public class VanillaTrainCargoTemplate : IBlockTemplate
{
RailComponent[] railComponents;
/// <summary>
/// 新規にブロック(および対応するRailComponent等)を生成する
/// </summary>
Expand All @@ -25,24 +22,21 @@ public IBlock New(
BlockPositionInfo positionInfo)
{
// 駅ブロックは常に2つのRailComponentを持つ
railComponents = new RailComponent[2];
var railSaverComponent = new RailSaverComponent(railComponents);
var railComponentPositions = VanillaTrainStationTemplate.CalculateRailComponentPositions(positionInfo);
var railComponents = RailComponentFactory.CreateRailComponents(2, positionInfo);// ①ここでは1つのstation内にある2つのRailComponentを直線で接続している
var railSaverComponent = RailComponentFactory.CreateRailSaverComponent(railComponents);
var station = StationComponentFactory.CreateAndConnectStationComponent<CargoplatformComponent>(
masterElement, positionInfo, railComponents
);//②stationをつなげて設置した場合に自動でrailComponentを接続するための処理もここでやってる

// 各RailComponentを生成
for (int i = 0; i < railComponents.Length; i++)
{
var componentId = new RailComponentID(positionInfo.OriginalPos, i);
railComponents[i] = new RailComponent(railComponentPositions[i], positionInfo.BlockDirection, componentId);
}
railComponents[0].ConnectRailComponent(railComponents[1], true, true);
var stationParam = masterElement.BlockParam as TrainCargoPlatformBlockParam;
//var inventoryComponents = CreateInventoryComponents(null, instanceId, stationParam, positionInfo);

var station = GetStation(masterElement, positionInfo);
// 生成したコンポーネントをブロックに登録する
var blockComponents = new List<IBlockComponent>();
blockComponents.Add(railSaverComponent);
blockComponents.AddRange(railComponents);
blockComponents.Add(station);
//blockComponents.AddRange(inventoryComponents);
return new BlockSystem(instanceId, masterElement.BlockGuid, blockComponents, positionInfo);
}

Expand All @@ -52,9 +46,8 @@ public IBlock Load(
BlockInstanceId instanceId,
BlockPositionInfo positionInfo)
{
// 保存されたRailComponent群を復元
railComponents = VanillaTrainStationTemplate.RestoreRailComponents(componentStates, positionInfo);
// 復元したRailComponentを管理するRailSaverComponentを作成
// 保存されたRailComponent群を復元。railSaverComponentからセーブ情報の中にrailcomponent同士の接続情報が含まれているのでそれを復元(これで①1つのstation内にある2つのRailComponentを直線で接続と、②stationをつなげて設置した場合に自動でrailComponentを接続、の両方が満たされる)
var railComponents = RailComponentUtility.RestoreRailComponents(componentStates, positionInfo);
var railSaverComponent = new RailSaverComponent(railComponents);

var stationParam = masterElement.BlockParam as TrainCargoPlatformBlockParam;
Expand All @@ -68,30 +61,28 @@ public IBlock Load(
return new BlockSystem(instanceId, masterElement.BlockGuid, blockComponents, positionInfo);
}

private CargoplatformComponent GetStation(BlockMasterElement masterElement, BlockPositionInfo positionInfo)

/*
/// <summary>
/// インベントリ関連のコンポーネントを作成する
/// </summary>
private List<IBlockComponent> CreateInventoryComponents(Dictionary<string, string> componentStates, BlockInstanceId instanceId, TrainCargoPlatformBlockParam param, BlockPositionInfo blockPositionInfo)
{
var stationParam = masterElement.BlockParam as TrainCargoPlatformBlockParam;
var station = new CargoplatformComponent(stationParam.PlatformDistance, stationParam.InputSlotCount, stationParam.OutputSlotCount);
//進行方向チェック
var (v3, b) = StationConnectionChecker.IsStationConnectedToFront(positionInfo);
if (b == true)
{
//自分の1 frontから相手の0 frontに接続する
var railComponentId = new RailComponentID(v3, 0);
var dst = new ConnectionDestination(railComponentId, true);
VanillaTrainStationTemplate.EstablishConnection(railComponents[1], dst, true);
}
//逆方向チェック
(v3, b) = StationConnectionChecker.IsStationConnectedToBack(positionInfo);
if (b == true)
var inputConnectorComponent = BlockTemplateUtil.CreateInventoryConnector(param.InventoryConnectors, blockPositionInfo);
var inserter = new ConnectingInventoryListPriorityInsertItemService(inputConnectorComponent);

var chestComponent = componentStates == null ?
new VanillaChestComponent(instanceId, param.ItemSlotCount, inserter) :
new VanillaChestComponent(componentStates, instanceId, param.ItemSlotCount, inserter);

return new List<IBlockComponent>
{
//自分の0 backから相手の1 backに接続する
var railComponentId = new RailComponentID(v3, 1);
var dst = new ConnectionDestination(railComponentId, false);
VanillaTrainStationTemplate.EstablishConnection(railComponents[0], dst, false);
}
return station;
chestComponent,
inputConnectorComponent,
};
}
*/

}


Expand Down
Loading
Loading
0