/// <summary> /// If a player is near the ground and want to jump /// we should wall this method before to adjust his position /// and reinit his y velocity /// </summary> /// <param name="o">A physical object</param> public void magnetizeToGround(PhysicalObject o) { Vector2 pos = o.Position; pos.Y = pos.Y + ground_detection_space; o.Position = pos; adjustPositionAndVelocity(o); }
public static bool sprite_collision(PhysicalObject o1, PhysicalObject o2) { // Compute intersection rectangle Rectangle r1 = new Rectangle(o1.Position.ToPoint(), o1.Size); Rectangle r2 = new Rectangle(o2.Position.ToPoint(), o2.Size); Point intersection1 = new Point(Math.Max(r1.X, r2.X), Math.Max(r1.Y, r2.Y)); Point intersection2 = new Point(Math.Min(r1.X + r1.Width, r2.X + r2.Width), Math.Min(r1.Y + r1.Height, r2.Y + r2.Height)); Rectangle inter = new Rectangle(intersection1, intersection2 - intersection1); // Invert X ? bool invert_x1 = false; bool invert_x2 = false; if (o1.Sprite != null) { if ((o1.Sprite.Effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { invert_x1 = true; } } if (o2.Sprite != null) { if ((o2.Sprite.Effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { invert_x2 = true; } } // Compare texture masks in this rectangle Point offset1 = o1.TexturePosition - o1.Position.ToPoint(); int width1 = o1.Texture.Image.Width; Point offset2 = o2.TexturePosition - o2.Position.ToPoint(); int width2 = o2.Texture.Image.Width; int offset1_invert = 2 * o1.TexturePosition.X + o1.Size.X; int offset2_invert = 2 * o2.TexturePosition.X + o2.Size.X; for (int i = inter.X; i < inter.X + inter.Width; i++) { for (int j = inter.Y; j < inter.Y + inter.Height; j++) { Point pos1 = new Point(i, j) + offset1; if (invert_x1) { pos1.X = offset1_invert - pos1.X; } Point pos2 = new Point(i, j) + offset2; if (invert_x2) { pos2.X = offset2_invert - pos2.X; } if (o1.Texture.Mask[pos1.X + pos1.Y * width1] && o2.Texture.Mask[pos2.X + pos2.Y * width2]) { return(true); } } } return(false); }
protected override void ApplyCollision(Vector2 imp, PhysicalObject obj, float elapsed) { base.ApplyCollision(imp, obj, elapsed); if (obj is Monstar) { Controller.Direction other_dir = Controller.Direction.LEFT; if (Sprite.Direction == other_dir) { other_dir = Controller.Direction.RIGHT; } Sprite.ChangeDirection(other_dir); } }
public static Vector2?object_collision(PhysicalObject o1, PhysicalObject o2) { Vector2?rect_col = rect_collision(new Rectangle(o1.Position.ToPoint(), o1.Size), new Rectangle(o2.Position.ToPoint(), o2.Size)); if (rect_col == null) { return(null); } if (sprite_collision(o1, o2)) { return(rect_col); } return(null); }
/// <summary> /// Indicate whether a object is near the left wall or not /// </summary> /// <param name="o">A physical object</param> /// <returns>The response</returns> public bool nearLeftWall(PhysicalObject o) { Point pos = o.Position.ToPoint(); pos.X = pos.X - wall_detection_space; Rectangle ro = new Rectangle(pos, o.Size); foreach (Rectangle r in leftWalls) { if (Collision.rect_collision(ro, r) != null) { return(true); } } return(false); }
/// <summary> /// Indicate whether a object is on the ground or not /// Used to know if a player can jump ... /// </summary> /// <param name="o">A physical object</param> /// <returns>The response</returns> public bool nearTheGround(PhysicalObject o) { Point pos = o.Position.ToPoint(); pos.Y = pos.Y + ground_detection_space; Rectangle ro = new Rectangle(pos, o.Size); foreach (Rectangle r in grounds) { if (Collision.rect_collision(ro, r) != null) { return(true); } } return(false); }
/// <summary> /// Return the X velocity of the platform on which the player is /// Used in order to attract the player on the platform /// </summary> /// <param name="o">A physical Object</param> /// <returns>The velocity of the platform</returns> public float getXReferential(PhysicalObject o) { Point pos = o.Position.ToPoint(); pos.Y = pos.Y + ground_detection_space; Rectangle ro = new Rectangle(pos, o.Size); int i = 0; foreach (Rectangle r in grounds) { if (Collision.rect_collision(ro, r) != null) { return(allGroundVelocity[i]); } i++; } return(0f); }
/// <summary> /// Adjust position and velocity of an object according to the map /// </summary> /// <param name="o">A physical object</param> public void adjustPositionAndVelocity(PhysicalObject o) { Vector2 position = o.Position; Vector2 velocity = o.Velocity; foreach (Rectangle r in leftWalls) { if (Collision.rect_collision(new Rectangle(position.ToPoint(), o.Size), r) != null) { position.X = r.X + r.Size.X; velocity.X = Math.Max(velocity.X, 0); } } foreach (Rectangle r in rightWalls) { if (Collision.rect_collision(new Rectangle(position.ToPoint(), o.Size), r) != null) { position.X = r.X - o.Size.X; velocity.X = Math.Min(velocity.X, 0); } } foreach (Rectangle r in grounds) { if (Collision.rect_collision(new Rectangle(position.ToPoint(), o.Size), r) != null) { position.Y = r.Y - o.Size.Y; velocity.Y = Math.Min(velocity.Y, 0); } } foreach (Rectangle r in roofs) { if (Collision.rect_collision(new Rectangle(position.ToPoint(), o.Size), r) != null) { position.Y = r.Y + r.Size.Y; velocity.Y = Math.Max(velocity.Y, 0); } } o.Position = position; o.Velocity = velocity; }
protected override void ApplyCollision(Vector2 imp, PhysicalObject obj, float elapsed) { base.ApplyCollision(imp, obj, elapsed); Damaged = true; wait_before_die = 0f; }
protected virtual void ApplyCollision(Vector2 imp, PhysicalObject obj, float elapsed) { already_computed_collisions.Add(obj.ID); collisions_impulsion += imp; }
protected override void ApplyCollision(Vector2 imp, PhysicalObject obj, float elapsed) { base.ApplyCollision(imp, obj, elapsed); if (obj is NonPlayerObject) { NonPlayerObject e = (NonPlayerObject)obj; e.TouchPlayer(); // Bonus if (e.Bonus > 0 || e.Life > 0) { Life.incr(e.Life); Score.incr(e.Bonus); color = Color.Yellow; last_bonus_time = 0f; } switch (e.poisonState) { case NonPlayerObject.PoisonState.Horizontal: if (!flipH) { poison_remaining_time += time_poison; flipH = true; color = Color.DarkViolet; //last_damage_time = 0f; // It is for the color + immunity } break; case NonPlayerObject.PoisonState.Vertical: if (!flipV) { poison_remaining_time += time_poison; flipV = true; color = Color.DarkViolet; //last_damage_time = 0f; // It is for the color + immunity } break; case NonPlayerObject.PoisonState.Rotation: if (!rotation) { poison_remaining_time += time_poison; rotation = true; color = Color.DarkViolet; //last_damage_time = 0f; // It is for the color + immunity } break; case NonPlayerObject.PoisonState.Nothing: break; } // Damage int damage = 0; if (e is Monstar) { if (!e.Damaged) // If e is damaged, it is because the collision has already been treated { float y_player = position.Y + Size.Y; float y_e = e.Position.Y + e.Size.Y / 2; if (y_player < y_e) { velocity.Y = 0; // To give an impression of bouncing e.SufferDamage(); } else { damage = e.Damage; e.SufferDamage(); } } } else { damage = e.Damage; } // If we are not invincible .. if (last_damage_time >= time_invicibility) { if (damage > 0) { Life.decr(damage); color = Color.IndianRed; last_damage_time = 0f; } } } }