Page 1 of 1

Destroying a cave-in with a pickaxe

Posted: Fri Oct 31, 2014 11:19 am
by The cube
Hi guys! So, i have started playing around in the editor, and i came up withthis idea. Immediatedly as i saw the pickaxe in-game i thought it had some function like the shovel does, but it doesn't. Is it even possible to make the pickaxe do one of these:

1. Allow it to damage a cave-in

2. By right-clicking it in front of a cave-in, trigger a script.

2 Can also be done by always triggering a script and just checking if the character is in front of + facing a cave-in.

Thanks guys.

Re: Destroying a cave-in with a pickaxe

Posted: Fri Oct 31, 2014 3:24 pm
by Jgwman
Cool idea. Doesn't sound very hard. Maybe I'll write one this evening.

Re: Destroying a cave-in with a pickaxe

Posted: Sun Nov 02, 2014 1:21 am
by swampie
So I gave this a try and came up with this somewhat boilerplaty code (I crindged a little bit writing it) :D
SpoilerShow

Code: Select all

components = {
		{	
		class="MeleeAttack",
		onAttack = function(self) 				
			local facing = party.facing 
			local level = party.level
			local x = party.x
			local y = party.y
			local foundOne = false	
			local caveIns 
		--get entity in front of the party
			if facing == 0 then								
				for e in Dungeon.getMap(level):entitiesAt(x,y-1) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then									
						foundOne = true
						caveIns = e
					end
				end					
			elseif facing == 1 then				
				for e in Dungeon.getMap(level):entitiesAt(x+1,y) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			elseif facing ==2 then				
				for e in Dungeon.getMap(level):entitiesAt(x,y+1) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			elseif facing == 3 then				
				for e in Dungeon.getMap(level):entitiesAt(x-1,y) do
					if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then
						foundOne = true
						caveIns = e
					end
				end
			else
				return
			end				
			--if it did find one then destroy it
			if foundOne then					
				caveIns:destroy()
			end
		end
		},
	}
What I do is, I get the entity in front of the party, check if its go name is a cave_in, if so I destroy it.
You should probably add some fancy effects/sounds or a shovel-dig like fade in/out to smooth that out :D

So yeah, that's it from me.

//edit: the code is bound to the pickaxe clone/whatever item you wish
//edit2: took the below advice to heart and voilá, somewhat tidier :D
SpoilerShow

Code: Select all

components = {		
		{	
		class="MeleeAttack",
		onAttack = function(self) 				
			local facing = party.facing 
			local level = party.level
			local dx, dy = getForward(facing)
			local inFrontX = party.x + dx
			local inFrontY = party.y + dy			
			local foundOne = false	
			local caveIns 
		--get entity in front of the party										
			for e in Dungeon.getMap(level):entitiesAt(inFrontX,inFrontY) do
				if e.name == "dungeon_cave_in" or e.name == "mine_cave_in" or e.name == "mine_moss_cave_in"then									
					 e:destroy()
				 end
			end
		end	
		},
	}

Re: Destroying a cave-in with a pickaxe

Posted: Sun Nov 02, 2014 1:42 am
by JohnWordsworth
@Swampie: A little tip. Instead of doing 4 different cases depending on your facing, you can just do this...

Code: Select all

local dx,dy = getForward(facing)
local inFrontX = party.x + dx
local inFrontY = party.y + dy

for e in Dungeon.getMap(level):entitiesAt(inFrontX, inFrontY) do
   ...
end
Then you don't have to duplicate the code 4 times (which is generally a bad idea, because it's very easy to change one of the 4 blocks and forget to update the rest!).

Re: Destroying a cave-in with a pickaxe

Posted: Sun Nov 02, 2014 1:54 am
by Prozail
MeleeAttackComponent has an "onAttack" hook??? Well, that simplifies things ALOT :)

Thank you!

Re: Destroying a cave-in with a pickaxe

Posted: Sun Nov 02, 2014 2:02 am
by swampie
@JohnWordsworth Yeah, that's what I meant with boilerplatey :D
But, wow, I did oversee those two functions... would have made that somewhat easier, thank you.