public bool CheckClickScreen(Vector2 screenPos, Vector2 mousePos) { if (!IsVisible(checkWorld: false)) { return(false); } if (Icon.Appearance.MouseOpacity == MouseOpacity.Transparent) { return(false); } Vector2 size = Icon.DMI.IconSize / (float)EyeManager.PixelsPerMeter * (ScreenLocation.RepeatX, ScreenLocation.RepeatY); Box2 iconBox = Box2.FromDimensions(screenPos, size); if (!iconBox.Contains(mousePos)) { return(false); } switch (Icon.Appearance.MouseOpacity) { case MouseOpacity.Opaque: return(true); case MouseOpacity.PixelOpaque: return(Icon.CheckClickScreen(screenPos, mousePos)); default: throw new InvalidOperationException("Invalid mouse_opacity"); } }
public void Insert(T quadObject) { lock (syncLock) { if (sort && !objectSortOrder.ContainsKey(quadObject)) { objectSortOrder.Add(quadObject, objectSortId++); } var bounds = quadObject.Bounds; if (root == null) { var rootSizeF = new Vector2((float)Math.Ceiling(bounds.Width / minLeafSizeF.X), (float)Math.Ceiling(bounds.Height / minLeafSizeF.Y)); double multiplier = Math.Max(rootSizeF.X, rootSizeF.Y); rootSizeF = new Vector2((float)(minLeafSizeF.X * multiplier), (float)(minLeafSizeF.Y * multiplier)); var center = new Vector2i((int)(bounds.Left + bounds.Width / 2), (int)(bounds.Top + bounds.Height / 2)); var rootOrigin = new Vector2i((int)(center.X - rootSizeF.X / 2), (int)(center.Y - rootSizeF.Y / 2)); root = new QuadNode(Box2.FromDimensions((Vector2)rootOrigin, rootSizeF)); } while (!root.Bounds.Encloses(bounds)) { ExpandRoot(bounds); } InsertNodeObject(root, quadObject); } }
private void ExpandRoot(Box2 newChildBounds) { lock (syncLock) { bool isNorth = root.Bounds.Top < newChildBounds.Top; bool isWest = root.Bounds.Left < newChildBounds.Left; DiRectangleFion rootDiRectangleFion; if (isNorth) { rootDiRectangleFion = isWest ? DiRectangleFion.NW : DiRectangleFion.NE; } else { rootDiRectangleFion = isWest ? DiRectangleFion.SW : DiRectangleFion.SE; } double newX = (rootDiRectangleFion == DiRectangleFion.NW || rootDiRectangleFion == DiRectangleFion.SW) ? root.Bounds.Left : root.Bounds.Left - root.Bounds.Width; double newY = (rootDiRectangleFion == DiRectangleFion.NW || rootDiRectangleFion == DiRectangleFion.NE) ? root.Bounds.Top : root.Bounds.Top - root.Bounds.Height; var newRootBounds = Box2.FromDimensions((float)newX, (float)newY, (float)root.Bounds.Width * 2f, (float)root.Bounds.Height * 2f); QuadNode newRoot = new QuadNode(newRootBounds); SetupChildNodes(newRoot); newRoot[rootDiRectangleFion] = root; root = newRoot; } }
/// <inheritdoc /> public override void LoadParameters(YamlMappingNode mapping) { YamlNode node; if (mapping.TryGetNode("sizeX", out node)) { var width = node.AsFloat(); AABB = Box2.FromDimensions(AABB.Left + (AABB.Width - width) / 2f, AABB.Top, width, AABB.Height); } if (mapping.TryGetNode("sizeY", out node)) { var height = node.AsFloat(); AABB = Box2.FromDimensions(AABB.Left, AABB.Top + (AABB.Height - height) / 2f, AABB.Width, height); } if (mapping.TryGetNode("offsetX", out node)) { var x = node.AsFloat(); AABB = Box2.FromDimensions(x - AABB.Width / 2f, AABB.Top, AABB.Width, AABB.Height); } if (mapping.TryGetNode("offsetY", out node)) { var y = node.AsFloat(); AABB = Box2.FromDimensions(AABB.Left, y - AABB.Height / 2f, AABB.Width, AABB.Height); } }
private void MainWindowResizeEnd(object sender, SizeEventArgs e) { _stateManager.FormResize(); // Not sure if changing the view manually is needed. // Better safe than sorry. CluwneLib.Window.View = new Graphics.Views.View( Box2.FromDimensions(0, 0, CluwneLib.Window.Size.X, CluwneLib.Window.Size.Y)); }
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 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 ILight[] LightsIntersectingRect(MapId mapId, Box2 rect) { if (rect.IsEmpty()) { return(new ILight[0]); } return(_lights .FindAll(l => l.Coordinates.MapID == mapId && Box2.FromDimensions(l.LightPosition - l.LightMapSize / 2, l.LightMapSize).Intersects(rect)) .ToArray()); }
public virtual bool WasClicked(Vector2 worldPos) { if (sprite == null || !visible) { return(false); } Sprite spriteToCheck = sprite.GetCurrentSprite(); var bounds = spriteToCheck.GetLocalBounds(); var AABB = Box2.FromDimensions( Owner.GetComponent <ITransformComponent>().Position.X - (bounds.Width / 2), Owner.GetComponent <ITransformComponent>().Position.Y - (bounds.Height / 2), bounds.Width, bounds.Height); if (!AABB.Contains(new Vector2(worldPos.X, worldPos.Y))) { return(false); } // Get the sprite's position within the texture var texRect = spriteToCheck.TextureRect; // Get the clicked position relative to the texture var spritePosition = new Vector2i((int)(worldPos.X - AABB.Left + texRect.Left), (int)(worldPos.Y - AABB.Top + texRect.Top)); if (spritePosition.X < 0 || spritePosition.Y < 0) { return(false); } IResourceCache resCache = IoCManager.Resolve <IResourceCache>(); Dictionary <Texture, string> tmp = resCache.TextureToKey; if (!tmp.ContainsKey(spriteToCheck.Texture)) { return(false); } //if it doesn't exist, something's f****d string textureKey = tmp[spriteToCheck.Texture]; bool[,] opacityMap = TextureCache.Textures[textureKey].Opacity; //get our clickthrough 'map' if (!opacityMap[spritePosition.X, spritePosition.Y]) // Check if the clicked pixel is opaque { return(false); } return(true); }
public bool CheckCollision() { var drawsprite = GetSprite(pManager.CurrentBaseSpriteKey); var bounds = drawsprite.GetLocalBounds(); var spriteSize = CluwneLib.PixelToTile(new Vector2(bounds.Width, bounds.Height)); var spriteRectWorld = Box2.FromDimensions(mouseWorld.X - (spriteSize.X / 2f), mouseWorld.Y - (spriteSize.Y / 2f), spriteSize.X, spriteSize.Y); if (pManager.CollisionManager.IsColliding(spriteRectWorld)) { return(false); } return(true); }
public bool IsColliding(EntityCoordinates coordinates) { var bounds = pManager.ColliderAABB; var mapCoords = coordinates.ToMap(pManager.EntityManager); var(x, y) = mapCoords.Position; var collisionBox = Box2.FromDimensions( bounds.Left + x, bounds.Bottom + y, bounds.Width, bounds.Height); return(pManager.PhysicsManager.TryCollideRect(collisionBox, mapCoords.MapId)); }
public bool IsColliding(GridCoordinates coordinates) { var bounds = pManager.ColliderAABB; var worldcoords = coordinates.ToMapPos(pManager.MapManager); var collisionbox = Box2.FromDimensions( bounds.Left + worldcoords.X, bounds.Bottom + worldcoords.Y, bounds.Width, bounds.Height); if (pManager.PhysicsManager.TryCollideRect(collisionbox, pManager.MapManager.GetGrid(coordinates.GridID).ParentMapId)) return true; return false; }
public bool IsColliding(GridLocalCoordinates coordinates) { var bounds = pManager.ColliderAABB; var worldcoords = coordinates.ToWorld(); var collisionbox = Box2.FromDimensions( bounds.Left + worldcoords.Position.X, bounds.Bottom + worldcoords.Position.Y, bounds.Width, bounds.Height); if (pManager.PhysicsManager.IsColliding(collisionbox, coordinates.MapID)) { return(true); } return(false); }
private void _genTextureAtlas() { var defList = TileDefs.Where(t => !string.IsNullOrEmpty(t.SpriteName)).ToList(); const int tileSize = EyeManager.PIXELSPERMETER; var dimensionX = (int)Math.Ceiling(Math.Sqrt(defList.Count)); var dimensionY = (int)Math.Ceiling((float)defList.Count / dimensionX); var sheet = new Image <Rgba32>(dimensionX * tileSize, dimensionY * tileSize); for (var i = 0; i < defList.Count; i++) { var def = defList[i]; var column = i % dimensionX; var row = i / dimensionX; Image <Rgba32> image; using (var stream = _resourceCache.ContentFileRead($"/Textures/Tiles/{def.SpriteName}.png")) { image = Image.Load(stream); } if (image.Width != tileSize || image.Height != tileSize) { throw new NotSupportedException("Unable to use tiles with a dimension other than 32x32."); } var point = new Vector2i(column * tileSize, row * tileSize); image.Blit(new UIBox2i(0, 0, image.Width, image.Height), sheet, point); var w = (float)sheet.Width; var h = (float)sheet.Height; _tileRegions.Add(def.TileId, Box2.FromDimensions( point.X / w, (h - point.Y - EyeManager.PIXELSPERMETER) / h, tileSize / w, tileSize / h)); } TileTextureAtlas = Texture.LoadFromImage(sheet, "Tile Atlas"); }
/// <summary> /// Expands the AABB for this grid when a new tile is added. If the tile is already inside the existing AABB, /// nothing happens. If it is outside, the AABB is expanded to fit the new tile. /// </summary> /// <param name="gridTile">The new tile to check.</param> public void UpdateAABB(MapIndices gridTile) { var worldPos = GridTileToLocal(gridTile).ToWorld(); if (AABBWorld.Contains(worldPos.Position)) { return; } // rect union var a = AABBWorld; var b = worldPos; var min_x = Math.Min(a.Left, b.X); var max_x = Math.Max(a.Right, b.X); var min_y = Math.Min(a.Bottom, b.Y); var max_y = Math.Max(a.Top, b.Y); AABBWorld = Box2.FromDimensions(min_x, min_y, max_x - min_x, max_y - min_y); }
//Todo encapsulate this further down as components -- AnimatedSpriteState, AnimatedSpriteStateDirection public void LoadSprites(AnimationCollection collection, IResourceCache resourceCache) { float x = 0, y = 0, h = 0, w = 0; int t = 0; foreach (var info in collection.Animations) { _sprites.Add(info.Name, new Dictionary <Direction, Sprite[]>()); //Because we have a shitload of frames, we're going to store the average size as the AABB for each direction and each animation _averageAABBs.Add(info.Name, new Dictionary <Direction, Box2>()); var sprites = _sprites[info.Name]; var averageAABBs = _averageAABBs[info.Name]; AnimationStates.Add(info.Name, new AnimationState(info)); foreach (var dir in Enum.GetValues(typeof(Direction)).Cast <Direction>()) { sprites.Add(dir, new Sprite[info.Frames]); var thisDirSprites = sprites[dir]; for (var i = 0; i < info.Frames; i++) { var spritename = collection.Name.ToLowerInvariant() + "_" + info.Name.ToLowerInvariant() + "_" + DirectionToUriComponent(dir) + "_" + i; thisDirSprites[i] = resourceCache.GetSprite(spritename); var bounds = thisDirSprites[i].LocalBounds; x += bounds.Left; y += bounds.Top; w += bounds.Width; h += bounds.Height; t++; } averageAABBs.Add(dir, Box2.FromDimensions(x / t, y / t, w / t, h / t)); t = 0; x = 0; y = 0; w = 0; h = 0; } } }
public void UpdateGUIPosition() { _imgMainBg.Position = new Vector2i( (int)((CluwneLib.Window.Viewport.Size.X / 2f) - (_imgMainBg.ClientArea.Width / 2f)), (int)((CluwneLib.Window.Viewport.Size.Y / 2f) - (_imgMainBg.ClientArea.Height / 2f))); _imgMainBg.Update(0); _recStatus = Box2.FromDimensions(_imgMainBg.Position.X + 10, _imgMainBg.Position.Y + 63, 785, 21); _imgStatus.Position = new Vector2i((int)_recStatus.Left, (int)_recStatus.Top); _imgStatus.Update(0); _lblServer.Position = new Vector2i((int)_recStatus.Left + 5, (int)_recStatus.Top + 2); _lblServer.Update(0); _lblServerInfo.Position = new Vector2i(_lblServer.ClientArea.Right, _lblServer.ClientArea.Top); _lblServerInfo.Update(0); _lblMode.Position = new Vector2i(_lblServerInfo.ClientArea.Right + (int)_lastLblSpacing, _lblServerInfo.ClientArea.Top); _lblMode.Update(0); _lblModeInfo.Position = new Vector2i(_lblMode.ClientArea.Right, _lblMode.ClientArea.Top); _lblModeInfo.Update(0); _lblPlayers.Position = new Vector2i(_lblModeInfo.ClientArea.Right + (int)_lastLblSpacing, _lblModeInfo.ClientArea.Top); _lblPlayers.Update(0); _lblPlayersInfo.Position = new Vector2i(_lblPlayers.ClientArea.Right, _lblPlayers.ClientArea.Top); _lblPlayersInfo.Update(0); _lblPort.Position = new Vector2i(_lblPlayersInfo.ClientArea.Right + (int)_lastLblSpacing, _lblPlayersInfo.ClientArea.Top); _lblPort.Update(0); _lblPortInfo.Position = new Vector2i(_lblPort.ClientArea.Right, _lblPort.ClientArea.Top); _lblPortInfo.Update(0); _tabs.Position = _imgMainBg.Position + new Vector2i(5, 90); _tabs.Update(0); _lobbyChat.Position = new Vector2i(_imgMainBg.ClientArea.Left + 12, _imgMainBg.ClientArea.Bottom - _lobbyChat.ClientArea.Height - 12); //Wish the chat box wasnt such shit. Then i wouldnt have to do this here. _lobbyChat.Update(0); _imgChatBg.Position = new Vector2i(_lobbyChat.ClientArea.Left - 6, _lobbyChat.ClientArea.Top - 9); _imgChatBg.Update(0); _btnReady.Position = new Vector2i(_lobbyChat.ClientArea.Right - _btnReady.ClientArea.Width - 5, _lobbyChat.ClientArea.Top - _btnReady.ClientArea.Height - 8); _btnReady.Update(0); _btnBack.Position = new Vector2i(_lobbyChat.ClientArea.Left - _btnBack.ClientArea.Width - 20, _lobbyChat.ClientArea.Bottom - _btnBack.ClientArea.Height); _btnBack.Update(0); }
public override bool Update(Vector2i mouseS, IMapManager currentMap) { if (currentMap == null) { return(false); } mouseScreen = mouseS; mouseWorld = CluwneLib.ScreenToWorld(mouseScreen); if (pManager.CurrentPermission.IsTile) { return(false); } currentTile = currentMap.GetDefaultGrid().GetTile(mouseWorld); if (!RangeCheck()) { return(false); } var manager = IoCManager.Resolve <IClientEntityManager>(); IOrderedEnumerable <IEntity> snapToEntities = from IEntity entity in manager.GetEntitiesInRange(mouseWorld, snapToRange) where entity.Prototype == pManager.CurrentPrototype orderby (entity.GetComponent <ITransformComponent>( ).Position - mouseWorld).LengthSquared ascending select entity; if (snapToEntities.Any()) { IEntity closestEntity = snapToEntities.First(); if (closestEntity.TryGetComponent <ISpriteRenderableComponent>(out var component)) { var closestSprite = component.GetCurrentSprite(); var closestBounds = closestSprite.GetLocalBounds(); var closestRect = Box2.FromDimensions( closestEntity.GetComponent <ITransformComponent>().Position.X - closestBounds.Width / 2f, closestEntity.GetComponent <ITransformComponent>().Position.Y - closestBounds.Height / 2f, closestBounds.Width, closestBounds.Height); var sides = new Vector2[] { new Vector2(closestRect.Left + (closestRect.Width / 2f), closestRect.Top - closestBounds.Height / 2f), new Vector2(closestRect.Left + (closestRect.Width / 2f), closestRect.Bottom + closestBounds.Height / 2f), new Vector2(closestRect.Left - closestBounds.Width / 2f, closestRect.Top + (closestRect.Height / 2f)), new Vector2(closestRect.Right + closestBounds.Width / 2f, closestRect.Top + (closestRect.Height / 2f)) }; Vector2 closestSide = (from Vector2 side in sides orderby(side - mouseWorld).LengthSquared ascending select side).First(); mouseWorld = closestSide; mouseScreen = (Vector2i)CluwneLib.WorldToScreen(mouseWorld); } } if (CheckCollision()) { return(false); } return(true); }
public QuadNode(float x, float y, float width, float height) : this(Box2.FromDimensions(x, y, width, height)) { }
public void DrawTexture(Texture texture, Vector2 position, Color?modulate = null) { CheckDisposed(); DrawTextureRect(texture, Box2.FromDimensions(position, texture.Size / (float)Ppm), modulate); }
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); }
private Box2 GetQuad(Texture texture, Vector2 position) { return(Box2.FromDimensions(position, texture.Size / (float)Ppm)); }
public ILight[] LightsIntersectingRect(Box2 rect) { return(_lights .FindAll(l => Box2.FromDimensions(l.LightArea.LightPosition - l.LightArea.LightAreaSize / 2, l.LightArea.LightAreaSize).Intersects(rect)) .ToArray()); }
protected override void Draw(DrawingHandleBase handle, OverlaySpace overlay) { var drawHandle = (DrawingHandleWorld)handle; var mapId = _eyeManager.CurrentMap; var eye = _eyeManager.CurrentEye; var worldBounds = Box2.CenteredAround(eye.Position.Position, _clyde.ScreenSize / (float)EyeManager.PixelsPerMeter * eye.Zoom); // IF YOU ARE ABOUT TO INTRODUCE CHUNKING OR SOME OTHER OPTIMIZATION INTO THIS CODE: // -- THINK! -- // 1. "Is this going to make a critical atmos debugging tool harder to debug itself?" // 2. "Is this going to do anything that could cause the atmos debugging tool to use resources, server-side or client-side, when nobody's using it?" // 3. "Is this going to make it harder for atmos programmers to add data that may not be chunk-friendly into the atmos debugger?" // Nanotrasen needs YOU! to avoid premature optimization in critical debugging tools - 20kdc foreach (var mapGrid in _mapManager.FindGridsIntersecting(mapId, worldBounds)) { if (!_atmosDebugOverlaySystem.HasData(mapGrid.Index)) { continue; } var gridBounds = new Box2(mapGrid.WorldToLocal(worldBounds.BottomLeft), mapGrid.WorldToLocal(worldBounds.TopRight)); for (var pass = 0; pass < 2; pass++) { foreach (var tile in mapGrid.GetTilesIntersecting(gridBounds)) { var dataMaybeNull = _atmosDebugOverlaySystem.GetData(mapGrid.Index, tile.GridIndices); if (dataMaybeNull != null) { var data = (SharedAtmosDebugOverlaySystem.AtmosDebugOverlayData)dataMaybeNull !; if (pass == 0) { // -- Mole Count -- float total = 0; switch (_atmosDebugOverlaySystem.CfgMode) { case AtmosDebugOverlayMode.TotalMoles: foreach (float f in data.Moles) { total += f; } break; case AtmosDebugOverlayMode.GasMoles: total = data.Moles[_atmosDebugOverlaySystem.CfgSpecificGas]; break; case AtmosDebugOverlayMode.Temperature: total = data.Temperature; break; } var interp = ((total - _atmosDebugOverlaySystem.CfgBase) / _atmosDebugOverlaySystem.CfgScale); Color res; if (_atmosDebugOverlaySystem.CfgCBM) { // Greyscale interpolation res = Color.InterpolateBetween(Color.Black, Color.White, interp); } else { // Red-Green-Blue interpolation if (interp < 0.5f) { res = Color.InterpolateBetween(Color.Red, Color.Green, interp * 2); } else { res = Color.InterpolateBetween(Color.Green, Color.Blue, (interp - 0.5f) * 2); } } res = res.WithAlpha(0.75f); drawHandle.DrawRect(Box2.FromDimensions(mapGrid.LocalToWorld(new Vector2(tile.X, tile.Y)), new Vector2(1, 1)), res); } else if (pass == 1) { // -- Blocked Directions -- void CheckAndShowBlockDir(AtmosDirection dir) { if (data.BlockDirection.HasFlag(dir)) { // Account for South being 0. var atmosAngle = dir.ToAngle() - Angle.FromDegrees(90); var atmosAngleOfs = atmosAngle.ToVec() * 0.45f; var atmosAngleOfsR90 = new Vector2(atmosAngleOfs.Y, -atmosAngleOfs.X); var tileCentre = new Vector2(tile.X + 0.5f, tile.Y + 0.5f); var basisA = mapGrid.LocalToWorld(tileCentre + atmosAngleOfs - atmosAngleOfsR90); var basisB = mapGrid.LocalToWorld(tileCentre + atmosAngleOfs + atmosAngleOfsR90); drawHandle.DrawLine(basisA, basisB, Color.Azure); } } CheckAndShowBlockDir(AtmosDirection.North); CheckAndShowBlockDir(AtmosDirection.South); CheckAndShowBlockDir(AtmosDirection.East); CheckAndShowBlockDir(AtmosDirection.West); // -- Pressure Direction -- if (data.PressureDirection != AtmosDirection.Invalid) { // Account for South being 0. var atmosAngle = data.PressureDirection.ToAngle() - Angle.FromDegrees(90); var atmosAngleOfs = atmosAngle.ToVec() * 0.4f; var tileCentre = new Vector2(tile.X + 0.5f, tile.Y + 0.5f); var basisA = mapGrid.LocalToWorld(tileCentre); var basisB = mapGrid.LocalToWorld(tileCentre + atmosAngleOfs); drawHandle.DrawLine(basisA, basisB, Color.Blue); } // -- Excited Groups -- if (data.InExcitedGroup) { var tilePos = new Vector2(tile.X, tile.Y); var basisA = mapGrid.LocalToWorld(tilePos); var basisB = mapGrid.LocalToWorld(tilePos + new Vector2(1.0f, 1.0f)); var basisC = mapGrid.LocalToWorld(tilePos + new Vector2(0.0f, 1.0f)); var basisD = mapGrid.LocalToWorld(tilePos + new Vector2(1.0f, 0.0f)); drawHandle.DrawLine(basisA, basisB, Color.Cyan); drawHandle.DrawLine(basisC, basisD, Color.Cyan); } } } } } } }
public ILight[] LightsIntersectingPoint(Vector2 point) { return(_lights .FindAll(l => Box2.FromDimensions(l.LightArea.LightPosition - l.LightArea.LightAreaSize / 2, l.LightArea.LightAreaSize).Contains(point)) .ToArray()); }
protected override void Draw(DrawingHandleBase handle, OverlaySpace overlay) { var drawHandle = (DrawingHandleWorld)handle; var mapId = _eyeManager.CurrentMap; var eye = _eyeManager.CurrentEye; var worldBounds = Box2.CenteredAround(eye.Position.Position, _clyde.ScreenSize / (float)EyeManager.PixelsPerMeter * eye.Zoom); // IF YOU ARE ABOUT TO INTRODUCE CHUNKING OR SOME OTHER OPTIMIZATION INTO THIS CODE: // -- THINK! -- // 1. "Is this going to make a critical atmos debugging tool harder to debug itself?" // 2. "Is this going to do anything that could cause the atmos debugging tool to use resources, server-side or client-side, when nobody's using it?" // 3. "Is this going to make it harder for atmos programmers to add data that may not be chunk-friendly into the atmos debugger?" // Nanotrasen needs YOU! to avoid premature optimization in critical debugging tools - 20kdc foreach (var mapGrid in _mapManager.FindGridsIntersecting(mapId, worldBounds)) { if (!_atmosDebugOverlaySystem.HasData(mapGrid.Index)) { continue; } var gridBounds = new Box2(mapGrid.WorldToLocal(worldBounds.BottomLeft), mapGrid.WorldToLocal(worldBounds.TopRight)); for (var pass = 0; pass < 3; pass++) { foreach (var tile in mapGrid.GetTilesIntersecting(gridBounds)) { var dataMaybeNull = _atmosDebugOverlaySystem.GetData(mapGrid.Index, tile.GridIndices); if (dataMaybeNull != null) { var data = (SharedAtmosDebugOverlaySystem.AtmosDebugOverlayData)dataMaybeNull !; if (pass == 0) { float total = 0; foreach (float f in data.Moles) { total += f; } var interp = total / (Atmospherics.MolesCellStandard * 2); var res = Color.InterpolateBetween(Color.Red, Color.Green, interp).WithAlpha(0.75f); drawHandle.DrawRect(Box2.FromDimensions(mapGrid.LocalToWorld(new Vector2(tile.X, tile.Y)), new Vector2(1, 1)), res); } else if (pass == 1) { if (data.PressureDirection != AtmosDirection.Invalid) { var atmosAngle = data.PressureDirection.ToAngle(); var atmosAngleOfs = atmosAngle.ToVec() * 0.4f; var tileCentre = new Vector2(tile.X + 0.5f, tile.Y + 0.5f); var basisA = mapGrid.LocalToWorld(tileCentre); var basisB = mapGrid.LocalToWorld(tileCentre + atmosAngleOfs); drawHandle.DrawLine(basisA, basisB, Color.Blue); } } else if (pass == 2) { if (data.InExcitedGroup) { var tilePos = new Vector2(tile.X, tile.Y); var basisA = mapGrid.LocalToWorld(tilePos); var basisB = mapGrid.LocalToWorld(tilePos + new Vector2(1.0f, 1.0f)); var basisC = mapGrid.LocalToWorld(tilePos + new Vector2(0.0f, 1.0f)); var basisD = mapGrid.LocalToWorld(tilePos + new Vector2(1.0f, 0.0f)); drawHandle.DrawLine(basisA, basisB, Color.Cyan); drawHandle.DrawLine(basisC, basisD, Color.Cyan); } } } } } } }