Esempio n. 1
0
        public Game()
        {
            InitializeComponent();

            if (!InitializeDirect3D())
            {
                return;
            }

            sounds = new AlienSound(this);

            vertices = new VertexBuffer(typeof(CustomVertex.PositionColored), // Type of vertex
                                        4,                                    // How many
                                        device,                               // What device
                                        0,                                    // No special usage
                                        CustomVertex.PositionColored.Format,
                                        Pool.Managed);

            background = new Background(device, playingW, playingH);

            font = new Microsoft.DirectX.Direct3D.Font(device,                         // Device we are drawing on
                                                       40,                             // Font height in pixels
                                                       0,                              // Font width in pixels or zero to match height
                                                       FontWeight.Bold,                // Font weight (Normal, Bold, etc.)
                                                       0,                              // mip levels (0 for default)
                                                       false,                          // italics?
                                                       CharacterSet.Default,           // Character set to use
                                                       Precision.Default,              // The font precision, try some of them...
                                                       FontQuality.Default,            // Quality?
                                                       PitchAndFamily.FamilyDoNotCare, // Pitch and family, we don't care
                                                       "Arial");                       // And the name of the font

            // Determine the last time
            stopwatch.Start();
            lastTime = stopwatch.ElapsedMilliseconds;

            texture1          = TextureLoader.FromFile(device, "../../texture1.bmp");
            texture2          = TextureLoader.FromFile(device, "../../texture2.png");
            texture3          = TextureLoader.FromFile(device, "../../texture3.bmp");
            texture4          = TextureLoader.FromFile(device, "../../texture4.bmp");
            texture5          = TextureLoader.FromFile(device, "../../texture5.bmp");
            texture6          = TextureLoader.FromFile(device, "../../texture6.bmp");
            stoneTexture      = TextureLoader.FromFile(device, "../../texture7.bmp");
            finishLineTexture = TextureLoader.FromFile(device, "../../finishlineplatform.bmp");
            projectileGen     = new ProjectileGenerator(device);

            //Create barrier to keep player from walking off edges
            AddObstacle(-0.1f, 0, int.MinValue, int.MaxValue, Color.MintCream);
            AddObstacle(playingW, playingW + 0.1f, int.MinValue, int.MaxValue, Color.MintCream);

            Polygon startingPlat = new Polygon();

            startingPlat.AddVertex(new Vector2(0, .1f));
            startingPlat.AddVertex(new Vector2(.5f, .1f));
            startingPlat.AddVertex(new Vector2(.5f, 0));
            startingPlat.AddVertex(new Vector2(0, 0));
            startingPlat.Color = Color.DarkSlateGray;
            world.Add(startingPlat);

            Platform platform = new Platform();

            platform.AddVertex(new Vector2(3.2f, 2));
            platform.AddVertex(new Vector2(4.3f, 2));
            platform.AddVertex(new Vector2(4.3f, 1.8f));
            platform.AddVertex(new Vector2(3.2f, 1.8f));
            platform.Color = Color.DarkSlateGray;
            world.Add(platform);

            Platform platform4 = new Platform();

            platform4.AddVertex(new Vector2(21, 2));
            platform4.AddVertex(new Vector2(21.5f, 2));
            platform4.AddVertex(new Vector2(21.5f, 1.8f));
            platform4.AddVertex(new Vector2(21, 1.8f));
            platform4.Color = Color.DarkSlateGray;
            world.Add(platform4);

            Platform platform5 = new Platform();

            platform5.AddVertex(new Vector2(23, 2));
            platform5.AddVertex(new Vector2(23.5f, 2));
            platform5.AddVertex(new Vector2(23.5f, 1.8f));
            platform5.AddVertex(new Vector2(23, 1.8f));
            platform5.Color = Color.DarkSlateGray;
            platform5.setDtOffset(0.01f);
            world.Add(platform5);

            HorizontalPlat platform2 = new HorizontalPlat();

            platform2.AddVertex(new Vector2(11, 2));
            platform2.AddVertex(new Vector2(12f, 2));
            platform2.AddVertex(new Vector2(12f, 1.8f));
            platform2.AddVertex(new Vector2(11f, 1.8f));
            platform2.Speed = 1f;
            platform2.Color = Color.DarkSlateGray;
            platform2.Width = 4;
            world.Add(platform2);

            //addTexturedPolygon(left, right, bottom, top, outer, texture)
            addTexturedPolygon(1.2f, 1.9f, 1.4f, 1.6f, 1.3f, stoneTexture); //1st plat
            addTexturedPolygon(6, 7, 1.7f, 3f, 1.5f, texture1);             //3rd after moving

            addTexturedPolygon(9, 10, 1.2f, 1.7f, 1.0f, texture4);
            addTexturedPolygon(15, 16, 2.5f, 2.7f, 2.0f, texture5);
            addTexturedPolygon(18, 20, 2.2f, 2.4f, 1.5f, texture2);
            addTexturedPolygon(24.5f, 25, 2.2f, 2.4f, 1.5f, texture3);
            addTexturedPolygon(27, 29, 2.2f, 2.4f, 1.5f, texture6);

            EndPlat pt = new EndPlat();

            pt.Tex = finishLineTexture;
            pt.AddVertex(new Vector2(30, 1.5f)); //A
            pt.AddTex(new Vector2(0, 0));
            pt.AddVertex(new Vector2(32, 1.5f)); //B
            pt.AddTex(new Vector2(1, 0));
            pt.AddVertex(new Vector2(32, 0.5f)); //C
            pt.AddTex(new Vector2(1, 1));
            pt.AddVertex(new Vector2(30, 0.5f));
            pt.AddTex(new Vector2(0, 1));
            pt.Color = Color.Transparent;
            world.Add(pt);

            Texture spritetexture = TextureLoader.FromFile(device, "../../falco.png");

            player.Tex = spritetexture;
            player.AddVertex(new Vector2(-0.2f, 0));
            player.AddTex(new Vector2(0, 1));
            player.AddVertex(new Vector2(-0.2f, 0.5f));
            player.AddTex(new Vector2(0, 0));
            player.AddVertex(new Vector2(0.2f, 0.5f));
            player.AddTex(new Vector2(0.125f, 0));
            player.AddVertex(new Vector2(0.2f, 0));
            player.AddTex(new Vector2(0.125f, 1));
            player.Color       = Color.Transparent;
            player.Transparent = true;
            player.P           = new Vector2(0.5f, 1);
            player.A           = new Vector2(0, -9.8f);
            currentPlat        = null;

            scores = new Scoring("../../highscores.xml");
            scores.Load();
        }
Esempio n. 2
0
        /// <summary>
        /// Advance the game in time
        /// </summary>
        public void Advance()
        {
            // How much time change has there been?
            long time = stopwatch.ElapsedMilliseconds;

            score = 1000 - (int)time / 100;
            if (score < 0)
            {
                Defeat();
            }
            currentPlat = null;
            float delta = (time - lastTime) * 0.001f;       // Delta time in milliseconds

            lastTime = time;

            while (delta > 0)
            {
                float step = delta;
                if (step > 0.05f)
                {
                    step = 0.05f;
                }



                float maxspeed = Math.Max(Math.Abs(player.V.X), Math.Abs(player.V.Y));
                if (maxspeed > 0)
                {
                    step = (float)Math.Min(step, 0.05 / maxspeed);
                }

                player.Advance(step);

                foreach (Polygon p in world)
                {
                    p.Advance(step);
                }
                player.isStanding = false;
                foreach (Polygon p in world)
                {
                    if (collision.Test(player, p))
                    {
                        float depth = collision.P1inP2 ?
                                      collision.Depth : -collision.Depth;
                        player.P = player.P + collision.N * depth;
                        Vector2 v = player.V;
                        if (p is Projectile)
                        {
                            if (collision.N.Y != -1 && collision.N.X == 0)
                            {
                                sounds.Attack();
                                v.Y      = -1 * player.V.Y;
                                v.Y      = (v.Y < 5) ? v.Y : 5;
                                player.V = v;
                                ((Projectile)p).DropRate = -5;
                                continue;
                            }
                            else
                            {
                                PlayerDied();
                                continue;
                            }
                        }
                        if (collision.N.X != 0)
                        {
                            v.X = 0;
                        }
                        if (collision.N.Y != 0)
                        {
                            v.Y = 0;
                            if (collision.N.Y != -1)
                            {
                                //Check that player didn't just hit its head on ceiling
                                player.isStanding = true;
                            }
                            if (p is Platform)
                            {
                                Platform x  = (Platform)p;
                                Vector2  v2 = player.V;
                                v.Y               = x.GetSpeed;
                                player.V          = v;
                                player.isStanding = true;
                            }
                            if (p is HorizontalPlat)
                            {
                                HorizontalPlat x  = (HorizontalPlat)p;
                                Vector2        v2 = player.V;
                                currentPlat       = x;
                                v.X               = x.GetSpeed;
                                player.isStanding = true;
                            }
                            if (p is EndPlat)
                            {
                                Victory();
                                return;
                            }
                        }


                        player.V = v;
                        player.Advance(0);
                    }
                }
                world  = projectileGen.Advance(delta, world);
                delta -= step;
            }
            //Check to see if we need a reset
            if (player.P.Y < -.5f)
            {
                PlayerDied();
            }
        }