public bool IsCollidingLeft(Type exclude = null) { Rect2d newpos = new Rect2d(new Vec2d(position.X - DETECTION_TOLERANCE, position.Y), width, height); //TEST BLOCKS IN RANGE int left = (int)((newpos.br.X - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int bottom = (int)((newpos.br.Y - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); int right = (int)Math.Ceiling((newpos.tl.X + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int top = (int)Math.Ceiling((newpos.tl.Y + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Block b = owner.GetBlock(x, y); if (b != null && Entity.TestBlocking(b, this) && newpos.IsColldingWith(b.GetPosition()) && b.GetMiddle().X < this.GetMiddle().X&& (exclude == null || !exclude.IsAssignableFrom(b.GetType()))) { return(true); } } } // TEST ENTITIES foreach (Entity e in owner.GetSurroundingEntityList(GetCollisionMapPosition())) { if (e != this && Entity.TestBlocking(e, this) && newpos.IsColldingWith(e.GetPosition()) && e.GetMiddle().X < this.GetMiddle().X&& (exclude == null || !exclude.IsAssignableFrom(e.GetType()))) { return(true); } } return(false); }
private double DoXCollisionMove(Vec2d vec) { Rect2d newpos = new Rect2d(new Vec2d(ent.position.X + vec.X, ent.position.Y), ent.width, ent.height); // TEST ENTITIES foreach (Entity e in owner.GetSurroundingEntityList(ent.GetCollisionMapPosition())) { if (e != ent && Entity.TestBlocking(e, ent) && newpos.IsColldingWith(e.GetPosition())) { return(ent.GetPosition().GetDistanceTo(e.GetPosition()).X); } } //TEST BLOCKS IN RANGE int left = (int)((newpos.br.X - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int bottom = (int)((newpos.br.Y - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); int right = (int)Math.Ceiling((newpos.tl.X + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int top = (int)Math.Ceiling((newpos.tl.Y + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Block b = owner.GetBlock(x, y); if (b != null && Entity.TestBlocking(b, ent) && newpos.IsColldingWith(b.GetPosition())) { return(ent.GetPosition().GetDistanceTo(b.GetPosition()).X); } } } return(vec.X); }
/// <returns> Wether an illegal pushback happened</returns> public bool DoCollisions(bool ignoreIllegalPushback = false) { if (!alive) { return(false); } bool result = false; // Collide with Entities Rect2d nocollnewpos = new Rect2d( new Vec2d( position.X - DETECTION_TOLERANCE, position.Y - DETECTION_TOLERANCE), width + DETECTION_TOLERANCE * 2, height + DETECTION_TOLERANCE * 2); Rect2d currPosition = GetPosition(); foreach (DynamicEntity e in owner.GetSurroundingEntityList(GetCollisionMapPosition())) { if (nocollnewpos.IsColldingWith(e.GetPosition()) && e != this) { bool isColl = currPosition.IsColldingWith(e.GetPosition()); bool isTouch = currPosition.IsTouching(e.GetPosition()); bool isBlock = Entity.TestBlocking(e, this); if (e.alive) { //e.onCollide(this, false, isBlock, isColl, isTouch); this.onCollide(e, true, isBlock, isColl, isTouch); } if (isBlock && isColl) { result = true; if (!ignoreIllegalPushback) { OnIllegalIntersection(e); } } } } // Collide with Blocks int left = (int)((position.X - BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int bottom = (int)((position.Y - BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); int right = (int)Math.Ceiling((position.X + width + BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int top = (int)Math.Ceiling((position.Y + height + BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Block b = owner.GetBlock(x, y); if (b != null && nocollnewpos.IsColldingWith(b.GetPosition())) { bool isColl = currPosition.IsColldingWith(b.GetPosition()); bool isTouch = currPosition.IsTouching(b.GetPosition()); bool isBlock = Entity.TestBlocking(b, this); b.onCollide(this, false, isBlock, isColl, isTouch); this.onCollide(b, true, isBlock, isColl, isTouch); if (isBlock && isColl) { result = true; if (!ignoreIllegalPushback) { OnIllegalIntersection(b); } } } } } // Collide with TriggerZones for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { List <Trigger> tlist = owner.getTriggerList(x, y); if (tlist != null) { foreach (Trigger t in tlist) { bool isColl = currPosition.IsColldingWith(t.GetPosition()); if (isColl) { t.OnCollide(this); } } } } } return(result); }