コード例 #1
0
        public bool isTowerOnPath(TDTower t)
        {
            // first check if intersting any corner
            TDPathPoint lastPoint = null;

            foreach (TDPath p in this.Paths)
            {
                foreach (TDPathPoint pp in p.PathPoints)
                {
                    if (TDMath.Intersects(t.Location, t.Size, pp.Location, GridSize))
                    {
                        return(true);
                    }
                    else
                    {
                        // check intersecting any line
                        if (lastPoint != null)
                        {
                            if (TDMath.Intersects(t, pp, lastPoint))
                            {
                                return(true);
                            }
                        }

                        lastPoint = pp;
                    }
                }
            }

            return(false);
        }
コード例 #2
0
ファイル: TDMath.cs プロジェクト: HansonScott/TowerDefense
        /// <summary>
        /// Does the target interect the line between point A and point B.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="pointA"></param>
        /// <param name="pointB"></param>
        /// <returns></returns>
        internal static bool Intersects(TDTower target, TDPathPoint pointA, TDPathPoint pointB)
        {
            bool result = false;

            // adjust PathPoints by GridSize
            double aX = pointA.Location.X;
            double aY = pointA.Location.Y;
            double bX = pointB.Location.X;
            double bY = pointB.Location.Y;

            bool intersectXAxis = false;
            bool intersectYAxis = false;

            if (aX < bX)
            {
                intersectXAxis = target.Location.X <bX + (TDMap.GridSize - 2) && target.Location.X + (TDMap.GridSize - 2)> aX;
            }
            else
            {
                intersectXAxis = target.Location.X <aX + (TDMap.GridSize - 2) && target.Location.X + (TDMap.GridSize - 2)> bX;
            }

            // y between
            if (aY < bY)
            {
                intersectYAxis = target.Location.Y <bY + (TDMap.GridSize - 2) && target.Location.Y + (TDMap.GridSize - 2)> aY;
            }
            else
            {
                intersectYAxis = target.Location.Y <aY + (TDMap.GridSize - 2) && target.Location.Y + (TDMap.GridSize - 2)> bY;
            }

            // if it intersects the rectangle of the two
            result = intersectXAxis && intersectYAxis;

            // if it is within the square box, and is a diagonal line, check it additionally because it might not be on the actual path.
            if (result &&
                aX != bX && aY != bY) // neither a horizontal or vertical line
            {
                // use graphing formulas y=Mx+b to see if the location is on the line (or within range of the line).
                // y = (Mx + b)
                // y = ((^Y / ^X) * x + b)
                // target.y = ((changeY / changeX)* target.x + b)
                // target.y = (((bY-aY) / (bX-aX))* target.x + b))
                double slope = (bY - aY) / (bX - aX);
                // to find b, use b = y - mx, and we put in one of the points
                double b          = aY - (slope * aX);
                double estimatedY = slope * target.Location.X + b;
                // actual Y needs to be 'close' to the estimated Y of the line
                result = (Math.Abs(target.Location.Y - estimatedY) < (TDMap.GridSize - 1));
            }

            return(result);
        }
コード例 #3
0
        private void DrawPaths(Graphics g)
        {
            foreach (TDPath p in Paths)
            {
                TDPathPoint lastPoint = null;
                foreach (TDPathPoint pp in p.PathPoints)
                {
                    if (lastPoint == null)
                    {
                        // don't draw the first point
                        lastPoint = pp;
                        continue;
                    }

                    try
                    {
                        // draw a line from this point back to our last point.
                        g.DrawLine(pathPen, (int)lastPoint.Location.X, (int)lastPoint.Location.Y, (int)pp.Location.X, (int)pp.Location.Y);

                        // draw the last point differently (the base).
                        if (pp == p.PathPoints[p.PathPoints.Count - 1])
                        {
                            // draw the base
                            if (BaseImage != null)
                            {
                                g.DrawImage(BaseImage, (int)(pp.Location.X - (BaseImage.Width / 2) + 1), (int)(pp.Location.Y - (BaseImage.Width / 2) + 1), BaseImage.Width, BaseImage.Width);
                            }
                            else
                            {
                                g.FillRectangle(BaseBrush, (int)(pp.Location.X - (pathPen.Width / 2) + 1), (int)(pp.Location.Y - (pathPen.Width / 2) + 1), pathPen.Width, pathPen.Width);
                            }
                        }
                        else
                        {
                            // draw this point, specifically (catches the corners of the path)
                            g.FillEllipse(pathPen.Brush, (int)(pp.Location.X - (pathPen.Width / 2)), (int)(pp.Location.Y - (pathPen.Width / 2)), pathPen.Width, pathPen.Width);
                        }
                    }
                    catch { } // just skip drawing during this frame (no need to get picky)

                    // reset who is the last point
                    lastPoint = pp;
                }
            }
        }