public static bool runTests() { Box b1 = new Box(0, 0, 2, 2); Rectangle r1 = new Rectangle(1, -1, 1, 4); Box expected1 = new Box(1, 0, 1, 2); Box result1 = b1.shrinkWrap(r1); if (!expected1.Equals(result1)) return false; Rectangle r2 = new Rectangle(1, 0, 3, 3); Box expected2 = new Box(1, 0, 1, 2); Box result2 = b1.shrinkWrap(r2); if (!expected2.Equals(result2)) return false; Rectangle r3 = new Rectangle(-1, -1, 4, 4); Box result3 = b1.shrinkWrap(r3); if (!b1.Equals(result3)) return false; Rectangle r4 = new Rectangle(1, 1, 1, 1); Box result4 = b1.shrinkWrap(r4); if (!result4.Equals(new Box(r4))) return false; Box b2 = new Box(0.5f,0.5f, 2f,2f); Rectangle r5 = new Rectangle(0, 0, 3, 3); Box result5 = b2.shrinkWrap(r5); if (!result5.Equals(b2)) return false; return true; }
protected Vector2 adjustPos(ref Box mover, MapObject platform, Axis axis) { if (platform.Polygon != null) return Vector2.Zero; //return adjustPos(ref mover, platform.Polygon); else return adjustPos(ref mover, platform.Bounds, axis); }
public override void Initialize(Animation animation, Vector2 startPos) { base.Initialize(animation, startPos); health = 10; speed = 8.0f; grounded = false; collision = new Box(pos.X, pos.Y, animation.FrameWidth, animation.FrameHeight); }
public void Apply(GameTime gt, Player player, Level level) { float dt = gt.ElapsedGameTime.Milliseconds / 1000f; player.velocity.Y += Constants.gravity * dt; Box endBox = new Box(player.collision) ; float dx = player.velocity.X * dt; float dy = player.velocity.Y * dt; Rectangle checkRect ; IEnumerable<MapObject> collideObjects; if (dx != 0) { endBox.Left += dx; checkRect = endBox.rect; checkRect.Inflate(1, 1); collideObjects = level.map.GetObjectsInRegion(level.collisionLayer, checkRect); foreach (var obj in collideObjects) { Vector2 delta = adjustPos(ref endBox, obj, Axis.X); endBox.pos += delta; } } endBox.Top += dy; checkRect = endBox.rect; checkRect.Inflate(1, 1); collideObjects = level.map.GetObjectsInRegion(level.collisionLayer, checkRect); foreach (var obj in collideObjects) { Vector2 delta = adjustPos(ref endBox, obj, Axis.Y); endBox.pos -= delta; if (delta.Y != 0) { player.velocity.Y = 0; if (delta.Y > 0) player.grounded = true; } } player.pos = endBox.pos; }
protected Vector2 adjustPos(ref Box mover, Rectangle platform, Axis axis) { Rectangle checkRect = mover.rect; checkRect.Inflate(1, 1); Box delta = mover.shrinkWrap(Rectangle.Intersect(checkRect, platform)); if (axis == Axis.X) { if (mover.Left >= platform.Left && mover.Right <= platform.Right) return Vector2.Zero; return new Vector2(delta.Width, 0); } else { if (mover.Top >= platform.Top && mover.Bottom <= platform.Bottom) return Vector2.Zero; return new Vector2(0, delta.Height); } }
public bool Equals(Box b) { return (b.pos == pos && b.size == size); }
public Box(Box b) { pos = b.pos; size = b.size; }