Ejemplo n.º 1
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            float turnAmount = .04f;

            ParticleManager.Instance().ParticleSystems["ghost" + ghostNum].Update(.1f);


            switch (this.ghostState)
            {
            case GhostState.Dead:

                //TODO Dead moving and dead animation.
                //Until then
                this.ghostState = GhostState.Roving;
                //Pick random direction
                Random  r = new Random();
                Vector2 v = new Vector2((float)r.NextDouble() - 0.5f, (float)r.NextDouble() - 0.5f);
                Vector2.Normalize(ref v, out v);        //Normalize
                this.Direction = v;                     //Assign random direction

                break;

            case GhostState.Chasing:
                //Change texture if Chasing
                if (!(this.spriteTexture == this.ghost))
                {
                    //gameConsole.GameConsoleWrite(this.ToString() + " Chasing changed texture to spriteTexture");
                    this.spriteTexture = this.ghost;
                }
                if (pacMan.Location.Y > this.Location.Y)
                {
                    //this.Direction.Y = 1;
                    this.Direction.Y = MathHelper.Clamp(this.Direction.Y += turnAmount, -1, 1);
                }
                else
                {
                    //this.Direction.Y = -1;
                    this.Direction.Y = MathHelper.Clamp(this.Direction.Y -= turnAmount, -1, 1);
                }
                if (pacMan.Location.X > this.Location.X)
                {
                    //this.Direction.X = 1;
                    this.Direction.X = MathHelper.Clamp(this.Direction.X += turnAmount, -1, 1);
                }
                else
                {
                    //this.Direction.X = -1;
                    this.Direction.X = MathHelper.Clamp(this.Direction.X -= turnAmount, -1, 1);
                }
                break;

            case GhostState.Evading:
                //Change texture if evading
                if (this.spriteTexture != this.ghostHit)
                {
                    //gameConsole.GameConsoleWrite(this.ToString() + " Evading changed texture to ghostHit");
                    this.spriteTexture = this.ghostHit;
                }

                if (pacMan.Location.Y > this.Location.Y)
                {
                    this.Direction.Y = -1;
                }
                else
                {
                    this.Direction.Y = 1;
                }
                if (pacMan.Location.X > this.Location.X)
                {
                    this.Direction.X = -1;
                }
                else
                {
                    this.Direction.X = -1;
                }

                ParticleManager.Instance().ParticleSystems["ghost" + ghostNum].AddParticles(
                    new Vector2(this.Location.X + this.LocationRect.Width / 2,
                                this.Location.Y + this.LocationRect.Height / 2),
                    Vector2.Zero);


                break;

            case GhostState.Roving:
                //Change texture if Chasing
                if (!(this.spriteTexture == this.ghost))
                {
                    //gameConsole.GameConsoleWrite(this.ToString() + " Roving changed texture to spriteTexture");
                    this.spriteTexture = this.ghost;
                }
                //check if ghost can see pacman
                Vector2 normD = Vector2.Normalize(this.Direction);
                Vector2 p     = new Vector2(this.Location.X, this.Location.Y);
                while (p.X < graphics.GraphicsDevice.Viewport.Width &&
                       p.X > 0 &&
                       p.Y < graphics.GraphicsDevice.Viewport.Height &&
                       p.Y > 0)
                {
                    if (pacMan.LocationRect.Contains(new Point((int)p.X, (int)p.Y)))
                    {
                        this.ghostState = GhostState.Chasing;
                        gameConsole.GameConsoleWrite(this.ToString() + " saw pacman");
                        break;
                    }
                    p += this.Direction;
                }


                break;
            }

            //Borders
            if ((this.Location.Y + this.spriteTexture.Height / 2 > graphics.GraphicsDevice.Viewport.Height)
                ||
                (this.Location.Y - this.spriteTexture.Height / 2 < 0)
                )
            {
                this.Direction.Y *= -1;
                //this.ghostState = GhostState.Roving;
            }
            if ((this.Location.X + this.spriteTexture.Width / 2 > graphics.GraphicsDevice.Viewport.Width)
                ||
                (this.Location.X - this.spriteTexture.Width / 2 < 0)
                )
            {
                this.Direction.X *= -1;
                //this.ghostState = GhostState.Roving;
            }

            Location += ((this.Direction * (lastUpdateTime / 1000)) * Speed);      //Simple Move

            //Collision
            if (this.Intersects(pacMan))
            {
                //PerPixel Collision
                if (this.PerPixelCollision(pacMan))
                {
                    if (this.ghostState == GhostState.Evading)
                    {
                        this.Visible = false;
                        this.Enabled = false;
                    }
                    else
                    {
                        if (pacMan.PacManState != PacManState.Dying)
                        {
                            pacMan.Die();
                            this.Location   = new Vector2(100, 100);
                            this.ghostState = GhostState.Roving;
                        }
                    }
                }
            }



            base.Update(gameTime);
        }