// This name is a nice reminder of our origins. Never forget.
        public IEnumerable <EntityCoordinates> GridCoordinates()
        {
            var placementdiff = MouseCoords.ToMapPos(pManager.EntityManager) - pManager.StartPoint.ToMapPos(pManager.EntityManager);
            var distanceX     = new Vector2(placementdiff.X > 0 ? 1 : -1, 0) * GridDistancing;
            var distanceY     = new Vector2(0, placementdiff.Y > 0 ? 1 : -1) * GridDistancing;

            var iterationsX = Math.Abs(placementdiff.X / GridDistancing);
            var iterationsY = Math.Abs(placementdiff.Y / GridDistancing);

            for (var x = 0; x <= iterationsX; x++)
            {
                for (var y = 0; y <= iterationsY; y++)
                {
                    yield return(new EntityCoordinates(pManager.StartPoint.EntityId, pManager.StartPoint.Position + distanceX * x + distanceY * y));
                }
            }
        }
        public IEnumerable<GridCoordinates> LineCoordinates()
        {
            var (x, y) = MouseCoords.ToMapPos(pManager.MapManager) - pManager.StartPoint.ToMapPos(pManager.MapManager);
            float iterations;
            Vector2 distance;
            if (Math.Abs(x) > Math.Abs(y))
            {
                iterations = Math.Abs(x / GridDistancing);
                distance = new Vector2(x > 0 ? 1 : -1, 0) * GridDistancing;
            }
            else
            {
                iterations = Math.Abs(y / GridDistancing);
                distance = new Vector2(0, y > 0 ? 1 : -1) * GridDistancing;
            }

            for (var i = 0; i <= iterations; i++)
            {
                yield return new GridCoordinates(pManager.StartPoint.Position + distance * i, pManager.StartPoint.GridID);
            }
        }
Exemple #3
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);
            CurrentTile = GetTileRef(MouseCoords);

            if (pManager.CurrentPermission !.IsTile)
            {
                return;
            }

            if (!RangeCheck(MouseCoords))
            {
                return;
            }

            var mapId = MouseCoords.GetMapId(pManager.EntityManager);

            var snapToEntities = EntitySystem.Get <EntityLookupSystem>().GetEntitiesInRange(MouseCoords, SnapToRange)
                                 .Where(entity => pManager.EntityManager.GetComponent <MetaDataComponent>(entity).EntityPrototype == pManager.CurrentPrototype && pManager.EntityManager.GetComponent <TransformComponent>(entity).MapID == mapId)
                                 .OrderBy(entity => (pManager.EntityManager.GetComponent <TransformComponent>(entity).WorldPosition - MouseCoords.ToMapPos(pManager.EntityManager)).LengthSquared)
                                 .ToList();

            if (snapToEntities.Count == 0)
            {
                return;
            }

            var closestEntity    = snapToEntities[0];
            var closestTransform = pManager.EntityManager.GetComponent <TransformComponent>(closestEntity);

            if (!pManager.EntityManager.TryGetComponent <ISpriteComponent?>(closestEntity, out var component) || component.BaseRSI == null)
            {
                return;
            }

            var closestBounds = component.BaseRSI.Size;

            var closestRect =
                Box2.FromDimensions(
                    closestTransform.WorldPosition.X - closestBounds.X / 2f,
                    closestTransform.WorldPosition.Y - closestBounds.Y / 2f,
                    closestBounds.X, closestBounds.Y);

            var sides = new[]
            {
                new Vector2(closestRect.Left + closestRect.Width / 2f, closestRect.Top - closestBounds.Y / 2f),
                new Vector2(closestRect.Left + closestRect.Width / 2f, closestRect.Bottom + closestBounds.Y / 2f),
                new Vector2(closestRect.Left - closestBounds.X / 2f, closestRect.Top + closestRect.Height / 2f),
                new Vector2(closestRect.Right + closestBounds.X / 2f, closestRect.Top + closestRect.Height / 2f)
            };

            var closestSide =
                (from Vector2 side in sides orderby(side - MouseCoords.Position).LengthSquared select side).First();

            MouseCoords = new EntityCoordinates(MouseCoords.EntityId, MouseCoords.Position);
        }
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);
            var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID);

            CurrentTile = mapGrid.GetTileRef(MouseCoords);

            if (pManager.CurrentPermission !.IsTile)
            {
                return;
            }

            if (!RangeCheck(MouseCoords))
            {
                return;
            }

            var manager = IoCManager.Resolve <IClientEntityManager>();

            var snapToEntities = manager.GetEntitiesInRange(MouseCoords, SnapToRange)
                                 .Where(entity => entity.Prototype == pManager.CurrentPrototype && entity.Transform.MapID == mapGrid.ParentMapId)
                                 .OrderBy(entity => (entity.Transform.WorldPosition - MouseCoords.ToMapPos(pManager.MapManager)).LengthSquared)
                                 .ToList();

            if (snapToEntities.Count == 0)
            {
                return;
            }

            var closestEntity = snapToEntities[0];

            if (!closestEntity.TryGetComponent <ISpriteComponent>(out var component) || component.BaseRSI == null)
            {
                return;
            }

            var closestBounds = component.BaseRSI.Size;

            var closestRect =
                Box2.FromDimensions(
                    closestEntity.Transform.WorldPosition.X - closestBounds.X / 2f,
                    closestEntity.Transform.WorldPosition.Y - closestBounds.Y / 2f,
                    closestBounds.X, closestBounds.Y);

            var sides = new[]
            {
                new Vector2(closestRect.Left + closestRect.Width / 2f, closestRect.Top - closestBounds.Y / 2f),
                new Vector2(closestRect.Left + closestRect.Width / 2f, closestRect.Bottom + closestBounds.Y / 2f),
                new Vector2(closestRect.Left - closestBounds.X / 2f, closestRect.Top + closestRect.Height / 2f),
                new Vector2(closestRect.Right + closestBounds.X / 2f, closestRect.Top + closestRect.Height / 2f)
            };

            var closestSide =
                (from Vector2 side in sides orderby(side - MouseCoords.Position).LengthSquared select side).First();

            MouseCoords = new GridCoordinates(closestSide, MouseCoords.GridID);
        }