public void blockCollision(Level level) { onGround = false; foreach (Block b in level.Blocks) { if(b.Destroyed) continue; if (b.Solid) { if (body.Intersects(b.Position)) { Block final = b; foreach (Block n in level.getAdjacentBlocks(b)) { if (Rectangle.Intersect(body, n.Position).Width * Rectangle.Intersect(body, n.Position).Height > Rectangle.Intersect(body, final.Position).Width * Rectangle.Intersect(body, final.Position).Height) final = n; } if (Rectangle.Intersect(body, final.Position).Width >= Rectangle.Intersect(body, final.Position).Height) { if (Rectangle.Intersect(body, final.Position).Top == final.Position.Top) { body.Y = final.Position.Top - body.Height; velocity.Y = 0; onGround = true; } else if (Rectangle.Intersect(body, final.Position).Bottom == final.Position.Bottom) { body.Y = final.Position.Bottom; velocity.Y *= -1; final.onHit(level); if (level.getItemAboveBlock(final) != null) level.getItemAboveBlock(final).Velocity = new Vector2(level.getItemAboveBlock(final).Velocity.X, -6); if (level.getEnemyAboveBlock(final) != null) level.getEnemyAboveBlock(final).onHit(level); } } else { if (Rectangle.Intersect(body, final.Position).Left == final.Position.Left) { if (velocity.X > 0) { velocity.X = 0; body.X = final.Position.Left - body.Width; } } else if (Rectangle.Intersect(body, final.Position).Right == final.Position.Right) { if (velocity.X < 0) { velocity.X = 0; body.X = final.Position.Right; } } } break; } Rectangle temp = b.Position; temp.Y -=1; if (body.Intersects(temp)) onGround = true; } } }
public virtual void collision(Level level) { foreach (Block b in level.Blocks) { if (position.Intersects(b.Position)) { Block final = b; foreach (Block n in level.getAdjacentBlocks(b)) { if (Rectangle.Intersect(position, n.Position).Width * Rectangle.Intersect(position, n.Position).Height > Rectangle.Intersect(position, final.Position).Width * Rectangle.Intersect(position, final.Position).Height) final = n; } if (Rectangle.Intersect(position, final.Position).Width >= Rectangle.Intersect(position, final.Position).Height) { if (Rectangle.Intersect(position, final.Position).Top == final.Position.Top) { position.Y = final.Position.Top - position.Height; velocity.Y = -6; } else if (Rectangle.Intersect(position, final.Position).Bottom == final.Position.Bottom) { position.Y = final.Position.Bottom; velocity.Y *= -1; } } else { if (Rectangle.Intersect(position, final.Position).Left == final.Position.Left) { velocity.X = 0; position.X = final.Position.Left - position.Width; destroy(level); } else if (Rectangle.Intersect(position, final.Position).Right == final.Position.Right) { velocity.X = 0; position.X = final.Position.Right; destroy(level); } } break; } } foreach (Enemy e in level.Enemies) { if (e.Alive == false || e.Hurt != -1 || e.Stomped != -1) continue; if (position.Intersects(e.Position)) { this.destroy(level); e.onHit(level); break; } } }
public override void collision(Level level) { if (inShell) { foreach (Block b in level.Blocks) { if (Position.Intersects(b.Position)) { Block final = b; foreach (Block n in level.getAdjacentBlocks(b)) { if (Rectangle.Intersect(Position, n.Position).Width * Rectangle.Intersect(Position, n.Position).Height > Rectangle.Intersect(Position, final.Position).Width * Rectangle.Intersect(Position, final.Position).Height) final = n; } if (Rectangle.Intersect(Position, final.Position).Width >= Rectangle.Intersect(Position, final.Position).Height) { if (Rectangle.Intersect(Position, final.Position).Top == final.Position.Top) { Position = new Rectangle(Position.X, final.Position.Top - Position.Height, Position.Width, Position.Height); Velocity = new Vector2(Velocity.X, 0); } else if (Rectangle.Intersect(Position, final.Position).Bottom == final.Position.Bottom) { Position = new Rectangle(Position.X, final.Position.Bottom, Position.Width, Position.Height); Velocity = new Vector2(Velocity.X, Velocity.Y * -1); ; } } else { if (Rectangle.Intersect(Position, final.Position).Left == final.Position.Left) { Velocity = new Vector2(-4, Velocity.Y); Position = new Rectangle(final.Position.Left - Position.Width, Position.Y, Position.Width, Position.Height); Game1.otherSounds[1].Play(); } else if (Rectangle.Intersect(Position, final.Position).Right == final.Position.Right) { Velocity = new Vector2(4, Velocity.Y); Position = new Rectangle(final.Position.Right, Position.Y, Position.Width, Position.Height); Game1.otherSounds[1].Play(); } } break; } } } else base.collision(level); }
public virtual void collision(Level level) { foreach (Block b in level.Blocks) { if (position.Intersects(b.Position)) { Block final = b; foreach (Block n in level.getAdjacentBlocks(b)) { if (Rectangle.Intersect(position, n.Position).Width * Rectangle.Intersect(position, n.Position).Height > Rectangle.Intersect(position, final.Position).Width * Rectangle.Intersect(position, final.Position).Height) final = n; } if (Rectangle.Intersect(position, final.Position).Width >= Rectangle.Intersect(position, final.Position).Height) { if (Rectangle.Intersect(position, final.Position).Top == final.Position.Top) { position.Y = final.Position.Top - position.Height; velocity.Y = 0; } else if (Rectangle.Intersect(position, final.Position).Bottom == final.Position.Bottom) { position.Y = final.Position.Bottom; velocity.Y *= -1; } } else { if (Rectangle.Intersect(position, final.Position).Left == final.Position.Left) { velocity.X = 0; position.X = final.Position.Left - position.Width; } else if (Rectangle.Intersect(position, final.Position).Right == final.Position.Right) { velocity.X = 0; position.X = final.Position.Right; } } break; } } }