Exemplo n.º 1
0
        internal void ProcessMovement()
        {
            // update robot position
            PointD tOldPosition = m_tPosition;

            m_tPosition += m_tMovement;
            m_tMovement  = PointD.Empty;

            // check if we have hit a wall
            bool bHitWall = false;

            if (m_tPosition.X - Size.Width / 2.0 < 0)
            {
                m_tPosition.X = Size.Width / 2.0;
                bHitWall      = true;
            }
            else if (m_tPosition.X + Size.Width / 2.0 > m_pBattle.BattleFieldSize.Width)
            {
                m_tPosition.X = m_pBattle.BattleFieldSize.Width - Size.Width / 2.0;
                bHitWall      = true;
            }
            if (m_tPosition.Y - Size.Height / 2.0 < 0)
            {
                m_tPosition.Y = Size.Height / 2.0;
                bHitWall      = true;
            }
            else if (m_tPosition.Y + Size.Height / 2.0 > m_pBattle.BattleFieldSize.Height)
            {
                m_tPosition.Y = m_pBattle.BattleFieldSize.Height - Size.Height / 2.0;
                bHitWall      = true;
            }
            if (bHitWall)
            {
                m_pEvents.Add(new HitWallEvent());
                m_dEnergy -= 3.0;
            }

            // rotate robot
            m_nHeading += m_nRotation;
            if (m_nHeading < 0)
            {
                m_nHeading = 360 + m_nHeading;
            }
            if (m_nHeading > 359)
            {
                m_nHeading -= 360;
            }
            m_nRotation = 0;

            PointF tEndPoint = new PointF((float)(Position.X + Math.Sin(HeadingRadians) * BattleFieldSize.Width),
                                          (float)(Position.Y + Math.Cos(HeadingRadians) * BattleFieldSize.Width));

            // check if we can see another robot
            for (int i = 0; i < m_pBattle.Robots.Length; i++)
            {
                Robot pRobot = m_pBattle.Robots[i];

                // make sure we're not testing against ourselves
                if (pRobot != this)
                {
                    GraphicsPath pPath = new GraphicsPath();
                    pPath.AddLine(Position, tEndPoint);
                    pPath.CloseFigure();
                    pPath.Widen(new Pen(Color.Black, 1));
                    Region pRegion = new Region(pPath);
                    pRegion.Intersect(pRobot.Bounds);
                    if (!pRegion.IsEmpty(m_pGraphics))
                    {
                        // we can see this robot
                        PointD            tDistance = pRobot.Position - Position;
                        double            dDistance = Math.Sqrt(tDistance.X * tDistance.X + tDistance.Y * tDistance.Y);
                        ScannedRobotEvent pEvent    = new ScannedRobotEvent(
                            pRobot.Name, pRobot.Energy, pRobot.Heading - Heading,
                            dDistance, pRobot.Heading, pRobot.Velocity);
                        m_pEvents.Add(pEvent);
                    }
                }
            }

            // check if we have collided with another robot
            for (int i = 0; i < m_pBattle.Robots.Length; i++)
            {
                Robot pRobot = m_pBattle.Robots[i];

                // make sure we're not testing against ourselves
                if (pRobot != this)
                {
                    if (pRobot.Bounds.IntersectsWith(Bounds))
                    {
                        // send message to ourselves
                        bool          bAtFault = true;
                        HitRobotEvent pEvent   = new HitRobotEvent(pRobot.Name,
                                                                   Math.Abs(pRobot.Heading - Heading),
                                                                   pRobot.Energy, bAtFault);
                        m_pEvents.Add(pEvent);
                        m_tPosition = tOldPosition;
                        m_dEnergy  -= 3.0;
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (m_pObjects != null)
            {
                int nLength = m_pObjects.Length;
                for (int i = 0; i < nLength; i++)
                {
                    if (m_pObjects[i].IsAlive)
                    {
                        PointD tPositionD = m_pObjects[i].Position;
                        Point  tPosition  = new Point((int)tPositionD.X, (int)tPositionD.Y);
                        tPosition.Y = Height - tPosition.Y;
                        SizeD tSizeD = m_pObjects[i].Size;

                        // position labels
                        PointF tLabelEnergy = new PointF(tPosition.X, tPosition.Y - 35);
                        PointF tLabelName   = new PointF(tPosition.X, tPosition.Y + 27);
                        if (tLabelEnergy.Y < 10)
                        {
                            tLabelEnergy.Y = 10;
                        }
                        if (tLabelName.Y > Height - 20)
                        {
                            tLabelName.Y = Height - 20;
                        }

                        // draw bullets
                        e.Graphics.ResetTransform();
                        for (int j = 0; j < m_pObjects[i].Bullets.Length; j++)
                        {
                            Bullet pBullet = m_pObjects[i].Bullets[j];
                            e.Graphics.FillRectangle(new SolidBrush(Color.White),
                                                     (float)pBullet.Position.X, Height - (float)pBullet.Position.Y,
                                                     5, 5);
                        }

                        // draw robot
                        e.Graphics.ResetTransform();
                        tPosition.X -= (int)(tSizeD.Width / 2.0);
                        tPosition.Y -= (int)(tSizeD.Height / 2.0);
                        Matrix tMatrix = new Matrix();
                        tMatrix.Translate((float)tPosition.X, (float)tPosition.Y);
                        PointF tRotationPoint = new PointF((float)(tSizeD.Width / 2.0f),
                                                           (float)(tSizeD.Height / 2.0f));
                        tMatrix.RotateAt((float)m_pObjects[i].Heading, tRotationPoint);
                        e.Graphics.Transform = tMatrix;
                        e.Graphics.DrawImage(m_pObjects[i].BaseBitmap, 0, 0, 50, 50);

                        tMatrix.Translate(0, 0);
                        e.Graphics.Transform = tMatrix;
                        e.Graphics.DrawImage(m_pObjects[i].GunBitmap, 0, 0, 50, 50);

                        // draw labels
                        e.Graphics.ResetTransform();
                        StringFormat pFormat = new StringFormat();
                        pFormat.Alignment = StringAlignment.Center;
                        e.Graphics.DrawString(m_pObjects[i].Energy.ToString(), m_pLabelFont,
                                              new SolidBrush(Color.White), tLabelEnergy, pFormat);
                        e.Graphics.DrawString(m_pObjects[i].Name, m_pLabelFont,
                                              new SolidBrush(Color.White), tLabelName, pFormat);

                        /*tPosition = new Point((int) tPositionD.X, (int) tPositionD.Y);
                         * tPosition.Y = Height - tPosition.Y;
                         * PointF tEndPoint = m_pObjects[i].EndPoint;
                         * tEndPoint.Y = Height - tEndPoint.Y;
                         * Color tColour = (i == 0) ? Color.Red : Color.Blue;
                         * Pen pPen = new Pen(tColour);
                         * e.Graphics.DrawLine(pPen, tPosition, tEndPoint);*/
                    }
                }
            }
        }