Clipping

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
_Rocoo_
Prole
Posts: 2
Joined: Wed Aug 10, 2022 10:02 pm

Clipping

Post by _Rocoo_ »

In resume, i need a script to prevent my character to noclip through the map
Here's what i have

Code: Select all

function love.load()
   
  Slimo = love.graphics.newImage("slimo.png")
  Slime = love.graphics.newImage("slime.png")
  MAP = love.graphics.newImage("MAP.png")
  grad = 0
  rot = grad * (3.1416 / 180)
  R = 0
  G = 240
  B = 255
  r = (R/255)
  g = (G/255)
  b = (B/255)
  love.graphics.setBackgroundColor(r, g, b)

  circ = {}
  rect = {}
  rect.x = 400
  rect.y = 300
  rect.width = 1
  rect.height = 1
  circ.x = 200
  circ.y = 300
  mv = 3
  sc = 15
  sl = 50
  --Tamaño mínimo del círculo
  scc = 1
  --Tamaño máximo del círculo
  slc = 50
  sm = 1
  a = 760 - rect.width
  b = 560 - rect.height
  rad = 1
  c = 755 - rad 
  d = 555 - rad 
 
end

function love.draw()
  
  love.graphics.draw(Slime, rect.x, rect.y, 0, 0.2, 0.2, 50, 50)
  love.graphics.draw(Slimo, circ.x, circ.y, 0, 0.2, 0.2, 50, 50)
love.graphics.draw(MAP, 0, 0, 0, 1, 1, 0, 0)
end



function love.update()
  --definir límites del cuadrado
  if rect.x > a then
    rect.x = rect.x - mv
  end
  if rect.y > b then
   rect.y = rect.y - mv
  end
  if rect.x < 0 then
   rect.x = rect.x + mv
  end
  if rect.y < 0 then
   rect.y = rect.y + mv
  end
  --Límites círculo
  if circ.x > c then
    circ.x = circ.x - mv
  end
  if circ.y > d then
   circ.y = circ.y - mv
  end
  if circ.x < 45 + rad then
   circ.x = circ.x + mv
  end
  if circ.y < 45 + rad then  
   circ.y = circ.y + mv
  end
 
  --Definir tamaño máximo del cuadrado
  if rect.width > sl then
   rect.width = rect.width - sc
  end
  if rect.height > sl then
   rect.height = rect.height - sc
  end
  --Definir tamaño mínimo del cuadrado
  if rect.width < sm then
   rect.width = rect.width + sc
  end
  if rect.height < sm then
   rect.height = rect.height + sc
  end
  --Definir tamaño máximo del círculo
  if rad > slc then
    rad = rad - scc
  end
  --Definir tamaño minimo del círculo
  if rad < scc then
    rad = rad + scc
  end
  --Teclas movimientos del cuadrado
  if love.keyboard.isDown("d") then
     rect.x = rect.x + mv
  end
  if love.keyboard.isDown("s") then
     rect.y = rect.y + mv
  end
  if love.keyboard.isDown("a") then
     rect.x = rect.x - mv
  end
  if love.keyboard.isDown("w") then
     rect.y = rect.y - mv
  end
  --Teclas movimiento círculo  
  if love.keyboard.isDown("right") then
     circ.x = circ.x + mv
  end
  if love.keyboard.isDown("down") then
     circ.y = circ.y + mv
  end
  if love.keyboard.isDown("left") then
     circ.x = circ.x - mv
  end
  if love.keyboard.isDown("up") then
     circ.y = circ.y - mv
  end
  --Cambio de tamaño del círculo
  if love.keyboard.isDown("-") then
     rad = rad + sc
  end
  if love.keyboard.isDown(".") then
     rad = rad - sc
  end
  --Cambio de tamaño del cuadrado
  if love.keyboard.isDown("j") then
     rect.width = rect.width + sc
     a = 810 - rect.width
  end
  if love.keyboard.isDown("h") then
     rect.height = rect.height + sc
     b = 610 - rect.height
  end
  if love.keyboard.isDown("g") then
     rect.width = rect.width - sc
      a = 800 - rect.width
  end
  if love.keyboard.isDown("y") then
    rect.height = rect.height - sc
     b = 600 - rect.height
  end
 end
function chekCollision(a, b, c)
      --With locals it's common usage to use underscores instead of camelCasing
    local a_left = rect.x
    local a_right = rect.x + rect.width
    local a_top = rect.y
    local a_bottom = rect.y + rect.height

    local b_left = circ.x
    local b_right = circ.x + circ.width
    local b_top = circ.y
    local b_bottom = circ.y + circ.height
    
    local c

    --Directly return this boolean value without using if-statement
    return  a_right > map
        and a_left < map
        and a_bottom > map
        and a_top < map
        and b_right > map
        and b_left < map
        and b_bottom > map
        and b_top < map
      end

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 12 guests