Example #1
0
 public ImmutableArray <T> Add(T item)
 {
     using (Lock.Enter())
     {
         return(DistinctInOrderArrayStorage = DistinctSet.Add(item)
                         ? DistinctInOrderArrayStorage.Add(item)
                         : DistinctInOrderArrayStorage
                );
     }
 }
Example #2
0
        public virtual void CheckCollide(object sender, EventArgs e)
        {
            if (sender == this)
            {
                return;
            }

            //hack to prevent projectiles spinning around inside of mobs
            if ((this is Projectile && HasCollided.ContainsKey(sender.GetHashCode())) || (sender is Projectile && HasCollided.ContainsKey(GetHashCode())))
            {
                return;
            }

            Mob other = sender as Mob;

            if (IsCollidable && other != null && other.IsCollidable && !AlreadyCollided.Contains(other))
            {
                if (IsPhysicallyIntersecting(other))
                {
                    GhostPhysicalBody ghost      = PhysicalBody as GhostPhysicalBody;
                    GhostPhysicalBody otherGhost = other.PhysicalBody as GhostPhysicalBody;

                    if (ghost == null && otherGhost == null)
                    {
                        return; //two farseer bodies. Collision is handled elsewhere
                    }

                    thisCollidesWithOther = false;
                    otherCollidesWithThis = false;
                    VectorHelper.Zero(ref thisPositionOffset);
                    VectorHelper.Zero(ref otherPositionOffset);
                    VectorHelper.Zero(ref thisNewVelocity);
                    VectorHelper.Zero(ref otherNewVelocity);
                    VectorHelper.Zero(ref thisNewNetForces);
                    VectorHelper.Zero(ref otherNewNetForces);

                    //only apply collision forces if not a projectile or planet
                    thisApplyCollisionForces  = !(other is Projectile) && !PhysicalBody.IsStatic;
                    otherApplyCollisionForces = !(this is Projectile) && !other.PhysicalBody.IsStatic;

                    if (Collide(other) || (IsFixForBigColliding && other.IsFixForBigColliding && CollisionRectangle.Width > 100 && other.CollisionRectangle.Width > 100))
                    {
                        thisCollidesWithOther = true;
                        if (thisApplyCollisionForces)
                        {
                            CalculateCollisionForces(other, this, ghost, ref thisPositionOffset, ref thisNewVelocity, ref thisNewNetForces); //, true);
                        }

                        if (this is Projectile)
                        {
                            HasCollided.AddKeyIfMissing(other.GetHashCode());
                        }
                    }

                    if (other.Collide(this) || (IsFixForBigColliding && other.IsFixForBigColliding && CollisionRectangle.Width > 100 && other.CollisionRectangle.Width > 100))
                    {
                        otherCollidesWithThis = true;
                        if (otherApplyCollisionForces)
                        {
                            CalculateCollisionForces(this, other, otherGhost, ref otherPositionOffset, ref otherNewVelocity, ref otherNewNetForces); //, false);
                        }

                        if (other is Projectile)
                        {
                            HasCollided.AddKeyIfMissing(GetHashCode());
                        }
                    }

                    if (thisCollidesWithOther && thisApplyCollisionForces)
                    {
                        if (ghost != null) //This is null if the instance is a planet or the Gunman (the only Farseer bodies)
                        {
                            PhysicalBody.LinearVelocity = thisNewVelocity;
                            PhysicalBody.Position      += thisPositionOffset;
                            UpdateCollisionRectangle();

                            //normally, would do me.NetForces = newNetForces but need to hack so projectiles don't recollide with large bosses
                            if (!PhysicalBody.IsStatic && !other.PhysicalBody.IsStatic &&
                                (CollisionRectangle.Width >= 150 || other.CollisionRectangle.Width >= 150))
                            {
                                if (!ProjectileHasCollidedWithBoss)
                                {
                                    ghost.NetForces = thisNewNetForces;
                                }
                                //to prevent recollision, if colliding with 'boss' (i.e. large nonstatic mob), disable all further
                                //collisions WITH BOSSES ONLY (don't hack collision algorithm for cases involving small mobs)
                                ProjectileHasCollidedWithBoss = true;
                            }
                            else
                            {
                                ghost.NetForces = thisNewNetForces;
                            }
                        }
                    }

                    if (otherCollidesWithThis && otherApplyCollisionForces)
                    {
                        if (otherGhost != null) //This is null if the instance is a planet or the Gunman (the only Farseer bodies)
                        {
                            other.PhysicalBody.LinearVelocity = otherNewVelocity;
                            other.PhysicalBody.Position      += otherPositionOffset;
                            other.UpdateCollisionRectangle();

                            //normally, would do me.NetForces = newNetForces but need to hack so projectiles don't recollide with large bosses
                            if (!PhysicalBody.IsStatic && !other.PhysicalBody.IsStatic &&
                                (CollisionRectangle.Width >= 150 || other.CollisionRectangle.Width >= 150))
                            {
                                if (!other.ProjectileHasCollidedWithBoss)
                                {
                                    otherGhost.NetForces = otherNewNetForces;
                                }
                                //to prevent recollision, if colliding with 'boss' (i.e. large nonstatic mob), disable all further
                                //collisions WITH BOSSES ONLY (don't hack collision algorithm for cases involving small mobs)
                                other.ProjectileHasCollidedWithBoss = true;
                            }
                            else
                            {
                                otherGhost.NetForces = otherNewNetForces;
                            }
                        }
                    }

                    //Once the first mob handles the collision, we do not try again with the other mob
                    AlreadyCollided.Add(other);
                    //other.AlreadyCollided.Add(this); <- for whatever reason, this breaks collisions with ShardSkimmers. Handle with care
                }
            }
        }