Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MotionDetectorElectricElement.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
5 public const float m_range = 8f;
6
7 public const float m_speedThreshold = 0.25f;
8
9 public const float m_pollingPeriod = 0.25f;
10
12
14
16
18
19 public float m_voltage;
20
22
24
26
28
29 public DynamicArray<ComponentBody> m_bodies = [];
30
31 public MotionDetectorElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace) : base(subsystemElectricity, cellFace) {
32 m_subsystemBodies = subsystemElectricity.Project.FindSubsystem<SubsystemBodies>(true);
36 m_center = new Vector3(cellFace.X, cellFace.Y, cellFace.Z) + new Vector3(0.5f) - 0.25f * m_direction;
38 Vector3 vector = Vector3.One - new Vector3(MathF.Abs(m_direction.X), MathF.Abs(m_direction.Y), MathF.Abs(m_direction.Z));
39 Vector3 vector2 = m_center - 8f * vector;
40 Vector3 vector3 = m_center + 8f * (vector + m_direction);
41 m_corner1 = new Vector2(vector2.X, vector2.Z);
42 m_corner2 = new Vector2(vector3.X, vector3.Z);
43 }
44
45 public override float GetOutputVoltage(int face) => m_voltage;
46
47 public override bool Simulate() {
48 float voltage = m_voltage;
50 if (m_voltage > 0f
51 && voltage == 0f) {
52 SubsystemElectricity.SubsystemAudio.PlaySound("Audio/MotionDetectorClick", 1f, 0f, m_center, 1f, true);
53 }
54 float num = 0.25f * (0.9f + 0.000200000009f * (GetHashCode() % 1000));
56 return m_voltage != voltage;
57 }
58
59 public float CalculateMotionVoltage() {
60 float num = 0f;
61 m_bodies.Clear();
63 for (int i = 0; i < m_bodies.Count; i++) {
64 ComponentBody componentBody = m_bodies.Array[i];
65 if (!(componentBody.Velocity.LengthSquared() < 0.0625f)) {
66 num = MathUtils.Max(num, TestPoint(componentBody.Position + new Vector3(0f, 0.5f * componentBody.BoxSize.Y, 0f)));
67 }
68 }
69 foreach (IMovingBlockSet movingBlockSet in m_subsystemMovingBlocks.MovingBlockSets) {
70 if (movingBlockSet.CurrentVelocity.LengthSquared() < 0.0625f
71 || BoundingBox.Distance(movingBlockSet.BoundingBox(false), m_center) > 8f) {
72 continue;
73 }
74 foreach (MovingBlock block in movingBlockSet.Blocks) {
75 num = MathUtils.Max(num, TestPoint(movingBlockSet.Position + new Vector3(block.Offset) + new Vector3(0.5f)));
76 }
77 }
78 foreach (Projectile projectile in m_subsystemProjectiles.Projectiles) {
79 if (!(projectile.Velocity.LengthSquared() < 0.0625f)) {
80 num = MathUtils.Max(num, TestPoint(projectile.Position));
81 }
82 }
83 foreach (Pickable pickable in m_subsystemPickables.Pickables) {
84 if (!(pickable.Velocity.LengthSquared() < 0.0625f)) {
85 num = MathUtils.Max(num, TestPoint(pickable.Position));
86 }
87 }
88 if (!(num > 0f)) {
89 return 0f;
90 }
91 return MathUtils.Lerp(0.51f, 1f, MathUtils.Saturate(num * 1.1f));
92 }
93
94 public float TestPoint(Vector3 p) {
95 float num = Vector3.DistanceSquared(p, m_center);
96 if (num < 64f
97 && Vector3.Dot(Vector3.Normalize(p - (m_center - 0.75f * m_direction)), m_direction) > 0.5f
100 p,
101 false,
102 true,
103 delegate(int value, float _) {
105 return block.IsCollidable_(value)
106 && block.BlockIndex != 15
107 && block.BlockIndex != 60
108 && block.BlockIndex != 44
109 && block.BlockIndex != 18;
110 }
111 )
112 .HasValue) {
113 return MathUtils.Saturate(1f - MathF.Sqrt(num) / 8f);
114 }
115 return 0f;
116 }
117 }
118}
Engine.Vector3 Vector3
static float Saturate(float x)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
virtual bool IsCollidable_(int value)
MotionDetectorElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace)
MountedElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace)
void PlaySound(string name, float volume, float pitch, float pan, float delay)
void QueueElectricElementForSimulation(ElectricElement electricElement, int circuitStep)
virtual ? TerrainRaycastResult Raycast(Vector3 start, Vector3 end, bool useInteractionBoxes, bool skipAirBlocks, Func< int, float, bool > action)
static int ExtractContents(int value)
virtual Subsystem FindSubsystem(Type type, string name, bool throwOnError)
BoundingBox BoundingBox(bool extendToFillCells)
List< MovingBlock > Blocks
static float Distance(BoundingBox b, Vector3 p)
static float DistanceSquared(Vector3 v1, Vector3 v2)
static Vector3 Normalize(Vector3 v)
float LengthSquared()
static float Dot(Vector3 v1, Vector3 v2)
static Vector3 FaceToVector3(int face)