Beispiel #1
0
        public override void Start()
        {
            if (this.Enabled && !this.main.EditorEnabled)
            {
                if (this.AttachedVoxel.Value.Target == null)
                {
                    Vector3 target = Vector3.Transform(new Vector3(0, 0, this.Offset), this.Transform);

                    Entity closestMap           = null;
                    float  closestFloatDistance = 3.0f;

                    foreach (SceneryBlock block in SceneryBlock.All)
                    {
                        Vector3 pos      = block.Entity.Get <Transform>().Position;
                        float   distance = (pos - target).Length();
                        if (distance < closestFloatDistance)
                        {
                            closestFloatDistance = distance;
                            closestMap           = block.Entity;
                        }
                    }

                    int closestDistance = (int)Math.Floor(closestFloatDistance);
                    foreach (Voxel m in Voxel.Voxels)
                    {
                        SliderCommon s = m.Entity.Get <SliderCommon>();
                        Vector3      relativeTarget = Vector3.Transform(target, Matrix.Invert(s != null ? s.OriginalTransform : m.Transform)) + m.Offset;
                        Voxel.Coord  targetCoord    = m.GetCoordinateFromRelative(relativeTarget);
                        Voxel.Coord? c = m.FindClosestFilledCell(targetCoord, closestDistance);
                        if (c.HasValue)
                        {
                            float distance = (m.GetRelativePosition(c.Value) - m.GetRelativePosition(targetCoord)).Length();
                            if (distance < closestFloatDistance)
                            {
                                closestFloatDistance = distance;
                                closestDistance      = (int)Math.Floor(distance);
                                closestMap           = m.Entity;
                            }
                        }
                    }
                    if (closestMap == null)
                    {
                        this.Detach.Execute();
                    }
                    else
                    {
                        this.AttachedVoxel.Value = closestMap;
                    }
                }
                else
                {
                    this.AttachedVoxel.Reset();
                }
            }
        }
Beispiel #2
0
        public override void Awake()
        {
            base.Awake();
            Binding <Matrix> attachmentBinding = null;
            CommandBinding <IEnumerable <Voxel.Coord>, Voxel> cellEmptiedBinding = null;

            this.isInitialAttachment = this.AttachedVoxel.Value.GUID != 0;

            this.Add(new NotifyBinding(delegate()
            {
                if (attachmentBinding != null)
                {
                    this.Remove(attachmentBinding);
                }

                if (cellEmptiedBinding != null)
                {
                    this.Remove(cellEmptiedBinding);
                    cellEmptiedBinding = null;
                }

                Entity attachedVoxel = this.AttachedVoxel.Value.Target;
                if (attachedVoxel != null && attachedVoxel.Active)
                {
                    Voxel m = this.AttachedVoxel.Value.Target.Get <Voxel>();
                    if (m == null)
                    {
                        // We're attached to a scenery block
                        Property <Matrix> other = this.AttachedVoxel.Value.Target.Get <Transform>().Matrix;
                        Matrix offset           = this.Transform * Matrix.Invert(other);
                        attachmentBinding       = new Binding <Matrix>(this.Transform, x => offset * x, other);
                        this.Add(attachmentBinding);
                    }
                    else
                    {
                        SliderCommon s = m.Entity.Get <SliderCommon>();
                        Vector3 pos    = Vector3.Transform(this.Vector.Value.GetVector() * this.Offset, this.Transform);
                        Matrix voxelTransform;
                        if (this.isInitialAttachment)
                        {
                            voxelTransform           = m.Transform;
                            this.isInitialAttachment = false;
                        }
                        else if (s == null)
                        {
                            voxelTransform = m.Transform;
                        }
                        else
                        {
                            voxelTransform = s.OriginalTransform;
                        }

                        Vector3 relativePos = Vector3.Transform(pos, Matrix.Invert(voxelTransform)) + m.Offset;
                        this.Coord.Value    = m.GetCoordinateFromRelative(relativePos);

                        Matrix offset = this.Transform * Matrix.Invert(Matrix.CreateTranslation(m.Offset) * voxelTransform);

                        attachmentBinding = new Binding <Matrix>(this.Transform, () => offset * Matrix.CreateTranslation(m.Offset) * m.Transform, m.Transform, m.Offset);
                        this.Add(attachmentBinding);

                        cellEmptiedBinding = new CommandBinding <IEnumerable <Voxel.Coord>, Voxel>(m.CellsEmptied, delegate(IEnumerable <Voxel.Coord> coords, Voxel newMap)
                        {
                            foreach (Voxel.Coord c in coords)
                            {
                                if (c.Equivalent(this.Coord))
                                {
                                    if (newMap == null)
                                    {
                                        if (this.detachIfRemoved)
                                        {
                                            this.Detach.Execute();
                                        }
                                    }
                                    else
                                    {
                                        if (this.detachIfMoved)
                                        {
                                            this.Detach.Execute();
                                        }
                                        else if (newMap != m)
                                        {
                                            this.AttachedVoxel.Value = newMap.Entity;
                                        }
                                    }
                                    break;
                                }
                            }
                        });
                        this.Add(cellEmptiedBinding);
                    }
                }
            }, this.AttachedVoxel));
        }