public List<Entity> FindBelow(Entity entity) { List<Entity> found = new List<Entity>(); Rectangle entityBox = entity.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); float ePosY = Math.Abs(entity.GetPosY()); foreach (Entity target in entities) { if (entity != target && target.IsEntity(Entity.EntityType.OBSTACLE)) { Rectangle targetBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); Rectangle heightBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); float tPosY = Math.Abs(target.GetPosY()); if (entity.InBoundsZ(target, target.GetDepth()) && entityBox.Intersects(targetBox) && Math.Abs(entity.GetPosY()) + 50 >= Math.Abs(target.GetPosY()) + target.GetHeight()) { found.Add(target); } } } return found; }
public List<Entity> TouchBottom(Entity entity) { List<Entity> found = new List<Entity>(); Rectangle entityBox = entity.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); foreach (Entity target in entities) { if (entity != target) { Rectangle targetBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); if (entity.InBoundsZ(target, target.GetDepth()) && entityBox.TouchBottom(targetBox)) { found.Add(target); } } } return found; }
private void CheckLand(Entity entity) { Rectangle entityBox = entity.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); foreach (Entity target in entities) { if (entity != target && target.IsEntity(Entity.EntityType.OBSTACLE)) { Rectangle targetBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); if (entity.InBoundsZ(target, target.GetDepth()) && entityBox.Intersects(targetBox) && entityBox.TouchBottom(targetBox) && !entityBox.TouchTop(targetBox) && entity.InAir()) { float depth = entityBox.GetVerticalIntersectionDepth(targetBox); if (!target.IsToss()) { entity.MoveY(depth + 5); } entity.GetTossInfo().velocity.Y = 5; } } } foreach (Entity target in entities) { if (entity != target && target.IsEntity(Entity.EntityType.OBSTACLE)) { Rectangle targetBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); float posy = (Math.Abs(target.GetPosY()) + target.GetHeight()); if (entity.InBoundsZ(target, target.GetDepth()) && entityBox.Intersects(targetBox) && entityBox.InBoundsX(targetBox, 20)) { List<Entity> above = FindAbove(target); int totalHeight = (int)Math.Abs(target.GetPosY()) + target.GetHeight() + above.Sum(e => e.GetHeight()); totalHeight = (above.Count == 0 ? (int)Math.Abs(target.GetPosY()) + totalHeight : totalHeight); if (entity.GetVelocity().Y > 2 && Math.Abs(entity.GetPosY() + 20) > totalHeight) { entity.SetGround(-posy); } if (entity.GetVelocity().Y > 2 && Math.Abs(entity.GetPosY()) + 20 > Math.Abs(target.GetPosY()) + target.GetHeight()) { entity.SetGround(-posy); } if (target.IsToss() && Math.Abs(entity.GetPosY()) + 50 > Math.Abs(target.GetPosY()) + target.GetHeight()) { entity.SetPosY(-posy); entity.SetGround(-posy); } } } } }
private void CheckBounds(Entity entity) { List<CLNS.BoundsBox> bboxes = entity.GetBoxes(CLNS.BoxType.BOUNDS_BOX).Cast<CLNS.BoundsBox>().ToList(); bboxes.AddRange(entity.GetCurrentBoxes(CLNS.BoxType.BOUNDS_BOX).Cast<CLNS.BoundsBox>().ToList()); CLNS.BoundsBox entityBox = null; int ePosY = (int)Math.Abs(entity.GetPosY()); int eGround = (int)Math.Abs(entity.GetGround()); bool onTop = false; foreach (Entity target in entities) { if (entity != target && target.IsEntity(Entity.EntityType.OBSTACLE)) { List<CLNS.BoundsBox> tboxes = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX).Cast<CLNS.BoundsBox>().ToList(); tboxes.AddRange(target.GetCurrentBoxes(CLNS.BoxType.BOUNDS_BOX).Cast<CLNS.BoundsBox>().ToList()); CLNS.BoundsBox targetBox = null; bool hasCollided = false; int tPosY = (int)Math.Abs(target.GetPosY()); int tGround = (int)Math.Abs(target.GetGround()); foreach (CLNS.BoundsBox bb1 in bboxes) { foreach (CLNS.BoundsBox bb2 in tboxes) { if (entity.InBoundsZ(target, bb2.GetZdepth()) && bb1.GetRect().Intersects(bb2.GetRect())) { entityBox = bb1; targetBox = bb2; hasCollided = true; break; } } } if (hasCollided && entityBox != null && targetBox != null) { //Debug.WriteLine("TT tPosY: " + target.GetName() + ": " + tPosY); //Debug.WriteLine("E: " + entity.GetName() + " : " + (ePosY + entity.GetHeight())); //Problem with tposy not updating in time for comparison if (entityBox.GetRect().Intersects(targetBox.GetRect())) { int eHeight = (int)(entityBox.GetHeight() + eGround); int tHeight = (int)(targetBox.GetHeight() + tGround); Debug.WriteLine("tHeight: " + tHeight); if (entityBox.GetRect().InBoundsX(targetBox.GetRect(), 60) && entity.InBoundsZ(target, targetBox.GetZdepth() - 5) && entityBox.GetRect().TouchTop(targetBox.GetRect(), 10)) { onTop = true; } if (entityBox.GetRect().InBoundsX(targetBox.GetRect(), 60) && entity.InBoundsZ(target, targetBox.GetZdepth() - 5) && entityBox.GetRect().TouchTop(targetBox.GetRect(), 10) && entity.GetVelocity().Y > 1) { entity.SetGround(-(tHeight + 1)); } bool over = false; if (targetBox.GetRect().TouchTop(entityBox.GetRect(), 10) && tGround >= eGround) { over = true; } if (ePosY < tHeight - 10) { if (entity.InBoundsZ(target, targetBox.GetZdepth() - 5) && over == false) { float depth = entityBox.GetRect().GetHorizontalIntersectionDepth(targetBox.GetRect()); if (depth != 0) { if (!entity.IsLeft() && entityBox.GetRect().TouchLeft(targetBox.GetRect())) { entity.MoveX(depth + 5); entity.GetCollisionInfo().Right(); entity.VelX(0f); } else if (entity.IsLeft() && entityBox.GetRect().TouchRight(targetBox.GetRect())) { entity.MoveX(depth - 5); entity.GetCollisionInfo().Left(); entity.VelX(0f); } } } if (entity.GetDirZ() > 0 && entity.GetPosZ() <= target.GetPosZ()) { entity.VelZ(0f); entity.GetCollisionInfo().Top(); } else if (entity.GetDirZ() < 0 && entity.GetPosZ() >= target.GetPosZ()) { entity.VelZ(0f); entity.GetCollisionInfo().Bottom(); } } } } } } if ((onTop == false && entity.GetGround() != entity.GetGroundBase() && entity.HasLanded()) || (onTop == false && Math.Abs(entity.GetPosY()) != entity.GetGround() && !entity.IsToss())) { if (!entity.IsToss()) { entity.SetGround(entity.GetGroundBase()); entity.SetAnimationState(Animation.State.FALL1); entity.Toss(5); } } }
public List<Entity> FindAbove(Entity entity) { List<Entity> found = new List<Entity>(); Rectangle entityBox = entity.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); foreach (Entity target in entities) { if (entity != target && target.IsEntity(Entity.EntityType.OBSTACLE)) { Rectangle targetBox = target.GetBoxes(CLNS.BoxType.BOUNDS_BOX)[0].GetRect(); if (entity.InRangeZ(target, target.GetDepth()) && entityBox.InBoundsX(targetBox, 20) && entityBox.Intersects(targetBox) && (Math.Abs(target.GetPosY()) + Math.Abs(target.GetHeight()) + 1) > Math.Abs(entity.GetPosY()) && !(Math.Abs(entity.GetPosY()) + 20 >= (Math.Abs(target.GetPosY()) + Math.Abs(target.GetHeight()))) && Math.Abs(entity.GetPosY()) < Math.Abs(target.GetPosY())) { found.Add(target); } } } return found; }