Beispiel #1
0
        public void Update(GameCommand command)
        {
            if (isDead || freeze)
            {
                return;
            }

            if (command.Left != command.Right)
            {
                if (command.Left)
                {
                    vx -= SPEED_UP;
                    if (vx < -MAX_SPEED)
                    {
                        vx = -MAX_SPEED;
                    }
                }
                if (command.Right)
                {
                    vx += SPEED_UP;
                    if (vx > MAX_SPEED)
                    {
                        vx = MAX_SPEED;
                    }
                }
            }
            else
            {
                if (Math.Abs(vx) < SPEED_DOWN)
                {
                    vx = 0;
                }
                else
                {
                    vx -= Math.Sign(vx) * SPEED_DOWN;
                }
            }
            if (command.Up != command.Down)
            {
                if (command.Up)
                {
                    vy -= SPEED_UP;
                    if (vy < -MAX_SPEED)
                    {
                        vy = -MAX_SPEED;
                    }
                }
                if (command.Down)
                {
                    vy += SPEED_UP;
                    if (vy > MAX_SPEED)
                    {
                        vy = MAX_SPEED;
                    }
                }
            }
            else
            {
                if (Math.Abs(vy) < SPEED_DOWN)
                {
                    vy = 0;
                }
                else
                {
                    vy -= Math.Sign(vy) * SPEED_DOWN;
                }
            }

            X += vx;
            Y += vy;

            if (X < 16)
            {
                X = 16;
            }
            else if (X > Game.FieldWidth - 16)
            {
                X = Game.FieldWidth - 16;
            }
            if (Y < Game.FieldHeight / 4)
            {
                Y = Game.FieldHeight / 4;
            }
            else if (Y > Game.FieldHeight - 16)
            {
                Y = Game.FieldHeight - 16;
            }

            if (fireCount > 0)
            {
                fireCount--;
            }
            if (command.Button1)
            {
                if (fireCount == 0)
                {
                    if (Game.PlayerBulletList.Count < 8)
                    {
                        Bullet bullet = new PlayerBullet(Game, X, Y - 32, 16, 90);
                        Game.AddPlayerBullet(bullet);
                        Game.PlaySound(Sound.PlayerFire);
                        fireCount = 2;
                    }
                }
            }
        }
Beispiel #2
0
        public void Update(GameCommand command)
        {
            if (isDead || freeze)
            {
                return;
            }

            if (command.Left != command.Right)
            {
                if (command.Left)
                {
                    vx -= SPEED_UP;
                    if (vx < -MAX_SPEED)
                    {
                        vx = -MAX_SPEED;
                    }
                }
                if (command.Right)
                {
                    vx += SPEED_UP;
                    if (vx > MAX_SPEED)
                    {
                        vx = MAX_SPEED;
                    }
                }
            }
            else
            {
                if (Math.Abs(vx) < SPEED_DOWN)
                {
                    vx = 0;
                }
                else
                {
                    vx -= Math.Sign(vx) * SPEED_DOWN;
                }
            }
            if (command.Up != command.Down)
            {
                if (command.Up)
                {
                    vy -= SPEED_UP;
                    if (vy < -MAX_SPEED)
                    {
                        vy = -MAX_SPEED;
                    }
                }
                if (command.Down)
                {
                    vy += SPEED_UP;
                    if (vy > MAX_SPEED)
                    {
                        vy = MAX_SPEED;
                    }
                }
            }
            else
            {
                if (Math.Abs(vy) < SPEED_DOWN)
                {
                    vy = 0;
                }
                else
                {
                    vy -= Math.Sign(vy) * SPEED_DOWN;
                }
            }

            X += vx;
            Y += vy;

            if (X < 16)
            {
                X = 16;
            }
            else if (X > Game.FieldWidth - 16)
            {
                X = Game.FieldWidth - 16;
            }
            if (Y < Game.FieldHeight / 4)
            {
                Y = Game.FieldHeight / 4;
            }
            else if (Y > Game.FieldHeight - 16)
            {
                Y = Game.FieldHeight - 16;
            }

            if (fireCount > 0)
            {
                fireCount--;
            }
            if (command.Button1)
            {
                if (fireCount == 0)
                {
                    if (Game.PlayerBulletList.Count < 8)
                    {
                        Bullet bullet = new PlayerBullet(Game, X, Y - 32, 16, 90);
                        Game.AddPlayerBullet(bullet);
                        Game.PlaySound(Sound.PlayerFire);
                        fireCount = 2;
                    }
                }
            }
        }
Beispiel #3
0
 public override void Update()
 {
     if (waitCount > 0)
     {
         double newX = waitX + swingWidth * Utility.Sin(Game.Ticks * 8);
         double newY = waitY + swingHeight * Utility.Sin(Game.Ticks * 8);
         vx = newX - X;
         vy = newY - Y;
         X  = newX;
         Y  = newY;
         waitCount--;
     }
     if (waitCount == 0)
     {
         if (!attacking)
         {
             if (angle == 90)
             {
                 angle += Game.Random.Next(2) == 0 ? 1 : -1;
             }
             else
             {
                 int deg = Utility.NormalizeDeg(270 - angle);
                 if (Math.Abs(deg) < 8)
                 {
                     angle     = 270;
                     attacking = true;
                 }
                 else
                 {
                     if (deg < 0)
                     {
                         angle -= 8;
                     }
                     else if (deg > 0)
                     {
                         angle += 8;
                     }
                 }
             }
             vx += 0.5 * Utility.Cos(angle);
             vy -= 0.5 * Utility.Sin(angle);
             Move();
         }
         else
         {
             PlayerBullet playerBullet = null;
             foreach (PlayerBullet targetBullet in Game.PlayerBulletList)
             {
                 if (Math.Abs(targetBullet.X - X) < 32)
                 {
                     if (Y < targetBullet.Y && targetBullet.Y - 128 < Y)
                     {
                         if (playerBullet == null || targetBullet.Y < playerBullet.Y)
                         {
                             playerBullet = targetBullet;
                         }
                     }
                 }
             }
             if (playerBullet == null)
             {
                 if (X < Game.Player.X + playerXTraceOffset)
                 {
                     vx += 0.5;
                 }
                 else if (X > Game.Player.X + playerXTraceOffset)
                 {
                     vx -= 0.5;
                 }
             }
             else
             {
                 if (X < playerBullet.X)
                 {
                     vx -= 3;
                 }
                 else if (X > playerBullet.X)
                 {
                     vx += 3;
                 }
             }
             vy += 0.25;
             Move();
             {
                 int deg = Utility.NormalizeDeg(Utility.Atan2(Y - Game.Player.Y, Game.Player.X - X) - angle);
                 if (deg == -180)
                 {
                     angle += Game.Random.Next(2) == 0 ? 1 : -1;
                 }
                 else if (Math.Abs(deg) < 2)
                 {
                     angle = Utility.Atan2(Y - Game.Player.Y, Game.Player.X - X);
                 }
                 else
                 {
                     if (deg < 0)
                     {
                         angle -= 2;
                     }
                     else if (deg > 0)
                     {
                         angle += 2;
                     }
                 }
                 angle = (angle % 360 + 360) % 360;
                 if (angle < 225)
                 {
                     angle = 225;
                 }
                 else if (angle > 315)
                 {
                     angle = 315;
                 }
             }
             if (fireCount > 0)
             {
                 fireCount--;
             }
             else if (!Game.Player.IsDead)
             {
                 if (Y < Game.Player.Y)
                 {
                     Bullet bullet = new EnemyBullet(Game, X + 32 * Utility.Cos(angle), Y - 32 * Utility.Sin(angle), 12, angle);
                     Game.AddEnemyBullet(bullet);
                     Game.PlaySound(Sound.EnemyFire);
                     fireCount          = Game.Random.Next(60, 90);
                     playerXTraceOffset = Game.Random.Next(-32, 32 + 1);
                 }
                 else
                 {
                     fireCount          = Game.Random.Next(15, 30);
                     playerXTraceOffset = Game.Random.Next(-32, 32 + 1);
                 }
             }
         }
     }
     damaged = false;
 }