Challenge 2 Solution

From Zodiac
Jump to: navigation, search

Here is my simple solution to Challenge 2.

//#CLIENTSIDE
function onCreated() {
  // Initialize Boot Values
  this.boots = false;
  this.bootspeed = 1;
}

function onPlayerChats() {
  // Check for Boots Command
  if (player.chat == "/boots") {
    // Toggle Boots On/Off
    if (this.boots) {
      this.boots = false;
    } else {
      this.boots = true;
      setTimer(0.05);
    }
  }
  else if (player.chat == "/boots fast") {
    // Set Boots to Fast Speed
    this.bootspeed = 2;
  }
  else if (player.chat == "/boots normal") {
    // Set Boots to Normal Speed
    this.bootspeed = 1;
  }
  else if (player.chat == "/boots slow") {
    // Set Boots to Slow Speed
    this.bootspeed = 0.25;
  }
}

function onTimeout() {
  // Make sure boots are still on
  if (this.boots) {
    // Loop through Direction Keys (0-3)
    for (temp.i = 0; temp.i < 4; temp.i++) {
      // Check if Key Held Down
      if (keydown(temp.i)) {
        // Move player in that direction using vectors.
        player.x += vecx(temp.i) * this.bootspeed;
        player.y += vecy(temp.i) * this.bootspeed;
      }
    }
    // Continue looping while boots are on
    setTimer(0.05);
  }
}

Here is my slightly more advanced solution.

//#CLIENTSIDE
function onCreated() {
  this.bootspeed = 1;
  this.boots = false;
}

function ChatBar.onAction() {
  if (ChatBar.text.starts("/boots")) {
    if (ChatBar.text == "/boots") {
      // Toggles Boots On/Off
      this.boots = !this.boots;
      player.chat = "Boots are now " @ (this.boots ? "on" : "off") @ "!";
      // Check if Boots were turned on
      if (this.boots) {
        // Boots on! Begin looping 
        setTimer(0.05);
      }
    } else {
      // Allows player to set specific speed. I.e: /boots 5
      this.bootspeed = float(ChatBar.text.substring("/boots ".length()));
      player.chat = "Changed boot speed to " @ this.bootspeed;
    }
    // Prevents Chat from getting through.
    ChatBar.text = "";
  }
}

function onTimeout() {
  // Make sure boots are still on
  if (this.boots) {
    // Loop through Direction Keys (0-3)
    for (temp.i = 0; temp.i < 4; temp.i++) {
      // Check if Key Held Down
      if (keydown(temp.i)) {
        // Move player in that direction using vectors.
        player.x += vecx(temp.i) * this.bootspeed;
        player.y += vecy(temp.i) * this.bootspeed;
      }
    }
    // Continue looping while boots are on
    setTimer(0.05);
  }
}