about summary refs log tree commit diff stats
path: root/html/rogue/js/fow.js
blob: 291c862e563fee35060448b76917a982811e04ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const FogOfWar = {
    // Set of revealed hex coordinates (as strings)
    revealed: new Set(),
    
    // Configuration
    VISION_RANGE: Config.player.VISION_RANGE,
    
    init() {
        this.revealed.clear();
        this.updateVisibility(player.position);
    },
    
    // Convert hex to string key for Set storage
    hexToKey(hex) {
        return `${hex.q},${hex.r}`;
    },
    
    // Check if a hex is currently visible
    isVisible(hex) {
        const playerPos = player.getCurrentPosition();
        const distance = this.getHexDistance(hex, playerPos);
        return distance <= this.VISION_RANGE;
    },
    
    // Check if a hex has been revealed
    isRevealed(hex) {
        return this.revealed.has(this.hexToKey(hex));
    },
    
    // Calculate distance between two hexes
    getHexDistance(a, b) {
        return Math.max(
            Math.abs(a.q - b.q),
            Math.abs(a.r - b.r),
            Math.abs((a.q + a.r) - (b.q + b.r))
        );
    },
    
    // Update visibility based on player position
    updateVisibility(center) {
        // Get all hexes within vision range
        for (let q = -this.VISION_RANGE; q <= this.VISION_RANGE; q++) {
            for (let r = -this.VISION_RANGE; r <= this.VISION_RANGE; r++) {
                const hex = {
                    q: center.q + q,
                    r: center.r + r
                };
                
                if (this.getHexDistance(center, hex) <= this.VISION_RANGE) {
                    this.revealed.add(this.hexToKey(hex));
                }
            }
        }
    },
    
    getFogState(hex) {
        if (!this.isRevealed(hex)) return Config.fog.states.HIDDEN;
        if (!this.isVisible(hex)) return Config.fog.states.REVEALED;
        return Config.fog.states.VISIBLE;
    },
    
    // Draw fog of war effect
    draw(ctx) {
        HexGrid.getViewportHexes().forEach(hex => {
            const fogState = this.getFogState(hex);
            if (fogState.alpha > 0) {
                const screen = HexGrid.toScreenCoordinates(hex, Camera);
                
                // Draw fog fill
                ctx.fillStyle = fogState === Config.fog.states.HIDDEN ? 
                    Config.colors.FOG.HIDDEN : 
                    Config.colors.FOG.REVEALED;
                HexGrid.drawHexPath(ctx, screen.x, screen.y, HexGrid.SIZE, 1);
                ctx.fill();
                
                // Draw grid lines only for revealed but not visible hexes
                if (fogState === Config.fog.states.REVEALED) {
                    ctx.strokeStyle = Config.colors.FOG.GRID_DIM;
                    ctx.lineWidth = 1;
                    ctx.stroke();
                }
            }
        });
    }
};