```
/*
==== MEA 方块基类 v2.0 ====
作者: lilimin
QQ: 461353919
MEA QQ群: 530683714
创作时间: 2019年12月14日 18:43:21
==== 关于游戏IngameAPI更新的总结 ====
1、在编程块运行类Program中,即当下文本全局挂载了一个IGC对象,类型IMyIntergridCommunicationSystem,用于处理类似天线通讯。并且天线通讯时,可以传输任意类型数据,传输方法使用的是泛型。
2、在编程块中,挂载了一个string Storage,它基于编程块这个方块进行保存,在代码修改保存后依然保留原有的值。
2、将所有显示屏独立为一个类,主控座椅、LCD上的显示屏全部独立为这个类。
==== 主要更新说明 ====
总览:这是一次大更新,之前版本的方块基类已经废了一部分,新版的方块基类会更适合开发新的项目。方块基类明确一个宗旨:让开发者以最方便的方式操作方块。
1、将所有类放入一个大的MEA类中,相当于加上一层命名空间,防止与其他脚本名称冲突
2、按照2019年4月左右的最新InGame API进行更新,修复了之前K社更新游戏导致的大量问题
3、在MEA类中定义两个静态属性(PG和GTS),分别代表编程块和GridTerminalSystem,用于类中调用它来获取方块或使用Echo等方法。
因此在使用MEA类之前,必须赋值,MEA.PG = this; MEA.GTS = GridTerminalSystem;
*/
void Main(string arguments)
{
}
Program()
{
MEA.GTS = GridTerminalSystem;
MEA.PG = this;
Runtime.UpdateFrequency = UpdateFrequency.Update1;
}
public class MEA
{
public static IMyGridTerminalSystem GTS;
public static Program PG;
/// <summary>
/// 单一方块类
/// </summary>
public class Block
{
// == 构造方法
public Block() { }
public Block(Block b) { _block = b._block; }
public Block(string name) { _block = GTS.GetBlockWithName(name); }
public Block(IMyTerminalBlock block) { _block = block; }
// == 成员属性
public IMyTerminalBlock _block; //方块
// 唯一ID
public long Id { get { return _block.EntityId; } }
//方块是否获取成功,当获取方块失败时,返回False
public bool IsOK { get { return _block != null; } }
//是否启用
public bool OnOff
{
get { return _block is IMyFunctionalBlock ? (_block as IMyFunctionalBlock).Enabled : false; }
set { if (_block is IMyFunctionalBlock) { (_block as IMyFunctionalBlock).Enabled = value; } }
}
//是否被黑了
public bool IsBeingHacked { get { return _block.IsBeingHacked; } }
//是否可以工作(健康度)
public bool IsFunctional { get { return _block.IsFunctional; } }
//是否在工作
public bool IsWorking { get { return _block.IsWorking; } }
//在网格中的顺序
public int NumberInGrid { get { return _block.NumberInGrid; } }
//所属网格
public IMyCubeGrid Grid { get { return _block.CubeGrid; } }
//名称(K表中名称,CustomName)
public string Name
{
get { return _block.CustomName; }
set { _block.CustomName = value; }
}
//自定义数据 (CustomData)
public string Data
{
get { return _block.CustomData; }
set { _block.CustomData = value; }
}
//所有人ID
public long OwnerId { get { return _block.OwnerId; } }
//坐标(基于世界绝对坐标系的坐标)
public Vector3D Position { get { return _block.GetPosition(); } }
//矩阵(描述方块世界坐标几方位的矩阵)
public Matrix WorldMatrix { get { return _block.WorldMatrix; } }
//前向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Forward { get { return _block.WorldMatrix.Forward; } }
//后向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Backward { get { return _block.WorldMatrix.Backward; } }
//左向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Left { get { return _block.WorldMatrix.Left; } }
//右向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Right { get { return _block.WorldMatrix.Right; } }
//上向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Up { get { return _block.WorldMatrix.Up; } }
//下向矢量(归一化的,即矢量的长度恒等于1)
public Vector3D Down { get { return _block.WorldMatrix.Down; } }
//是否在终端显示
public bool ShowInTerminal { get { return _block.ShowInTerminal; } set { _block.ShowInTerminal = value; } }
//是否在库存中显示
public bool ShowInInventory { get { return _block.ShowInInventory; } set { _block.ShowInInventory = value; } }
//是否在HUD上显示
public bool ShowOnHUD { get { return _block.ShowOnHUD; } set { _block.ShowOnHUD = value; } }
}
/// <summary>
/// 多个方块类
/// </summary>
public class Blocks : IEnumerator
{
// == 构造方法
public Blocks() { }
public Blocks(string name, Func<IMyTerminalBlock, bool> collect = null)
{
List<IMyTerminalBlock> search_blocks = new List<IMyTerminalBlock>();
GTS.SearchBlocksOfName(name, search_blocks, collect);
foreach (IMyTerminalBlock b in search_blocks)
{
_blocks.Add(new Block(b));
}
}
public Blocks(List<Block> blocks)
{
foreach (Block b in blocks)
{
_blocks.Add(b);
}
}
public Blocks(List<IMyTerminalBlock> blocks)
{
foreach (IMyTerminalBlock b in blocks)
{
_blocks.Add(new Block(b));
}
}
// == 属性 ==
public List<Block> _blocks = new List<Block>(); // 方块List
public int _index = -1; // 当前指针下标
public int Count
{ // 方块个数
get
{
return _blocks.Count;
}
}
// == 实现数组或List访问
public Block this[int index]
{
get
{
try
{
return _blocks[index];
}
catch (Exception e)
{
throw e;
}
}
}
// == 实现IEnumerator接口的方法,目的是可以在foreach中使用
public bool MoveNext() { _index++; return (_index < _blocks.Count); }
public void Reset() { _index = 0; }
object IEnumerator.Current
{
get
{
try
{
return _blocks[_index];
}
catch (Exception e)
{
throw e;
}
}
}
public IEnumerator GetEnumerator()
{
return this;
}
// 实现IEnumerator接口完成
// == 模拟实现Linq相关方法
/// <summary>
/// 筛选
/// </summary>
/// <param name="collect"></param>
public Blocks Where(Func<Block, bool> collect)
{
Blocks blocks = new Blocks();
foreach (Block b in _blocks)
{
if (collect(b))
{
blocks.Add(b);
}
}
return blocks;
}
/// <summary>
/// 查找
/// </summary>
/// <param name="collect"></param>
public Block Find(Func<Block, bool> collect)
{
foreach (Block b in _blocks)
{
if (collect(b)) return b;
}
return null;
}
/// <summary>
/// 添加一个方块
/// </summary>
/// <param name="b"></param>
public void Add(Block b)
{
_blocks.Add(b);
}
// 移除一个方块
public bool Remove(Block b)
{
_blocks.Remove(b); return true;
}
public bool Remove(int index)
{
if (index >= 0 && index <= _blocks.Count - 1)
{
return Remove(_blocks[index]);
}
return false;
}
/// <summary>
/// 转为List
/// </summary>
public List<Block> ToList()
{
List<Block> l = new List<Block>();
foreach (Block b in _blocks)
{
l.Add(b);
}
return l;
}
}
/// <summary>
/// 显示屏幕类(LCD或Cockpit中的屏幕)
/// </summary>
public class DisplayScreen
{
// == 构造方法
public DisplayScreen(IMyTextSurface s) { _screen = s; }
// == 属性
// 屏幕对象
public IMyTextSurface _screen;
// 字体大小
public double FontSize
{
get { return (double)_screen.FontSize; }
set { _screen.FontSize = (float)value; }
}
// 文字颜色
public Color FontColor
{
get { return _screen.FontColor; }
set { _screen.FontColor = value; }
}
// 背景颜色
public Color BackgroundColor
{
get { return _screen.BackgroundColor; }
set { _screen.BackgroundColor = value; }
}
// 背景透明度(类似int,范围0-255)
public byte BackgroundAlpha
{
get { return _screen.BackgroundAlpha; }
set { _screen.BackgroundAlpha = value; }
}
// 脚本背景颜色
public Color ScriptBackgroundColor
{
get { return _screen.ScriptBackgroundColor; }
set { _screen.ScriptBackgroundColor = value; }
}
// 脚本前景颜色
public Color ScriptForegroundColor
{
get { return _screen.ScriptForegroundColor; }
set { _screen.ScriptForegroundColor = value; }
}
// 切换时间间隔
public double ChangeInterval
{
get { return (double)_screen.ChangeInterval; }
set { _screen.ChangeInterval = (float)value; }
}
// 对齐方式(LEFT=0、RIGHT=1、CENTER=2)
public TextAlignment Alignment
{
get { return _screen.Alignment; }
set { _screen.Alignment = value; }
}
// 当前脚本
public string Script
{
get { return _screen.Script; }
set { _screen.Script = value; }
}
// 显示类型
/*
NONE = 0,
TEXT_AND_IMAGE = 1,
[Obsolete("Use TEXT_AND_IMAGE instead.")]
IMAGE = 2,
SCRIPT = 3
*/
public VRage.Game.GUI.TextPanel.ContentType ContentType
{
get { return _screen.ContentType; }
set { _screen.ContentType = value; }
}
// 像素尺寸
public Vector2I SurfaceSize
{
get { return new Vector2I((int)_screen.SurfaceSize.X, (int)_screen.SurfaceSize.Y); }
}
// 图像尺寸
public Vector2I TextureSize
{
get { return new Vector2I((int)_screen.TextureSize.X, (int)_screen.TextureSize.Y); }
}
// 保持长宽比
public bool PreserveAspectRatio
{
get { return _screen.PreserveAspectRatio; }
set { _screen.PreserveAspectRatio = value; }
}
// 当前图像
public string CurrentlyShownImage
{
get { return _screen.CurrentlyShownImage; }
}
// 字体
public string Font
{
get { return this._screen.Font; }
set { this._screen.Font = value; }
}
// 文字内边距
public double TextPadding
{
get { return (double)_screen.TextPadding; }
set { _screen.TextPadding = (float)value; }
}
// 文字内容
public string Text
{
get { return _screen.GetText(); }
set { _screen.WriteText(value); }
}
// 屏幕名称
public string Name
{
get { return _screen.Name; }
}
// 屏幕显示名称
public string DisplayName
{
get { return _screen.DisplayName; }
}
// == 方法
// 获取所有字体列表
public void GetFonts(List<string> fonts) { _screen.GetFonts(fonts); }
// 获取所有脚本列表
public void GetScripts(List<string> scripts) { _screen.GetScripts(scripts); }
// 获取选中的图像列表
public void GetSelectedImages(List<string> output) { _screen.GetSelectedImages(output); }
}
/// <summary>
/// LCD面板类
/// 依赖Block类、DisplayScreen类
/// </summary>
public class LCD : Block
{
// == 构造方法
public LCD() { }
public LCD(string name) : base(name) { _lcd = _block as IMyTextPanel; Screen = new DisplayScreen(_lcd as IMyTextSurface); }
public LCD(IMyTextPanel block) : base(block) { _lcd = _block as IMyTextPanel; }
public LCD(LCD block) : base(block) { _lcd = _block as IMyTextPanel; }
// LCD方块
public IMyTextPanel _lcd;
public DisplayScreen Screen;
// K表上的标题
public string Title
{
get { return _lcd.GetPublicTitle(); }
set { _lcd.WritePublicTitle(value); }
}
}
/// <summary>
/// 主控座椅块
/// 依赖Block类、DisplayScreen类
/// </summary>
public class Cockpit : Block
{
// == 构造方法
public Cockpit() { }
public Cockpit(string name) : base(name)
{
if (IsOK) _cockpit = _block as IMyCockpit;
_initScreens();
}
public Cockpit(IMyTerminalBlock b) : base(b)
{
if (IsOK) _cockpit = _block as IMyCockpit;
_initScreens();
}
protected void _initScreens()
{
List<DisplayScreen> ss = new List<DisplayScreen>();
for (int i = 0; i < _cockpit.SurfaceCount; i++)
{
ss.Add(new DisplayScreen(_cockpit.GetSurface(i)));
}
Screens = ss;
}
// == 属性
//主控方块
public IMyCockpit _cockpit;
//屏幕
public List<DisplayScreen> Screens = new List<DisplayScreen>();
//是否可以控制飞船
public bool CanControlShip
{
get { return _cockpit.CanControlShip; }
}
//是否正在被玩家控制
public bool IsUnderControl
{
get { return _cockpit.IsUnderControl; }
}
//是否有轮子
public bool HasWheels
{
get { return _cockpit.HasWheels; }
}
//是否勾选了可以控制轮子
public bool ControlWheels
{
get { return _cockpit.ControlWheels; }
set { _cockpit.ControlWheels = value; }
}
//是否勾选了可以控制推进器
public bool ControlThrusters
{
get { return _cockpit.ControlThrusters; }
set { _cockpit.ControlThrusters = value; }
}
//是否勾选了手刹
public bool HandBrake
{
get { return _cockpit.HandBrake; }
set { _cockpit.HandBrake = value; }
}
//是否勾选了惯性抑制
public bool DampenersOverride
{
get { return _cockpit.DampenersOverride; }
set { _cockpit.DampenersOverride = value; }
}
//是否是主控
public bool IsMainCockpit
{
get { return _cockpit.IsMainCockpit; }
set { _cockpit.IsMainCockpit = value; }
}
//是否勾选了显示地平线高度
public bool ShowHorizonIndicator
{
get { return _cockpit.ShowHorizonIndicator; }
set { _cockpit.ShowHorizonIndicator = value; }
}
//按键输入,X表示AD,Y表示空格和C,Z表示SW
public Vector3D InputKey
{
get
{
Vector3D temp = new Vector3D();
Vector3D.TryParse(_cockpit.MoveIndicator.ToString(), out temp);
return temp;
}
}
//QE按键输入
public double InputRoll
{
get { return (double)_cockpit.RollIndicator; }
}
//鼠标输入,X表示左右,Y表示上下
public Vector2D InputMouse
{
get
{
return new Vector2D((double)_cockpit.RotationIndicator.X, (double)_cockpit.RotationIndicator.Y);
}
}
//质心(不考虑活塞转子等外挂部分)
public Vector3D CenterOfMass
{
get { return _cockpit.CenterOfMass; }
}
//飞船基础质量
public double BaseMass
{
get { return _cockpit.CalculateShipMass().BaseMass; }
}
//飞船总体质量
public double TotalMass
{
get { return _cockpit.CalculateShipMass().TotalMass; }
}
//飞船物理质量
public double PhysicalMass
{
get { return _cockpit.CalculateShipMass().PhysicalMass; }
}
//人工重力
public Vector3D ArtificialGravity
{
get { return _cockpit.GetArtificialGravity(); }
}
//自然重力
public Vector3D NaturalGravity
{
get { return _cockpit.GetNaturalGravity(); }
}
//飞船线速度矢量
public Vector3D Velocity
{
get { return _cockpit.GetShipVelocities().LinearVelocity; }
}
//飞船角速度矢量
public Vector3D AngleVelocity
{
get { return _cockpit.GetShipVelocities().AngularVelocity; }
}
//飞船偏航Yaw速度(基于自己的坐标系,单位角度)
public double YawVelocity
{
get
{
MatrixD refLookAtMatrix = MatrixD.CreateLookAt(new Vector3D(), WorldMatrix.Forward, WorldMatrix.Up);
//求出自己当前角速度相对自己的值,这是一套弧度值,其中最大值是0.45,最小值是0.0005,x表示俯仰Pitch(上+下-),y表示偏航Yaw(左+右-),z表示滚转(顺时针-逆时针+)
Vector3D MeAngleVelocityToMe = Vector3D.TransformNormal(AngleVelocity, refLookAtMatrix);
return MeAngleVelocityToMe.Y * 180 / Math.PI;
}
}
//飞船俯仰Pitch速度(基于自己的坐标系,单位角度)
public double PitchVelocity
{
get
{
MatrixD refLookAtMatrix = MatrixD.CreateLookAt(new Vector3D(), WorldMatrix.Forward, WorldMatrix.Up);
//求出自己当前角速度相对自己的值,这是一套弧度值,其中最大值是0.45,最小值是0.0005,x表示俯仰Pitch(上+下-),y表示偏航Yaw(左+右-),z表示滚转(顺时针-逆时针+)
Vector3D MeAngleVelocityToMe = Vector3D.TransformNormal(AngleVelocity, refLookAtMatrix);
return MeAngleVelocityToMe.X * 180 / Math.PI;
}
}
//飞船滚转Roll速度(基于自己的坐标系,单位角度)
public double RollVelocity
{
get
{
MatrixD refLookAtMatrix = MatrixD.CreateLookAt(new Vector3D(), WorldMatrix.Forward, WorldMatrix.Up);
//求出自己当前角速度相对自己的值,这是一套弧度值,其中最大值是0.45,最小值是0.0005,x表示俯仰Pitch(上+下-),y表示偏航Yaw(左+右-),z表示滚转(顺时针-逆时针+)
Vector3D MeAngleVelocityToMe = Vector3D.TransformNormal(AngleVelocity, refLookAtMatrix);
return MeAngleVelocityToMe.Z * 180 / Math.PI;
}
}
//是否在星球上
public bool IsInPlanet
{
get
{
Vector3D temp = new Vector3D();
return _cockpit.TryGetPlanetPosition(out temp);
}
}
//所处的星球坐标(处于星球状态中才能获取)
public Vector3D PlanetPosition
{
get
{
Vector3D temp = new Vector3D();
_cockpit.TryGetPlanetPosition(out temp);
return temp;
}
}
//所处的星球海平面高度(处于星球状态中才能获取)
public double PlanetElevationSealevel
{
get
{
double e = 0;
_cockpit.TryGetPlanetElevation(MyPlanetElevation.Sealevel, out e);
return e;
}
}
//所处的星球地表高度(处于星球状态中才能获取)
public double PlanetElevationSurface
{
get
{
double e = 0;
_cockpit.TryGetPlanetElevation(MyPlanetElevation.Surface, out e);
return e;
}
}
//氧气容量
public double OxygenCapacity
{
get { return (double)_cockpit.OxygenCapacity; }
}
//氧气填充率
public double OxygenFilledRatio
{
get { return (double)_cockpit.OxygenFilledRatio; }
}
}
/// <summary>
/// 推进器
/// 依赖Block类、BlockDirections类
/// </summary>
public class Thrust : Block
{
// == 构造方法
public Thrust(IMyTerminalBlock b) : base(b) { _thrust = _block as IMyThrust; }
public Thrust(Block b) : base(b) { _thrust = _block as IMyThrust; }
public Thrust(string name) : base(name) { _thrust = _block as IMyThrust; }
// == 成员属性
public IMyThrust _thrust;
//越级出力值(单位:百分比)
public double Power
{
get { return _thrust.ThrustOverridePercentage; }
set { _thrust.ThrustOverridePercentage = (float)value; }
}
//越级出力值(单位:牛顿)
public double PowerN
{
get { return _thrust.ThrustOverride; }
set { _thrust.ThrustOverride = (float)value; }
}
//最大出力值(单位:牛顿)
public double MaxThrust
{
get { return _thrust.MaxThrust; }
}
//最大实际出力值(单位:牛顿)
public double MaxEffectiveThrust
{
get { return _thrust.MaxEffectiveThrust; }
}
//当前出力值(单位:牛顿)
public double CurrentThrust
{
get { return _thrust.CurrentThrust; }
}
//基于网格方向的相对方向,相当于安装方向
public Vector3I GridThrustDirection
{
get { return _thrust.GridThrustDirection; }
}
}
/// <summary>
/// 方块方向枚举
/// </summary>
public enum BlockDirections
{
All = 0, Up = 1, Down = 2, Forward = 3, Backward = 4, Left = 5, Right = 6
}
/// <summary>
/// 推进器组
/// 依赖Thrust类,Blocks类
/// </summary>
public class Thrusts : Blocks, IEnumerator
{
// == 构造方法
public Thrusts() : base() { InitThrusts(); }
public Thrusts(string name, Func<IMyTerminalBlock, bool> collect = null) : base(name, collect) { InitThrusts(); }
public Thrusts(List<Block> blocks) : base(blocks) { InitThrusts(); }
public Thrusts(List<IMyTerminalBlock> blocks) : base(blocks) { InitThrusts(); }
public void InitThrusts()
{
_thrusts = new List<Thrust>();
foreach (Block b in _blocks)
{
_thrusts.Add(new Thrust(b));
}
}
// == 属性
// 方向识别用的方块
public Block DirectionBlock;
// 多个推进器
public List<Thrust> _thrusts;
// 推进器方向
public List<BlockDirections> _fields;
// == 方法
// 初始化方位
public void InitDirection(Block cp = null)
{
if (cp != null) DirectionBlock = cp;
_fields = new List<BlockDirections>();
for (int i = 0; i < _thrusts.Count; i++)
{
Base6Directions.Direction CockpitForward = _thrusts[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Forward);
Base6Directions.Direction CockpitUp = _thrusts[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Up);
Base6Directions.Direction CockpitLeft = _thrusts[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Left);
switch (CockpitForward)
{ case Base6Directions.Direction.Forward: _fields.Add(BlockDirections.Forward); break; case Base6Directions.Direction.Backward: _fields.Add(BlockDirections.Backward); break; }
switch (CockpitUp)
{ case Base6Directions.Direction.Forward: _fields.Add(BlockDirections.Up); break; case Base6Directions.Direction.Backward: _fields.Add(BlockDirections.Down); break; }
switch (CockpitLeft)
{ case Base6Directions.Direction.Forward: _fields.Add(BlockDirections.Left); break; case Base6Directions.Direction.Backward: _fields.Add(BlockDirections.Right); break; }
}
}
// 实现接口
object IEnumerator.Current
{
get
{
try
{
return _thrusts[_index];
}
catch (Exception e)
{
throw e;
}
}
}
// 添加一个推进器
public void Add(Thrust t)
{
base.Add(t);
_thrusts.Add(t);
InitDirection();
}
// 移除一个推进器
public bool Remove(Thrust b)
{
base.Remove(b);
_thrusts.Remove(b);
InitDirection();
return true;
}
public bool Remove(int index, bool over = false)
{
base.Remove(index);
if (index >= 0 && index <= _thrusts.Count - 1)
{
_thrusts.Remove(_thrusts[index]);
}
return false;
}
// 遍历所有推进器
public void Each(Func<Thrust, bool> call = null)
{
if (call == null) return;
foreach (Thrust t in _thrusts)
{
call(t);
}
}
// 设置出力值
public void SetValue(double value, BlockDirections direction = 0, bool resetOthers = false, bool isPercent = true)
{
for (int i = 0; i < _thrusts.Count; i++)
{
if (direction == BlockDirections.All || _fields[i] == direction)
{
if (isPercent) { _thrusts[i].Power = value; }
else { _thrusts[i].PowerN = value; }
}
}
}
// 设置方块开关
public void SetOnOff(bool onoff)
{
Each(t => t.OnOff = onoff);
}
}
/// <summary>
/// 陀螺仪
/// 依赖Block类
/// </summary>
public class Gyro : Block
{
// == 静态属性
public static double MaxMotionValue = 30; //最大出力值
// == 构造方法
public Gyro(IMyTerminalBlock b) : base(b) { _gyro = _block as IMyGyro; }
public Gyro(Block b) : base(b) { _gyro = _block as IMyGyro; }
public Gyro(string name) : base(name) { _gyro = _block as IMyGyro; }
// == 成员属性
public IMyGyro _gyro; //陀螺仪方块本体
public int MotionRatio = 1; //正反系数
//能量
public double Pwoer
{
get { return _gyro.GyroPower; }
set { _gyro.GyroPower = (float)value; }
}
//是否开启越级
public bool IsOverride
{
get { return _gyro.GyroOverride; }
set { _gyro.GyroOverride = value; }
}
//偏航值,开启越级后有效
public double Yaw
{
get { return _gyro.Yaw * MotionRatio; }
set { _gyro.Yaw = (float)value * MotionRatio; }
}
//俯仰值,开启越级后有效
public double Pitch
{
get { return _gyro.Pitch * MotionRatio; }
set { _gyro.Pitch = (float)value * MotionRatio; }
}
//滚转值,开启越级后有效
public double Roll
{
get { return _gyro.Roll * MotionRatio; }
set { _gyro.Roll = (float)value * MotionRatio; }
}
}
/// <summary>
/// 陀螺仪组
/// 依赖Gyro类、Blocks类
/// </summary>
public class Gyros : Blocks, IEnumerator
{
// == 构造方法
public Gyros() : base() { InitGyros(); }
public Gyros(string name, Func<IMyTerminalBlock, bool> collect = null) : base(name, collect) { InitGyros(); }
public Gyros(List<Block> blocks) : base(blocks) { InitGyros(); }
public Gyros(List<IMyTerminalBlock> blocks) : base(blocks) { InitGyros(); }
// == 属性
// 方向识别用的方块
public Block DirectionBlock;
public List<Gyro> _gyros;
public List<string> _yawField;
public List<string> _pitchField;
public List<string> _rollField;
public List<double> _yawFactor;
public List<double> _pitchFactor;
public List<double> _rollFactor;
// == 方法
public void InitGyros()
{
_gyros = new List<Gyro>();
foreach (Block b in _blocks)
{
_gyros.Add(new Gyro(b));
}
}
// 初始化方向
public void InitDirection(Block b)
{
DirectionBlock = b;
_yawField = new List<string>();
_pitchField = new List<string>();
_rollField = new List<string>();
_yawFactor = new List<double>();
_pitchFactor = new List<double>();
_rollFactor = new List<double>();
for (int i = 0; i < _gyros.Count; i++)
{
Base6Directions.Direction gyroUp = _gyros[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Up);
Base6Directions.Direction gyroLeft = _gyros[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Left);
Base6Directions.Direction gyroForward = _gyros[i].WorldMatrix.GetClosestDirection(DirectionBlock.WorldMatrix.Forward);
switch (gyroUp)
{
case Base6Directions.Direction.Up: _yawField.Add("Yaw"); _yawFactor.Add(1); break;
case Base6Directions.Direction.Down: _yawField.Add("Yaw"); _yawFactor.Add(-1); break;
case Base6Directions.Direction.Left: _yawField.Add("Pitch"); _yawFactor.Add(1); break;
case Base6Directions.Direction.Right: _yawField.Add("Pitch"); _yawFactor.Add(-1); break;
case Base6Directions.Direction.Forward: _yawField.Add("Roll"); _yawFactor.Add(-1); break;
case Base6Directions.Direction.Backward: _yawField.Add("Roll"); _yawFactor.Add(1); break;
}
switch (gyroLeft)
{
case Base6Directions.Direction.Up: _pitchField.Add("Yaw"); _pitchFactor.Add(1); break;
case Base6Directions.Direction.Down: _pitchField.Add("Yaw"); _pitchFactor.Add(-1); break;
case Base6Directions.Direction.Left: _pitchField.Add("Pitch"); _pitchFactor.Add(1); break;
case Base6Directions.Direction.Right: _pitchField.Add("Pitch"); _pitchFactor.Add(-1); break;
case Base6Directions.Direction.Forward: _pitchField.Add("Roll"); _pitchFactor.Add(-1); break;
case Base6Directions.Direction.Backward: _pitchField.Add("Roll"); _pitchFactor.Add(1); break;
}
switch (gyroForward)
{
case Base6Directions.Direction.Up: _rollField.Add("Yaw"); _rollFactor.Add(1); break;
case Base6Directions.Direction.Down: _rollField.Add("Yaw"); _rollFactor.Add(-1); break;
case Base6Directions.Direction.Left: _rollField.Add("Pitch"); _rollFactor.Add(1); break;
case Base6Directions.Direction.Right: _rollField.Add("Pitch"); _rollFactor.Add(-1); break;
case Base6Directions.Direction.Forward: _rollField.Add("Roll"); _rollFactor.Add(-1); break;
case Base6Directions.Direction.Backward: _rollField.Add("Roll"); _rollFactor.Add(1); break;
}
}
}
// 设置越级值
public void SetValue(double yaw = 0, double pitch = 0, double roll = 0)
{
for (int i = 0; i < _gyros.Count; i++)
{
_gyros[i]._gyro.SetValue(_yawField[i], (float)yaw * (float)_yawFactor[i]);
_gyros[i]._gyro.SetValue(_pitchField[i], (float)pitch * (float)_pitchFactor[i]);
_gyros[i]._gyro.SetValue(_rollField[i], (float)roll * (float)_rollFactor[i]);
}
}
public void SetYaw(double v)
{
for (int i = 0; i < _gyros.Count; i++)
{
_gyros[i]._gyro.SetValue(_yawField[i], (float)v * (float)_yawFactor[i]);
}
}
public void SetPitch(double v)
{
for (int i = 0; i < _gyros.Count; i++)
{
_gyros[i]._gyro.SetValue(_pitchField[i], (float)v * (float)_pitchFactor[i]);
}
}
public void SetRoll(double v)
{
for (int i = 0; i < _gyros.Count; i++)
{
_gyros[i]._gyro.SetValue(_rollField[i], (float)v * (float)_rollFactor[i]);
}
}
//开关越级
public void SetOverride(bool onoff = true)
{
for (int i = 0; i < this._gyros.Count; i++)
{
this._gyros[i].IsOverride = onoff;
}
}
//开关方块
public void SetOnOff(bool onoff = true)
{
for (int i = 0; i < this._gyros.Count; i++)
{
this._gyros[i].OnOff = onoff;
}
}
}
}
```
- 序言
- 写在前面的话
- 太空工程师
- MEA小组
- 一、入门
- 1.1 基础概念
- 1.2 编程工具
- 1.3 变量
- 1.4 函数 Function
- 1.5 基本语法
- 1.5.1 运算符
- 1.5.2 if
- 1.5.3 for
- 1.5.4 其他语法
- 1.3 类 Class
- 二、编程块
- 2.1 方块的概念
- 2.2 List<T>结构
- 2.3 获取方块
- 2.4 方块的使用
- 三、Ship 类
- 3.1 简介
- Ship v0.5
- 代码
- 手册(待更新)
- 例子(待更新)
- Ship v1.0
- 代码
- 例子
- 文档
- 实例化
- 内置变量
- 内置方法
- Target类
- 四、运动控制算法在SE中的应用
- 4.1 运动控制介绍
- 4.2 过程控制
- 4.3 震荡和动态误差
- 4.4 误差累加方案
- 4.5 PID算法
- 4.6 对PID算法的一点点简化
- 4.7 一阶惯性系统的PID算法优化的研究
- 五、MEA方块类
- 5.0 核心代码目录
- v1.0核心代码
- v1.1 核心代码
- v2.0 核心代码
- 5.1 类的概念
- 5.2 MEA的方块类(Block)
- 5.3 方块类文档
- 5.4 方块类2.0 全教程
- 5.4.1 安装和使用
- 5.4.2 方块类(Block)
- 5.4.3 显示屏类(DisplayScreen)
- 5.4.4 LCD类(LCD)
- 5.4.5 主控座椅类(Cockpit)
- 六、疯猴的编程笔记
- 第一个程序
- 获取和控制其他块
- 物流与生产
- 界面与通信
- 运动与姿态
- 侦测与导航
- 七、SteamZhou的笔记
- 有趣而花里胡哨的IDEA
- 八、质子对撞炮的笔记
- 属性 Property
- 接口 interface