Пример #1
0
    public int[,] getFieldOfViewFromMap(int x, int y)
    {
        int[,] aField = new int[entity.visionRange * 2 + 1, entity.visionRange *2 + 1];

        int mentalMapWidth  = entity.getMentalMap().GetLength(0);
        int mentalMapHeight = entity.getMentalMap().GetLength(1);

        // If the mental map exist and if the vision range is greater than 0
        if (entity.getMentalMap() != null && entity.visionRange > 0)
        {
            // We initialize the field of view
            initialiseCurrentFieldOfView(aField);
            int    radiusSquare = entity.visionRange * entity.visionRange; // use to know the maximum distance a tile can have to be in the vision range
            double distanceSquare;                                         // use to know the distance of a tile from the x and y coordinate
            //For each tile that could be in the vision range (a square around the x and y coordinate)
            int width, height;
            for (width = x - entity.visionRange; width <= x + entity.visionRange; width++)
            {
                for (height = y - entity.visionRange; height <= y + entity.visionRange; height++)
                {
                    //We calculate his distance to the x and y coordinate
                    distanceSquare = Math.Pow(width - x, 2) + Math.Pow(height - y, 2);
                    //If it is in the vision range
                    if (distanceSquare < radiusSquare)
                    {
                        // If it doesn't go out of bounds
                        if (0 < width && width < mentalMapWidth && 0 < height && height < mentalMapHeight)
                        {
                            // We add it to the mental map
                            aField[width - x + entity.visionRange, height - y + entity.visionRange] = entity.getPointFromMentalMap(width, height);
                        }
                    }
                }
            }
        }
        return(aField);
    }