예제 #1
0
        public void Grid(float tileSize, byte maxValue, GridObject<byte> paint, GridObject<byte> canvas)
        {
            if (paint == null || canvas == null)
                return;

            float dx = Math.Abs(normal.X);
            float dy = Math.Abs(normal.Y);

            int x = (int)(point.X / tileSize);
            int y = (int)(point.Y / tileSize);

            float error = dx - dy;

            int x_inc = (normal.X > 0) ? 1 : -1;
            int y_inc = (normal.Y > 0) ? 1 : -1;

            dx *= 2;
            dy *= 2;

            float distance = 0;
            byte value = 0;

            while(true)
            {
                value = Math.Max(value, paint.Get(x, y, maxValue));
                canvas.Set(x, y, value);

                if (value >= maxValue)
                {
                    return;
                }

                if (error > 0)
                {
                    x += x_inc;
                    error -= dy;
                    distance += dy * tileSize;
                }
                else
                {
                    y += y_inc;
                    error += dx;
                    distance += dx * tileSize;
                }
            }
        }
예제 #2
0
        public bool TryFindPath(Vector2 v1, Vector2 v2, float raduis, GridObject<float> weightMap, out List<Vector2> path)
        {
            for (int i = 0; i < _width; i++)
                for (int j = 0; j < _height; j++)
                    _visitedMap[i, j] = false;

            int startX =    (int)(v1.X / _tileSize);
            int startY =    (int)(v1.Y / _tileSize);
            int goalX =     (int)(v2.X / _tileSize);
            int goalY =     (int)(v2.Y / _tileSize);

            //int tileSize = (int)((raduis * 2 - 1) / _tileSize) + 1;
            float r = raduis / _tileSize;

            //int d = (int)((raduis * 2 * TwoSqrt - 1) / _tileSize) + 1;

            bool success = false;

            path = null;

            if (startX < 0 || startY < 0
                || startX >= _width || startY >= _height
                || goalX < 0 || goalY < 0
                || goalX >= _width || goalY >= _height
                || _heightMap.Get(startX, startY, 0) != 0 || _heightMap.Get(goalX, goalY, 0) != 0)
            {
                return false;
            }

            List<GridNode> inList = new List<GridNode>();
            inList.Add(new GridNode(-1, startX, startY, 0, GridNode.EstimateDistanceTo(startX, startY, goalX, goalY)));

            List<GridNode> outList = new List<GridNode>();

            _visitedMap[startX, startY] = true;

            float offset = 0.5f * ((int)(r * 2 + 1) % 2);

            while (inList.Count > 0 && !success)
            {
                float distance = float.MaxValue;
                int current = -1;

                for (int i = 0; i < inList.Count; i++)
                {
                    if (distance > inList[i].GetDistance())
                    {
                        current = i;
                        distance = inList[i].GetDistance();
                    }
                }

                GridNode n = inList[current];

                if (n.x == goalX && n.y == goalY)
                    success = true;

                if (!success)
                {
                    for (int i = -1; i < 2; i++)
                    {
                        for (int j = -1; j < 2; j++)
                        {
                            if (i + n.x >= 0 && i + n.x < _width && j + n.y >= 0 && j + n.y < _height)
                            {
                                if (_canFit(i + n.x + offset, j + n.y + offset, r))
                                {
                                    bool diagonal = Math.Abs(i - j) != 1;

                                    if (!diagonal ||
                                        _canFit(i * 0.5f + n.x + offset, j * 0.5f + n.y + offset, r))
                                    {
                                        var tmpNode = new GridNode(outList.Count, i + n.x, j + n.y,
                                               n.steps + (diagonal ? TwoSqrt : 1) * weightMap.Get(i + n.x, j + n.y, 1),
                                                GridNode.EstimateDistanceTo(i + n.x, j + n.y, goalX, goalY));

                                        if (!_visitedMap[i + n.x, j + n.y])
                                        {
                                            inList.Add(tmpNode);

                                            _visitedMap[i + n.x, j + n.y] = true;
                                        }
                                        else
                                        {
                                            for (int k = 0; k < inList.Count; k++)
                                            {
                                                if (inList[k].x == i + n.x && inList[k].y == j + n.y)
                                                {
                                                    if (inList[k].steps > tmpNode.steps)
                                                        inList[k] = tmpNode;

                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                inList.RemoveAt(current);
                outList.Add(n);
            }

            if (!success)
                return false;

            GridNode node = outList.Last();

            path = new List<Vector2>();

            path.Add(new Vector2((node.x + offset) * _tileSize, (node.y + offset) * _tileSize));

            while (node.parent != -1)
            {
                node = outList[node.parent];
                path.Add(new Vector2((node.x + offset) * _tileSize, (node.y + offset) * _tileSize));
            }

            path.Reverse();

            return true;
        }
예제 #3
0
        public void DrawHeightGrid(GridObject<byte> grid, Color min, Color max)
        {
            var cm = Atlas.GetManager<CameraManager>();
            var t = Atlas.Content.GetContent<Texture2D>("blop");

            var rec = new Rectangle(0, 0, _tileSize, _tileSize);

            for (int i = Math.Max(0, (int)((cm.Position.X - cm.Width * 0.6f) / _tileSize));
                i < Math.Min(_width, (int)((cm.Position.X + cm.Width * 0.6f) / _tileSize));
                i++)
            {
                for (int j = Math.Max(0, (int)((cm.Position.Y - cm.Height * 0.6f) / _tileSize));
                    j < Math.Min(_height, (int)((cm.Position.Y + cm.Height * 0.6f) / _tileSize));
                    j++)
                {
                    Atlas.Graphics.DrawSprite(t,
                        new Vector2(i * _tileSize, j * _tileSize), rec,
                        Color.Lerp(min, max,
                        grid.Get(i, j, MAX_HEIGHT) / ((MAX_HEIGHT + 1) * 1f)));
                }
            }
        }