1 Star 1 Fork 0

Vern/综合连锁

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SubsystemChainBlocksBehavior.cs 14.53 KB
一键复制 编辑 原始数据 按行查看 历史
Vern 提交于 2024-09-08 12:20 . 优化一点点细节
using TemplatesDatabase;
using System.Collections.Generic;
using Game;
using Random = Game.Random;
using System;
namespace Chain
{
public class SubsystemChainBlocksBehavior : SubsystemBlockBehavior, IUpdateable
{
public override int[] HandledBlocks => new int[]
{
OakWoodBlock.Index,
BirchWoodBlock.Index,
MimosaWoodBlock.Index,
SpruceWoodBlock.Index,
CoalOreBlock.Index, // 煤矿
CopperOreBlock.Index, // 铜矿
IronOreBlock.Index, // 铁矿
SulphurOreBlock.Index, // 硫矿
DiamondOreBlock.Index, // 钻石矿
GermaniumOreBlock.Index, // 锗矿
SaltpeterOreBlock.Index, // 硝矿
RyeBlock.Index, // 黑麦
CottonBlock.Index, // 棉花
PumpkinBlock.Index, // 南瓜
RottenPumpkinBlock.Index, // 坏的南瓜
};
public UpdateOrder UpdateOrder => UpdateOrder.Default;
public SubsystemTerrain m_subsystemTerrain;
public bool isChainAll;
public bool isChainTress;
public bool isChainOres;
public bool isChainCrops;
public bool isChainLeaves;
public float noDropLeavesProbability;
public int toolLevel = 1;
public Random m_random = new Random();
public override void OnBlockRemoved(int value, int newValue, int x, int y, int z)
{
if (isChainAll)
{
if (isChainTress)
{
// 连锁树木
ChainTrees(value, newValue, x, y, z);
}
if (isChainOres)
{
// 连锁矿石
ChainOres(value, newValue, x, y, z);
}
if (isChainCrops)
{
// 连锁农作物
ChainCrops(value, newValue, x, y, z);
}
ChainOthers(value, newValue, x, y, z);
}
}
public void ChainTrees(int value, int newValue, int x, int y, int z)
{
// 如果不是是树木,则直接返回
if (!(BlocksManager.Blocks[Terrain.ExtractContents(value)] is WoodBlock))
{
return;
}
// 遍历周围的方块
for (int Nx = -1; Nx <= 1; Nx++)
{
for (int Ny = -1; Ny <= 1; Ny++)
{
for (int Nz = -1; Nz <= 1; Nz++)
{
// 如果是树木则执行连锁
int cellValue = m_subsystemTerrain.Terrain.GetCellValue(x + Nx, y + Ny, z + Nz);
if (BlocksManager.Blocks[Terrain.ExtractContents(cellValue)] is WoodBlock)
{
m_subsystemTerrain.DestroyCell(toolLevel, x + Nx, y + Ny, z + Nz, newValue, noDrop: false, noParticleSystem: false);
if (isChainLeaves)
{
ChainLeaves(x + Nx, y + Ny, z + Nz, newValue);
}
}
}
}
}
return;
}
private void ChainLeaves(int x, int y, int z, int newValue)
{
// 遍历周围的方块
for (int Nx = -1; Nx <= 1; Nx++)
{
for (int Ny = -1; Ny <= 1; Ny++)
{
for (int Nz = -1; Nz <= 1; Nz++)
{
// 如果是树木则执行连锁
int cellValue = m_subsystemTerrain.Terrain.GetCellValue(x + Nx, y + Ny, z + Nz);
if (BlocksManager.Blocks[Terrain.ExtractContents(cellValue)] is LeavesBlock)
{
bool noDrop = false;
if (m_random.Bool(noDropLeavesProbability))
{
noDrop = true;
}
m_subsystemTerrain.DestroyCell(toolLevel, x + Nx, y + Ny, z + Nz, newValue, noDrop: noDrop, noParticleSystem: true);
}
}
}
}
}
public void ChainOres(int value, int newValue, int x, int y, int z)
{
List<int> ores = new List<int>
{
CoalOreBlock.Index, // 煤矿
CopperOreBlock.Index, // 铜矿
IronOreBlock.Index, // 铁矿
SulphurOreBlock.Index, // 硫矿
DiamondOreBlock.Index, // 钻石矿
GermaniumOreBlock.Index, // 锗矿
SaltpeterOreBlock.Index, // 硝矿
};
for (int i = 0; i < ores.Count; i++)
{
// 如果不是是对应的矿石,则直接返回
if (!(Terrain.ExtractContents(value) == ores[i]))
{
if (i == ores.Count)
{
return;
}
continue;
}
// 遍历周围的方块
for (int Nx = -1; Nx <= 1; Nx++)
{
for (int Ny = -1; Ny <= 1; Ny++)
{
for (int Nz = -1; Nz <= 1; Nz++)
{
// 如果是对应的矿石则执行连锁
int cellValue = m_subsystemTerrain.Terrain.GetCellValue(x + Nx, y + Ny, z + Nz);
if (Terrain.ExtractContents(cellValue) == ores[i])
{
m_subsystemTerrain.DestroyCell(toolLevel, x + Nx, y + Ny, z + Nz, newValue, noDrop: false, noParticleSystem: false);
//m_subsystemSoundMaterials.PlayImpactSound(cellValue, new Vector3(x + Nx, y + Ny, z + Nz), 2f);
}
}
}
}
}
}
public void ChainCrops(int value, int newValue, int x, int y, int z)
{
List<int> crops = new List<int>
{
RyeBlock.Index, // 黑麦
CottonBlock.Index, // 棉花
PumpkinBlock.Index, // 南瓜
RottenPumpkinBlock.Index, // 坏的南瓜
};
for (int i = 0; i < crops.Count; i++)
{
// 如果不是是对应的农作物,则直接返回
if (!(Terrain.ExtractContents(value) == crops[i]))
{
continue;
}
bool flag = true;
switch (crops[i])
{
case RyeBlock.Index:
flag = CheckRye(value, 0, 1);
break;
case CottonBlock.Index:
flag = CheckCotton(value, 0, 1);
break;
case PumpkinBlock.Index:
case RottenPumpkinBlock.Index:
flag = CheckPumpkin(value, 0, 1);
break;
}
if (flag)
{
// 遍历周围的方块
for (int Nx = -1; Nx <= 1; Nx++)
{
for (int Nz = -1; Nz <= 1; Nz++)
{
// 如果是对应的农作物则执行连锁
int cellValue = m_subsystemTerrain.Terrain.GetCellValue(x + Nx, y, z + Nz);
if (Terrain.ExtractContents(cellValue) == crops[i])
{
switch (crops[i])
{
case RyeBlock.Index:
flag = CheckRye(value, cellValue, 2);
break;
case CottonBlock.Index:
flag = CheckCotton(value, cellValue, 2);
break;
case PumpkinBlock.Index:
case RottenPumpkinBlock.Index:
flag = CheckPumpkin(value, cellValue, 2);
break;
}
if (flag)
{
m_subsystemTerrain.DestroyCell(toolLevel, x + Nx, y, z + Nz, newValue, noDrop: false, noParticleSystem: false);
}
}
}
}
}
}
}
public bool CheckRye(int value, int cellValue, int stage)
{
int data = Terrain.ExtractData(value);
int size = RyeBlock.GetSize(data);
if (stage == 1)
{
if (!(RyeBlock.GetIsWild(data) || size == 7))
{
return false;
}
}
else if (stage == 2)
{
int data2 = Terrain.ExtractData(cellValue);
if (!(RyeBlock.GetSize(data2) == 7 || RyeBlock.GetIsWild(data2)))
{
return false;
}
}
return true;
}
public bool CheckCotton(int value, int cellValue, int stage)
{
int data = Terrain.ExtractData(value);
int size = CottonBlock.GetSize(data);
if (stage == 1)
{
if (!(CottonBlock.GetIsWild(data) || size == 2))
{
return false;
}
}
else if (stage == 2)
{
int data2 = Terrain.ExtractData(cellValue);
if (!(CottonBlock.GetSize(data2) == 2 || CottonBlock.GetIsWild(data2)))
{
return false;
}
}
return true;
}
public bool CheckPumpkin(int value, int cellValue, int stage)
{
int data = Terrain.ExtractData(value);
int size = BasePumpkinBlock.GetSize(data);
if (stage == 1)
{
if (!(BasePumpkinBlock.GetSize(data) >= 7 || Terrain.ExtractContents(value) == RottenPumpkinBlock.Index))
{
return false;
}
}
else if (stage == 2)
{
int data2 = Terrain.ExtractData(cellValue);
if (!(BasePumpkinBlock.GetSize(data2) >= 7 || Terrain.ExtractContents(cellValue) == RottenPumpkinBlock.Index))
{
return false;
}
}
return true;
}
public void ChainOthers(int value, int newValue, int x, int y, int z)
{
List<int> chainBlocksList = ChainManager.ChainBlockValues;
if (chainBlocksList == null)
{
return;
}
for (int i = 0; i < chainBlocksList.Count; i++)
{
if (!(value == chainBlocksList[i]))
{
if (i == chainBlocksList.Count)
{
return;
}
continue;
}
for (int Nx = -1; Nx <= 1; Nx++)
{
for (int Ny = -1; Ny <= 1; Ny++)
{
for (int Nz = -1; Nz <= 1; Nz++)
{
int cellValue = m_subsystemTerrain.Terrain.GetCellValue(x + Nx, y + Ny, z + Nz);
if (Terrain.ExtractContents(cellValue) == chainBlocksList[i])
{
m_subsystemTerrain.DestroyCell(toolLevel, x + Nx, y + Ny, z + Nz, newValue, noDrop: false, noParticleSystem: false);
}
}
}
}
}
}
public override void Load(ValuesDictionary valuesDictionary)
{
m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
isChainTress = valuesDictionary.GetValue<bool>("Trees");
isChainOres = valuesDictionary.GetValue<bool>("Ores");
isChainCrops = valuesDictionary.GetValue<bool>("Crops");
isChainLeaves = valuesDictionary.GetValue<bool>("Leaves");
noDropLeavesProbability = valuesDictionary.GetValue<float>("NoDropOfLeavesProbability");
isChainAll = valuesDictionary.GetValue<bool>("ChainAll");
string strChainBlocks = valuesDictionary.GetValue<string>("ChainBlocks");
if (strChainBlocks != string.Empty)
{
string[] parts = strChainBlocks.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
int value = int.Parse(part);
if (ChainManager.ChainBlockValues.Contains(value))
{
continue;
}
ChainManager.ChainBlockValues.Add(value);
}
}
}
public override void Save(ValuesDictionary valuesDictionary)
{
string strChainBlocks = "";
foreach (int value in ChainManager.ChainBlockValues)
{
strChainBlocks += (value.ToString() + ";");
}
valuesDictionary.SetValue("ChainBlocks", strChainBlocks);
}
public void Update(float dt)
{
SubsystemBlockBehaviors subsystemBlockBehaviors = Project.FindSubsystem<SubsystemBlockBehaviors>(true);
if (ChainManager.ChainBlockValues != null)
{
foreach (int index in ChainManager.ChainBlockValues)
{
List<SubsystemBlockBehavior> behaviors = new List<SubsystemBlockBehavior>();
string[] array = BlocksManager.Blocks[index].Behaviors.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string text in array)
{
SubsystemBlockBehavior item = base.Project.FindSubsystem<SubsystemBlockBehavior>(text.Trim(), throwOnError: true);
behaviors.Add(item);
}
subsystemBlockBehaviors.m_blockBehaviorsByContents[index] = behaviors.ToArray();
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vern05/integrated-chain.git
git@gitee.com:vern05/integrated-chain.git
vern05
integrated-chain
综合连锁
master

搜索帮助