[Help]Making boxes change textures

Mods, guides how to use and install mods go right in here.
Post Reply
User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 10 Jun 2013, 07:00

Hey, guys. I'm not comfortable for making a thread only to ask this, but I need to.
Well, the point is: I'm making a mod (and if you read my "Pop A Portal - Mappack" thread you should know), but this is my very first mod: "Mario +Portal"
I was trying to make boxes and buttons changing textures when colliding with each other. It was quite easy to do with buttons, but not with the boxes. I'm not expert in coding (in fact, I don't know almost anything about coding), but I would like a little help from you guys (and, of course, you'll be credited when I release my mod).

Pictures so you can understand what I'm talking about:
What happens actually:
Image
(As you may see, the button already changes it's texture)

What I want to happen:
Image

*The activated box texture inside main.lua:
boximageon = love.graphics.newImage("graphics/" .. graphicspack .. "/boxon.png")
and the image is:

Image
Thanks for helping!

User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 10 Jun 2013, 23:47

So... Anyone? I'm not asking you guys to make the mod/modded file for me. I just need a little help with the code. I tried making ifs - elses in the collision section, like:
if a == "button" then
self.graphic = boximageon
else
self.graphic = boximage
end

I've tried a few other things (in fact, I've spent almost 2 hours trying this and other ways), but none of them seems to work. So, I would really appreciate a llittle help. Thanks!

User avatar
lolpoop89
Posts: 123
Joined: 05 Apr 2012, 17:38

Post » 10 Jun 2013, 23:48

It's coming in SE. At least I think it is.

User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 10 Jun 2013, 23:50

lolpoop89 wrote:It's coming in SE. At least I think it is.
I know, but I wanted to add it anyway. The buttons are also gonna change texture when activated in SE, but I made them myself. It is gonna be a graphic mod, with some retextured tiles and some new graphical entities. But I only got problems with the cube...

User avatar
Qcode
Posts: 1479
Joined: 05 Feb 2012, 18:00
Contact:

Post » 11 Jun 2013, 00:32

...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.

User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 11 Jun 2013, 01:48

Qcode wrote:So this probably isn't the most efficient way of doing it, but I couldn't figure out using checkrect. It works anyway and you shouldn't see a drop in performance.
First go to button.lua, then button:update(dt).
At the end of that function you should see

Code: Select all

if self.destroying then
	return true
else
	return false
end
Anywhere before that (in button:update) paste in this chunk of code.

Code: Select all

local on
self.graphic = boximage
for i, v in pairs(objects["button"]) do
	if aabb(self.x, self.y, 1, 1, v.x, v.y, 1.875, .1875) then
		on = true
	end
end
if on then
	self.graphic = boximageon
end
That should give the desired effect.
Thanks! I'll try this and see how it works. I haven't even thought on adding the code inside button.lua. I've tried everything, but always inside the box.lua. Again, thanks!

User avatar
Qcode
Posts: 1479
Joined: 05 Feb 2012, 18:00
Contact:

Post » 11 Jun 2013, 01:51

...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.

User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 11 Jun 2013, 02:09

Qcode wrote:Wait no sorry.
All of that I meant with box.lua.
Should be in box.lua, and should be in box:update.
All the code in

Code: Select all

 tags should remain the same though.
Completely my fault there.[/quote]

No problems, I put it in box.lua. Also, I replaced "on" by "toggled", because it's easier to me. So, should it look like this?
[spoiler][code]
box = class:new()

function box:init(x, y)
	--PHYSICS STUFF
	self.cox = x
	self.coy = y
	self.x = x-14/16
	self.y = y-12/16
	self.speedy = 0
	self.speedx = 0
	self.width = 12/16
	self.height = 12/16
	self.static = false
	self.active = true
	self.category = 9
	self.parent = nil
	self.portaloverride = true

	self.mask = {	true,
					false, false, false, false, false,
					false, true, false, true, true,
					false, false, true, false, false,
					true, true, false, false, true,
					false, true, true, false, false,
					true, false, true, true, true}
					
	self.emancipatecheck = true

	self.userect = adduserect(self.x, self.y, 12/16, 12/16, self)
	
	--IMAGE STUFF
	self.drawable = true
	self.quad = boxquad
	self.offsetX = 6
	self.offsetY = 2
	self.quadcenterX = 6
	self.quadcenterY = 6
	
	self.rotation = 0 --for portals
	
	self.falling = false
	self.destroying = false
	self.outtable = {}
	self.portaledframe = false
end

function box:update(dt)

	local toggled
	self.graphic = boximage
	for i, v in pairs(objects["button"]) do
   		if aabb(self.x, self.y, 1, 1, v.x, v.y, 1.875, .1875) then
			toggled = true
		end
	end
	if toggled then
	   self.graphic = boximageon
	end

	local friction = boxfrictionair
	if self.falling == false then
		friction = boxfriction
	end
	
	if not self.pushed then
		if self.speedx > 0 then
			self.speedx = self.speedx - friction*dt
			if self.speedx < 0 then
				self.speedx = 0
			end
		else
			self.speedx = self.speedx + friction*dt
			if self.speedx > 0 then
				self.speedx = 0
			end
		end
	else
		self.pushed = false
	end
	
	--rotate back to 0 (portals)
	self.rotation = math.mod(self.rotation, math.pi*2)
	if self.rotation > 0 then
		self.rotation = self.rotation - portalrotationalignmentspeed*dt
		if self.rotation < 0 then
			self.rotation = 0
		end
	elseif self.rotation < 0 then
		self.rotation = self.rotation + portalrotationalignmentspeed*dt
		if self.rotation > 0 then
			self.rotation = 0
		end
	end
	
	if self.parent then
		local oldx = self.x
		local oldy = self.y
		
		self.x = self.parent.x+math.sin(-self.parent.pointingangle)*0.3
		self.y = self.parent.y-math.cos(-self.parent.pointingangle)*0.3
		
		if self.portaledframe == false then
			for h, u in pairs(emancipationgrills) do
				if u.dir == "hor" then
					if inrange(self.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, oldy, self.y, true) then
						self:emancipate(h)
					end
				else
					if inrange(self.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, oldx, self.x, true) then
						self:emancipate(h)
					end
				end
			end
		end
	end
	
	self.userect.x = self.x
	self.userect.y = self.y

	--check if offscreen
	if self.y > 17 then
		self:destroy()
	end
	
	self.portaledframe = false
	
	if self.destroying then
		return true
	else
		return false
	end
end

function box:leftcollide(a, b)
	if a == "button" then
		self.y = b.y - self.height
		self.x = b.x + b.width - 0.01
		if self.speedy > 0 then
			self.speedy = 0
		end
		return false
	elseif a == "player" then
		self.pushed = true
		return false
	end
end

function box:rightcollide(a, b)
	if a == "button" then
		self.y = b.y - self.height
		self.x = b.x - self.width+0.01
		if self.speedy > 0 then
			self.speedy = 0
		end
		return false
	elseif a == "player" then
		self.pushed = true
		return false
	end
end

function box:floorcollide(a, b)
	if self.falling then
		self.falling = false
	end
	
	if a == "goomba" or a == "bulletbill" then
		b:stomp()
		addpoints(200, self.x, self.y)
		playsound(stompsound)
		self.falling = true
		return false
	end
end

function box:passivecollide(a, b)
	if a == "player" then
		if self.x+self.width > b.x+b.width then
			self.x = b.x+b.width
		else
			self.x = b.x-self.width
		end
	end
end

function box:startfall()
	self.falling = true
end

function box:emancipate()
	if self.parent then
		self.parent:cubeemancipate()
	end
	self:destroy()
end

function box:destroy()
	self.userect.delete = true
	self.destroying = true
	
	for i = 1, #self.outtable do
		if self.outtable[i].input then
			self.outtable[i]:input("toggle")
		end
	end
end

function box:addoutput(a)
	table.insert(self.outtable, a)
end

function box:used(id)
	self.parent = objects["player"][id]
	self.active = false
	objects["player"][id]:pickupbox(self)
end

function box:dropped()
	self.parent = nil
	self.active = true
end

function box:portaled()
	self.portaledframe = true
end
[/spoiler]

User avatar
Qcode
Posts: 1479
Joined: 05 Feb 2012, 18:00
Contact:

Post » 11 Jun 2013, 02:16

...
Last edited by Qcode on 21 Oct 2021, 17:48, edited 1 time in total.

User avatar
HugoBDesigner
Posts: 2189
Joined: 19 Sep 2012, 02:23
Contact:

Post » 11 Jun 2013, 03:46

Qcode wrote:Yep that's how it should look.
Well, it worked pretty well, but then I found a problem: you were calculating the box changing textures through x and y axis, but they aren't precise (I've changed the values to some small decimal number, but they keep being imprecise). And it, also, changes it texture even if the cube is parented to the player (and makes the process less... "realistic"). I'll keep using your code until I find something different to work with or until I give up. Thanks!

User avatar
Qcode
Posts: 1479
Joined: 05 Feb 2012, 18:00
Contact:

Post » 11 Jun 2013, 03:56

...

Post Reply