Jump to content

Photo

Adjusting Link's Walking Speed


  • Please log in to reply
2 replies to this topic

#1 grayswandir

grayswandir

    semi-genius

  • ZC Developers

Posted 19 August 2013 - 05:59 PM

This lets you adjust Link's walking speed. Just include the calls in your main loop, and any changes to the LS_Speed variable will change how fast Link walks (the default speed being 1.0).

 

In your Active loop, you'll need to include ScreenChange_Update() so it knows when you've changed screens, and LS_Update() to adjust Link's position.

 

This has only been tested with Z3 movement, but it should work without it.

 

If you set LS_Speed to a negative number, Link will walk backwards, but it messes up the collision detection and he will be able to go through walls.

////////////////////////////////////////////////////////////////
//// screen-change.zh
 
// Last noticed DMap.
int LastDMap = -1;
// Last noticed DScreen.
int LastDScreen = -1;
// If the screen has changed.
bool ScreenChanged = false;
// If the dmap has changed.
bool DMapChanged = false;
 
void ScreenChange_Update() {
  int dMap = Game->GetCurDMap();
  int dScreen = Game->GetCurDMapScreen();
  DMapChanged = dMap != LastDMap;
  ScreenChanged = DMapChanged || dScreen != LastDScreen;
  LastDMap = dMap;
  LastDScreen = dScreen;}
 
////////////////////////////////////////////////////////////////
//// link-speed.zh
 
// Link's locaton last frame.
int LS_OldX = 0;
int LS_OldY = 0;
 
// Link's Partial movement
int LS_PartialX = 0;
int LS_PartialY = 0;
 
// Link's speed multiplier.
int LS_Speed = 1.0;
 
void LS_Update() {
  if (ScreenChanged) {
    LS_PartialX = 0;
    LS_PartialY = 0;
    LS_OldX = Link->X;
    LS_OldY = Link->Y;}

  if (Link->Action == LA_RAFTING ||
      Link->Action == LA_SWIMMING) {
    LS_OldX = Link->X;
    LS_OldY = Link->Y;
    return;}

  int dx = Link->X - LS_OldX;
  int dy = Link->Y - LS_OldY;
 
  // Slow Link Down.
  if (LS_Speed < 1) {
    int max = 1.5 * LS_Speed;
    if (Abs(dx) > max) {
      if (dx > 0 && Link->InputRight) {dx = max;}
      else if (Link->InputLeft) {dx = -max;}}
    if (Abs(dy) > max) {
      if (dy > 0 && Link->InputDown) {dy = max;}
      else if (Link->InputUp) {dy = -max;}}
 
    Link->X = LS_OldX + (dx >> 0);
    LS_PartialX += dx - (dx >> 0);
    Link->Y = LS_OldY + (dy >> 0);
    LS_PartialY += dy - (dy >> 0);}
 
  // Speed Link Up.
  else if (LS_Speed > 1) {
    if (dy < 0 && Link->InputUp) {
      LS_PartialY -= 1.5 * LS_Speed + dy;}
    if (dy > 0 && Link->InputDown) {
      LS_PartialY += 1.5 * LS_Speed - dy;}
    if (dx < 0 && Link->InputLeft) {
      LS_PartialX -= 1.5 * LS_Speed + dx;}
    if (dx > 0 && Link->InputRight) {
      LS_PartialX += 1.5 * LS_Speed - dx;}}
 
  // Apply partial movement.
  while (LS_PartialY < -0.5 && CanWalk(Link->Y, Link->Y, DIR_UP, 1, false)) {
    Link->Y--;
    LS_PartialY += 1;}
  while (LS_PartialY > 0.5 && CanWalk(Link->Y, Link->Y, DIR_DOWN, 1, false)) {
    Link->Y++;
    LS_PartialY -= 1;}
  while (LS_PartialX < -0.5 && CanWalk(Link->X, Link->Y, DIR_LEFT, 1, false)) {
    Link->X--;
    LS_PartialX += 1;}
  while (LS_PartialX > 0.5 && CanWalk(Link->X, Link->Y, DIR_RIGHT, 1, false)) {
    Link->X++;
    LS_PartialX -= 1;}
 
  // Get rid of excess partial movement.
  if (LS_PartialY < -0.5) {LS_PartialY = -0.5;}
  if (LS_PartialY > 0.5) {LS_PartialY = 0.5;}
  if (LS_PartialX < -0.5) {LS_PartialX = -0.5;}
  if (LS_PartialX > 0.5) {LS_PartialX = 0.5;}
 
  // Save new position.
  LS_OldX = Link->X;
  LS_OldY = Link->Y;}
 
////////////////////////////////////////////////////////////////
//// Sample Active Script
 
global script Active {
  void run() {
    while (true) {
      if (Link->PressL) {LS_Speed -= 0.1;}
      if (Link->PressR) {LS_Speed += 0.1;}
      ScreenChange_Update();
      LS_Update();
      Waitframe();}}}

Edited by grayswandir, 19 August 2013 - 06:03 PM.

  • Aquamentus (Facing Right) likes this

#2 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 19 August 2013 - 07:35 PM

Thanks a bunch grayswandir! This will really come in handy for an item I want to make in my quest. I've been looking for this and could never find it. Thanks so much! You're really an amazing scripted! :D

#3 Shadowblitz16

Shadowblitz16

    Illustrious

  • Members

Posted 04 January 2017 - 10:03 PM

@greyswandir

 

this is nice but it has a few bugs in it

the collisions on the y position are weird but that's fixed by changing this

Spoiler

to this 

Spoiler

there still seem to be a few collision bugs in it though

 

and the jittering can be fixed by calling Waitdraw() before the ScreenChange_Update()

but then there is the screen scrolling it seems that if link is at a low speed and the screen just has scrolled if the player continues to press in that direction, then link will get set back a pixel causing the screen to scroll backwards.

 

hopefully this can be fixed I would like to use this script


Edited by Shadowblitz16, 05 January 2017 - 02:10 PM.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users