public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);
            var gridId = MouseCoords.GetGridId(pManager.EntityManager);
            var grid   = pManager.MapManager.GetGrid(gridId);

            CurrentTile = grid.GetTileRef(MouseCoords);

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

            var tileCoordinates = grid.GridTileToLocal(CurrentTile.GridIndices);

            var offsets = new Vector2[]
            {
                (0f, 0.5f),
                (0.5f, 0f),
                (0, -0.5f),
                (-0.5f, 0f)
            };

            var closestNode = offsets
                              .Select(o => tileCoordinates.Offset(o))
                              .OrderBy(node => node.TryDistance(pManager.EntityManager, MouseCoords, out var distance) ? distance : (float?)null)
                              .First(f => f != null);

            MouseCoords = closestNode;
        }
Exemple #2
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);
            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            CurrentTile = pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords);
        }
Exemple #3
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);

            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            SnapSize = 1f;
            if (gridId.IsValid())
            {
                Grid     = pManager.MapManager.GetGrid(gridId);
                SnapSize = Grid.TileSize; //Find snap size for the grid.
            }
            else
            {
                Grid = null;
            }

            GridDistancing = SnapSize;

            var mouseLocal = new Vector2( //Round local coordinates onto the snap grid
                (float)(MathF.Round((MouseCoords.Position.X / SnapSize - 0.5f), MidpointRounding.AwayFromZero) + 0.5) * SnapSize,
                (float)(MathF.Round((MouseCoords.Position.Y / SnapSize - 0.5f), MidpointRounding.AwayFromZero) + 0.5) * SnapSize);

            //Adjust mouseCoords to new calculated position
            MouseCoords = new EntityCoordinates(MouseCoords.EntityId, mouseLocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y));
        }
Exemple #4
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);

            snapSize = 1f;

            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            if (gridId.IsValid())
            {
                snapSize = pManager.MapManager.GetGrid(gridId).SnapSize; //Find snap size for the grid.
                onGrid   = true;
            }
            else
            {
                onGrid = false;
            }

            GridDistancing = snapSize;

            var mouselocal = new Vector2( //Round local coordinates onto the snap grid
                (float)MathF.Round(MouseCoords.X / snapSize, MidpointRounding.AwayFromZero) * snapSize,
                (float)MathF.Round(MouseCoords.Y / snapSize, MidpointRounding.AwayFromZero) * snapSize);

            //Convert back to original world and screen coordinates after applying offset
            MouseCoords =
                new EntityCoordinates(
                    MouseCoords.EntityId, mouselocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y));
        }
        /// <summary>
        ///     Returns the tile ref for a grid, or a map.
        /// </summary>
        public TileRef GetTileRef(EntityCoordinates coordinates)
        {
            var mapCoords = coordinates.ToMap(pManager.EntityManager);
            var gridId    = coordinates.GetGridId(pManager.EntityManager);

            return(gridId.IsValid() ? pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords)
                : new TileRef(mapCoords.MapId, gridId,
                              MouseCoords.ToVector2i(pManager.EntityManager, pManager.MapManager), Tile.Empty));
        }
Exemple #6
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);
        }
Exemple #7
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);

            var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GetGridId(pManager.EntityManager));

            CurrentTile = mapGrid.GetTileRef(MouseCoords);
            float tileSize = mapGrid.TileSize; //convert from ushort to float

            GridDistancing = tileSize;

            if (pManager.CurrentPermission !.IsTile)
            {
                MouseCoords = new EntityCoordinates(MouseCoords.EntityId,
                                                    (CurrentTile.X + tileSize / 2, CurrentTile.Y + tileSize / 2));
            }
        // 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));
                }
            }
        }
Exemple #9
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);

            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            snapSize       = pManager.MapManager.GetGrid(gridId).SnapSize; //Find snap size.
            GridDistancing = snapSize;
            onGrid         = true;

            var mouseLocal = new Vector2( //Round local coordinates onto the snap grid
                (float)(MathF.Round((MouseCoords.Position.X / snapSize - 0.5f), MidpointRounding.AwayFromZero) + 0.5) * snapSize,
                (float)(MathF.Round((MouseCoords.Position.Y / snapSize - 0.5f), MidpointRounding.AwayFromZero) + 0.5) * snapSize);

            //Adjust mouseCoords to new calculated position
            MouseCoords = new EntityCoordinates(MouseCoords.EntityId, mouseLocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y));
        }
        public IEnumerable <GridLocalCoordinates> GridCoordinates()
        {
            var placementdiff = MouseCoords.ToWorld().Position - pManager.StartPoint.ToWorld().Position;
            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 GridLocalCoordinates(pManager.StartPoint.Position + distanceX * x + distanceY * y, pManager.StartPoint.Grid));
                }
            }
        }
Exemple #11
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            MouseCoords = ScreenToCursorGrid(mouseScreen);
            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            CurrentTile = pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords);

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

            var nodes = new List <Vector2>();

            if (pManager.CurrentPrototype !.MountingPoints != null)
            {
                nodes.AddRange(
                    pManager.CurrentPrototype.MountingPoints.Select(
                        current => new Vector2(MouseCoords.X, CurrentTile.Y + current)));
            }
        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);
            }
        }
        public IEnumerable <GridLocalCoordinates> LineCoordinates()
        {
            var     placementdiff = MouseCoords.ToWorld().Position - pManager.StartPoint.ToWorld().Position;
            var     iterations    = 0f;
            Vector2 distance;

            if (Math.Abs(placementdiff.X) > Math.Abs(placementdiff.Y))
            {
                iterations = Math.Abs(placementdiff.X / GridDistancing);
                distance   = new Vector2(placementdiff.X > 0 ? 1 : -1, 0) * GridDistancing;
            }
            else
            {
                iterations = Math.Abs(placementdiff.Y / GridDistancing);
                distance   = new Vector2(0, placementdiff.Y > 0 ? 1 : -1) * GridDistancing;
            }

            for (var i = 0; i <= iterations; i++)
            {
                yield return(new GridLocalCoordinates(pManager.StartPoint.Position + distance * i, pManager.StartPoint.Grid));
            }
        }
Exemple #14
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            const float SearchBoxSize = 1.5f; // size of search box in meters

            MouseCoords = ScreenToCursorGrid(mouseScreen).AlignWithClosestGridTile(SearchBoxSize, pManager.EntityManager, pManager.MapManager);

            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            if (!pManager.MapManager.TryGetGrid(gridId, out var mapGrid))
            {
                return;
            }

            CurrentTile = mapGrid.GetTileRef(MouseCoords);
            float tileSize = mapGrid.TileSize; //convert from ushort to float

            GridDistancing = tileSize;

            if (pManager.CurrentPermission !.IsTile)
            {
                MouseCoords = new EntityCoordinates(MouseCoords.EntityId, (CurrentTile.X + tileSize / 2,
                                                                           CurrentTile.Y + tileSize / 2));
            }
Exemple #15
0
        public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
        {
            const float SearchBoxSize = 1.5f; // size of search box in meters

            MouseCoords = ScreenToCursorGrid(mouseScreen);

            var gridId = MouseCoords.GetGridId(pManager.EntityManager);

            IMapGrid?mapGrid = null;

            if (!gridId.IsValid() || !pManager.MapManager.TryGetGrid(gridId, out mapGrid))
            {
                // create a box around the cursor
                var gridSearchBox = Box2.UnitCentered.Scale(SearchBoxSize).Translated(MouseCoords.Position);

                // find grids in search box
                var gridsInArea = pManager.MapManager.FindGridsIntersecting(MouseCoords.GetMapId(pManager.EntityManager), gridSearchBox);

                // find closest grid intersecting our search box.
                IMapGrid?closest   = null;
                var      distance  = float.PositiveInfinity;
                var      intersect = new Box2();
                foreach (var grid in gridsInArea)
                {
                    // figure out closest intersect
                    var gridIntersect = gridSearchBox.Intersect(grid.WorldBounds);
                    var gridDist      = (gridIntersect.Center - MouseCoords.Position).LengthSquared;

                    if (gridDist >= distance)
                    {
                        continue;
                    }

                    distance  = gridDist;
                    closest   = grid;
                    intersect = gridIntersect;
                }

                if (closest != null) // stick to existing grid
                {
                    // round to nearest cardinal dir
                    var normal = new Angle(MouseCoords.Position - intersect.Center).GetCardinalDir().ToVec();

                    // round coords to center of tile
                    var tileIndices     = closest.WorldToTile(intersect.Center);
                    var tileCenterWorld = closest.GridTileToWorldPos(tileIndices);

                    // move mouse one tile out along normal
                    var newTilePos = tileCenterWorld + normal * closest.TileSize;

                    MouseCoords = new EntityCoordinates(closest.GridEntityId, closest.WorldToLocal(newTilePos));
                    mapGrid     = closest;
                }
                //else free place
            }

            if (mapGrid == null)
            {
                return;
            }

            CurrentTile = mapGrid.GetTileRef(MouseCoords);
            float tileSize = mapGrid.TileSize; //convert from ushort to float

            GridDistancing = tileSize;

            if (pManager.CurrentPermission !.IsTile)
            {
                MouseCoords = new EntityCoordinates(MouseCoords.EntityId, (CurrentTile.X + tileSize / 2,
                                                                           CurrentTile.Y + tileSize / 2));
            }
        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);
        }
        public override bool FrameUpdate(RenderFrameEventArgs e, ScreenCoordinates mouseS)
        {
            if (mouseS.MapID == MapId.Nullspace)
            {
                return(false);
            }

            MouseScreen = mouseS;
            MouseCoords = pManager.eyeManager.ScreenToWorld(MouseScreen);

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

            CurrentTile = MouseCoords.Grid.GetTile(MouseCoords);

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

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

            var snapToEntities = manager.GetEntitiesInRange(MouseCoords, SnapToRange)
                                 .Where(entity => entity.Prototype == pManager.CurrentPrototype && entity.GetComponent <ITransformComponent>().MapID == MouseCoords.MapID)
                                 .OrderBy(entity => (entity.GetComponent <ITransformComponent>().WorldPosition - MouseCoords.ToWorld().Position).LengthSquared)
                                 .ToList();

            if (snapToEntities.Any())
            {
                var closestEntity = snapToEntities.First();
                if (closestEntity.TryGetComponent <ISpriteRenderableComponent>(out var component))
                {
                    var closestSprite = component.CurrentSprite;
                    var closestBounds = closestSprite.Size;

                    var closestRect =
                        Box2.FromDimensions(
                            closestEntity.GetComponent <ITransformComponent>().WorldPosition.X - closestBounds.X / 2f,
                            closestEntity.GetComponent <ITransformComponent>().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 LocalCoordinates(closestSide, MouseCoords.Grid);
                    MouseScreen = pManager.eyeManager.WorldToScreen(MouseCoords);
                }
            }

            if (CheckCollision())
            {
                return(false);
            }
            return(true);
        }