public override void Update() { if (GetKeyPressed(Keys.Tab)) { FileDialog(); } if (AStarGrid != null) { if (Mouse.LeftButton == ButtonState.Pressed && Keyboard.IsKeyUp(Keys.LeftShift) && Keyboard.IsKeyUp(Keys.Space)) { if (GridSnapMouse.X < 960 && GridSnapMouse.Y < 540 && GridSnapMouse.X > 0 && GridSnapMouse.Y > 0) { List <CollideObject> solids = CollideObject.GetList(typeof(Solid)); if (solids != null) { bool canCreate = true; foreach (Solid obj in solids) { if (obj.X == GridSnapMouse.X && obj.Y == GridSnapMouse.Y) { canCreate = false; } } if (canCreate) { new Brick(GridSnapMouse.X, GridSnapMouse.Y); } } else { new Brick(GridSnapMouse.X, GridSnapMouse.Y); } } } if (Mouse.RightButton == ButtonState.Pressed) { List <CollideObject> solids = CollideObject.GetList(typeof(Solid)); foreach (Solid obj in solids.ToList()) { if (obj.X == GridSnapMouse.X && obj.Y == GridSnapMouse.Y) { DestroyInstance(obj); } } } } if (GetKeyPressed(Keys.R)) { RestartRoom(); } base.Update(); }
public CollideObject BoxCollision(float ExtraX, float ExtraY, CollideObject obj) { if (BoundingBox.X + ExtraX + BoundingBox.Width > obj.BoundingBox.X && BoundingBox.X + ExtraX < obj.BoundingBox.X + obj.BoundingBox.Width && BoundingBox.Y + ExtraY + BoundingBox.Height > obj.BoundingBox.Y && BoundingBox.Y + ExtraY < obj.BoundingBox.Y + obj.BoundingBox.Height) { return(obj); } return(null); }
public override void DrawGUI() { List <CollideObject> tempList = CollideObject.GetList(typeof(Solid)); if (tempList != null) { SpriteBatch.DrawString(Font, tempList.Count.ToString(), new Vector2(20, 80), Color.Black); } }
public void AddInstance(CollideObject gameObject, Type type) { if (!CollisionList.ContainsKey(type)) { CollisionList.Add(type, new List <CollideObject>()); } CollisionList.TryGetValue(type, out list); list.Add(gameObject); }
public bool LineToEdgeIntersection(Vector2 lineStart, Vector2 lineEnd, CollideObject obj) { //BoundingPoints Object Vector2 OBottomRight = obj.BoxPosition + new Vector2(obj.BoxWidth, obj.BoxHeight); Vector2 OBottomLeft = obj.BoxPosition + new Vector2(0, obj.BoxHeight); Vector2 OTopLeft = obj.BoxPosition + new Vector2(0, 0); Vector2 OTopRight = obj.BoxPosition + new Vector2(obj.BoxWidth, 0); OBottomRight = Rotate(obj.BoxX + obj.BoxWidth, obj.BoxY + obj.BoxHeight, obj.Angle, OBottomRight + new Vector2(0, 0)); OBottomLeft = Rotate(obj.BoxX + obj.BoxWidth, obj.BoxY + obj.BoxHeight, obj.Angle, OBottomLeft + new Vector2(0, 0)); OTopLeft = Rotate(obj.BoxX + obj.BoxWidth, obj.BoxY + obj.BoxHeight, obj.Angle, OTopLeft + new Vector2(0, 0)); OTopRight = Rotate(obj.BoxX + obj.BoxWidth, obj.BoxY + obj.BoxHeight, obj.Angle, OTopRight + new Vector2(0, 0)); Vector2[] ORectangleEdges = new Vector2[4]; Vector2[] LineEdges = new Vector2[2]; ORectangleEdges[0] = OBottomRight; ORectangleEdges[1] = OBottomLeft; ORectangleEdges[2] = OTopLeft; ORectangleEdges[3] = OTopRight; LineEdges[0] = lineStart; LineEdges[1] = lineEnd; for (int i = 0; i < LineEdges.Length; i++) { for (int j = 0; j < ORectangleEdges.Length; j++) { if (LineIntersection(LineEdges[i], LineEdges[(i + 1) % LineEdges.Length], ORectangleEdges[j], ORectangleEdges[(j + 1) % ORectangleEdges.Length])) { return(true); } } } return(false); }
public void DestroyInstance(CollideObject gameObject, Type type) { CollisionList.TryGetValue(type, out list); list.Remove(gameObject); }
public bool CheckEdges(CollideObject t, CollideObject o) { //This whole operation of checking edges is expensive and therefore we make sure //that the box is actually within the possible viscinity of collision //Which I actually don't do right now XDDDDDDXDXDXDXDXDDXXDXDDDD //BoundingPoints This Vector2 TBottomRight = t.BoxPosition + new Vector2(t.BoxWidth, t.BoxHeight); Vector2 TBottomLeft = t.BoxPosition + new Vector2(0, t.BoxHeight); Vector2 TTopLeft = t.BoxPosition + new Vector2(0, 0); Vector2 TTopRight = t.BoxPosition + new Vector2(t.BoxWidth, 0); //BoundingPoints Other Vector2 OBottomRight = o.BoxPosition + new Vector2(o.BoxWidth, o.BoxHeight); Vector2 OBottomLeft = o.BoxPosition + new Vector2(0, o.BoxHeight); Vector2 OTopLeft = o.BoxPosition + new Vector2(0, 0); Vector2 OTopRight = o.BoxPosition + new Vector2(o.BoxWidth, 0); //RotatePoints This TBottomRight = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Angle, TBottomRight + new Vector2(-0.5f, -0.5f)); TBottomLeft = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Angle, TBottomLeft + new Vector2(0.5f, -0.5f)); TTopLeft = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Angle, TTopLeft + new Vector2(0.5f, 0.5f)); TTopRight = Rotate(t.BoxX + t.BoxWidth / 2, t.BoxY + t.BoxHeight / 2, t.Angle, TTopRight + new Vector2(-0.5f, 0.5f)); //RotatePoints Other OBottomRight = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Angle, OBottomRight + new Vector2(-0.5f, -0.5f)); OBottomLeft = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Angle, OBottomLeft + new Vector2(0.5f, -0.5f)); OTopLeft = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Angle, OTopLeft + new Vector2(0.5f, 0.5f)); OTopRight = Rotate(o.BoxX + o.BoxWidth / 2, o.BoxY + o.BoxHeight / 2, o.Angle, OTopRight + new Vector2(-0.5f, 0.5f)); //Define all Edges from RotatePoints Vector2[] TRectangleEdges = new Vector2[4]; Vector2[] ORectangleEdges = new Vector2[4]; Vector2[] TRectangleDiagonals = new Vector2[4]; Vector2[] ORectangleDiagonals = new Vector2[4]; TRectangleEdges[0] = TBottomRight; TRectangleEdges[1] = TBottomLeft; TRectangleEdges[2] = TTopLeft; TRectangleEdges[3] = TTopRight; ORectangleEdges[0] = OBottomRight; ORectangleEdges[1] = OBottomLeft; ORectangleEdges[2] = OTopLeft; ORectangleEdges[3] = OTopRight; TRectangleDiagonals[0] = TTopLeft; TRectangleDiagonals[1] = TBottomRight; TRectangleDiagonals[2] = TBottomLeft; TRectangleDiagonals[3] = TTopRight; ORectangleDiagonals[0] = OTopLeft; ORectangleDiagonals[1] = OBottomRight; ORectangleDiagonals[2] = OBottomLeft; ORectangleDiagonals[3] = OTopRight; //Check Intersection for rectangle edges for (int i = 0; i < TRectangleEdges.Length; i++) { for (int j = 0; j < ORectangleEdges.Length; j++) { if (LineIntersection(TRectangleEdges[i], TRectangleEdges[(i + 1) % TRectangleEdges.Length], ORectangleEdges[j], ORectangleEdges[(j + 1) % ORectangleEdges.Length])) { return(true); } } } //Check Intersection for internal diagonal lines for (int i = 0; i < TRectangleDiagonals.Length; i++) { for (int j = 0; j < ORectangleDiagonals.Length; j++) { if (LineIntersection(TRectangleDiagonals[i], TRectangleDiagonals[(i + 1) % TRectangleDiagonals.Length], ORectangleDiagonals[j], ORectangleDiagonals[(j + 1) % ORectangleDiagonals.Length])) { return(true); } } } return(false); }
public override void Update() { //check keys W = Keyboard.IsKeyDown(Keys.W); A = Keyboard.IsKeyDown(Keys.A); S = Keyboard.IsKeyDown(Keys.S); D = Keyboard.IsKeyDown(Keys.D); //axes axisXDir = Convert.ToInt32(D) - Convert.ToInt32(A); axisYDir = Convert.ToInt32(S) - Convert.ToInt32(W); //acceleration axisXAdd = axisXDir * axisXAcceleration; axisYAdd = axisYDir * axisYAcceleration; //restitution axisXSub = MathHelper.Min(axisXRestitution, Math.Abs(xSpeed)) * Math.Sign(xSpeed) * Convert.ToInt32(axisXDir == 0); axisYSub = MathHelper.Min(axisYRestitution, Math.Abs(ySpeed)) * Math.Sign(ySpeed) * Convert.ToInt32(axisYDir == 0); xSpeed = MathHelper.Clamp(xSpeed + axisXAdd - axisXSub, -axisXMax, axisXMax); ySpeed = MathHelper.Clamp(ySpeed + axisYAdd - axisYSub, -axisYMax, axisYMax); //adjust axes if (xSpeed != 0 && ySpeed != 0) { var dist = Math.Sqrt((xSpeed * xSpeed) + (ySpeed * ySpeed)); var mdist = MathHelper.Min(movementSpeed + 1, (float)dist); xSpeed = (xSpeed / (float)dist) * (mdist); ySpeed = (ySpeed / (float)dist) * (mdist); } CollideBlock = BoxCollisionList(xSpeed, 0, typeof(Solid)); if (CollideBlock != null) { //stop moving and get as close as possible xSpeed = 0; X = (float)Math.Round(X); for (int i = 0; i < Math.Abs(xSpeed); i++) { if (BoxCollision(Math.Sign(xSpeed), 0, CollideBlock) != null) { break; } X += Math.Sign(xSpeed); } //prevents getting stuck when moving diagonally if (X > CollideBlock.X && BoxCollision(0, 0, CollideBlock) != null) { float p_difference = Math.Abs((X - Sprite.Texture.Width / 2) - (CollideBlock.X + CollideBlock.Sprite.Texture.Width / 2)); if (p_difference > 0) { X += Math.Sign(p_difference); } } else if (X <= CollideBlock.X && BoxCollision(0, 0, CollideBlock) != null) { float p_difference = Math.Abs((X + Sprite.Texture.Width / 2) - (CollideBlock.X - CollideBlock.Sprite.Texture.Width / 2)); if (p_difference > 0) { X -= Math.Sign(p_difference); } } } CollideBlock = BoxCollisionList(0, ySpeed, typeof(Solid)); if (CollideBlock != null) { //stop moving and get as close as possible ySpeed = 0; Y = (float)Math.Round(Y); for (int i = 0; i < Math.Abs(ySpeed); i++) { if (BoxCollision(0, Math.Sign(ySpeed), CollideBlock) != null) { break; } Y += Math.Sign(ySpeed); } } X += xSpeed; Y += ySpeed; X = MathHelper.Clamp(X, 0 + Sprite.Bounds.X / 2, Camera.Width - Sprite.Bounds.X / 2); Y = MathHelper.Clamp(Y, 0 + Sprite.Bounds.Y / 2, Camera.Height - Sprite.Bounds.Y / 2); base.Update(); }