about summary refs log tree commit diff stats
path: root/html/rogue/js/items.js
blob: dc6dc1e7889520f003fdbd08fd975155cacba666 (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
const Items = {
    items: new Map(), // Map of items on the grid, key is "q,r"
    
    // Item types
    TYPES: {
        COIN: {
            name: 'Coin',
            color: '#FFD700',
            size: 0.2 // Size relative to hex
        },
        GEM: {
            name: 'Gem',
            color: '#50C878',
            size: 0.25
        }
    },
    
    // Initialize items on the map
    init() {
        this.items.clear();
        
        // Add some random items
        for (let i = 0; i < 10; i++) {
            const q = Math.floor(Math.random() * HexGrid.GRID_SIZE - HexGrid.GRID_SIZE/2);
            const r = Math.floor(Math.random() * HexGrid.GRID_SIZE - HexGrid.GRID_SIZE/2);
            
            // Don't place items at player start position
            if (q !== 0 || r !== 0) {
                const type = Math.random() < 0.5 ? this.TYPES.COIN : this.TYPES.GEM;
                this.addItem(q, r, type);
            }
        }
    },
    
    // Add an item to the map
    addItem(q, r, type) {
        this.items.set(`${q},${r}`, { type, q, r });
    },
    
    // Remove an item from the map
    removeItem(q, r) {
        return this.items.delete(`${q},${r}`);
    },
    
    // Get item at position
    getItem(q, r) {
        return this.items.get(`${q},${r}`);
    },
    
    // Draw all items
    draw(ctx, hexToPixel, camera, HEX_SIZE) {
        this.items.forEach(item => {
            const pixelPos = hexToPixel({ q: item.q, r: item.r });
            const screenX = pixelPos.x - camera.x;
            const screenY = pixelPos.y - camera.y;
            
            ctx.fillStyle = item.type.color;
            ctx.beginPath();
            ctx.arc(screenX, screenY, HEX_SIZE * item.type.size, 0, Math.PI * 2);
            ctx.fill();
        });
    }
};