hrdngx / Nine

0 stars 0 forks source link

Camera #4

Open skrhm opened 4 months ago

skrhm commented 4 months ago

画面外に行くと遊びづらい

skrhm commented 3 months ago
function gameLoop() {

    let moved = false;

    if (true) {

        if (Math.max(0, Math.min(currentPlayer.y, 1600 - currentPlayer.size))) {

            if (keyState['ArrowUp']) {
                currentPlayer.y -= currentPlayer.speed;
                moved = true;
            }

        }

        check = (x) => {
            if (x >= 1600) return false;

            return true;
        }

        //if(Math.min(currentPlayer.y, 1600 - currentPlayer.size)){
        if (Math.max(1600 - currentPlayer.size, currentPlayer.y)) {
            //if(check(Math.max(1600, currentPlayer.y))){
            if (keyState['ArrowDown']) {
                currentPlayer.y += currentPlayer.speed;
                moved = true;
            }
        }

        if (Math.max(0, Math.min(currentPlayer.x, 1600 - currentPlayer.size))) {
            if (keyState['ArrowLeft']) {
                currentPlayer.x -= currentPlayer.speed;
                moved = true;
            }
        }

        if (Math.max(0, Math.min(currentPlayer.x, 1600 - currentPlayer.size)) != 1600 - currentPlayer.size) {
            if (keyState['ArrowRight']) {
                currentPlayer.x += currentPlayer.speed;
                moved = true;
            }
        }

        if (moved) {
            socket.emit('move', { x: currentPlayer.x, y: currentPlayer.y });
        }

    }

    render();

    requestAnimationFrame(gameLoop);
}

// window size 変更時
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
    canvas.width = document.documentElement.clientWidth;
    canvas.height = document.documentElement.clientHeight;
}

gameLoop();

// Color Create
function hColor(color) {
    var rgbVal = color.slice(4, -1);
    var [R, G, B] = rgbVal.split(',').map(Number);

    if (!isNaN(R + G + B) && 0 <= R && R <= 255 && 0 <= G && G <= 255 && 0 <= B && B <= 255) {
        var max = Math.max(R, Math.max(G, B));
        var min = Math.min(R, Math.min(G, B));
        var sum = max + min;
        var newR = sum - R;
        var newG = sum - G;
        var newB = sum - B;
        var hColor = `rgb(${newR}, ${newG}, ${newB})`;
        return hColor;
    } else {
        return `rgb(0,0,0)`;
    }
}

// Game Over
function gameOver() {
    window.location.href = `GameOver.html?score=${players[socket.id].size}`;
}