⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ship.cs

📁 使用蓝牙技术开发的小游戏(在Windows Mobile 6 下开发)
💻 CS
📖 第 1 页 / 共 2 页
字号:

            // Notify the enemy.
            if (sink != null)
            {
                sink.OnShipMove(Position);
            }

            // Check if any damage has been fixed yet.
            if (damaged && Environment.TickCount > damageTimeout)
            {
                Damaged = false;
            }

            // Check if the two ships collided.
            if ((Position - OtherShip.Position).Magnitude < Radius + otherShip.Radius)
            {
                Damaged = true;
            }

            // Update each friendly missile
            for (int i = 0; i < maxMissiles; i++)
            {
                Missile m = missiles[i];
                if (m.Launched)
                {
                    m.Update(ticks);

                    // Notify the enemy.
                    if (sink != null)
                    {
                        sink.OnMissileMove(i, m.Position);
                        m.SinkThinksItIsLaunched = true;
                    }

                    if (m.CheckForCollision(this))
                    {
                        // The ship was hit by its own missile.
                        Damaged = true;
                        if (sink != null)
                        {
                            sink.OnMissileGone(i);
                            m.SinkThinksItIsLaunched = false;
                        }
                    }
                }
                else
                {
                    System.Diagnostics.Debug.Assert(!m.Launched);
                    if (sink != null && m.SinkThinksItIsLaunched)
                    {
                        sink.OnMissileGone(i);
                        m.SinkThinksItIsLaunched = false;
                    }
                }
            }

            // Check enemy missiles
            for (int i = 0; i < maxMissiles; i++)
            {
                Missile m = OtherShip.Missiles[i];
                if (m.Launched)
                {
                    if (m.CheckForCollision(this))
                    {
                        // The friendly ship was hit by an enemy missile.
                        Damaged = true;
                        if (sink != null)
                        {
                            sink.OnOtherShipsMissileGone(i);
                        }
                    }
                }
            }
        }

        public void LaunchMissile()
        {
            if (Damaged)
            {
                return;
            }
            for (int i = 0; i < maxMissiles; i++)
            {
                Missile m = Missiles[i];
                if (!m.Launched)
                {
                    // Found one to launch.

                    // Missile starts off at the tip of the ship's nose.
                    Vector missilePosition = new Vector(Position.X + (int)(Radius * Cos(Rotation) * 1.1),
                                                        Position.Y + (int)(Radius * Sin(Rotation) * 1.1));

                    // Missile's velocity relative to the ship
                    Vector missileRelativeVelocity = new Vector((int)(missileRelativeSpeed * Cos(Rotation)),
                                                                (int)(missileRelativeSpeed * Sin(Rotation)));

                    m.Launch(missilePosition, this.Velocity + missileRelativeVelocity);
                    break;
                }
            }
        }


        /// <summary>
        /// The angle the ship is pointing.
        /// </summary>
        public int Rotation
        {
            get
            {
                return rotation;
            }
            set
            {
                if (!Damaged)
                {
                    rotation = value;
                    if (rotation < 0)
                    {
                        rotation += 360;
                    }
                    else if (rotation >= 360)
                    {
                        rotation -= 360;
                    }
                    if (sink != null)
                    {
                        sink.OnShipRotate(rotation);
                    }
                }
            }
        }



        /// <summary>
        /// Returns the sine of the angle.
        /// </summary>
        /// <param name="angle">The angle in degrees.</param>
        /// <returns></returns>
        private double Sin(int angle)
        {
            return Math.Sin(angle * Math.PI / 180);
        }

        /// <summary>
        /// Returns the cosine of the angle.
        /// </summary>
        /// <param name="angle">The angle in degrees.</param>
        /// <returns></returns>
        private double Cos(int angle)
        {
            return Math.Cos(angle * Math.PI / 180);
        }



        /// <summary>
        /// Paint the ship.
        /// </summary>
        /// <param name="g">The Graphics object.</param>
        public void Paint()
        {
            Point[] pts = new Point[3];
          
            // An arbitary angle that determines the pointy-ness of the ship
            const int shipShapeAngle = 36;

            // Nose
            pts[0].X = Draw.GameToScreenX(Position.X + Radius * Cos(Rotation));
            pts[0].Y = Draw.GameToScreenY(Position.Y + Radius * Sin(Rotation));

            // Left fin
            pts[1].X = Draw.GameToScreenX(Position.X + Radius * Cos(Rotation + 180 + shipShapeAngle));
            pts[1].Y = Draw.GameToScreenY(Position.Y + Radius * Sin(Rotation + 180 + shipShapeAngle));

            // Right fin
            pts[2].X = Draw.GameToScreenX(Position.X + Radius * Cos(Rotation + 180 - shipShapeAngle));
            pts[2].Y = Draw.GameToScreenY(Position.Y + Radius * Sin(Rotation + 180 - shipShapeAngle));
            
            Draw.BackBuffer.DrawPolygon(Damaged ? Draw.DamagedPen : Draw.WhitePen, pts);

            if (Thrusting)
            {
                // Draw the thruster's flame
                Draw.BackBuffer.DrawLine(Draw.WhitePen,
                                        Draw.GameToScreenX(Position.X - Radius * Cos(Rotation)),
                                        Draw.GameToScreenY(Position.Y - Radius * Sin(Rotation)),
                                        Draw.GameToScreenX(Position.X - (Radius * 1.4 * Cos(Rotation))),
                                        Draw.GameToScreenY(Position.Y - (Radius * 1.4 * Sin(Rotation))));
            }

            // Paint the missiles
            for (int i = 0; i < maxMissiles; i++)
            {
                Missile m = Missiles[i];
                if (m.Launched)
                {
                    Draw.BackBuffer.DrawEllipse(Draw.WhitePen, 
                                  Draw.GameToScreenX(m.Position.X - m.Radius), 
                                  Draw.GameToScreenY(m.Position.Y - m.Radius),
                                  m.Radius / draw.ScaleFactor, 
                                  m.Radius / draw.ScaleFactor);
                }
            }
        }


        #region IGameStateChangeSink
        // These methods will only be called on an enemy ship.

        public void OnShipMove(Vector vPos)
        {
            Position = vPos;
        }

        public void OnShipDamage(bool damaged)
        {
            this.damaged = damaged;
            if (damaged)
            {
                damageCount++;
            }
        }

        public void OnShipThrust(bool thrusting)
        {
            this.thrusting = thrusting;
        }

        public void OnShipRotate(int rotation)
        {
            this.rotation = rotation;
        }

        public void OnMissileMove(int iMissile, Vector vPos)
        {
            missiles[iMissile].Position = vPos;
            missiles[iMissile].Launched = true;
        }

        public void OnMissileGone(int iMissile)
        {
            missiles[iMissile].Launched = false;
        }

        public void OnOtherShipsMissileGone(int iMissile)
        {
            OtherShip.Missiles[iMissile].Launched = false;
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -