示例#1
0
        public void Update(GameTime time)
        {
            if (Visible)
            {
                UpdateFrame(time);
                if (!Shooting)
                {
                    if (CanMove(faceDirection))
                    {
                        if (faceDirection == Direction.Right)
                        {
                            Position.X += 1 * Settings.gameSpeed;
                        }
                        else
                        {
                            Position.X -= 1 * Settings.gameSpeed;
                        }
                    }
                    else
                    {
                        faceDirection = (faceDirection == Direction.Right) ? Direction.Left : Direction.Right;
                    }
                }

                if (yat != null)
                {
                    Vector2 yatPos = yat.Vector;
                    if (yatPos.Y + yat.Height == Position.Y + frameSize.Y)
                    {
                        float distance = Vector2.Distance(yatPos, Position);
                        if (distance < attackDistance && !Shooting && fireball.Ready())
                        {
                            Attacking         = true;
                            Shooting          = true;
                            fireball.Position = Position;
                            fireball.Play();
                            if (yatPos.X < Position.X)
                            {
                                faceDirection          = Direction.Left;
                                fireball.faceDirection = Direction.Right;
                            }
                            else
                            {
                                faceDirection          = Direction.Right;
                                fireball.faceDirection = Direction.Left;
                            }
                        }
                    }
                    else
                    {
                        Attacking = false;
                    }

                    if (yat.Attacking)
                    {
                        Rectangle hammer = yat.GetBoundingBox();
                        Rectangle bb     = GetBoundingBox();
                        if (hammer.Intersects(bb))
                        {
#if DEBUG
                            Debugger.Log(1, "Main", string.Format("Yat killed mushroom.\n", GetHashCode()));
#endif
                            Score.Points += 1500;
                            Visible       = false;
                            Play();
                            xpgen.Generate(Rand.Next(1, 5), Position);
                        }
                    }
                }
            }
            if (Shooting)
            {
                fireball.Update(time);
                bool      hitYat = false;
                Rectangle fb     = fireball.GetBoundingBox();
                Rectangle theyat = yat.GetBoundingBox();
                if (fb.Intersects(theyat) && !yat.Invulnerable)
                {
                    yat.healthLeft -= fireball.power;
                    hitYat          = true;
#if DEBUG
                    Debugger.Log(1, "Main", string.Format("Fireball inflicted {0} damage on yat. Yat's health left {1}\n", fireball.power, yat.healthLeft));
#endif
                }

                if (fireball.faceDirection == Direction.Left)
                {
                    fireball.Position.X += fireballSpeed * Settings.gameSpeed;
                }
                else
                {
                    fireball.Position.X -= fireballSpeed * Settings.gameSpeed;
                }

                float distanceTraveled = Vector2.Distance(Position, fireball.Position);
                if (distanceTraveled > attackDistance || hitYat)
                {
                    fireball.Position = Position;
                    Shooting          = false;
                    Attacking         = false;
                }
            }
            xpgen.Update(time);
        }