how to attach text bubbles with text to a monster?

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.
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

how to attach text bubbles with text to a monster?

Post by pch »

hello,
I'm slowly moving forward with my dungeons, but there is still a lot to learn.
anyway I just decided to add a "text bubbles" so the monsters could speak.

I managed to make some kind of bubbles (just a rectangle so far) but don't know how to add some text to it to have it in one field (text)

here it goes:
"u" stands for unit
"fs" - fontSize - that's something lige grid size

Code: Select all

u.text = love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
should I make a table?

Code: Select all

u.text = {
love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
love.graphics.rectangle("line", u.x * fs, u.y * fs, 100, 20)
love.graphics.print("blablabla", u.x * fs + 5, u.y * fs + 5, 100, 20)
}
or there is a better way?

file just in case
(units.lua - there is a unit.txt, player.lua - theres a function that adds rectangle to unit)
to see a bubble, you must be "touching" a monster
and that is for love 8.0 - otherwise PO2 !!!!
Attachments
game.love
(195.52 KiB) Downloaded 135 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: how to attach text bubbles with text to a monster?

Post by Robin »

Just so you know: functions like love.graphics.rectangle(), love.graphics.print(), etc. don't create objects and return nil. They just draw to the screen.

Try something like this:

Code: Select all

-- whenever the text changes:
u.text = "blablabla"

-- in love.draw:
love.graphics.rectangle("fill", u.x * fs, u.y * fs, 100, 20)
love.graphics.rectangle("line", u.x * fs, u.y * fs, 100, 20)
love.graphics.print(u.text, u.x * fs + 5, u.y * fs + 5, 100, 20)
Help us help you: attach a .love.
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

Re: how to attach text bubbles with text to a monster?

Post by pch »

ok, so I put only text into .text - the rest will go to .draw

thanks, I had a different "vision" of it so that's why I couldnt figure it out:)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: how to attach text bubbles with text to a monster?

Post by coffee »

Hello, nice to see you see still working in rogue games. Ok Robin already answered your question. I just want to quick point you for a probably unnoticed thing in your engine. If you in tile mode, call "display whole lvl" and then "switch to ascii" it only changes the player but not the remain tiles (as it does in "only discovered rooms"). And you really need to get a "quit". :D
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

Re: how to attach text bubbles with text to a monster?

Post by pch »

hello there,
coffee wrote:...a probably unnoticed thing in your engine. If you in tile mode, call "display whole lvl" and then "switch to ascii" it only changes the player but not the remain tiles (as it does in "only discovered rooms")...
I'm aware of it, but I just left it like this because I was concetrated on other things - will change it later :)
coffee wrote: And you really need to get a "quit". :D
well... there is one but it is only visible in ASCII mode [>] and once you get there press SHIFT . (dot) :)

edit:
oops. there is no exit:) - looks like I disabled it for some reason.
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: how to attach text bubbles with text to a monster?

Post by iemfi »

Here's the text bubble function I just made for my project, all you need to feed it are the x and y position and the text. You can also feed it a font object, "ratio" of the width to height, the dialog box colour, and the text and outline colour.

Code: Select all

love.draw()
   drawBubble(love.mouse.getX(),love.mouse.getY(),'Hello, the quick brown fox jumped over the rubber pig')
end
function drawBubble(x,y,text,font,ratio,bgColour,textColour)
    bgColour=bgColour or {200,200,200,255}
    textColour=textColour or {0,0,0,255}
    ratio=ratio or 10
    local fontHeight=font:getHeight()
    local width=font:getWidth(text)
    local border=fontHeight
    local tail=fontHeight*1.5
    local w=0
    local lines=1
    if ratio<width/fontHeight then
        width=width/((width/fontHeight)/(ratio*2))
        w,lines=font:getWrap(text,width)
    end
    local boxWidth=width+border*2
    local boxHeight=fontHeight*lines+border*2
    local direction={1,1}
    if x+boxWidth+tail>=love.graphics.getWidth() then
        direction[1]=-1
    end
    if y+boxHeight+tail>=love.graphics.getHeight() then
        direction[2]=-1
    end
    local x1=x+tail*direction[1]
    local y1=y+tail/2*direction[2]
    local x2=x+(tail/2+boxWidth)*direction[1]
    local y2=y1
    local x3=x2
    local y3=y+(tail/2+boxHeight)*direction[2]
    local x4=x+(tail/2)*direction[1]
    local y4=y3
    local x5=x4
    local y5=y+tail*direction[2]
    local points={x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5}
    love.graphics.setColor(bgColour)
    local xFill=x+math.min(0,direction[1])*boxWidth+direction[1]*tail/2
    local yFill=y+math.min(0,direction[2])*boxHeight+direction[2]*tail/2

    love.graphics.rectangle('fill',xFill,yFill,boxWidth,boxHeight)
    love.graphics.triangle('fill',x,y,x1,y1,x5,y5)
    love.graphics.setColor(textColour)
    love.graphics.setLineWidth(1)
    love.graphics.polygon('line',points)
    love.graphics.setFont(font)
    love.graphics.printf(text,xFill+border,yFill+border,width,'center')
end
Last edited by iemfi on Wed Apr 18, 2012 8:26 pm, edited 2 times in total.
User avatar
pch
Prole
Posts: 32
Joined: Sun Mar 04, 2012 5:17 pm
Location: poland

Re: how to attach text bubbles with text to a monster?

Post by pch »

thanks a lot !
I would try it right away if I only could :)

...but I will have to wait till tommorow.

edit:
wow, it works like a charm - thank you.
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: how to attach text bubbles with text to a monster?

Post by Petunien »

iemfi wrote:

Code: Select all

love.draw()
    font=font or love.graphics.newFont(12)
Hmm, what is if no font is set? It will create a new font every frame, or?
That's not good for the performance. ^^
"Docendo discimus" - Lucius Annaeus Seneca
iemfi
Citizen
Posts: 52
Joined: Fri Mar 30, 2012 11:03 am

Re: how to attach text bubbles with text to a monster?

Post by iemfi »

Yes, not good for performance, but unless your game is very performance intensive or you want to draw a lot of bubbles I don't think it would be a problem at all.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: how to attach text bubbles with text to a monster?

Post by Robin »

iemfi wrote:Yes, not good for performance, but unless your game is very performance intensive or you want to draw a lot of bubbles I don't think it would be a problem at all.
Oh, yes it will be a problem. The garbage collection will make overtime, consuming memory and CPU time.
Help us help you: attach a .love.
Post Reply

Who is online

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