/// <summary> /// 获取编辑器对象的宽度 /// </summary> /// <returns></returns> //public float GetWidth() //{ // return Math.Abs(this.Left - this.Right); //} ///// <summary> ///// 获取编辑器对象的高度 ///// </summary> ///// <returns></returns> //public float GetHeight() //{ // return Math.Abs(this.Top - this.Bottom); //} /// <summary> /// 矩形区域与形参中的区域是否重叠交叉 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="right"></param> /// <param name="bottom"></param> /// <returns></returns> public bool IsCross(float x,float y,float right,float bottom) { bool isCoross = false; System.Drawing.RectangleF srcRect = new System.Drawing.RectangleF(this.Left, this.Top, this.Width, this.Height); System.Drawing.RectangleF destRect = new System.Drawing.RectangleF(x, y, right, bottom); if (srcRect.IntersectsWith(destRect)) isCoross = true; return isCoross; }
public void update() { MouseState mouseState; if (entitiesInSelection.Count > 0) { if (inputState.IsNewRightMouseClick(out mouseState)) { if (Global.Viewport.Contains(mouseState.Position)) { Vector2 pos = Global.Camera.ScreenToWorld(mouseState.Position.ToVector2()); //Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Goal), typeof(component.Physics), typeof(component.Formation))); int2 goalPos = new int2((int)pos.X / Global.tileSize, (int)pos.Y / Global.tileSize); Bag<Entity> bag = new Bag<Entity>(); PathGoal pathGoal = new PathGoal(entitiesInSelection, disFieldMixer, new int2(Global.mapWidth, Global.mapHeight), goalPos); setPathGoal(pathGoal); pathGoal.updatePath(); EntityFormation formation = new EntityFormation(entitiesInSelection, disFieldMixer, goalPos); foreach (Entity e in entitiesInSelection) { e.GetComponent<component.Formation>().EntityFormation = formation; } formation.update(); } } } if (Mouse.GetState().LeftButton == ButtonState.Pressed && !isSelecting) { isSelecting = true; firstCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); foreach (Entity old in entitiesInSelection) { old.GetComponent<component.HealthComponent>().Visible = false; } entitiesInSelection.Clear(); } else if (Mouse.GetState().LeftButton == ButtonState.Released && isSelecting) { isSelecting = false; Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y)); Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y)); RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); Console.WriteLine(rect.X / 32 + " - " + rect.Y / 32); if (rect.Width > 0 && rect.Height > 0) { Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent), typeof(component.Unit))); foreach (Entity e in entities) { if (e.GetComponent<component.Unit>().Lord != lord) continue; component.Physics phys = e.GetComponent<component.Physics>(); Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize); RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f); if (rect.IntersectsWith(entityRect)) { e.GetComponent<component.HealthComponent>().Visible = true; entitiesInSelection.Add(e); } } if (onSelectUnit != null) onSelectUnit(); } } if (isSelecting) { Vector2 secondCorner = Global.Camera.ScreenToWorld(Mouse.GetState().Position.ToVector2()); Vector2 topLeft = new Vector2(Math.Min(firstCorner.X, secondCorner.X), Math.Min(firstCorner.Y, secondCorner.Y)); Vector2 bottomRight = new Vector2(Math.Max(firstCorner.X, secondCorner.X), Math.Max(firstCorner.Y, secondCorner.Y)); RectangleF rect = new RectangleF(topLeft.X, topLeft.Y, bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y); foreach (Entity e in tempEntitiesInSelection) { e.GetComponent<component.HealthComponent>().Visible = false; } tempEntitiesInSelection.Clear(); if (rect.Width > 0 && rect.Height > 0) { Bag<Entity> entities = entityWorld.EntityManager.GetEntities(Aspect.All(typeof(component.Physics), typeof(component.Formation), typeof(component.HealthComponent))); foreach (Entity e in entities) { component.Physics phys = e.GetComponent<component.Physics>(); Vector2 newPos = new Vector2(phys.Position.X * Global.tileSize, phys.Position.Y * Global.tileSize); RectangleF entityRect = new RectangleF(newPos.X - 0.5f * 32f, newPos.Y - 0.5f * 32f, 1f * 32f, 1f * 32f); if (rect.IntersectsWith(entityRect)) { e.GetComponent<component.HealthComponent>().Visible = true; tempEntitiesInSelection.Add(e); } } } } }