Exemplo n.º 1
0
        static void Main()
        {
            window = new RenderWindow(new VideoMode(1280, 720), "", Styles.Close);
            window.SetFramerateLimit(60);
            window.Closed += (sender, eventArgs) => window.Close();

            shipTex     = new Texture("Ship.png");
            asteroidTex = new Texture("Asteroid.png");

            var shipSpr = new Sprite(shipTex);

            shipSpr.Origin = new Vector2f(shipTex.Size.X / 2f, shipTex.Size.Y / 2f);

            var asteroidSpr = new Sprite(asteroidTex);

            asteroidSpr.Origin = new Vector2f(asteroidTex.Size.X / 2f, asteroidTex.Size.Y / 2f);

            world = new World(new Vector2(0, 0));

            var debugView = new SFMLDebugView(world);

            debugView.AppendFlags(DebugViewFlags.Shape);

            CreateBounds(20, 11.25f);

            var ship = CreateShip();

            ship.Position = new Vector2(3, 1);
            ship.Rotation = 1.7f;

            var ship2 = CreateShip();

            ship2.Position = new Vector2(3, 1);
            ship2.Rotation = 1.7f;

            var asteroid = CreateAsteroid();

            asteroid.Position = new Vector2(4, 4);

            window.KeyPressed += (sender, eventArgs) =>
            {
                if (eventArgs.Code == Keyboard.Key.Space)
                {
                    CreateBullets(ship);
                }
            };

            while (window.IsOpen())
            {
                window.DispatchEvents();

                if (Keyboard.IsKeyPressed(Keyboard.Key.W))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, -25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.S))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, 25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.A))
                {
                    ship.ApplyTorque(-10);
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.D))
                {
                    ship.ApplyTorque(10);
                }

                world.Step(1f / 60);

                window.Clear(Color.Black);

                shipSpr.Position = new Vector2f(ship.Position.X * 64, ship.Position.Y * 64);
                shipSpr.Rotation = (ship.Rotation * (180 / (float)Math.PI));
                window.Draw(shipSpr);

                asteroidSpr.Position = new Vector2f(asteroid.Position.X * 64, asteroid.Position.Y * 64);
                asteroidSpr.Rotation = (asteroid.Rotation * (180 / (float)Math.PI));
                window.Draw(asteroidSpr);

                var  start = ship.Position;
                var  step  = (float)(2 * Math.PI) / 200;
                byte col   = 0;
                var  line  = new VertexArray(PrimitiveType.Lines, 2);
                line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), Color.White);
                for (var dir = 0f; dir <= 2 * Math.PI; dir += step)
                {
                    float min   = 100;
                    byte  res   = 255;
                    var   point = start + LengthDir(dir, 20);
                    world.RayCast((f, p, n, fr) =>
                    {
                        if (fr > min)
                        {
                            return(1);
                        }

                        min   = fr;
                        res   = (byte)(fr * 255);
                        point = p;
                        return(fr);
                    }, start, point);

                    line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), new Color(col, 0, 0));
                    line[1] = new Vertex(new Vector2f(point.X * 64, point.Y * 64), new Color(col, 0, 0));
                    window.Draw(line);
                    col++;
                }

                debugView.Draw(window);

                window.Display();
            }
        }
Exemplo n.º 2
0
        static void Main()
        {
            window = new RenderWindow(new VideoMode(1280, 720), "", Styles.Close);
            window.SetFramerateLimit(60);
            window.Closed += (sender, eventArgs) => window.Close();

            shipTex = new Texture("Ship.png");
            asteroidTex = new Texture("Asteroid.png");

            var shipSpr = new Sprite(shipTex);
            shipSpr.Origin = new Vector2f(shipTex.Size.X / 2f, shipTex.Size.Y / 2f);

            var asteroidSpr = new Sprite(asteroidTex);
            asteroidSpr.Origin = new Vector2f(asteroidTex.Size.X / 2f, asteroidTex.Size.Y / 2f);

            world = new World(new Vector2(0, 0));

            var debugView = new SFMLDebugView(world);
            debugView.AppendFlags(DebugViewFlags.Shape);

            CreateBounds(20, 11.25f);

            var ship = CreateShip();
            ship.Position = new Vector2(3, 1);
            ship.Rotation = 1.7f;

            var ship2 = CreateShip();
            ship2.Position = new Vector2(3, 1);
            ship2.Rotation = 1.7f;

            var asteroid = CreateAsteroid();
            asteroid.Position = new Vector2(4, 4);

            window.KeyPressed += (sender, eventArgs) =>
            {
                if (eventArgs.Code == Keyboard.Key.Space)
                {
                    CreateBullets(ship);
                }
            };

            while (window.IsOpen())
            {
                window.DispatchEvents();

                if (Keyboard.IsKeyPressed(Keyboard.Key.W))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, -25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.S))
                {
                    ship.ApplyForce(ship.GetWorldVector(new Vector2(0.0f, 25.0f)));
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.A))
                {
                    ship.ApplyTorque(-10);
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.D))
                {
                    ship.ApplyTorque(10);
                }

                world.Step(1f / 60);

                window.Clear(Color.Black);

                shipSpr.Position = new Vector2f(ship.Position.X * 64, ship.Position.Y * 64);
                shipSpr.Rotation = (ship.Rotation * (180 / (float)Math.PI));
                window.Draw(shipSpr);

                asteroidSpr.Position = new Vector2f(asteroid.Position.X * 64, asteroid.Position.Y * 64);
                asteroidSpr.Rotation = (asteroid.Rotation * (180 / (float)Math.PI));
                window.Draw(asteroidSpr);

                var start = ship.Position;
                var step = (float)(2 * Math.PI) / 200;
                byte col = 0;
                var line = new VertexArray(PrimitiveType.Lines, 2);
                line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), Color.White);
                for (var dir = 0f; dir <= 2 * Math.PI; dir += step)
                {
                    float min = 100;
                    byte res = 255;
                    var point = start + LengthDir(dir, 20);
                    world.RayCast((f, p, n, fr) =>
                    {
                        if (fr > min)
                            return 1;

                        min = fr;
                        res = (byte)(fr * 255);
                        point = p;
                        return fr;
                    }, start, point);

                    line[0] = new Vertex(new Vector2f(start.X * 64, start.Y * 64), new Color(col, 0, 0));
                    line[1] = new Vertex(new Vector2f(point.X * 64, point.Y * 64), new Color(col, 0, 0));
                    window.Draw(line);
                    col++;
                }

                debugView.Draw(window);

                window.Display();
            }
        }