Setting the viewport?

Started by piraatlife4me, Sat 13/12/2003 23:30:22

Previous topic - Next topic

piraatlife4me

Hey AGS forum! I gotta question I have a vertical scrolling room and I need the viewport to be set all the way to the top of the screen when the player walks up it, right now the screen automatically follows the player when he walks.  What can I use to have the view port scroll all the way to top when the player is close to the top of the screen(instead of partially scolling)Any ideas?

Daniel
Winebarger

Scorpiorus

#1
EDIT: <in order to rename the viewport_width to viewport_height>

try that one: :P

at the top of room script:

int viewport_height = 200; // viewport height 200, 240 or 300 depending on your game;
int top_edge = 40;        // when reached by player viewport starts to scroll;
int walk_up  = 10;        // how far the character goes up when he appears on the next screen;
int scroll_speed = 4;     // viewport scrolling speed (values: 1, 2, or 4)

function mod(int a, int b) { return a - (b*(a/b)); }



room's player enters screen:

SetViewport(GetViewportX(), game.room_height);


room's repeatedly execute:

// script for room: Repeatedly execute
 
   int PC = GetPlayerCharacter();
   
   int offset = mod(character[PC].y, viewport_height);
   
   if (offset < top_edge && character[PC].y > top_edge) {
      StopMoving(PC);
      int i=0;
      while (i<viewport_height) {
         SetViewport(GetViewportX(), GetViewportY() - scroll_speed);
         i+=scroll_speed;
         Wait(1);
      }
      MoveCharacterBlocking(PC, character[PC].x, character[PC].y - (top_edge+walk_up), 1);
   }


~Cheers

piraatlife4me

Hey Scorpius! thanks alot again! for the help I really appreciate it.  One problem though I put all this code in the right places and everything is great the problem is it automatically pauses the game and moves my player character all the way to the top of the screen.  Are you sure this last bunch of code is supposed to be put in a repeatedley execute function? Or is there something I am leaving out?  Anyways really appreciate the help again.

Daniel
Winebarger

Scorpiorus

#3
QuoteAre you sure this last bunch of code is supposed to be put in a repeatedley execute function?
Yeah, though it has to be enclosed within the if (...) statement, just check it in case:

function room_*() { // room's repeatedly

if (offset < top_edge && character[PC].y > top_edge) {
   .....
   .....
   MoveCharacterBlocking(PC, character[PC].x, character[PC].y - (top_edge+walk_up), 1);
}

}

hmm, so does the scrolling itself work correct?

What resolution is your game designed for?

What are room's height and width? (<- that one is the most important)

Did you change the variables at the top of the script (i.e. viewport_width, top_edge, etc.)? If you did please tell me their new values.

piraatlife4me

Hey scorpius! Its Dan here. Yeah the room scrolls but
It still dosent work when I get to a certain point on the screen the game pauses and my pc  just starts going very very slowly scrolling to with the room to the very top of the screen. My game is in 320 by 240 resolution.  The rooms actual size is 320 by 500, it's a long scrolling up type room. I need the screen to shift all the way to the top with the pc all the way at the bottom of the viewport when the pc gets to certain part of the screen otherwise the room scrolls with the pc and a big chunk of the rooms background is never seen.I also need to scroll back when he gets to the top of a certain point.
here are my values so for the top part

int viewport_width=320;    //view port max width
int top_edge = 130;   //when reached by player viewport starts to scroll
int walk_up = 150; //how far the player has to go up when he appears on the next screen
int scroll_speed = 4;//viewport set scrolling speed
function mod(int a, int b) { return a - (b*(a/b)); }

Any ideas?
Thanks again

Daniel
Winebrager

Scorpiorus

#5
Quoteint viewport_width=320; //view port max width
width?! ooops, my bad - I named it viewport_width instead of viewport_height :)

Anyway that script is suitable for rooms with height size multiple to the viewport resolution, i.e. 480, 720, 960 ... etc

So, for your particular case:

Instead of the script I posted add the following functions at the top of the room:

// room script

function abs(int v) { if (v<0) return -v; else return v; }

function MoveViewportY(int y, int scroll_speed) {
 Â Â Â scroll_speed = abs(scroll_speed);
   if (y < GetViewportY()) scroll_speed = -scroll_speed;
   int prevY = -1;
   while (abs(y - GetViewportY()) > abs(scroll_speed) && GetViewportY() != prevY) {
      prevY = GetViewportY();
      SetViewport(GetViewportX(), GetViewportY() + scroll_speed);
      Wait(1);
   }
   SetViewport(GetViewportX(), y);
}

ok, now you have a new MoveViewportY(int Y, int scroll_speed) function which moves the viewport along Y-axis to new Y coordinate with scroll_speed velocity value.


Next draw a region where you want viewport to scroll and in it's Player walks into region:

MoveViewportY(0, 4); // scroll up to the top of the room (speed = 4);
MoveCharacterBlocking(.....); // move the character as well;

don't forget on player enters screen:

SetViewport(GetViewportX(), game.room_height);


if you want it to scroll back also (using the same region) you can create a special variable defining where your character is:

// room script

int bottom_of_the_room = 1; //yes initially he is at the bottom of the room


Player walks into region interaction (modified):

if (bottom_of_the_room == 1) {
   MoveViewportY(0, 4); // scroll up to the top of the room (speed = 4);
   MoveCharacterBlocking(.....); // move the character as well;
   bottom_of_the_room = 0; // he is ontop now
} else {

   MoveViewportY(game.room_height, 4); // scroll downto the bottom of the room (speed = 4);
   MoveCharacterBlocking(.....); // move the character as well;
   bottom_of_the_room = 1;
}



how does it go? :P

piraatlife4me

Hey Scorpius! You're the man! I got it to work well check it out I did this instead :

// room script file
int bottom_of_the_room = 0;
function abs(int v) { if (v<0) return -v; else return v; }

function MoveViewportY(int y, int scroll_speed) {
  scroll_speed = abs(scroll_speed);
  if (y < GetViewportY()) scroll_speed = -scroll_speed;
  int prevY = -1;
  while (abs(y - GetViewportY()) > abs(scroll_speed) && GetViewportY() != prevY) {
     prevY = GetViewportY();
     SetViewport(GetViewportX(), GetViewportY() + scroll_speed);
     Wait(1);
  }
  SetViewport(GetViewportX(), y);
}

function room_a() {
 // script for room: Player enters screen (before fadein)
FaceLocation(EGO, 150, -1000);  
}

function room_b() {
 // script for room: Walk off bottom screen edge
NewRoomEx(27, 418, 79);  
}

function region2_a() {
 // script for region2: Player walks onto region
if (bottom_of_the_room == 0) {
  MoveCharacterBlocking(EGO, 187, 222, 1); // move the character as well;
  MoveViewportY(0, 3);
  bottom_of_the_room = 0; // he is ontop now
}
else if (bottom_of_the_room == 1) {
  MoveViewportY(game.room_height, 3); // scroll downto the bottom of the room (speed = 4);
  MoveCharacterBlocking(EGO, 141, 300, 1); // move the character as well;
  FaceLocation(EGO, 150, 1000);
  bottom_of_the_room = 0;
}
}
function room_c() {
 // script for room: Player enters screen (after fadein)
SetViewport(GetViewportX(), game.room_height);
 
}
function region3_c() {
 // script for region3: While player stands on region
MoveViewportY(0, 3);
bottom_of_the_room = 1;  
}

What I did is I took your idea on drawing out regions but instead I drew out two of them one to move the pc and one that sets the variables value and sets the viewport.  It works perfectly!
thanks again for all your time and effort!

Daniel
Winebarger

Scorpiorus

Yeah, I see. There are many opportunities to experiment with. Glad it works now! :)

~Cheers

piraatlife4me

Hey scorpius, Its Dan again I had one more quick question.  The script that you gave me on how to make to room scroll up the screen. What if I wanted it to scroll across the screen intead of up?  What values would I change to do so?  Because I have this other really long room and when the first time the player enters the screen I want it to scoll all the way to the left because if the player decides to enter the room from comming from another direction  I want the room to scroll and the way over to the left wait for the animation sequence to run and then when it is finished scroll all the way back to the right and then give control to the player.  Kind of like in Maniac Mansion when Weird Ed opens the fridge in the kitchen how even if you are in the same long room it will scroll all the way over and then scroll back.
I am just looking for a good way to manually scroll the screen back and forth when I need to.  
Thanks again

Daniel
Winebarger

Scorpiorus

Almost like MoveViewportY ;)

function MoveViewportX(int x, int scroll_speed) {
   scroll_speed = abs(scroll_speed);
   if (x < GetViewportX()) scroll_speed = -scroll_speed;
   int prevX = -1;
   while (abs(x - GetViewportX()) > abs(scroll_speed) && GetViewportX() != prevX) {
      prevX = GetViewportX();
      SetViewport(GetViewportX() + scroll_speed, GetViewportY());
      Wait(1);
   }
   SetViewport(x, GetViewportY());
}

The abs() function is the same

SMF spam blocked by CleanTalk