Наследование: Bug.GameObject
Пример #1
0
 public void ApplyGravity(Dynamic o)
 {
     if (o.GetBoundingBox().Bottom < GroundY)
     {
         o.Vel = new Vector2(o.Vel.X, (float)(o.Vel.Y * (1 - Gravity) + GSpeed * Gravity));
     }
     else
     {
         o.Vel = new Vector2(o.Vel.X, Math.Min(o.Vel.Y, 0));
         o.SetPos(new Vector2(o.Pos.X, (float)(GroundY - o.GetBoundingBox().Height)));
     }
 }
Пример #2
0
        private void Collision(Dynamic o1, GameObject o2)
        {
            Rectangle bb1 = o1.GetBoundingBox();
            Rectangle bb2 = o2.GetBoundingBox();

            if (bb1.Intersects(bb2))
            {
                //Calculate how far you would need to move
                //in each direction to get out
                int left = Math.Abs(bb1.Right - bb2.Left);
                int top = Math.Abs(bb1.Bottom - bb2.Top);
                int right = Math.Abs(bb1.Left - bb2.Right);
                int bot = Math.Abs(bb1.Top - bb2.Bottom);

                if (top < bot)
                {
                    if (left < right)
                    {
                        //Send collision message
                        o1.OnCollision(o2, top < left ? Direction.S : Direction.E);
                        if (o2 is Dynamic)
                        {
                            //Send collision message
                            Dynamic do2 = (Dynamic)o2;
                            do2.OnCollision(o1, top < left ? Direction.N : Direction.W);

                            
                        }

                        if (!(o2 is HitBox))
                        {
                            if (top < left)
                            {
                                o1.SetPos(new Vector2(o1.Pos.X, o1.Pos.Y - top));
                                //do2.SetPos(new Vector2(do2.Pos.X, do2.Pos.Y + top / 2.0f));
                            }
                            else
                            {
                                o1.SetPos(new Vector2(o1.Pos.X - left, o1.Pos.Y));
                                //do2.SetPos(new Vector2(do2.Pos.X + left / 2.0f, do2.Pos.Y));
                            }
                        }


                    }
                    else
                    {
                        o1.OnCollision(o2, top < right ? Direction.S : Direction.W);
                        if (o2 is Dynamic)
                        {
                            Dynamic do2 = (Dynamic)o2;
                            do2.OnCollision(o1, top < right ? Direction.N : Direction.E);
                            
                           
                        }

                        if (!(o2 is HitBox))
                        {
                            if (top < right)
                            {
                                o1.SetPos(new Vector2(o1.Pos.X, o1.Pos.Y - top));
                                //do2.SetPos(new Vector2(do2.Pos.X, do2.Pos.Y + top / 2.0f));
                            }
                            else
                            {
                                o1.SetPos(new Vector2(o1.Pos.X + right, o1.Pos.Y));
                                //do2.SetPos(new Vector2(do2.Pos.X - right / 2.0f, do2.Pos.Y));
                            }
                        }
                        
                    }
                }
                else
                {
                    if (left < right)
                    {
                        o1.OnCollision(o2, bot < left ? Direction.N : Direction.E);
                        if (o2 is Dynamic)
                        {
                            Dynamic do2 = (Dynamic)o2;
                            do2.OnCollision(o1, bot < left ? Direction.S : Direction.W);

                            
                        }

                        if (!(o2 is HitBox))
                        {
                            if (bot < left)
                            {
                                o1.SetPos(new Vector2(o1.Pos.X, o1.Pos.Y + bot));
                                //do2.SetPos(new Vector2(do2.Pos.X, do2.Pos.Y - bot / 2.0f));
                            }
                            else
                            {
                                o1.SetPos(new Vector2(o1.Pos.X - left, o1.Pos.Y));
                                //do2.SetPos(new Vector2(do2.Pos.X + left / 2.0f, do2.Pos.Y));
                            }
                        }
                    }
                    else
                    {
                        o1.OnCollision(o2, bot < right ? Direction.N : Direction.W);
                        if (o2 is Dynamic)
                        {
                            Dynamic do2 = (Dynamic)o2;
                            do2.OnCollision(o1, bot < right ? Direction.S : Direction.E);

                            
                        }
                        if (!(o2 is HitBox))
                        {

                            if (bot < right)
                            {
                                o1.SetPos(new Vector2(o1.Pos.X, o1.Pos.Y + bot));
                                //do2.SetPos(new Vector2(do2.Pos.X, do2.Pos.Y - bot / 2.0f));
                            }
                            else
                            {
                                o1.SetPos(new Vector2(o1.Pos.X + right, o1.Pos.Y));
                                //do2.SetPos(new Vector2(do2.Pos.X - right / 2.0f, do2.Pos.Y));
                            }
                        }
                    }
                }

                

               /*
                if (left > 0 && left < (bb1.Width + bb2.Width) && top > 0 && top < (bb1.Height + bb2.Height))
                {
                    bool l = left < (bb1.Width + bb2.Width) / 2;
                    bool t = top < (bb1.Height + bb2.Height) / 2;
                    int xDist = l ? left : (bb1.Width + bb2.Width) - left;
                    int yDist = t ? top : (bb1.Height + bb2.Height) - top;
                

                    if (xDist > yDist)
                    {
                        o1.OnCollision(o2, t ? Direction.N : Direction.S);
                    }
                    else
                    {
                        o1.OnCollision(o2, l ? Direction.W : Direction.E);
                    }
                }
                */ 
            }
        }