Map Scrolling via the mouse instead of the player's location?

Started by Vault15, Thu 04/04/2013 01:05:19

Previous topic - Next topic

Vault15

I'm using this module:
http://www.adventuregamestudio.co.uk/forums/index.php?topic=33142.0

Note: I'm still deciding whether I want the mouse to control map scrolling or the player's location (I'd like to try both).

I've been trying to change "player.x" to "mouse.x" where obvious, but the screen is still confined to the character rather than freely scrolling around. Also when the mouse is at the right, the screen will go left.

I enjoy learning from the best, so thank you.

Gilbert

I don't know what module it is, but for scrollable rooms you cannot just substitute player.x with mouse.x, as the mouse cursor uses screen coordinates, whereas characters and objects use room coordinates.

So, change player.x to 'mouse.x + GetViewportX()' and player.y to 'mouse.y + GetViewportY()' instead and see whether it makes any difference.

Vault15

Quote from: Iceboty V7000a on Fri 05/04/2013 03:05:07
I don't know what module it is, but for scrollable rooms you cannot just substitute player.x with mouse.x, as the mouse cursor uses screen coordinates, whereas characters and objects use room coordinates.

So, change player.x to 'mouse.x + GetViewportX()' and player.y to 'mouse.y + GetViewportY()' instead and see whether it makes any difference.



I appreciate the suggestion Iceboty :). I did actually try this, but the problem seems to be that I can only move the screen via the mouse once I walk the character up a bit. It's really awkward, which I imagine is because I need to do something a bit more elaborate. If it's too much to go through, I completely understand. I suppose it may help someone else sometime down the line too. I really need to pay someone to help me get through some of the grit.

Here's the original main section of the module:

Code: AGS

function repeatedly_execute_always() {

  if (targetCharacter == null) targetCharacter = player;
  if (targetCharacter.Room != player.Room) targetCharacter.ChangeRoom (player.Room);

  if (IsGamePaused()==false){
    
    if (System.OperatingSystem != eOSLinux){ // Disable smooth scrolling on Linux OS because of a problem with the Emulator

        if (SmoothScroll_ScrollStatus){



            float goalScrollSpeedX = customScrollSpeedX;
            float goalScrollSpeedY = customScrollSpeedY;
                    
            Count++;
            
            if (Count == 2){

              TargetVelocityX = targetCharacter.x -TargetPosX;
              TargetVelocityY = targetCharacter.y - TargetPosY;
              
              TargetPosX = targetCharacter.x;
              TargetPosY = targetCharacter.y;
              
              Count = 0;
            }
            
            TargetScreenX = targetCharacter.x - GetViewportX();
            TargetScreenY = targetCharacter.y - GetViewportY();

            //Horizontal Scrolling:
            
            if ((TargetScreenX < (EdgeX))||TargetScreenX > (ScreenWidth-EdgeX)) ScrollingAllowedX = true;
            else ScrollingAllowedX = false; // Otherwise, don't allow scrolling.
            
            if (Centring) TargetPointX = targetCharacter.x;
            else if (ScrollingAllowedX && TargetScreenX < (EdgeX) && TargetVelocityX <= 0) TargetPointX = targetCharacter.x; // Scroll towards the player when they are at the edge of the screen
            else if (ScrollingAllowedX && TargetScreenX > (ScreenWidth-EdgeX) && TargetVelocityX >= 0) TargetPointX = targetCharacter.x; 
            else if (ScrollingAllowedX && TargetScreenX < (EdgeX) && TargetVelocityX > 0) TargetPointX = GetViewportX() + HalfScreenWidth - 6;  //Except if they change direction
            else if (ScrollingAllowedX && TargetScreenX > (ScreenWidth-EdgeX) && TargetVelocityX < 0) TargetPointX = GetViewportX() + HalfScreenWidth + 6;
            else if (ScrollSpeedX < 0.0 && TargetVelocityX > 0) TargetPointX = GetViewportX() + HalfScreenWidth - 6;
            else if (ScrollSpeedX > 0.0 && TargetVelocityX < 0) TargetPointX = GetViewportX() + HalfScreenWidth + 6;

            if (TargetPointX < HalfScreenWidth) ScrollOffsetX = IntToFloat(HalfScreenWidth) - ScreenCentreX;
            else if ((Room.Width - TargetPointX)<HalfScreenWidth) ScrollOffsetX = IntToFloat(Room.Width-HalfScreenWidth) - ScreenCentreX;
            else ScrollOffsetX = IntToFloat(TargetPointX) - ScreenCentreX;
            
            if (ScrollOffsetX > -slowDownRangeX && ScrollOffsetX < slowDownRangeX) // If the scrollOffset is within the slowDownRange...
            {
               goalScrollSpeedX = (ScrollOffsetX/slowDownRangeX)*goalScrollSpeedX; // ...then shrink the targetScrollSpeed to slow the scrolling.
            }
            else	if (ScrollOffsetX < 0.0) // If the scrollOffset is negative...
            {
              goalScrollSpeedX = -1.0 * goalScrollSpeedX; // ...make the targetScrollSpeed negative.
            }
            
            ScrollSpeedX += timeStep*(goalScrollSpeedX - ScrollSpeedX); // Increase/decrease the scrollSpeed dependent upon the targetScrollSpeed.
            

            if (Centring == false && ScrollSpeedX < 0.7 && ScrollSpeedX > -0.7) ScrollSpeedX = 0.0;
            
            
            ScreenCentreX += (timeStep*ScrollSpeedX);
            
            //Vertical Scrolling:
            
            if ((TargetScreenY - GetTargetHeight() < (EdgeY)) || (TargetScreenY > (ScreenHeight-EdgeY))) ScrollingAllowedY = true; // If the player is within the top or bottom 30% of the screen, allow scrolling.
            else ScrollingAllowedY = false;
            
            if (Centring) TargetPointY = GetTargetMiddleY();
            else if (ScrollingAllowedY && TargetScreenY - GetTargetHeight() < (EdgeY) && TargetVelocityY <= 0) TargetPointY = GetTargetMiddleY(); // Scroll towards the player when they are at the edge of the screen
            else if (ScrollingAllowedY && TargetScreenY > (ScreenHeight-EdgeY) && TargetVelocityY >= 0) TargetPointY = GetTargetMiddleY(); 
            else if (ScrollingAllowedY && TargetScreenY - GetTargetHeight() < (EdgeY) && TargetVelocityY > 0) TargetPointY = GetViewportY() + HalfScreenHeight - 6;  //Except if they change direction
            else if (ScrollingAllowedY && TargetScreenY > (ScreenHeight-EdgeY) && TargetVelocityY < 0) TargetPointY = GetViewportY() + HalfScreenHeight + 6;
            else if (ScrollSpeedY < 0.0 && TargetVelocityY > 0) TargetPointY = GetViewportY() + HalfScreenHeight - 6;
            else if (ScrollSpeedY > 0.0 && TargetVelocityY < 0) TargetPointY = GetViewportY() + HalfScreenHeight + 6;


            if (TargetPointY < HalfScreenHeight) ScrollOffsetY = IntToFloat(HalfScreenHeight) - ScreenCentreY;
            else if (Room.Height - TargetPointY<HalfScreenHeight) ScrollOffsetY = IntToFloat(Room.Height-HalfScreenHeight) - ScreenCentreY;
            else ScrollOffsetY = IntToFloat(TargetPointY) - ScreenCentreY;

            if (ScrollOffsetY > -slowDownRangeY && ScrollOffsetY < slowDownRangeY) // If the scrollOffset is within the slowDownRange...
            {
               goalScrollSpeedY = (ScrollOffsetY/slowDownRangeY)*goalScrollSpeedY; // ...then shrink the targetScrollSpeed to slow the scrolling.
            }
            else	if (ScrollOffsetY < 0.0) // If the scrollOffset is negative...
            {
              goalScrollSpeedY = -1.0 * goalScrollSpeedY; // ...make the targetScrollSpeed negative.
            }
            
            ScrollSpeedY += timeStep * (goalScrollSpeedY - ScrollSpeedY); // Increase/decrease the scrollSpeed dependent upon the targetScrollSpeed.
                        
            if (Centring == false && ScrollSpeedY < 0.7 && ScrollSpeedY > -0.7) ScrollSpeedY = 0.0; // Comment Out this line for very slow scroll speeds (doesn't look very good).
            
            ScreenCentreY += (timeStep * ScrollSpeedY); // Increase/decrease the screenCentre by a fraction of the scrollSpeed.
                        
            //Do Scrolling:
            
            SetViewport(FloatToInt(ScreenCentreX, eRoundDown) - HalfScreenWidth, FloatToInt(ScreenCentreY, eRoundDown) - HalfScreenHeight); // Set the Viewport position relative to ScreenCentreX and Y.  

        }
    }
        
    else {
      ScreenCentreX = IntToFloat(targetCharacter.x);
      ScreenCentreY = IntToFloat(GetTargetMiddleY());
      ScrollSpeedX = 0.0;
      ScrollSpeedY = 0.0;
      ScrollOffsetX = 0.0;
      ScrollOffsetY = 0.0;
    }

    if (SmoothScroll_PxStatus){
    
    PositionObj();
    
    }	
      
  }

}

Khris

Let's forget smooth scrolling and the module for now and focus on mouse scrolling:

One problem here is that the mouse doesn't move in relation to the room but the screen. When the player character walks somewhere, there's going to be a specific target location for the viewport as well, namely to the left of and above the player, so they're centered again.
Moving the mouse works differently, because when the screen scrolls, the mouse doesn't move back to the center, like a character would.

One way to solve this is to scroll the screen when the mouse is close to one of the edges. Another option is to use the vector from screen center to mouse to determine direction and speed of the scroll. This method would actually have to move the mouse back towards the screen's center. It's going to work like looking around in a shooter.
The third option is to map screen coords to room coords directly.

Every method requires different code.

Vault15

Quote from: Khris on Fri 05/04/2013 10:36:57
One problem here is that the mouse doesn't move in relation to the room but the screen. When the player character walks somewhere, there's going to be a specific target location for the viewport as well, namely to the left of and above the player, so they're centered again.
Moving the mouse works differently, because when the screen scrolls, the mouse doesn't move back to the center, like a character would.

One way to solve this is to scroll the screen when the mouse is close to one of the edges. Another option is to use the vector from screen center to mouse to determine direction and speed of the scroll. This method would actually have to move the mouse back towards the screen's center. It's going to work like looking around in a shooter.
The third option is to map screen coords to room coords directly.

Alright, these are some interesting suggestions I'll keep in mind for a future project. Now that I see that it will likely not have a clean, smooth scrolling effect (e.g., scrolling around in Starcraft), I'll go ahead and scrap my mouse-scrolling idea. I'm not giving up, I just know that I'll be making a fool of myself even further  ;).

I have some great news though! Apparently the tearing effect with the scrolling module is solved. All I had to do is change it from DirectX9 to DirectDraw mode. It was worrying me since it started to hurt my eyes. Anyway, I really owe Ali for the brilliant module since my game would not be nearly as pleasant to play. Meanwhile I'll be working on the combat system and will try not to ask any stupid questions when I run into snags  :P.

I'd love to see what you think of a Demo in about a year or so. Thanks so much for helping with my inventory issues awhile back. Everything is much more polished now.

SMF spam blocked by CleanTalk