public Collider(ICollidable owner, Rectangle bounds, ColliderType type, int npc_id) { this.m_owner = owner; this.m_bounds = new DoubleRect(bounds.X, bounds.Y, bounds.Width, bounds.Height); this.m_type = type; this.m_npc_id = npc_id; }
public Collider(ICollidable owner, Rectangle bounds, ColliderType type) { this.m_owner = owner; this.m_bounds = new DoubleRect(bounds.X, bounds.Y, bounds.Width, bounds.Height); this.m_bounds2 = new DoubleRect(bounds.X - 5, bounds.Y - 5, bounds.Width + 10, bounds.Height + 10); this.m_type = type; this.m_other = null; }
/// <summary> /// Moves a collider in the direction provided until it hits something /// </summary> /// <param name="mover">Collider which is moving</param> /// <param name="deltaPosition">Change in position the collider is trying to make</param> public void handleMovement(Collider mover, Vector2 deltaPosition) { double areaOfMovementX1 = Math.Min(mover.Bounds.X, mover.Bounds.X + deltaPosition.X); double areaOfMovementY1 = Math.Min(mover.Bounds.Y, mover.Bounds.Y + deltaPosition.Y); double areaOfMovementX2 = Math.Max(mover.Bounds.X + mover.Bounds.Width, mover.Bounds.X + mover.Bounds.Width + deltaPosition.X); double areaOfMovementY2 = Math.Max(mover.Bounds.Y + mover.Bounds.Height, mover.Bounds.Y + mover.Bounds.Height + deltaPosition.Y); DoubleRect areaOfMovement = new DoubleRect( areaOfMovementX1, areaOfMovementY1, areaOfMovementX2 - areaOfMovementX1, areaOfMovementY2 - areaOfMovementY1); //DoubleRect newbounds = mover.Bounds + deltaPosition; List<Collider> collisions = m_tree.Query(areaOfMovement); List<Collider>.Enumerator i = collisions.GetEnumerator(); Vector2 allowedMovement = deltaPosition; Vector2 temp; while (i.MoveNext()) { // we will usually collide with our old position - ignore that case if (i.Current != mover) { bool canMove = CollisionHandler.handleMovement(mover, i.Current, deltaPosition, out temp); if (!canMove) { return; // don't allow movement } if (allowedMovement.X > 0.0f) allowedMovement.X = Math.Min(allowedMovement.X, temp.X); else if (allowedMovement.X < 0.0f) allowedMovement.X = Math.Max(allowedMovement.X, temp.X); if (allowedMovement.Y > 0.0f) allowedMovement.Y = Math.Min(allowedMovement.Y, temp.Y); else if (allowedMovement.Y < 0.0f) allowedMovement.Y = Math.Max(allowedMovement.Y, temp.Y); } } mover.move(allowedMovement); }
public void move(Vector2 dp) { bounds += dp; owner.DrawPosition += dp; RaiseBoundsChanged(); }
public Collider(Collidable owner, Rectangle bounds, ColliderType type) { this.owner = owner; this.bounds = new DoubleRect(bounds.X, bounds.Y, bounds.Width, bounds.Height); this.type = type; }
public List<Collider> queryDetector(DoubleRect queryRect) { return cd.query(queryRect); }
/// <summary> /// Sample of interaction - initiates dialogue. /// Perty hackish. /// </summary> private void handleInteract() { string facing = angleTo4WayAnimation(m_previousAngle); DoubleRect queryRectangle; if (facing == "up") { queryRectangle = new DoubleRect( m_collider.Bounds.X, m_collider.Bounds.Y - 50, m_collider.Bounds.Width, 50); } else if (facing == "down") { queryRectangle = new DoubleRect( m_collider.Bounds.X, m_collider.Bounds.Y + m_collider.Bounds.Height, m_collider.Bounds.Width, 50); } else if (facing == "left") { queryRectangle = new DoubleRect( m_collider.Bounds.X - 50, m_collider.Bounds.Y + m_collider.Bounds.Height / 2 - 25, 50, 50); } else if (facing == "right") { queryRectangle = new DoubleRect( m_collider.Bounds.X + m_collider.Bounds.Width, m_collider.Bounds.Y + m_collider.Bounds.Height / 2 - 25, 50, 50); } else { throw new Exception("Something aint right"); } List<Collider> queries = m_collider.queryDetector(queryRectangle); foreach (Collider c in queries) { if (c != this.m_collider && c.m_type == ColliderType.NPC) { EngineManager.pushState(new EngineStateDialogue(0, null, null, false)); InputSet.getInstance().setAllToggles(); return; } } }
/// <summary> /// Queries the CollisionDetector for all colliders which intersect /// with the query area. /// </summary> /// <param name="queryArea">Area to retrieve all intersectors</param> /// <returns>List of all Colliders which intersect with QueryArea</returns> public List<Collider> query(DoubleRect queryArea) { return m_tree.Query(queryArea); }
/// <summary> /// Determines the draw depth of a sprite based on its collision bounds. /// The bottom edge of the bounds is used, so elements in the world with lower bottom edges /// are drawn in front of those with higher bottom edges. /// </summary> /// <param name="bounds"></param> /// <returns></returns> public float getDrawDepth(DoubleRect bounds) { // map coordinates to a 3x3 cluster of Areas centered around this one // (done so that we eventually can draw neighboring areas' items) double mappedX = bounds.X + bounds.Width + getWidthInPixels(); double mappedY = bounds.Y + bounds.Height + getHeightInPixels(); return Constants.DepthBaseGameplay + Constants.DepthRangeGameplay * ((float)(mappedY) / (getHeightInPixels() * 3) + (float)(mappedX) / (getWidthInPixels() * 3) / 10000.0f); }
public bool IntersectsWith(DoubleRect other) { // TODO verify this if (this.y + this.height <= other.y) return false; if (this.y >= other.y + other.height) return false; if (this.x + this.width <= other.x) return false; if (this.x >= other.x + other.width) return false; return true; }
public bool Contains(DoubleRect other) { // TODO verify this return (this.x <= other.x && this.x + this.width >= other.x + other.width && this.y <= other.y && this.y + this.height >= other.y + other.height); }
private List<Collider> getCollisions() { string facing = angleTo4WayAnimation(m_previousAngle); DoubleRect queryRectangle; if (facing == "up") { queryRectangle = new DoubleRect( m_collider.Bounds.X, m_collider.Bounds.Y - 50, m_collider.Bounds.Width, 50); } else if (facing == "down") { queryRectangle = new DoubleRect( m_collider.Bounds.X, m_collider.Bounds.Y + m_collider.Bounds.Height, m_collider.Bounds.Width, 50); } else if (facing == "left") { queryRectangle = new DoubleRect( m_collider.Bounds.X - 50, m_collider.Bounds.Y + m_collider.Bounds.Height / 2 - 25, 50, 50); } else if (facing == "right") { queryRectangle = new DoubleRect( m_collider.Bounds.X + m_collider.Bounds.Width, m_collider.Bounds.Y + m_collider.Bounds.Height / 2 - 25, 50, 50); } else { throw new Exception("Something aint right"); } List<Collider> queries = m_collider.queryDetector(queryRectangle); return queries; }