Main
Projects
Art
Music
Scripting
LiveJournal
Forum
Links
Zelda Attack
by James `SPAM Man' Paige

James tells me that this is a popular script for people on the Help Me board..


include, plotscr.hsd
include, scancode.hsi

# The purpose of this script is to make a sword appear in front of your
# hero when you press the 'Z' key. The sword will kill any NPCs it hits.

# If you are adding these scripts to your own, make sure to change the
# script and global ID numbers to avoid conflicts with the ID numbers
# you have already used

# Known problems: You can attack while walking, but it looks pretty silly,
# as the attack doesnt line up properly with the hero

#-------------------------------------------------------------------------

# The "do attack" script needs to be attached to the "on-keypress" event
# for each map where you want to be able to do the attack
define script(1,do attack,none)

# The "endless loop" script should run as the "new game" and "load game"
# scripts, that way it will always be running at all times.
define script(2,endless loop,none)

# this script is called directly by the "endless loop script"
define script(autonumber,kill NPC on the same tile as,1,0)

# this variable keeps track of if the sword is on the screen
global variable(1,attack reference)

# this keeps track fo how long the sword has been on the screen
global variable(2,attack age)

# an NPC that looks like a sword must be defined as the 35th NPC on
# every map where attacking is allowed.
define constant(35,Sword NPC ID)

# This is the key that will be used for attacking. By default the
# letter 'Z' but you can change it to anything in scancode.hsi
define constant(KEY:Z,Attack Key)

#-------------------------------------------------------------------------

script,do attack,begin
  # variables
  variable(x,y,d)

  # check to see if the key is being pressed
  if (key is pressed (attack key)) then,begin

    # if there is already a sword on screen,
    # then we do not want to create another
    if (attack reference==0) then,begin
  
      # initialize the variables to the hero's position and direction
      x:=hero X(me)
      y:=hero Y(me)
      d:=hero direction(me)
    
      # adjust the position depending on direction. We want the sword to
      # appear one space in front of the hero
      if (d==north) then(decrement(y))
      if (d==south) then(increment(y))
      if (d==west)  then(decrement(x))
      if (d==east)  then(increment(x))
    
      # create the Sword NPC and store a reference to it
      attack reference:=create NPC(Sword NPC ID,x,y,d)
  
      # reset the attack age
      attack age:=0
  
    end

  end

end

#-------------------------------------------------------------------------

script, endless loop, begin
  # initialize the attack reference before the loop starts
  attack reference:=0

  # the endless loop
  while(true) do,begin

    # every endless loop must have a wait command
    wait(1)

    # check to see if a sword exists
    if (attack reference) then,begin

       kill NPC on the same tile as(attack reference)

       increment(attack age)

       if (attack age >= 2) then,begin
         destroy NPC(attack reference)
         attack reference:=0
       end

    end

  end

end

#-------------------------------------------------------------------------

script, kill NPC on the same tile as, Weapon, begin
  variable(NPC)

  variable(x,y)
  x:=NPC X(Weapon)
  y:=NPC Y(Weapon)

  NPC:=NPC at spot(x,y,0)
  if (NPC<>Weapon,and,NPC<>0) then,begin
    destroy NPC(NPC)
  end, else, begin
    NPC:=NPC at spot(x,y,1)
    if (NPC<>Weapon,and,NPC<>0) then,begin
      destroy NPC(NPC)
    end
  end

end

#-------------------------------------------------------------------------


Site layout (c)2000 Jennifer Raby. Used by permission.