using System; using System.Collections.Generic; using System.Diagnostics; using System.Xml.Linq; using FarseerPhysics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System.Collections.ObjectModel; namespace Barotrauma { class MapEntity : Entity { public static List mapEntityList = new List(); //which entities have been selected for editing protected static List selectedList = new List(); protected static GUIComponent editingHUD; protected static Vector2 selectionPos = Vector2.Zero; protected static Vector2 selectionSize = Vector2.Zero; protected static Vector2 startMovingPos = Vector2.Zero; private MapEntityPrefab prefab; protected List linkedToID; //observable collection because some entities may need to be notified when the collection is modified public ObservableCollection linkedTo; //protected float soundRange; //protected float sightRange; //is the mouse inside the rect protected bool isHighlighted; protected bool isSelected; private static bool disableSelect; public static bool DisableSelect { get { return disableSelect; } set { disableSelect = value; if (disableSelect) { startMovingPos = Vector2.Zero; selectionSize = Vector2.Zero; selectionPos = Vector2.Zero; } } } public static bool SelectedAny { get { return selectedList.Count > 0; } } public bool MoveWithLevel { get; set; } //the position and dimensions of the entity protected Rectangle rect; private static bool resizing; private int resizeDirX, resizeDirY; public virtual Rectangle Rect { get { return rect; } set { rect = value; } } public Rectangle WorldRect { get { return Submarine == null ? rect : new Rectangle((int)(Submarine.Position.X + rect.X), (int)(Submarine.Position.Y + rect.Y), rect.Width, rect.Height); } } public virtual Sprite Sprite { get { return null; } } public virtual bool IsLinkable { get { return false; } } public virtual bool SelectableInEditor { get { return true; } } public override Vector2 Position { get { Vector2 rectPos = new Vector2( rect.X + rect.Width / 2.0f, rect.Y - rect.Height / 2.0f); //if (MoveWithLevel) rectPos += Level.Loaded.Position; return rectPos; } } public override Vector2 SimPosition { get { return ConvertUnits.ToSimUnits(Position); } } public float SoundRange { get { if (aiTarget == null) return 0.0f; return aiTarget.SoundRange; } set { aiTarget.SoundRange = value; } } public float SightRange { get { if (aiTarget == null) return 0.0f; return aiTarget.SightRange; } set { aiTarget.SightRange = value; } } public bool IsHighlighted { get { return isHighlighted; } set { isHighlighted = value; } } public bool IsSelected { get { return isSelected; } set { isSelected = value; } } protected bool ResizeHorizontal { get { return prefab != null && prefab.ResizeHorizontal; } } protected bool ResizeVertical { get { return prefab != null && prefab.ResizeVertical; } } public virtual string Name { get { return ""; } } public MapEntity(MapEntityPrefab prefab, Submarine submarine) : base(submarine) { this.prefab = prefab; } public virtual void Move(Vector2 amount) { rect.X += (int)amount.X; rect.Y += (int)amount.Y; } public virtual bool IsMouseOn(Vector2 position) { return (Submarine.RectContains(WorldRect, position)); } protected void InsertToList() { int i = 0; if (Sprite==null) { mapEntityList.Add(this); return; } while (i= 0; i-- ) { linkedTo[i].RemoveLinked(this); } linkedTo.Clear(); } } static Dictionary timeElapsed = new Dictionary(); /// /// Call Update() on every object in Entity.list /// public static void UpdateAll(Camera cam, float deltaTime) { foreach (Hull hull in Hull.hullList) { hull.Update(cam, deltaTime); } foreach (Gap gap in Gap.GapList) { gap.Update(cam, deltaTime); } foreach (Item item in Item.ItemList) { item.Update(cam, deltaTime); } Item.Spawner.Update(); Item.Remover.Update(); } public virtual void Update(Camera cam, float deltaTime) { } /// /// Update the selection logic in editmap-screen /// public static void UpdateSelecting(Camera cam) { if (resizing) { if (selectedList.Count == 0) resizing = false; return; } foreach (MapEntity e in mapEntityList) { e.isHighlighted = false; e.isSelected = false; } if (DisableSelect) { DisableSelect = false; return; } if (GUIComponent.MouseOn != null || !PlayerInput.MouseInsideWindow) return; if (MapEntityPrefab.Selected != null) { selectionPos = Vector2.Zero; selectedList.Clear(); return; } if (PlayerInput.KeyDown(Keys.Delete)) { foreach (MapEntity e in selectedList) e.Remove(); selectedList.Clear(); } Vector2 position = cam.ScreenToWorld(PlayerInput.MousePosition); MapEntity highLightedEntity = null; if (startMovingPos == Vector2.Zero) { foreach (MapEntity e in mapEntityList) { if (!e.SelectableInEditor) continue; if (highLightedEntity == null || e.Sprite == null || (highLightedEntity.Sprite != null && e.Sprite.Depth < highLightedEntity.Sprite.Depth)) { if (e.IsMouseOn(position)) highLightedEntity = e; } e.isSelected = false; } if (highLightedEntity != null) highLightedEntity.isHighlighted = true; } foreach (MapEntity e in selectedList) { e.isSelected = true; } //started moving selected entities if (startMovingPos != Vector2.Zero) { if (PlayerInput.LeftButtonReleased()) { //mouse released -> move the entities to the new position of the mouse Vector2 moveAmount = position - startMovingPos; moveAmount = Submarine.VectorToWorldGrid(moveAmount); if (moveAmount != Vector2.Zero) { foreach (MapEntity e in selectedList) e.Move(moveAmount); } startMovingPos = Vector2.Zero; } } //started dragging a "selection rectangle" else if (selectionPos != Vector2.Zero) { selectionSize.X = position.X - selectionPos.X; selectionSize.Y = selectionPos.Y - position.Y; List newSelection = new List();// FindSelectedEntities(selectionPos, selectionSize); if (Math.Abs(selectionSize.X) > Submarine.GridSize.X || Math.Abs(selectionSize.Y) > Submarine.GridSize.Y) { newSelection = FindSelectedEntities(selectionPos, selectionSize); } else { if (highLightedEntity != null) newSelection.Add(highLightedEntity); } foreach (MapEntity e in newSelection) { e.isHighlighted = true; } if (PlayerInput.LeftButtonReleased()) { if (PlayerInput.KeyDown(Keys.LeftControl) || PlayerInput.KeyDown(Keys.RightControl)) { foreach (MapEntity e in newSelection) { bool alreadySelected = false; foreach (MapEntity e2 in selectedList) { if (e.ID == e2.ID) alreadySelected = true; } if (alreadySelected) selectedList.Remove(e); else selectedList.Add(e); } } else { selectedList = newSelection; } selectionPos = Vector2.Zero; selectionSize = Vector2.Zero; } } //default, not doing anything specific yet else { if (PlayerInput.LeftButtonHeld() && PlayerInput.KeyUp(Keys.Space)) { //if clicking a selected entity, start moving it foreach (MapEntity e in selectedList) { if (e.IsMouseOn(position)) startMovingPos = position; } selectionPos = position; } } } /// /// Draw the "selection rectangle" and outlines of entities that are being dragged (if any) /// public static void DrawSelecting(SpriteBatch spriteBatch, Camera cam) { if (GUIComponent.MouseOn != null) return; Vector2 position = PlayerInput.MousePosition; position = cam.ScreenToWorld(position); if (startMovingPos != Vector2.Zero) { Vector2 moveAmount = position - startMovingPos; moveAmount = Submarine.VectorToWorldGrid(moveAmount); moveAmount.Y = -moveAmount.Y; //started moving the selected entities if (moveAmount != Vector2.Zero) { foreach (MapEntity e in selectedList) GUI.DrawRectangle(spriteBatch, new Vector2(e.WorldRect.X, -e.WorldRect.Y) + moveAmount, new Vector2(e.rect.Width, e.rect.Height), Color.DarkRed); //stop dragging the "selection rectangle" selectionPos = Vector2.Zero; } } if (selectionPos != null && selectionPos != Vector2.Zero) { GUI.DrawRectangle(spriteBatch, new Vector2(selectionPos.X, -selectionPos.Y), selectionSize, Color.DarkRed); } } /// /// Call DrawEditing() if only one entity is selected /// public static void Edit(SpriteBatch spriteBatch, Camera cam) { if (selectedList.Count == 1) { selectedList[0].DrawEditing(spriteBatch, cam); if (selectedList[0].ResizeHorizontal || selectedList[0].ResizeVertical) { selectedList[0].DrawResizing(spriteBatch, cam); } } else { if (editingHUD == null) return; foreach (GUIComponent component in editingHUD.children) { var textBox = component as GUITextBox; if (textBox == null) continue; textBox.Deselect(); } editingHUD = null; } } public static void SelectEntity(MapEntity entity) { foreach (MapEntity e in selectedList) { e.isSelected = false; } selectedList.Clear(); entity.isSelected = true; selectedList.Add(entity); } public virtual void DrawEditing(SpriteBatch spriteBatch, Camera cam) {} private void DrawResizing(SpriteBatch spriteBatch, Camera cam) { isHighlighted = true; int startX = ResizeHorizontal ? -1 : 0; int StartY = ResizeVertical ? -1 : 0; for (int x = startX; x < 2; x += 2) { for (int y = StartY; y < 2; y += 2) { Vector2 handlePos = cam.WorldToScreen(Position + new Vector2(x * (rect.Width * 0.5f + 5), y * (rect.Height * 0.5f + 5))); bool highlighted = Vector2.Distance(PlayerInput.MousePosition, handlePos)<5.0f; GUI.DrawRectangle(spriteBatch, handlePos - new Vector2(3.0f, 3.0f), new Vector2(6.0f, 6.0f), Color.White * (highlighted ? 1.0f : 0.6f), true); if (highlighted) { if (PlayerInput.LeftButtonDown()) { selectionPos = Vector2.Zero; resizeDirX = x; resizeDirY = y; resizing = true; } } } } if (resizing) { Vector2 placePosition = new Vector2(rect.X, rect.Y); Vector2 placeSize = new Vector2(rect.Width, rect.Height); Vector2 mousePos = Submarine.MouseToWorldGrid(cam, Submarine.MainSub); if (resizeDirX >0) { mousePos.X = Math.Max(mousePos.X, rect.X + Submarine.GridSize.X); placeSize.X = mousePos.X - placePosition.X; } else if (resizeDirX <0) { mousePos.X = Math.Min(mousePos.X, rect.Right - Submarine.GridSize.X); placeSize.X = (placePosition.X + placeSize.X)-mousePos.X; placePosition.X = mousePos.X; } if (resizeDirY < 0) { mousePos.Y = Math.Min(mousePos.Y, rect.Y - Submarine.GridSize.Y); placeSize.Y = placePosition.Y-mousePos.Y; } else if (resizeDirY > 0) { mousePos.Y = Math.Max(mousePos.Y, rect.Y - rect.Height + Submarine.GridSize.X); placeSize.Y = mousePos.Y - (rect.Y - rect.Height); placePosition.Y = mousePos.Y; } if ((int)placePosition.X != rect.X || (int)placePosition.Y != rect.Y || (int)placeSize.X != rect.Width || (int)placeSize.Y != rect.Height) { Rect = new Rectangle((int)placePosition.X, (int)placePosition.Y, (int)placeSize.X, (int)placeSize.Y); } if (!PlayerInput.LeftButtonHeld()) { resizing = false; } } } public static List FindMapEntities(Vector2 pos) { List foundEntities = new List(); foreach (MapEntity e in mapEntityList) { if (Submarine.RectContains(e.rect, pos)) foundEntities.Add(e); } return foundEntities; } public static MapEntity FindMapEntity(Vector2 pos) { foreach (MapEntity e in mapEntityList) { if (Submarine.RectContains(e.rect, pos)) return e; } return null; } /// /// Find entities whose rect intersects with the "selection rect" /// public static List FindSelectedEntities(Vector2 pos, Vector2 size) { List foundEntities = new List(); Rectangle selectionRect = Submarine.AbsRect(pos, size); foreach (MapEntity e in mapEntityList) { if (!e.SelectableInEditor) continue; if (Submarine.RectsOverlap(selectionRect, e.rect)) foundEntities.Add(e); } return foundEntities; } public virtual XElement Save(XElement parentElement) { DebugConsole.ThrowError("Saving entity " + GetType() + " failed."); return null; } /// /// Update the linkedTo-lists of the entities based on the linkedToID-lists /// Has to be done after all the entities have been loaded (an entity can't /// be linked to some other entity that hasn't been loaded yet) /// public static void MapLoaded(Submarine sub) { foreach (MapEntity e in mapEntityList) { if (e.Submarine != sub) continue; if (e.linkedToID == null) continue; if (e.linkedToID.Count == 0) continue; e.linkedTo.Clear(); foreach (ushort i in e.linkedToID) { MapEntity linked = FindEntityByID(i) as MapEntity; if (linked != null) e.linkedTo.Add(linked); } } List linkedSubs = new List(); for (int i = 0; i