Scripts:Diagonal walking

From OHRRPGCE-Wiki
Jump to navigationJump to search

Below is a script ("walk diagonal") which walks a specified hero diagonally, side on (as if descending/ascending stairs to the left or right). The "diagonal demo" script demonstrates its use; if you set it as an on-keypress script, then you can use the Q, W, A, S keys to move diagonally.

The script can be easily modified to work on NPCs instead.

global variable (1, inside onkeypress)

#Example onkeypress script for testing
plotscript, diagonal demo, begin
  #Prevent double triggering of this script
  if (inside onkeypress) then (exit script)
  inside onkeypress := true

  if (hero is walking (me) == false) then (
    if (key is pressed (key:q)) then (walk diagonal (me, up, left))
    if (key is pressed (key:w)) then (walk diagonal (me, up, right))
    if (key is pressed (key:a)) then (walk diagonal (me, down, left))
    if (key is pressed (key:s)) then (walk diagonal (me, down, right))
  )

  inside onkeypress := false
end



#Walks hero 'who' diagonally, side-on, some number of tiles (default: 1 tile).
#Do not call this script while the hero is moving! Check with "hero is walking" if needed.
#'climbing' should be either "up" or "down"
#'direction' should be either "left" or "right"
script, walk diagonal, who, climbing, direction, tiles=1, begin
  variable (i, speed, steps)

  #You can use some other value for speed, as long as it divides into 20
  speed := get hero speed (who)

  #Calculate number of ticks the animation will take
  steps := (20 / speed) * tiles

  suspend player
  set hero direction (who, direction)

  variable (x, y, frame)
  x := hero pixel x (who)
  y := hero pixel y (who)
  frame := hero frame (who)
  for (i, 1, steps) do (
    if (direction == left) then (
      x -= speed
    ) else (
      x += speed
    )
    if (climbing == up) then (
      y -= speed
    ) else (
      y += speed
    )

    #Every second tick (just like when walking), change the frame
    #(Note 1,xor,1 = 0, and 0,xor,1 = 1)
    if (i,mod,2 == 1) then (frame := frame, xor, 1)

    put hero (who, x, y)
    set hero frame (who, frame)
    wait
  )

  resume player
end