Example #1
0
        public void update(GameTime gameTime)
        {
            dest = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
            if (state == PlayerState.BLINKING)
            {
                blinktimer -= gameTime.ElapsedGameTime.Milliseconds;
                if (blinktimer <= 0)
                {
                    state = PlayerState.NORMAL;
                    opacity = 255;
                }    
            }
            if (state == PlayerState.NORMAL)
            {
                foreach (Enemy e in Enemy.enemies)
                {
                    if (e.bounds.Contains((int)location.X, (int)location.Y))
                    {
                        Hp--;
                      //  Game1.beginHpTrans(Hp, --Hp, 500);
                        state = PlayerState.BLINKING;
                        blinktimer = 2000;
                        if (Hp <= 0)
                        {
                           // state = PlayerState.DEAD;
                            state = PlayerState.BLINKING;
                            Hp = 5;
                            location = new Vector2(100, 100);
                            Game1.score = 0;
                        }
                        break;
                    }
                }
            }
            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (!pressed)
                {

                    pressed = true;
                }
                else
                {
                    if (Charge < 20)
                    {
                        Charge++;
                    }
                }
            }
            else if (Mouse.GetState().LeftButton == ButtonState.Released)
            {
                if (pressed)
                {
                    for (int i = 0; i < 10 + Charge; i++)
                    {
                        Bullet toadd = new Bullet();
                        toadd.angle = angleofrot;
                        Random r = new Random();
                        toadd.col = new Color(r.Next(255), 255, 255);
                        toadd.pos = location - (new Vector2((float)Math.Cos(angleofrot), (float)Math.Sin(angleofrot)) * i);
                        Bullet.bullets.Add(toadd);
                    }
                    pressed = false;
                    Charge = 0;
                }
            }
            if (Mouse.GetState().RightButton == ButtonState.Pressed && canpress2)
            {
                // createBurst(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
                Bullet toadd = new Bullet();
                toadd.angle = angleofrot;
                Random r = new Random();
                toadd.col = new Color(r.Next(255), 255, 255);
                toadd.pos = location;// -(new Vector2((float)Math.Cos(angleofrot), (float)Math.Sin(angleofrot)) * i);
                toadd.tracking = true;
                Bullet.bullets.Add(toadd);
                // Enemy.spawnWave();
                canpress2 = false;
            }

            else if (Mouse.GetState().RightButton == ButtonState.Released)
            {
                canpress2 = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                //apply upwards thrust

                force.Y += (gameTime.ElapsedGameTime.Milliseconds / 1000f) * multiplier;
                if (force.Y > upperbound)
                {
                    force.Y = upperbound;
                }
            }
            else
            {
                if (force.Y > 0)
                {
                    force.Y -= (gameTime.ElapsedGameTime.Milliseconds / 1000f) * (multiplier / 2f);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                force.Y -= (gameTime.ElapsedGameTime.Milliseconds / 1000f) * multiplier;
                if (force.Y < -upperbound)
                {
                    force.Y = -upperbound;
                }
            }
            else
            {
                if (force.Y < 0)
                {
                    force.Y += (gameTime.ElapsedGameTime.Milliseconds / 1000f) * (multiplier / 2f);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                force.X -= (gameTime.ElapsedGameTime.Milliseconds / 1000f) * multiplier;
                if (force.X < -upperbound)
                {
                    force.X = -upperbound;
                }
                //  angleofrot -= (gameTime.ElapsedGameTime.Milliseconds / 1000f);
            }
            else
            {
                if (force.X < 0)
                {
                    force.X += (gameTime.ElapsedGameTime.Milliseconds / 1000f) * (multiplier / 2f);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                force.X += (gameTime.ElapsedGameTime.Milliseconds / 1000f) * multiplier;
                //Console.WriteLine(gameTime.ElapsedGameTime.Milliseconds / 1000f);
                if (force.X > upperbound)
                {
                    force.X = upperbound;
                }
            }
            else
            {
                if (force.X > 0)
                {
                    force.X -= (gameTime.ElapsedGameTime.Milliseconds / 1000f) * (multiplier / 2f);
                }
            }
            if (dest != new Vector2(float.NaN, float.NaN) && Config.mouseplay)
            {
                if (Math.Abs(location.X - dest.X) > 3 || Math.Abs(location.Y - dest.Y) > 3)
                {
                    //new Vector4(location.X,location.Y,dest.X,dest.Y)
                    Vector2 tocalc = new Vector2(location.X - dest.X, location.Y - dest.Y);
                    tocalc.Normalize();
                    angleofrot = Math.Atan2(tocalc.Y, tocalc.X);
                    /*     if (MathHelper.ToDegrees((float)Math.Abs(angleofrot - lastangle)) > 2)
                          {
                              if (angleofrot - lastangle > 0)
                              {
                                  tocalc = new Vector2((float)Math.Cos(lastangle + 2), (float)Math.Sin(lastangle + 2));
                              }
                              else
                              {
                                  tocalc = new Vector2((float)Math.Cos(lastangle - 2), (float)Math.Sin(lastangle - 2));

                              }
                          } */
                    tocalc *= 6;
                    int calcx = (int)((location.X - tocalc.X));
                    // Console.WriteLine("{0} {1} {2}", tocalc.X, tocalc.Y, calcx);
                    location -= tocalc;
                    //(calcx, (int)((location.Y - tocalc.Y)), arrow.Width, arrow.Height);
                    lastangle = (float)angleofrot;
                }
                else
                {
                    dest = new Vector2(float.NaN, float.NaN);
                }

            }
            else
            {
                Vector2 tocalc = new Vector2(location.X - Mouse.GetState().X, location.Y - Mouse.GetState().Y);
                tocalc.Normalize();
                angleofrot = Math.Atan2(tocalc.Y, tocalc.X);
                Point p = Program.theGame.Window.ClientBounds.Location;
                if (Program.theGame.Window.ClientBounds.Contains(new Point((int)(location.X + force.X + p.X), (int)(location.Y + force.Y + p.Y))))
                {
                    location.X += force.X;
                    location.Y += force.Y;
                }
                else
                {
                    force.X = 0;
                    force.Y = 0;
                }
            }
        }
Example #2
0
 /// <summary>
 /// Allows the game to run logic such as updating the world,
 /// checking for collisions, gathering input, and playing audio.
 /// </summary>
 /// <param name="gameTime">Provides a snapshot of timing values.</param>                     
 void createBurst(Vector2 initial)
 {
     Random r = new Random();
     for (int i = 0; i < 360; i += r.Next(1, 7))
     {
         Bullet b = new Bullet();
         b.angle = MathHelper.ToRadians(i);
         b.pos = initial;
         b.speed = r.Next(7, 15);
         b.col = Color.White;
         Bullet.bullets.Add(b);
     }
 }