How to find object collided into in bump.lua??

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
WhiteLocke
Prole
Posts: 7
Joined: Tue Feb 21, 2017 11:45 am

How to find object collided into in bump.lua??

Post by WhiteLocke »

Hey so I'm pretty awful at programming so be patient. Basically, I want bump.lua to tell me which NPC I bumped into so I can have him say his scripted words over his head when "a" is pressed. Right now I have this which obviously I know is wrong but you can see my thinking...

Code: Select all

if love.keyboard.isDown("right") then
         local actualX, actualY, cols, len  = world:move(player, x + 5, y)
         if len > 0 then
           for i=1,len do
                  collision = true
                   wx, wy = cols.x, cols.y
              break
              end
          else
              collision = false
          end
    end
 
So "collision" just listens for a key press to start dialogue so that pressing that key all over the screen doesn't pause everything. wx and wy are short for "write x" and "write y" which are passed into the draw function to tell it where to draw. The missing element here is where I guessed at cols.x and cols.y, which aren't the right way to find what I collided with. What is the right way?
WhiteLocke
Prole
Posts: 7
Joined: Tue Feb 21, 2017 11:45 am

Re: How to find object collided into in bump.lua??

Post by WhiteLocke »

Hey! I figured it out:

Code: Select all

cols[i].otherRect.x, cols[i].otherRect.y
But now when that's passed to the function that draws the dialogue box, the background disappears. Any ideas why?
Annabelle
Prole
Posts: 13
Joined: Sun Feb 19, 2017 1:37 pm

Re: How to find object collided into in bump.lua??

Post by Annabelle »

Can you post the dialogue/background code?
WhiteLocke
Prole
Posts: 7
Joined: Tue Feb 21, 2017 11:45 am

Re: How to find object collided into in bump.lua??

Post by WhiteLocke »

Okay well it's all kind of referencing each other so here is the full code (it's really messy and the dialogue part is kind of broken right now)

Main.lua:

Code: Select all


bump = require 'bump'
require 'drawWords'

local world = bump.newWorld()

local level = {elems = {}}


function level:addElem(x, y, w, h, drawLambda)
  local t = {draw = drawLambda}
  world:add(t, x, y, w, h)
  table.insert(self.elems, t)
  return t
end

function level:draw()
  for _, v in pairs(self.elems) do
  v:draw(world:getRect(v))
  end
end

player_image = love.graphics.newImage("NonameSprite.png")





function love.load()
	love.graphics.setDefaultFilter('nearest','nearest')
  background_image = love.graphics.newImage("background.png")
  collision = false
	words = " "
	dialogue = false
	key = nil
  wx = 0
  wy = 0
  updateWords()
  dude_controller:spawnDude(200, 200, dude1)
  dude_controller:spawnDude(200, 400, dude2)
  
  
  for _, d in pairs(dude_controller.dudes) do
    level:addElem(d.x, d.y, d.w, d.h,
    function (self, x, y, w, h)
    love.graphics.setColor(0, 255, 255)
    love.graphics.rectangle('fill', x, y, w, h)
  end)
end

  player = level:addElem(10, 10, 64, 64, 
    function (self, x, y, w, h)
    love.graphics.setColor(255, 255, 255)
    love.graphics.draw(player_image, x, y-64, 0, 1, 1)
    end)
end

function love.draw()
  local sx = love.graphics.getWidth() / background_image:getWidth()
  local sy = love.graphics.getHeight() / background_image:getHeight()
  love.graphics.draw(background_image, 0, 0, 0, sx, sy)

  level:draw()

  
    
  drawWords(wx, wy)

  end      

function love.update(dt)
local x, y, _,_ = world:getRect(player)

function love.keyreleased(key)
  if collision then
    
          if key == "a" then
	           dialogue = true
          end
    end
  end

if dialogue then
    function love.keyreleased(key)
          if key == "a" then
		             dialogue = false
                 collision = false
          end
    end
end

 
 if dialogue == false then
   
   if love.keyboard.isDown("right") then
         local actualX, actualY, cols, len  = world:move(player, x + 5, y)
         if len > 0 then
           for i=1,len do
                  collision = true
                   wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
              break
            end
        end
    end
  end

  if love.keyboard.isDown("left") then
	         local actualX, actualY, cols, len  = world:move(player, x - 5, y)
            if len > 0 then
           for i=1,len do
                  collision = true
                   wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
              break
              end
          
          end
  end    
  
  if love.keyboard.isDown("up") then
	          local actualX, actualY, cols, len  = world:move(player, x, y - 5)
             if len > 0 then
           for i=1,len do
                  collision = true
                   wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
              break
              end
          
          end
  end     
  
  if love.keyboard.isDown("down") then
	           local actualX, actualY, cols, len  = world:move(player, x, y + 5)
              if len > 0 then
           for i=1,len do
                  collision = true
                   wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
              break
              end
          
          end
  end    
end


     
drawWords.lua

Code: Select all

dude_controller = {}
dude_controller.dudes = {}
dude = {}
name = {}

function drawWords(x, y)
    if dialogue then
           
           love.graphics.setColor(255,255,255)
           love.graphics.rectangle("fill", x, y, 64 - 20, 20)
           
    for i,d in ipairs(dude_controller.dudes) do
        if d.x == x and d.y == y then
           words = d.words
           break
        end
    end
           love.graphics.setColor(0,0,0)
           love.graphics.print(words, x, y)
    end
end



function dude_controller:spawnDude(x, y, name)
  dude = {}
  dude.x = x
	dude.y = y
	dude.w = 64
	dude.h = 128
  dude.words = {}
  dude.name = name
  table.insert(self.dudes, dude)
end

function updateWords()
  if dude.name == dude1 then
   dude.words = 'Hi'
elseif dude.name == dude2 then
   dude.words = 'What?'
   end
end
WhiteLocke
Prole
Posts: 7
Joined: Tue Feb 21, 2017 11:45 am

Re: How to find object collided into in bump.lua??

Post by WhiteLocke »

Update on this, finally made a better way to streamline matching dialogue to names and realized names need to be strings to be recognized. The background is still going black when dialogue is drawn though.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 4 guests