Tower tutorial

Making a big tower in the map engine is easy.
(Just draw out all the tiles and place them down)

But say, when we get up to a certain point, we want to look up at the tower?

function Wait()
{
  while (AreKeysLeft()) GetKey();
  GetKey();
}

function LookUpWaitThenLookDownAgain(pixels)
{ // must be called from within the map engine
  // detach the input
  var input_person = "":
  if (IsInputAttached())
  {
    input_person = GetInputPerson();
    DetachInput();
  }

  var pixels_done_so_far = 0;
  var delay = 80;
  var time = GetTime();

  while (current_pixel <= pixels)
  {
    if (time + delay < GetTime())
    {
      current_pixel += 1;
      time = GetTime();
    }
    SetCameraX(current_pixel);
    RenderMap();
  }

  Wait();

  while (current_pixel <= pixels)
  {
    if (time + delay < GetTime())
    {
      current_pixel -= 1;
      time = GetTime();
    }
    SetCameraX(current_pixel);
    RenderMap();
  }

  // reattach the input
  if (input_person != "")
  {
    AttachInput(input_person);
  }
}

Then that should look up then down then back to where it was :)
Insert Entity > Trigger :- LookUpWaitThenLookDownAgain(100);

You could break this up into 2 functions, look up and look down
Then do:-

LookUp(100);
// say some text
LookDown(100);

So let's do that then :)

function LookUp(pixels)
{
  var pixels_done_so_far = 0;
  var delay = 80;
  var time = GetTime();
  var direction = 1; 

  while (current_pixel <= pixels)
  {
    if (time + delay < GetTime())
    {
      current_pixel += direction;
      time = GetTime();
    }
    SetCameraX(current_pixel);
    RenderMap();
  }
}

function LookDown(pixels)
{
  var pixels_done_so_far = 0;
  var delay = 80;
  var time = GetTime();
  var direction = -1;

  while (current_pixel <= pixels)
  {
    if (time + delay < GetTime())
    {
      current_pixel += direction;
      time = GetTime();
    }
    SetCameraX(current_pixel);
    RenderMap();
  }
}

Hmm, I've now dicided that I'm going to rewrite those as:-

function BaseLook(pixels, increase_per_delay)
{
  var pixels_done_so_far = 0;
  var delay = 80;
  var time = GetTime();

  while (current_pixel <= pixels)
  {
    if (time + delay < GetTime())
    {
      current_pixel += increase_per_delay;
      time = GetTime();
    }
    SetCameraX(current_pixel);
    RenderMap();
  }
}

function LookUp(pixels)  { BaseLook(pixels,  1); }
function LookDown(pixels){ BaseLook(pixels, -1); }

Thus being lazy by design :D

What about the input detaching/reattching code?
*Sigh* okay..

function GetInputPersonAndDetach()
{
  var input_person = "":
  if (IsInputAttached())
  {
    input_person = GetInputPerson();
    DetachInput();
  }
  return input_person;  
}

function SetInputPerson(input_person)
{
  if (input_person != "")
    AttachInput(input_person);
}

So now we can do:-

var p = GetInputPersonAndDetach();
LookUp(100);
ShowText("Ooh, pretty tower.");
LookDown(100);
SetInputPerson(p);

And that makes that code reuseable, I guess.

And recently (forgetting that I had this) I made this:

function SlideCameraTo(end_x, end_y, relative, time)
{
  var camera_person = "";
  if (IsCameraAttached()) {
    camera_person = GetCameraPerson();
  }

  var start_x = GetCameraX();
  var start_y = GetCameraY();

  if (relative) {
    end_x = start_x + end_x;
    end_y = start_y + end_y;
  }

  CameraSlide(start_x, start_y, end_x, end_y, time);

  if (camera_person != "") {
    AttachCamera(camera_person);
  }
}

function CameraSlide(sx, sy, ex, ey, time)
{
  var camera_person = "";
  if (IsCameraAttached()) {
    camera_person = GetCameraPerson();
  }

  var old_x = GetCameraX();
  var old_y = GetCameraY();

  SetCameraX(sx);
  SetCameraY(sy);

  var x_diff = ex - sx;
  var y_diff = ey - sy;
  
  var start = GetTime();
  while (start + time > GetTime()) {
    var time_passed = GetTime() - start;

    var x_offset = (time ? time_passed / time : 0) * x_diff;
    var y_offset = (time ? time_passed / time : 0) * y_diff;
    
    SetCameraX(sx + x_offset);
    SetCameraY(sy + y_offset);
    
    RenderMap();
    UpdateMapEngine();    
    FlipScreen();
  }
  
  SetCameraX(ex);
  SetCameraY(ey);

  if (camera_person != "") {
    AttachCamera(camera_person);
  }
}

Essentially, the same, but easier? :)

CameraSlide(0, 0, 500, 500, 5000); // taking 5 seconds (or 5000 milliseconds) slide from (x = 0, y = 0) to (x = 500, y = 500)

Anyway.
That's enough code for now.