Exemple #1
0
        private bool TryGetSubparts()
        {
            if ((Entity as MyEntity).Subparts == null)
            {
                return(false);
            }

            if (Entity.TryGetSubpart(OUTER, out SubpartOuter))
            {
                if (SubpartOuter.TryGetSubpart(INNER, out SubpartInner))
                {
                    return(true);
                }
            }

            return(false);
        }
        public override void UpdateAfterSimulation()
        {
            base.UpdateAfterSimulation();

            try
            {
                base.UpdateBeforeSimulation();

                List <BeaconStorage> temp = store.ToList();
                foreach (var beaconStorage in temp)
                {
                    var  entity     = beaconStorage.Beacon as MyEntity;
                    bool shouldSpin = beaconStorage.Beacon.IsWorking;    // if block is functional and enabled and powered.

                    if (beaconStorage.Beacon.CubeGrid == null && beaconStorage.Effect != null)
                    {
                        beaconStorage.Effect.Stop();
                        store.Remove(beaconStorage);
                    }

                    if (beaconStorage.Beacon == null || !beaconStorage.Beacon.Enabled || !beaconStorage.Beacon.IsWorking || !beaconStorage.Beacon.IsFunctional)
                    {
                        if (entity != null && entity.TryGetSubpart(SUBPART_NAME, out subpart))
                        {
                            subpart.SetEmissiveParts("EmissiveSpotlight", Color.DarkRed, 1.0f);
                        }

                        if (beaconStorage.Effect != null)
                        {
                            beaconStorage.Effect.Stop();
                        }
                        beaconStorage.Once = false;
                        return;
                    }

                    if (!shouldSpin && Math.Abs(targetSpeedMultiplier) < 0.00001f)
                    {
                        return;
                    }

                    if (shouldSpin && targetSpeedMultiplier < 1)
                    {
                        targetSpeedMultiplier = Math.Min(targetSpeedMultiplier + ACCELERATE_PERCENT_PER_TICK, 1);
                    }
                    else if (!shouldSpin && targetSpeedMultiplier > 0)
                    {
                        targetSpeedMultiplier = Math.Max(targetSpeedMultiplier - DEACCELERATE_PERCENT_PER_TICK, 0);
                    }

                    var camPos = MyAPIGateway.Session.Camera.WorldMatrix.Translation;    // local machine camera position

                    if (Vector3D.DistanceSquared(camPos, beaconStorage.Beacon.GetPosition()) > MAX_DISTANCE_SQ)
                    {
                        return;
                    }

                    if (entity != null && entity.TryGetSubpart(SUBPART_NAME, out subpart)) // subpart does not exist when block is in build stage
                    {
                        if (subpartFirstFind)                                              // first time the subpart was found
                        {
                            subpartFirstFind   = false;
                            subpartLocalMatrix = subpart.PositionComp.LocalMatrix;
                        }


                        if (targetSpeedMultiplier > 0)
                        {
                            subpartLocalMatrix *= Matrix.CreateFromAxisAngle(ROTATION_AXIS, MathHelper.ToRadians(targetSpeedMultiplier * DEGREES_PER_TICK));
                            subpartLocalMatrix  = Matrix.Normalize(subpartLocalMatrix);  // normalize to avoid any rotation inaccuracies over time resulting in weird scaling
                        }
                        subpart.PositionComp.LocalMatrix = subpartLocalMatrix;
                    }

                    if (subpart != null)
                    {
                        subpart.SetEmissiveParts("EmissiveSpotlight", Color.LimeGreen, beaconStorage.Beacon.Radius / 6000);
                    }

                    if (beaconStorage.Beacon.Radius < 600)
                    {
                        size = 0.75f;
                    }
                    else if (beaconStorage.Beacon.Radius > 600 && beaconStorage.Beacon.Radius < 3000)
                    {
                        size = beaconStorage.Beacon.Radius / 1000;
                    }
                    else
                    {
                        size = 3.0f;
                    }

                    if (!beaconStorage.Once)
                    {
                        MyParticleEffect e;
                        MyParticlesManager.TryCreateParticleEffect("ExhaustElectricSmall", subpartLocalMatrix, out e);
                        e.WorldMatrix                         = beaconStorage.Beacon.WorldMatrix;
                        beaconStorage.Effect                  = e;
                        beaconStorage.Once                    = true;
                        beaconStorage.Effect.UserScale        = size;
                        beaconStorage.Effect.UserEmitterScale = size;
                        beaconStorage.Effect.Play();
                    }


                    if (beaconStorage.Effect != null)
                    {
                        beaconStorage.Effect.UserScale        = size;
                        beaconStorage.Effect.UserEmitterScale = size;
                        beaconStorage.Effect.WorldMatrix      = beaconStorage.Beacon.WorldMatrix;
                        beaconStorage.Effect.Update();
                    }



                    MyEntitySubpart subpart2;
                    if (subpart != null && subpart.TryGetSubpart("Ring", out subpart2))
                    {
                        beaconStorage.RingRotation -= 1;
                        var rotationMatrix = Matrix.CreateRotationY((beaconStorage.Beacon.Radius / 600) * beaconStorage.RingRotation);
                        subpart2.PositionComp.SetLocalMatrix(ref rotationMatrix, null, true);
                    }
                }
            }
            catch (Exception ex)
            {
                MyLog.Default.WriteLine($"Error in Jump Inhibitor {ex}");
            }
        }