예제 #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 byte[,] CanSee(int x, int y, byte height)
        {
            if (_sightMap == null || _width != _sightMap.GetLength(0) || _height != _sightMap.GetLength(1))
                _sightMap = new byte[_width, _height][][,];

            Vector2 v = new Vector2(x + 0.5f, y + 0.5f);

            if (_sightMap[x, y] == null)
            {
                _sightMap[x, y] = new byte[MAX_HEIGHT + 1][,];
            }

            if (_sightMap[x, y][height] == null)
            {
                var tmpSight = new GridObject<byte>(_width, _height, height);

                for (int i = 0; i < _width; i++)
                {
                    for (int j = 0; j < _height; j++)
                    {
                        var tmp = Math.Min(Math.Min(
                            Raytrace(1, v, new Vector2(i + 0.3f, j + 0.3f)),
                            Raytrace(1, v, new Vector2(i + 0.7f, j + 0.3f))),
                        Math.Min(
                            Raytrace(1, v, new Vector2(i + 0.3f, j + 0.7f)),
                            Raytrace(1, v, new Vector2(i + 0.7f, j + 0.7f))));

                        tmpSight.Set(i, j, tmp);
                    }
                }
                _sightMap[x, y][height] = tmpSight.GetGrid();
            }

            return _sightMap[x, y][height];
        }