예제 #1
0
        public CollisionDemo()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            DisplayMode displayMode = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode;
            this._graphics.PreferredBackBufferFormat = displayMode.Format;
            this._graphics.PreferredBackBufferWidth = (int)(displayMode.Width / 1.1);
            this._graphics.PreferredBackBufferHeight = (int)(displayMode.Height / 1.1);

            _ball1 = new Ball(this, "BallR", 2, 1);
            _ball2 = new Ball(this, "BallB", 2, 1);
            _rect1 = new AxisAlignedRectangle(this, "RectB", 2, 1);
            Reset();

            this.Components.Add(_ball1);
            //this.Components.Add(_ball2);
            this.Components.Add(_rect1);

            IEnumerable<ICollidable> collidableObjects = this.Components.OfType<ICollidable>();

            _collisionManager = new CollisionManager(this, collidableObjects);
        }
예제 #2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            var menu = new MainMenu(this);
            this.Components.Add(menu);

            var pitch = new Pitch(this);
            this.Components.Add(pitch);

            GameMode = AirHockey.GameMode.Menu;

            PlayerTouchBinder binder = new PlayerTouchBinder();
            _player1 = new Player(this, PlayerNumber.Player1, binder);
            _player2 = new Player(this, PlayerNumber.Player2, binder);
            _puck = new Puck(this);

            var collideables = new Collection<ICollidable>();
            collideables.Add(_player1);
            collideables.Add(_player2);
            collideables.Add(_puck);
            _collisionManager = new CollisionManager(this, collideables);

            this.Components.Add(_player1);
            this.Components.Add(_player2);
            this.Components.Add(_puck);

            _player1ScorePosition = new Vector2(GraphicsDevice.Viewport.Width / 2 - 10, 0);
            _player2ScorePosition = new Vector2(GraphicsDevice.Viewport.Width / 2 + 10, 0);
            _messageP1Position = new Vector2(GraphicsDevice.Viewport.Width / 2 - 100, GraphicsDevice.Viewport.Height / 2);
            _messageP2Position = new Vector2(GraphicsDevice.Viewport.Width / 2 + 100, GraphicsDevice.Viewport.Height / 2);

            InitialiseToNewGameState();

            base.Initialize();
        }
예제 #3
0
        private void HandleCollision(CollisionResults results)
        {
            if (IsPlayer && results.Type == CollisionType.enAbsolute)
            {
                CollisionResults reaffirm = CollisionManager.TestCollisions(this, (this == results.Object1) ? results.Object2 : results.Object1);
            }
            if (IsPlayer && ((results.CollisionTime > 0.0f && results.CollisionTime < 1.0f) || (results.Sides.Count > 1)))
            {
                int i = 0;
            }
            BoundingBox.Color = SFML.Graphics.Color.Green;
            //For testing purposes.  This will stop an object BEFORE it collides.
            if (results.Object1 == this)
            {
                foreach (AABB.AABBSide collision in results.Sides.Keys)
                {
                    switch (collision)
                    {
                    case AABB.AABBSide.enTop:
                    {
                        if (velocity.Y < 0.0f)
                        {
                            velocity.Y = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enRight:
                    {
                        if (velocity.X > 0.0f)
                        {
                            velocity.X = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enBottom:
                    {
                        if (velocity.Y > 0.0f)
                        {
                            velocity.Y = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enLeft:
                    {
                        if (velocity.X < 0.0f)
                        {
                            velocity.X = 0.0f;
                        }
                    }
                    break;
                    }
                    ;

                    BoundingBox.Sides[(int)collision].SetColor(SFML.Graphics.Color.Red);
                }
            }
            else
            {
                foreach (AABB.AABBSide collision in results.Sides.Values)
                {
                    switch (collision)
                    {
                    case AABB.AABBSide.enTop:
                    {
                        if (velocity.Y < 0.0f)
                        {
                            velocity.Y = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enRight:
                    {
                        if (velocity.X > 0.0f)
                        {
                            velocity.X = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enBottom:
                    {
                        if (velocity.Y > 0.0f)
                        {
                            velocity.Y = 0.0f;
                        }
                    }
                    break;

                    case AABB.AABBSide.enLeft:
                    {
                        if (velocity.X < 0.0f)
                        {
                            velocity.X = 0.0f;
                        }
                    }
                    break;
                    }
                    ;

                    BoundingBox.Sides[(int)collision].SetColor(SFML.Graphics.Color.Red);
                }
            }
        }