Example #1
0
        public void Draw()
        {
            var fire = IsShooting;

            var result = _raycastResult;
            var matrix = _dummy.Dummy.WorldMatrix;
            var origin = matrix.Translation;
            var dir    = matrix.Forward;


            if (fire)
            {
                var from = origin;
                var to   = from + dir * Definition.MaxLazeDistance;

                if (result.HasValue)
                {
                    // if hitting a grid go an extra quarter block.  Otherwise go an extra fourth of the bounding box
                    var extraDist = (float)Math.Abs((result.Value.Root as IMyCubeGrid)?.GridSize ??
                                                    result.Value.Root?.PositionComp?.WorldAABB.HalfExtents
                                                    .Dot(dir) ??
                                                    0f) / 4f;
                    if (result.Value.Root is IMyVoxelBase)
                    {
                        extraDist = 0.1f;
                    }

                    to = result.Value.HitPosition + extraDist * dir;
                }

                var thickness = BeamController.BeamWidth(_energyThroughput);
                var color     = BeamController.BeamColor(_beamColor, _energyThroughput);
                MySimpleObjectDraw.DrawLine(from, to, BeamController.LaserMaterial, ref color, thickness);
            }

            if (!string.IsNullOrEmpty(Definition?.FxImpactName))
            {
                _fxImpactCount = Math.Max(_fxImpactCount - 1, 0);
                if (fire)
                {
                    _fxImpactCount = Math.Min(_fxImpactCount + Definition.FxImpactBirthRate,
                                              Definition.FxImpactMaxCount);
                }
                if (_fxImpactCount > 0 && result.HasValue)
                {
                    if (_fxImpactParticles == null)
                    {
                        var up    = (Vector3)dir;
                        var fwd   = Vector3.Normalize(Vector3.Cross(up, new Vector3(up.Y, up.Z, up.X)));
                        var world = MatrixD.CreateWorld(result.Value.HitPosition, fwd, up);
//                        MyParticlesManager.TryCreateParticleEffect(Definition?.FxImpactName, world, out _fxImpactParticles);
                        MyParticlesManager.TryCreateParticleEffect(Definition.FxImpactName, out _fxImpactParticles);
                    }

                    if (_fxImpactParticles != null)
                    {
                        {
                            var up  = (Vector3)dir;
                            var fwd = Vector3.Normalize(Vector3.Cross(up, new Vector3(up.Y, up.Z, up.X)));
                            _fxImpactParticles.WorldMatrix = MatrixD.CreateWorld(result.Value.HitPosition, fwd, up);
                            _fxImpactParticles.UserScale   = Definition.FxImpactScale;
                            _fxImpactParticles.Velocity    =
                                result.Value.Root?.Physics?.GetVelocityAtPoint(result.Value.HitPosition) ??
                                Vector3.Zero;
                        }

                        _fxImpactParticles.UserBirthMultiplier = _fxImpactCount;
                    }
                }
                else
                {
                    _fxImpactParticles?.StopEmitting();
                    if (_fxImpactParticles != null)
                    {
                        MyParticlesManager.RemoveParticleEffect(_fxImpactParticles);
                    }
                    _fxImpactParticles = null;
                }
            }
        }