about summary refs log tree commit diff stats
path: root/js/puzzle-dungeon/commandHandler.js
blob: 21193e5d9584cda8259a89af8a8666bb9e089a3e (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { player, grid, drawGrid, updatePlayerPosition, updatePlayerStatus, alertGameOver } from './game.js';

let commandQueue = [];
let processingCommands = false;

export function handleCommand(command) {
    const [action, itemOrSteps] = command.split(' ');

    if (action === 'get' || action === 'use' || action === 'scan' || isMovementCommand(action)) {
        commandQueue.push({ action, itemOrSteps });
    }

    if (!processingCommands) {
        processCommandQueue();
    }
}

function processCommandQueue() {
    if (commandQueue.length === 0) {
        processingCommands = false;
        return;
    }

    processingCommands = true;
    const { action, itemOrSteps } = commandQueue.shift();

    if (action === 'get') {
        handleGetCommand(itemOrSteps, processCommandQueue);
    } else if (action === 'use') {
        handleUseCommand(itemOrSteps, processCommandQueue);
    } else if (action === 'scan') {
        handleScanCommand(processCommandQueue);
    } else if (isMovementCommand(action)) {
        const steps = parseInt(itemOrSteps, 10) || 1;
        handleMovement(action, steps, processCommandQueue);
    }
}

function handleGetCommand(item, callback) {
    const { x, y } = player.position;
    const cell = grid[x][y];
    if (cell && cell.type === item) {
        player.inventory.push(cell.type);
        grid[x][y] = null; // remove the item from the grid
        console.log(`Picked up ${item}`);
        flashPlayer(callback);
    } else {
        player.health -= 1;
        console.log(`Tried to get ${item} but it's not there, health is now ${player.health}`);
        if (player.health <= 0) {
            alertGameOver();
            return;
        }
        flashPlayer(callback);
    }
}

function handleUseCommand(item, callback) {
    const itemIndex = player.inventory.indexOf(item);
    if (itemIndex > -1) {
        player.inventory.splice(itemIndex, 1); // remove the item from the inventory
        if (item === 'potion') {
            player.health += 1; // MOAR HEALTH (by 1)
            console.log(`Used ${item}, health is now ${player.health}`);
            updatePlayerStatus();
            flashPlayer(callback);
        } else if (item === 'battery') {
            player.power = Math.min(player.power + 2, (10 + player.level)); // Increase power by 2, up to a max of 10 + the current level. This may be tooooo gigantic a number
            console.log(`Used ${item}, power is now ${player.power}`);
            updatePlayerStatus();
            flashPlayer(callback);
        } else {
            callback();
        }
    } else {
        player.health -= 1;
        console.log(`Tried to use ${item} but it's not in inventory. Ouch! Health is now ${player.health}`);
        if (player.health <= 0) {
            alertGameOver();
            return;
        }
        flashPlayer(callback);
    }
}

function handleScanCommand(callback) {
    if (player.power > 0) {
        player.didScan = true;
        player.power -= 3;
        drawGrid(grid); // redraw the grid to show the revealed traps
        updatePlayerStatus();
    } else {
        player.health -= 1;
        console.log(`Tried to scan but power is depleted, ouch! Health is now ${player.health}`);
        if (player.health <= 0) {
            alertGameOver();
            return;
        }
    }
    callback(); // continue processing commands
}

function handleMovement(direction, steps, callback) {
    let stepCount = 0;

    const moveInterval = setInterval(() => {
        if (stepCount >= steps) {
            clearInterval(moveInterval);
            callback();
            return;
        }

        const { x, y } = player.position;
        if (direction === 'up' && y > 0) {
            updatePlayerPosition(x, y - 1);
        } else if (direction === 'down' && y < grid[0].length - 1) {
            updatePlayerPosition(x, y + 1);
        } else if (direction === 'left' && x > 0) {
            updatePlayerPosition(x - 1, y);
        } else if (direction === 'right' && x < grid.length - 1) {
            updatePlayerPosition(x + 1, y);
        }
        console.log(`Moved ${direction}, new position is (${player.position.x}, ${player.position.y})`);

        player.steps += 1;
        if (player.steps % 8 === 0) {
            player.power = Math.max(player.power - 1, 0);  // decrease power but, don't let it go below 0
            if (player.power <= 0) {
                player.health -= 1;
                console.log(`Power depleted, health is now ${player.health}`);
                if (player.health <= 0) {
                    alertGameOver();
                    return;
                }
            }
            console.log(`Power decreased, power is now ${player.power}`);
        }

        drawGrid(grid);
        updatePlayerStatus();
        stepCount++;
    }, 500); // this interval controls the speed of the animation
}

function flashPlayer(callback) {
    player.flashing = true;
    drawGrid(grid);
    setTimeout(() => {
        player.flashing = false;
        drawGrid(grid);
        updatePlayerStatus();
        if (callback) callback();
    }, 250); // Flash duration
}

function isMovementCommand(action) {
    return ['up', 'down', 'left', 'right'].includes(action);
}