Page 1 of 1

{script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 2:17 pm
by cromcrom
Hi, another script. I use it to spawn skeletons when you disturb some skulls. Makes for a nasty surprise :-) Enjoy.

Code: Select all

function spawnInFrontOfParty(objectToSpawn)
local spawnX = 0
local spawnY = 0
local spawnfacing = 0

if party.facing == 0 then
spawnX = party.x
spawnY = (party.y)-1
spawnfacing = 2
end
if party.facing == 1 then
spawnX = 1+party.x
spawnY = party.y
spawnfacing = 3
end
if party.facing == 2 then
spawnX = party.x
spawnY = 1+party.y
spawnfacing = 0
end
if party.facing == 3 then
spawnX = (party.x)-1
spawnY = party.y
spawnfacing = 1
end
spawn(objectToSpawn,party.level,spawnX,spawnY,spawnfacing)
end

Re: {script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 2:58 pm
by JohnWordsworth
That's really cool and makes it very easy to spook the party - I like it (and look forward to using it!). A quick suggestion if you would like to reduce the number of lines (not all that important I guess, but I just noticed you could reduce the script to about 40% of the size!).

Code: Select all

function spawnInFrontOfParty(objectToSpawn)
  local spawnX = party.x
  local spawnY = party.y
  local spawnFacing = (party.facing + 2) % 4

  if party.facing == 0 then 
    spawnY = spawnY - 1
  elseif party.facing == 1 then
    spawnX = spawnX + 1
  elseif party.facing == 2 then 
    spawnY = spawnY + 1
  else 
    spawnX = spawnX - 1
  end

  spawn(objectToSpawn, party.level, spawnX, spawnY, spawnFacing)
end

Re: {script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 3:00 pm
by cromcrom
I am a lua beginner, I love it how you guys can make things much more nice and simple. Thanks :-)

Re: {script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 3:02 pm
by JohnWordsworth
Sorry, I didn't mean to be one of those annoying people that optimises every little line for the sake of it, I just copied it into my 'Fun Lab' dungeon for later use and thought I might as well post back the version I used as it was a bit shorter.

Cool idea - and I can't wait to use it!

Re: {script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 4:25 pm
by cromcrom
Don't be, I really genuinely admire code optimizers. Mines are so "brutal" ...
I am adding a lot of little systems like that in The Lost Continent :-) (I now, I am advertising, well, you know... :-) )

Re: {script} Spawn something just in front of the party

Posted: Mon Oct 01, 2012 5:04 pm
by petri
To get the location in front of an entity you could also use the getForward() function:
local dx,dy = getForward(party.facing)
local x,y = party.x + dx, party.y + dy
Now x and y hold the coordinates in front of the party.

Same technique can be used to get the location behind the party or beside the party. E.g. Replace party.facing by (party.facing+2)%4 to get the location behind the party.

Naturally you can also replace party with any other entity.

Re: {script} Spawn something just in front of the party

Posted: Fri Oct 05, 2012 7:20 pm
by Komag
so if petri's notes were applied, what would be a good final version of this to put in the Script Repository?