(Solved) Need some coding advice (alternative methods/improvements)

Started by Vault15, Sat 13/04/2013 12:15:05

Previous topic - Next topic

Vault15

So I was brainstorming how to have the screen scroll left, right, up or down if they position the cursor at the edges of the screen.

Then it hit me. I can have an invisible character that teleports to the player's location when the mouse is at the screen edges. This invisible character would teleport either 400 spaces west, east, north or south of the player respectively. For example, if the mouse is positioned on the very left of the screen, then the invisible character is moved 400 to the left of the player's location and set as the main character (movement disabled). Since I am currently using the Smooth Scrolling & Parallax Mod, this method seems to work nicely.

I haven't seen anyone mention this method for viewing around freely with the mouse, so is this due to problems I can't foresee as of now? Is this idea somehow original or absurd? I appreciate any advice or warnings. I'm mainly looking to improve upon the code that I just came up with.

Edit: My thoughts on possible improvements: The Left, Right, Up, Down movements are rather rigid/stiff (no angled viewing around (e.g., northwest)). Maybe to fix this, the invisible character could quickly run towards the current mouse position and so the screen would move where the mouse goes. I'm not exactly sure how to do it yet.


Here is my currently working portion of code. So far it works perfectly as it should with no errors and very slight stalls.
Ignore quickstart. My current map is 6265x3720. MouseScroll is a blank character used while in scrolling mode (cursor on the edge of the screen).
Code: AGS

if(mouse.x >= 0 && mouse.x <= 30 && quickstart == 0 && scrolling == 0 && player.x > 400)
  {
      Mouse.DisableMode(eModeWalkto);
      MouseScroll.Move(cEgo.x-400, cEgo.y);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.x >= 994 && mouse.x <= 1024 && quickstart == 0 && scrolling == 0 && player.x < 5865)
  {
      Mouse.DisableMode(eModeWalkto);
      MouseScroll.Move(cEgo.x+400, cEgo.y);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.y >= 0 && mouse.y <= 30 && quickstart == 0 && scrolling == 0 && player.y > 400)
  {
      Mouse.DisableMode(eModeWalkto);
      MouseScroll.Move(cEgo.x, cEgo.y-400);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.y <= 768 && mouse.y >= 738 && quickstart == 0 && scrolling == 0 && player.y < 3320)
  {
      Mouse.DisableMode(eModeWalkto);
      MouseScroll.Move(cEgo.x, cEgo.y+400);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.x > 30 && mouse.x < 994 && mouse.y > 30 && mouse.y < 738 && player == MouseScroll)
  {
    cEgo.SetAsPlayer();
    targetCharacter = cEgo;
    scrolling = 0;
    Mouse.EnableMode(eModeWalkto);
    Mouse.Mode = eModeWalkto;
  }



Version 2 (Slightly Improved). I re-enabled walking while scrolling with the mouse so that you can continue walking along (convenient).
Code: AGS

 
  
  //U, D, L, R of Char. View
  //MouseScroll = Invisible Char.
  //targetCharacter is the variable for the Smooth Scrolling & Parallax Module.
  
  if(mouse.x >= 0 && mouse.x <= 30 && quickstart == 0 && scrolling == 0 && player.x > 400)
  {
      
      MouseScroll.Move(cEgo.x-400, cEgo.y);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.x >= 994 && mouse.x <= 1024 && quickstart == 0 && scrolling == 0 && player.x < 5865)
  {
      
      MouseScroll.Move(cEgo.x+400, cEgo.y);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.y >= 0 && mouse.y <= 30 && quickstart == 0 && scrolling == 0 && player.y > 400)
  {
      
      MouseScroll.Move(cEgo.x, cEgo.y-400);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.y >= 738 && mouse.y <= 768 && quickstart == 0 && scrolling == 0 && player.y < 3320)
  {
      
      MouseScroll.Move(cEgo.x, cEgo.y+400);
      MouseScroll.SetAsPlayer();
      targetCharacter = MouseScroll;
      scrolling = 1;
  }
  
  if(mouse.x > 30 && mouse.x < 994 && mouse.y > 30 && mouse.y < 738 && player == MouseScroll)
  {
    cEgo.SetAsPlayer();
    targetCharacter = cEgo;
    scrolling = 0;
  }
  
  if(scrolling == 1 && Mouse.IsButtonDown(eMouseLeft))
  {
    cEgo.Walk(mouse.x + GetViewportX(),  mouse.y + GetViewportY());
    MouseScroll.Walk(mouse.x + GetViewportX(),  mouse.y + GetViewportY());
  }

Khris

Did some scrolling without the module:

Code: ags
// settings
int scroll_border = 30;
int scroll_speed = 10;
float lazy = 0.1;

// room variables
int currentx = 0;
int currenty = 0;
float speedx;
float speedy;

function room_RepExec()
{
  int mx = mouse.x;
  int my = mouse.y;
  
  int vpxmax = Room.Width - System.ViewportWidth;
  int vpymax = Room.Height - System.ViewportHeight;
  
  int xa = -(mx < scroll_border) + (mx > System.ViewportWidth - scroll_border);
  int ya = -(my < scroll_border) + (my > System.ViewportHeight - scroll_border);

  int targetspeedx = (xa < 0) * (-scroll_speed) * (currentx > 0) + (xa > 0) * scroll_speed * (currentx < vpxmax);
  int targetspeedy = (ya < 0) * (-scroll_speed) * (currenty > 0) + (ya > 0) * scroll_speed * (currenty < vpymax);

  float dsx = (IntToFloat(targetspeedx) - speedx) * lazy;
  float dsy = (IntToFloat(targetspeedy) - speedy) * lazy;
  
  speedx += dsx;
  speedy += dsy;
  
  currentx += FloatToInt(speedx, eRoundNearest);
  if (currentx < 0) currentx = 0; if (currentx > vpxmax) currentx = vpxmax;
  currenty += FloatToInt(speedy, eRoundNearest);
  if (currenty < 0) currenty = 0; if (currenty > vpymax) currenty = vpymax;
  SetViewport(currentx, currenty);
}

Vault15

Quote from: Khris on Sat 13/04/2013 15:01:21
Did some scrolling without the module:

I keep trying "cEgo.SetAsPlayer();" but my screen moves to the top left corner and when I scroll back I can't move the main character.

The scrolling is extremely smooth though, thank you! I'm trying to figure out what on earth I did that interferes with your code. I like how your code doesn't re-center on the character all the time.

Note: Yes I did turn off the module.

Khris

The code ignores the player character's position; not sure though why the viewport would keep moving to 0;0.
It does starts out there though, unless you change the initial values of currentx/currenty.

Vault15

Quote from: Khris on Sat 13/04/2013 15:46:17
The code ignores the player character's position; not sure though why the viewport would keep moving to 0;0.
It does starts out there though, unless you change the initial values of currentx/currenty.

I deleted my custom code shown above too, so it should just be normal. I noticed that when I click the inventory button then all of sudden the screen is scrollable as it should be. Trying to figure out what on earth could be conflicting.

Vault15

Quote from: Khris on Sat 13/04/2013 15:46:17
The code ignores the player character's position; not sure though why the viewport would keep moving to 0;0.
It does starts out there though, unless you change the initial values of currentx/currenty.

Is there a way that I can force it to center on cEgo? I've already tried cEgo.SetAsPlayer(); . Basically I'll see the main character, click the mouse, then the screen juts over to the top left corner. I'm able to scroll slowly back over to the starting area and then it's normal from that point on.

Khris

cEgo.SetAsPlayer(); won't do anything since like I said, my code ignores the player's position.

You can try adding this in room_Load:
Code: ags
  currentx = player.x - System.ViewportWidth/2;
  currenty = player.y - System.ViewportHeight/2;


I'm wondering why it only moves to 0;0 after you click the mouse though. My code should take control of the viewport's position immediately.

Edit: errors corrected

Vault15

Quote from: Khris on Sun 14/04/2013 10:40:01
cEgo.SetAsPlayer(); won't do anything since like I said, my code ignores the player's position.

You can try adding this in room_Load:
Code: ags
  currentx = player.x - Screen.ViewportWidth/2;
  currenty = player.y - Screen.ViewportHeight/2;


I'm wondering why it only moves to 0;0 after you click the mouse though. My code should take control of the viewport's position immediately.

Brilliant, it works. I fixed the typo to "system.ViewportHeight/2" and "system.ViewportWidth/2", thank you.

SMF spam blocked by CleanTalk