Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Cantos com posições diferentes

Olá pessoal, tudo bom?

Eu estava seguindo os passos da aula exatamente como mostrados, mas notei que na hora de fazer a colisão com as paredes, os valores do meu pivot eram diferentes. Ao que parece, o pivot do personagem dele está no meio da sprite, e o meu está no canto superior esquerdo. Gostaria de entender o que eu fiz de diferente ou se mudou alguma coisa na linguagem da época do video pra agora.

Segue o meu código:

-- title:  One
-- author: Aborbora
-- desc:   2D RPG
-- script: lua

SCREEN_WIDTH = 240
SCREEN_HEIGHT = 136
TILE_SIZE = 8

player =
{
    sprite = 32,
    x = SCREEN_WIDTH / 2 - TILE_SIZE,
    y = SCREEN_HEIGHT / 2 - TILE_SIZE,
    scale = 1,
    rotation = 0,
    backgroundColor = 4,
    mirror = 0,
    horizontalBlock = 2,
    verticalBlock = 2,

    speed = 4,

    size = {x=16,y=16}
}

key = 
{
    up = 0,
    down = 1,
    left = 2,
    right = 3
}

function logic()

    if btn(key.up) then
        trymove(0, -player.speed)
    end

    if btn(key.down) then
        trymove(0, player.speed)
    end

    if btn(key.left) then
        trymove(-player.speed, 0)
    end

    if btn(key.right) then
        trymove(player.speed, 0)
    end

end

function draw()

    cls()

    map
    (
        0,
        0,
        SCREEN_WIDTH,
        SCREEN_HEIGHT,
        0,
        0
    )

    spr
    (
        player.sprite,
        player.x,
        player.y,
        player.backgroundColor,
        player.scale,
        player.mirror,
        player.rotation,
        player.horizontalBlock,
        player.verticalBlock
    )    
end

function trymove(motionx, motiony)
    topleft = 
    {
        x = player.x + motionx, 
        y = player.y + motiony 
    }
    topright =    
    {
        x = player.x + player.size.x-1 + motionx, 
        y = player.y + motiony
    }
    bottomleft =    
    {
        x = player.x + motionx, 
        y = player.y + player.size.y-1 + motiony
    }
    bottomright =
    {
        x = player.x + player.size.x-1 + motionx, 
        y = player.y + player.size.y-1 + motiony
    }

    if
        willCollide(topleft) or
        willCollide(topright) or
        willCollide(bottomright) or 
        willCollide(bottomleft) 
    then
        -- collision!
    else
        player.x = player.x + motionx
        player.y = player.y + motiony
    end
end

function willCollide(point)
    blockx = point.x / 8
    blocky = point.y / 8
    blockId = mget(blockx, blocky)

    return blockId >= 128
end

function TIC()

    logic()

    draw()

end

Obrigado!

1 resposta
solução!

Ah, acabei de ver que é porque o instrutor coloca - 8 na hora de desenhar a sprite do jogador! hahah

Desculpa pela dúvida boba!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software