public void SetPosition(PointF pos) { // fix float errors by rounding pos.X = (float)Math.Round(pos.X, 3); pos.Y = (float)Math.Round(pos.Y, 3); Position = pos; }
public void SetPositionWithValues() { var start = new PointF(0, 0); var end = new PointF(5.1f, 3.66f); Assert.AreEqual(start, _position.Position); _position.SetPosition(5.1f, 3.66f); Assert.AreEqual(end, _position.Position); }
public void Offset() { var start = new PointF(0, 0); var end = new PointF(5.1f, 3.66f); Assert.AreEqual(start, _position.Position); _position.Offset(2.4f, 1.92f); _position.Offset(2.7f, 1.74f); Assert.AreEqual(end.X, _position.Position.X, 0.00001); Assert.AreEqual(end.Y, _position.Position.Y, 0.00001); }
public override void BeginScroll(ScreenHandler next, PointF playerPos) { doorOne.SendMessage(new StateMessage(null, "Opening")); if (direction == Direction.Down) triggerSize = (int)(height - playerPos.Y); else if (direction == Direction.Left) triggerSize = (int)playerPos.X; else if (direction == Direction.Right) triggerSize = (int)(width - playerPos.X); else triggerSize = (int)playerPos.Y; base.BeginScroll(next, playerPos); if (doorTwo != null) { doorTwo.SendMessage(new StateMessage(null, "Open")); } }
public override bool Trigger(PointF position) { if (direction == Direction.Right || direction == Direction.Down) { if (JoinInfo.direction == JoinDirection.BackwardOnly) return false; } else if (JoinInfo.direction == JoinDirection.ForwardOnly) return false; return (doorOne.GetComponent<CollisionComponent>()).TouchedBy("Player"); }
public void Offset(PointF pos) { this.Offset(pos.X, pos.Y); }
public bool Contains(PointF pt) { return this.Contains(pt.X, pt.Y); }
public virtual RectangleF BoxAt(PointF offset, bool vflip) { if (vflip) return new RectangleF(box.X + offset.X, offset.Y - box.Y - box.Height, box.Width, box.Height); return new RectangleF(box.X + offset.X, box.Y + offset.Y, box.Width, box.Height); }
private void CheckEnvironmentTile(List<MapSquare> hitSquares, CollisionBox hitbox, RectangleF hitRect, MapSquare tile, ref PointF offset) { if (hitbox.EnvironmentCollisions(PositionSrc.Position, tile, ref offset)) { hitSquares.Add(tile); if (hitbox.PushAway) PositionSrc.Offset(offset.X, offset.Y); } else if (hitRect.IntersectsWith(tile.BoundBox)) { hitSquares.Add(tile); } }
// change those last bools into an enum or something else! public PointF GetIntersectionOffset(RectangleF tileBox, RectangleF boundBox, float approach_vx, float approach_vy, bool uponly, bool downonly) { float top = -1, bottom = -1, left = -1, right = -1; RectangleF intersection = RectangleF.Intersect(boundBox, tileBox); PointF offset = new PointF(0, 0); if (intersection.Width == 0 && intersection.Height == 0) return offset; if (Math.Abs(intersection.Bottom - boundBox.Bottom) < Const.PixelEpsilon) bottom = intersection.Height; if (Math.Abs(intersection.Top - boundBox.Top) < Const.PixelEpsilon) top = intersection.Height; if (Math.Abs(intersection.Right - boundBox.Right) < Const.PixelEpsilon) right = intersection.Width; if (Math.Abs(intersection.Left - boundBox.Left) < Const.PixelEpsilon) left = intersection.Width; if (top > 0 || bottom > 0 || left > 0 || right > 0) { bool vert = CollisionComponent.VerticalApproach(intersection, boundBox, approach_vx, approach_vy); if (vert) { if (downonly) { if (approach_vy > 0 && boundBox.Bottom <= tileBox.Bottom) { offset.Y = -bottom; return offset; } else return new PointF(0, 0); } else if (uponly) { if (approach_vy < 0 && boundBox.Top >= tileBox.Top) { offset.Y = top; return offset; } else return new PointF(0, 0); } if (top >= 0) offset.Y = top; if (bottom >= 0) offset.Y = -bottom; } else { if (uponly || downonly) return new PointF(0, 0); if (left >= 0) { if (approach_vx < 0) offset.X = left; } if (right >= 0) { if (approach_vx > 0) offset.X = -right; } } } return offset; }
public bool EnvironmentCollisions(PointF position, IMapSquare square, ref PointF offset) { offset.X = 0; offset.Y = 0; // some optimizations RectangleF tileBox = square.BlockBox; if (box.Right + position.X < tileBox.Left) return false; if (box.Left + position.X > tileBox.Right) return false; RectangleF boundBox = BoxAt(position); return EnvironmentContact(square, tileBox, boundBox, out offset); }
public override RectangleF BoxAt(PointF offset, bool vflip) { float x = (parentComponent.MovementSrc != null && parentComponent.MovementSrc.Direction == Direction.Left) ? offset.X - box.X - box.Width : box.X + offset.X; if (vflip) return new RectangleF(x, offset.Y - box.Y - box.Height, box.Width, box.Height); return new RectangleF(x, box.Y + offset.Y, box.Width, box.Height); }
public RectangleF BoxAt(PointF offset) { float x = (parentComponent.MovementSrc != null && parentComponent.MovementSrc.Direction == Direction.Left) ? offset.X - box.X - box.Width : box.X + offset.X; if (parentComponent.Parent.IsGravitySensitive && parentComponent.Parent.Container.IsGravityFlipped) return new RectangleF(x, offset.Y - box.Y - box.Height, box.Width, box.Height); return new RectangleF(x, box.Y + offset.Y, box.Width, box.Height); }
public virtual bool Trigger(PointF position) { if (direction == Direction.Right || direction == Direction.Down) { if (JoinInfo.direction == JoinDirection.BackwardOnly) return false; } else if (JoinInfo.direction == JoinDirection.ForwardOnly) return false; return (position.X >= threshXmin && position.X <= threshXmax && position.Y >= threshYmin && position.Y <= threshYmax); }
public virtual void BeginScroll(ScreenHandler next, PointF playerPos) { scrollDist = 0; tickdist = (TriggerSize() + OffsetDist()) / ticks; this.nextHeight = next.Screen.PixelHeight; this.nextWidth = next.Screen.PixelWidth; if (direction == Direction.Down) NextScreenY = -height; else if (direction == Direction.Right) NextScreenX = -width; else if (direction == Direction.Left) NextScreenX = nextWidth; else if (direction == Direction.Up) NextScreenY = nextHeight; Calculate(); }
private bool EnvironmentContact(IMapSquare square, RectangleF tileBox, RectangleF boundBox, out PointF offset) { // can't use intersection, use epsilon offset = PointF.Empty; if (tileBox.Top < boundBox.Top) { if (tileBox.Bottom - boundBox.Top + Const.PixelEpsilon <= 0) return false; } else { if (boundBox.Bottom - tileBox.Top + Const.PixelEpsilon <= 0) return false; } if (tileBox.Left < boundBox.Left) { if (tileBox.Right - boundBox.Left + Const.PixelEpsilon <= 0) return false; } else { if (boundBox.Right - tileBox.Left + Const.PixelEpsilon <= 0) return false; } bool down = (!parentComponent.Parent.Container.IsGravityFlipped && square.Properties.Climbable); bool up = (parentComponent.Parent.Container.IsGravityFlipped && square.Properties.Climbable); if (parentComponent.MovementSrc != null) offset = GetIntersectionOffset(tileBox, boundBox, parentComponent.MovementSrc.VelocityX, parentComponent.MovementSrc.VelocityY, up, down); else offset = GetIntersectionOffset(tileBox, boundBox, 0, 0, up, down); // Quicksand sinking property tells us not to push the hitbox outward if (square.Properties.Sinking > 0) { // don't clip left or right at all offset.X = 0; if (parentComponent.Parent.Container.IsGravityFlipped) { // don't clip them downward out of the collision if (offset.Y > 0) { offset.Y = 0; } } else { // don't clip them upward out of the collision if (offset.Y < 0) { offset.Y = 0; } } } return true; }
public void Offset(float x, float y) { Position = new PointF(Position.X + x, Position.Y + y); }
public void Draw(GameRenderEventArgs renderArgs, PointF playerPos, float adj_x = 0, float adj_y = 0, float off_x = 0, float off_y = 0) { int width = Screen.PixelWidth; int height = Screen.PixelHeight; if (!isAutoscrolling) { OffsetX = OffsetY = 0; centerX = playerPos.X + adj_x; centerY = playerPos.Y + adj_y; if (centerX > Game.CurrentGame.PixelsAcross / 2) { OffsetX = centerX - Game.CurrentGame.PixelsAcross / 2; if (OffsetX > width - Game.CurrentGame.PixelsAcross) OffsetX = width - Game.CurrentGame.PixelsAcross; } if (centerY > Game.CurrentGame.PixelsDown / 2) { OffsetY = centerY - Game.CurrentGame.PixelsDown / 2; if (OffsetY > height - Game.CurrentGame.PixelsDown) OffsetY = height - Game.CurrentGame.PixelsDown; if (OffsetY < 0) OffsetY = 0; } OffsetX += off_x; OffsetY += off_y; } foreach (var layer in this.layers) { layer.Draw(renderArgs, this.Screen.PixelWidth); } }
private void CheckEnvironment(List<MapSquare> hitSquares, CollisionBox hitbox) { PointF offset = new PointF(0, 0); RectangleF hitRect = hitbox.BoxAt(PositionSrc.Position); // this bounds checking prevents needlessly checking collisions way too far away // it's a very effective optimization (brings busy time from ~60% down to 45%!) int size = Parent.Screen.TileSize; for (float y = hitRect.Top - size; y < hitRect.Bottom; y += size) { for (float x = hitRect.Left - size; x < hitRect.Right; x += size) { var tile = Parent.Screen.SquareAt(x, y); if (tile == null) continue; CheckEnvironmentTile(hitSquares, hitbox, hitRect, tile, ref offset); } var rightEdge = Parent.Screen.SquareAt(hitRect.Right, y); if (rightEdge != null) { CheckEnvironmentTile(hitSquares, hitbox, hitRect, rightEdge, ref offset); } } for (float x = hitRect.Left - size; x < hitRect.Right; x += size) { var tile = Parent.Screen.SquareAt(x, hitRect.Bottom); if (tile == null) continue; CheckEnvironmentTile(hitSquares, hitbox, hitRect, tile, ref offset); } var lastCorner = Parent.Screen.SquareAt(hitRect.Right, hitRect.Bottom); if (lastCorner != null) { CheckEnvironmentTile(hitSquares, hitbox, hitRect, lastCorner, ref offset); } }