Author Topic: Game Map - Best Approach  (Read 4084 times)

Game Map - Best Approach
« on: 27 Aug 2017, 17:12 »
Hey,

I've been recently playing around with a map screen for my game.
The main issue is that the desired map is much bigger then original game size (Game Resolution: 384x216, Map: 975x618) and because of that I struggle to decide on the best approach to set the whole thing up.

Brief idea behind the map's functionality is that you should be able to pan it around. The character does not directly move around the map, but rather it has various locations on top, which you can click to access info about it. A label of some sort would be in place to show the location's name.


I looked around on forums, and it seems that there are three possible approaches, but all with some disadvantages. I wonder if I'm missing anything, or if I just need to stick to one of them and accept certain quirks.

Map as a GUI - seems like the ideal approach for my needs. I can add locations as buttons. I can add lists for location names. Clicking on button can trigger related the stuff. The issue? As you may already figured it out, a GUI cannot be bigger than the game's size. Using this approach gives me a warning when playing the game, but other than that everything works as intended. I guess just ignoring the warning is not acceptable?

Map as a separate Room - at glance, this could be a good alternative for the GUI map. I can pan around with SetViewport(), locations could be in a form of hotspots/objects - I'm not entirely sure about the labels though. The main issue here seems to be more of a visual one - having the map in a separate room means that I will need to have the fade in/out effect when accessing the map. The way all of the GUI in the game is meant to work is that it has this "flow". When you switch between various menus, they slide from one to another, and having this one menu to have to fade in/out is just very out of place.

DynamicSprite Approach - from what I found on the forums, it seems to be a good idea to create a DynamicSprite for the map. From what I can understand you create a DynamicSprite, then create a copy and crop it. I imagine that you can later pan it around achieving the desired effect. However, it seems to me that adding locations or UI info (e.g. label with name) to the map would be a huge hassle. I guess you could create another DynamicSprites for each location on top of the map one, but to declare x,y positions for each location seems like a nightmare, where I can't easily see how the location presents itself on the map.



As already mentioned above, GUI approach would be my favourite, where I can easily move the buildings (buttons) on the map to make sure they look just fine. I can easily add labels, and trigger anything I may need when the building is clicked. I was wondering if there would be some kind of way to perhaps crop the GUI so it does't give me the warning message? So basically the GUI is larger than screen in the editor, but when you start the game the GUI size is cropped down and you can pan around when dragging.

Are there any other ways to deal with the maps in AGS that I missed? Perhaps there's something I missed about the any of the above approaches?
Any suggestions or pointers in the right directions are welcome!
« Last Edit: 27 Aug 2017, 17:16 by bulka_tarta »

Re: Game Map - Best Approach
« Reply #1 on: 27 Aug 2017, 18:42 »
You can combine (1) and (3) by setting the DynamicSprite as GUI background. That way you can slide it in like your other GUIs but still pan around.

You can still use buttons for the locations, you just need to move or hide them, depending on where the current map offset is. They wouldn't even have to pop in and out as the player scrolls across the map; buttons can be resized dynamically, and you can also use cropped DynamicSprites for the button images.

As for (2), the fading can be disabled (once) using SetNextScreenTransition(eTransitionInstant); you'd simply call that right before switching to the map room, and again before switching back.

One approach I like is to map the screen to the entire map, as in, the mouse coordinates get converted into offset coordinates so that moving the mouse to the top left of the screen will move the map so its top left corner is visible; and moving the mouse to the bottom right of the screen will move the map so its bottom right is visible.

Scorpiorus

  • 100101101010b
    • Scorpiorus worked on one or more games that won an AGS Award!
    •  
    • Scorpiorus worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #2 on: 27 Aug 2017, 19:12 »
Also, with a map-GUI you don't lose the current room context, so to say.

For example, if the player interacts with something in the current room, you can just have your map-GUI reflecting it by simply enabling a corresponding GUI control element (like a button or a label) immediately. With a separate room for the map, you cannot update it instantly and you have to set up some global flags or something to apply them afterwards when the map room is actually loaded.

On the other hand, a room is a good choice if you want your player character to actually walk around the map, respecting walkable areas, interacting with other characters and objects there and so on. You cannot use AGS pathfinder/walkables with the GUI-as-map approach.

Another good thing with rooms -- for games with a higher screen resolution (say, 640x480 and greater), you can have a smooth map-room scrolling by using an invisible current player character moving in the direction, you want your map viewport to scroll in.

Slasher

  • .
    • I can help with AGS tutoring
    • I can help with scripting
    • I can help with story design
    • Lifetime Achievement Award Winner
    • Slasher worked on one or more games that won an AGS Award!
    •  
    • Slasher worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #3 on: 27 Aug 2017, 19:23 »
Quote
Another good thing with rooms -- for games with a higher screen resolution (say, 640x480 and greater), you can have a smooth map-room scrolling by using an invisible current player character moving in the direction, you want your map viewport to scroll in.
I used this method for "Splinter"...

Monsieur OUXX

  • Mittens Vassal
  • Cavefish
  • Mittens Half Initiate
    • I can help with proof reading
    • I can help with translating
    • I can help with voice acting
    • Monsieur OUXX worked on one or more games that won an AGS Award!
    •  
    • Monsieur OUXX worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #4 on: 27 Aug 2017, 20:45 »
the "map as separate room" technique is the one that requires the least coding and thinking. Locations can be hotspots (with a Region to trigger a small character walking on the map exiting the map if needed). And you can disable the fade In/fade out by changing this general setting in your code just before calling the map.


+1 for smooth scrolling by combining invisible character+smooth scrolling module.
 

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #5 on: 27 Aug 2017, 22:08 »
You can combine (1) and (3) by setting the DynamicSprite as GUI background. That way you can slide it in like your other GUIs but still pan around.

You can still use buttons for the locations, you just need to move or hide them, depending on where the current map offset is. They wouldn't even have to pop in and out as the player scrolls across the map; buttons can be resized dynamically, and you can also use cropped DynamicSprites for the button images.

Knowing a bit about your game, this would be my recommendation. And in AGS 3.4 you have unlimited GUI controls per GUI, so you can just have a fixed button for each location on the map (rather than having max 30 buttons that you have to constantly reassign to whichever locations are currently on-screen), which makes it a whole lot easier. The trickiest bit might be labels that are partially off-screen: I don't know off the top of my head how AGS deals with that. (I think having them spill over to the right or on the bottom is fine, but positioning them so that they're off on top or on the left might be harder.)

Using a dynamic sprite on a GUI, you could even zoom the map (e.g. with the scroll wheel).

Re: Game Map - Best Approach
« Reply #6 on: 29 Aug 2017, 21:49 »
Thank you all for the replies.

I was worried that DynamicSprite + GUI would be the best solution for what I need, because DynamicSprites are... terrifying.

It seems that even something as simple as panning is fairly tricky to do, as DynamicSprites don't have .X/.Y property (unless I'm wrong).
Originally I thought I could use this piece of code and modify it to work with DynamicSprites.

Closest I got to panning the map is this:
Code: Adventure Game Studio
  1. DynamicSprite* mapSprite;
  2. DynamicSprite* sprite;
  3. DrawingSurface* surface;
  4.  
  5. void SetMap()
  6. {  
  7.   mapSprite = DynamicSprite.Create(384, 216);
  8.  
  9.   gMapDyna.BackgroundGraphic = mapSprite.Graphic;
  10.  
  11.   sprite = DynamicSprite.CreateFromExistingSprite(80);
  12.   sprite.Crop(0, 0, 384, 216);
  13.  
  14.   surface = mapSprite.GetDrawingSurface();
  15.   surface.DrawImage(0, 0, sprite.Graphic);
  16.   surface.Release();
  17. }
  18.  
  19. bool map_is_dragged;
  20. int xa, ya;
  21.  
  22. //Drag the map
  23. void on_event (EventType event, int data) {
  24.   if (event == eEventGUIMouseDown && mouse.IsButtonDown(eMouseLeft)) {
  25.       map_is_dragged = true;
  26.     }
  27.   }
  28.  
  29. void repeatedly_execute() {
  30.   if (map_is_dragged) {
  31.     if (!mouse.IsButtonDown(eMouseLeft)) map_is_dragged = false;
  32.     else {
  33.       xa = mouse.x;
  34.       ya = mouse.y;
  35.      
  36.       sprite = DynamicSprite.CreateFromExistingSprite(80);
  37.       sprite.Crop(xa, ya, 384, 216);
  38.      
  39.       surface = mapSprite.GetDrawingSurface();
  40.       surface.DrawImage(0, 0, sprite.Graphic);
  41.       surface.Release();
  42.     }
  43.   }
  44. }
  45.  
Which as you may guess, does not provide desired results.

What happens is that the map follows the mouse in a weird fashion. If I understand correctly, to "move" a DynamicSprite you need to delete it and draw again at a new position. The code above doesn't actually delete anything, because calling sprite.Delete(); does nothing and mapSprite.Delete(); simply gets rid of the map when clicked.

Currently I'm wondering if I need to somehow calculate the map's position using the DynamicSprite.Width/Height properties... But I can't see how that would help. Is there anyway to simply detect DynamicSprite's X and Y values? Surely there isn't anything about it in the manual.

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #7 on: 29 Aug 2017, 22:18 »
It seems that even something as simple as panning is fairly tricky to do, as DynamicSprites don't have .X/.Y property (unless I'm wrong).

Sprites don't have x/y properties because you can draw them anywhere on screen. Or, to put it another way, their x/y properties will be the same as the GUI they're being rendered on.

There are two challenges here: 1. Figure out how to get mouse-dragging to work. 2. Render the right section of the map to the GUI background using dynamic sprites.

For the first part, I would do this:

-When user presses button:
-a. Store the map offset (the map coordinates of the top-left corner of the GUI)
-b. Store the mouse coordinates (simply mouse.x, mouse.y)

-While button is pressed, in repeatedly_execute()
-a. Calculate the difference between the current mouse coordinates and the stored mouse coordinates.
-b. If the difference is not zero:
--i. add/subtract that difference to/from the stored map offset – but with opposite sign (if the mouse moves down-right, the top-left corner of the GUI moves up-left on the map)
--ii. Update the stored mouse coordinates
--iii. Re-render the map

I'll leave the second part, the rendering, for now.
« Last Edit: 29 Aug 2017, 22:20 by Snarky »

Re: Game Map - Best Approach
« Reply #8 on: 30 Aug 2017, 20:46 »
Sprites don't have x/y properties because you can draw them anywhere on screen. Or, to put it another way, their x/y properties will be the same as the GUI they're being rendered on.

I guess that makes sense, but the reason I tried to figure out the sprite's position is that it seems the GUI's position is useless, since I don't want to drag the GUI, but rather the crop of the map. The GUI should always stay at 0,0 but the crop needs to change and if the GUI is at 0,0 how can you determine the cropped area?

Regarding the "Figure out how to get mouse-dragging to work":
I combined the code from dragging the GUI with Snarky's directions. You can now drag the map, but it's not ideal.

1) It leaves a trail of the map behind (basically the sprites duplicates so many times it creates a pattern)
2) The map jumps back to original position when you unclick and click again (not exactly original position, I guess it's based on the place where you click the second time).


Here's the code:
Code: Adventure Game Studio
  1. bool map_is_dragged;
  2. GUI*dragged_map;
  3. int ox, oy; // map offest x/y
  4. int sx, sy; // stored mouse x/y
  5. int dx, dy; // difference x/y
  6. int currentX, currentY;
  7.  
  8. //When user presses button
  9. void on_event (EventType event, int data) {
  10.   if (event == eEventGUIMouseDown && mouse.IsButtonDown(eMouseLeft)) {
  11.     GUIControl* gc = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  12.     GUI* g = GUI.GetAtScreenXY(mouse.x, mouse.y);
  13.     if (g == gMapDyna) {
  14.       //Store the map offset
  15.       ox = g.X;
  16.       oy = g.Y;
  17.      
  18.       //Store the mouse coordinates
  19.       sx = mouse.x;
  20.       sy = mouse.y;
  21.      
  22.       map_is_dragged = true;
  23.     }
  24.   }
  25. }
  26.  
  27. //While button is pressed
  28. void repeatedly_execute() {
  29.   if (map_is_dragged) {
  30.     if (!mouse.IsButtonDown(eMouseLeft)) map_is_dragged = false;
  31.     else {
  32.       //Calculate difference between current and stored mouse coordinates
  33.       dx = mouse.x - sx;
  34.       dy = mouse.y - sy;
  35.      
  36.       //If difference is not zero
  37.       if ((dx != 0)&&(dy != 0))
  38.       {
  39.         //Add/subtract difference from the stored map offset
  40.         ox =  ox - dx;
  41.         oy =  oy - dy;
  42.        
  43.         //Update the stored mouse coordinates
  44.         sx = mouse.x;
  45.         sy = mouse.y;
  46.       }
  47.      
  48.       //Re-render the map
  49.       sprite = DynamicSprite.CreateFromExistingSprite(80);
  50.       sprite.Crop(ox, oy, 384, 216);
  51.      
  52.       surface = mapSprite.GetDrawingSurface();
  53.       surface.DrawImage(0, 0, sprite.Graphic);
  54.       surface.Release();
  55.       sprite.Delete();
  56.     }
  57.   }
  58. }
  59.  


Btw, I know the names of the variables are not so great, I realise that's something I need to work on!

Scorpiorus

  • 100101101010b
    • Scorpiorus worked on one or more games that won an AGS Award!
    •  
    • Scorpiorus worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #9 on: 31 Aug 2017, 22:14 »
I think with dynamic sprites approach you have to redraw the whole composition from scratch each time the map is moved or animated.
You can probably prepare a huge map sprite and show a portion of it through a map viewport by repeatedly creating an instant cropped map-viewport-area-sized sprite from the map sprite, but it won't allow animating things on the map then.

However, wouldn't simply moving/translating all GUI controls (buttons) around give you a similar effect?

Basic code in the GlobalScript:
Code: Adventure Game Studio
  1. int mouse_px = -1; // previous mouse x
  2. int mouse_py = -1; // previous mouse y
  3.  
  4. void repeatedly_execute( )
  5. {
  6.     if (mouse.IsButtonDown(eMouseLeft) && (mouse_px!=-1) && (mouse_py!=-1))
  7.     {
  8.         int dx = mouse.x - mouse_px;
  9.         int dy = mouse.y - mouse_py;
  10.         for (int i=0; i < gMapDyna.ControlCount; i++)
  11.         {
  12.             gMapDyna.Controls[i].X += dx;
  13.             gMapDyna.Controls[i].Y += dy;
  14.         }
  15.     }
  16.    
  17.     mouse_px = mouse.x;
  18.     mouse_py = mouse.y;
  19. }

The good thing with buttons is that they are already interact-able.
Also, try using a large button as your map background.

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #10 on: 01 Sep 2017, 06:41 »
However, wouldn't simply moving/translating all GUI controls (buttons) around give you a similar effect?

No? This will only move the buttons, not the background. Of course you have to do that too, but that's the simple part Once the rest is working. And what happens with buttons that are outside the GUI area? Particularly if the GUI doesn't cover the entire screen? I seem to remember that AGS is pretty sloppy about it, so that they may be drawn outside of the GUI bounds.

Also, try using a large button as your map background.

How does that help?

Scorpiorus

  • 100101101010b
    • Scorpiorus worked on one or more games that won an AGS Award!
    •  
    • Scorpiorus worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #11 on: 01 Sep 2017, 07:03 »

eri0o

Re: Game Map - Best Approach
« Reply #12 on: 01 Sep 2017, 23:45 »

Re: Game Map - Best Approach
« Reply #13 on: 02 Sep 2017, 00:37 »
And what happens with buttons that are outside the GUI area? Particularly if the GUI doesn't cover the entire screen? I seem to remember that AGS is pretty sloppy about it, so that they may be drawn outside of the GUI bounds.

GUIs are rendered as a single ("flattened") bitmap, of the size of main gui, which technically prevents anything that is positioned outside of gui's bounds to be shown.

(This was probably easiest method at times of software-only renderer, because it is counter-productive in regards to hardware acceleration)
« Last Edit: 02 Sep 2017, 00:41 by Crimson Wizard »

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #14 on: 02 Sep 2017, 11:32 »
https://files.fm/u/ja3s39yu

You're right, using a button as the background works. I didn't realize that buttons are allowed to be bigger than the GUI they're on, and that they get correctly cropped to the GUI dimensions (both graphically and in terms of interactivity).

Scorpiorus

  • 100101101010b
    • Scorpiorus worked on one or more games that won an AGS Award!
    •  
    • Scorpiorus worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #15 on: 02 Sep 2017, 15:02 »
Yeah, surprisingly it works pretty well, even if implemented with basic scripting.
And it's a good starting point to add extra functionality like, for instance, a map scalling/zooming feature, as you suggest.

The GUI editing may not always be convenient, depending on the desktop resolution, since we don't have scrollbars in the GUI editor. But overall, it's quite manageable, I think.
And the "background" button can always be Locked, so that we don't accidentally move it, adjusting other buttons/labels, etc...

It may actually be a good candidate for a script-module/template/exported-GUI thing.
I see if I can pull out a script module or something out of that quick mock-up I made...


EDIT:
Some time ago I worked on a script equivalent to the snow/rain plugin to assist portability and it was one of the main problems in terms of how render snow flakes at different screen depth levels. Probably, GUI controls may work well as a complementary solution to other standard methods. It needs further testing, though...
« Last Edit: 02 Sep 2017, 15:14 by Scorpiorus »

Re: Game Map - Best Approach
« Reply #16 on: 03 Sep 2017, 15:03 »
Setting up the map as a button seems to work pretty well!

I started playing around with it, and I have managed to make the map stop at the edges of the screen (the map in my game is fullscreen, rather than a section of it).

Code: Adventure Game Studio
  1. int mouse_px = -1; // previous mouse x
  2. int mouse_py = -1; // previous mouse y
  3.  
  4. void repeatedly_execute( )
  5. {
  6.   if (mouse.IsButtonDown(eMouseLeft) && (mouse_px!=-1) && (mouse_py!=-1))  
  7.   {
  8.     int dx = mouse.x - mouse_px;
  9.     int dy = mouse.y - mouse_py;
  10.      
  11.     for (int i=0; i < gMap.ControlCount; i++)
  12.     {
  13.       gMap.Controls[i].X += dx;
  14.       gMap.Controls[i].Y += dy;
  15.     }
  16.      
  17.     // Check X
  18.     if (gMap.Controls[0].X > gMap.Width - System.ViewportWidth) gMap.Controls[0].X = System.ViewportWidth - gMap.Width; //gMap.Controls[0] is the button that has the map background
  19.     if (gMap.Controls[0].X < (gMap.X - (gMap.Controls[0].Width - System.ViewportWidth))) gMap.Controls[0].X = gMap.X - (gMap.Controls[0].Width - System.ViewportWidth);
  20.      
  21.     // Check Y
  22.     if (gMap.Controls[0].Y > gMap.Height - System.ViewportHeight) gMap.Controls[0].Y = System.ViewportHeight - gMap.Height;
  23.     if (gMap.Controls[0].Y < (gMap.Y - (gMap.Controls[0].Height - System.ViewportHeight))) gMap.Controls[0].Y = gMap.Y - (gMap.Controls[0].Height - System.ViewportHeight);
  24.   }
  25.  
  26.   mouse_px = mouse.x;
  27.   mouse_py = mouse.y;
  28. }
  29.  

The problem I'm encountering now is that the other buttons (so buildings) keep sliding after the map background reaches any of the edge.
I tried setting up some kind of bool to stop it from scrolling when you meet the edge, or storing last positions for the buildings, but I can't get it to work properly.
Any idea how to stop the buttons from moving when the map reaches the edge?


EDIT: Wait, hang on, don't reply with solution! I think I got this!

EDIT2: Ok, it's getting somewhere. I now started storing the position of each button, which later is applied. The problem is that if you drag the map real fast, the buildings move by couple of pixel. If I understand correctly, the game doesn't call all the script fast enough, so I imagine I would need to speed up the game (never did this thought, so I'm not sure if that would help here at all?)

Code: Adventure Game Studio
  1. int mouse_px = -1; // previous mouse x
  2. int mouse_py = -1; // previous mouse y
  3. int btnX[10];
  4. int btnY[10];
  5.  
  6. void repeatedly_execute( )
  7. {
  8.     if (mouse.IsButtonDown(eMouseLeft) && (mouse_px!=-1) && (mouse_py!=-1))
  9.     {
  10.       // Store button locations
  11.       for (int i=1; i < gMap.ControlCount; i++)
  12.       {
  13.         btnX[i] = gMap.Controls[i].X;
  14.         btnY[i] = gMap.Controls[i].Y;
  15.       }
  16.      
  17.       int dx = mouse.x - mouse_px;
  18.       int dy = mouse.y - mouse_py;
  19.      
  20.       for (int i=0; i < gMap.ControlCount; i++)
  21.       {
  22.         gMap.Controls[i].X += dx;
  23.         gMap.Controls[i].Y += dy;
  24.       }
  25.  
  26.  
  27.       // Check X
  28.       if (gMap.Controls[0].X > gMap.Width - System.ViewportWidth)
  29.       {
  30.         gMap.Controls[0].X = System.ViewportWidth - gMap.Width;
  31.        
  32.         for (int i=1; i < gMap.ControlCount; i++)
  33.         {
  34.           gMap.Controls[i].X = btnX[i];
  35.         }
  36.       }
  37.       if (gMap.Controls[0].X < (gMap.X - (gMap.Controls[0].Width - System.ViewportWidth)))
  38.       {
  39.         gMap.Controls[0].X = gMap.X - (gMap.Controls[0].Width - System.ViewportWidth);
  40.        
  41.         for (int i=1; i < gMap.ControlCount; i++)
  42.         {
  43.           gMap.Controls[i].X = btnX[i];
  44.         }
  45.       }
  46.      
  47.      
  48.       // Check Y
  49.       if (gMap.Controls[0].Y > gMap.Height - System.ViewportHeight)
  50.       {
  51.         gMap.Controls[0].Y = System.ViewportHeight - gMap.Height;
  52.         for (int i=1; i < gMap.ControlCount; i++)
  53.         {
  54.           gMap.Controls[i].Y = btnY[i];
  55.         }
  56.       }
  57.      
  58.       if (gMap.Controls[0].Y < (gMap.Y - (gMap.Controls[0].Height - System.ViewportHeight)))
  59.       {
  60.         gMap.Controls[0].Y = gMap.Y - (gMap.Controls[0].Height - System.ViewportHeight);
  61.         for (int i=1; i < gMap.ControlCount; i++)
  62.         {
  63.           gMap.Controls[i].Y = btnY[i];
  64.         }
  65.       }
  66.     }
  67.  
  68.     mouse_px = mouse.x;
  69.     mouse_py = mouse.y;
  70. }
  71.  
« Last Edit: 03 Sep 2017, 16:19 by bulka_tarta »

Snarky

  • Global Moderator
  • Global Moderator
  • Mittens Lord
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #17 on: 03 Sep 2017, 17:37 »
The answer is to check whether you're off the edge before moving any of the controls, and change dx and dy so that you stay inside.

Scorpiorus

  • 100101101010b
    • Scorpiorus worked on one or more games that won an AGS Award!
    •  
    • Scorpiorus worked on one or more games that was nominated for an AGS Award!
Re: Game Map - Best Approach
« Reply #18 on: 03 Sep 2017, 18:21 »
And here is an example of the script code to help you managing your map:

ScriptModule Header:
Code: Adventure Game Studio
  1. #define Map_GUI              gMap     // Map GUI
  2. #define Map_BKG_BUTTON_ID    0        // ID of the "background" button of the Map GUI
  3.  
  4. #define Map_MARGIN_TOP       (-10)    // top margin
  5. #define Map_MARGIN_BOTTOM    (-10)    // bottom margin
  6. #define Map_MARGIN_LEFT      (-10)    // left margin
  7. #define Map_MARGIN_RIGHT     (-10)    // right margin
  8.  
  9. struct Map
  10. {
  11.     import static int GetWidth();
  12.     import static int GetHeight();    
  13.     import static int GetViewportX();
  14.     import static int GetViewportY();
  15.     import static int GetViewportWidth();
  16.     import static int GetViewportHeight();
  17.     import static void SetViewport(int newX, int newY);
  18. };

ScriptModule Script:
Code: Adventure Game Studio
  1. static int Map::GetWidth()
  2. {
  3.     return Map_GUI.Controls[Map_BKG_BUTTON_ID].Width;
  4. }
  5.  
  6. static int Map::GetHeight()
  7. {
  8.     return Map_GUI.Controls[Map_BKG_BUTTON_ID].Height;
  9. }
  10.  
  11. static int Map::GetViewportX()
  12. {
  13.     return -Map_GUI.Controls[Map_BKG_BUTTON_ID].X;
  14. }
  15.  
  16. static int Map::GetViewportY()
  17. {
  18.     return -Map_GUI.Controls[Map_BKG_BUTTON_ID].Y;
  19. }
  20.  
  21. static int Map::GetViewportWidth()
  22. {
  23.     return Map_GUI.Width;
  24. }
  25.  
  26. static int Map::GetViewportHeight()
  27. {
  28.     return Map_GUI.Height;
  29. }
  30.  
  31. static void Map::SetViewport(int newX, int newY)
  32. {
  33.     // just a shortcut for Map_BKG_BUTTON_ID button
  34.     readonly GUIControl *BKG = Map_GUI.Controls[Map_BKG_BUTTON_ID];
  35.    
  36.     // remembering current viewport position
  37.     int x0 = Map.GetViewportX();
  38.     int y0 = Map.GetViewportY();
  39.    
  40.     // translating viewport / "background" button
  41.     BKG.X = -newX;
  42.     BKG.Y = -newY;
  43.    
  44.     // calculating viewport position constraints
  45.     readonly int MIN_X = Map_MARGIN_LEFT;
  46.     readonly int MIN_Y = Map_MARGIN_TOP;
  47.     readonly int MAX_X = Map.GetWidth() - Map.GetViewportWidth() - Map_MARGIN_RIGHT;
  48.     readonly int MAX_Y = Map.GetHeight() - Map.GetViewportHeight() - Map_MARGIN_BOTTOM;
  49.  
  50.     // applying viewport position constraints
  51.     if (Map.GetViewportX() < MIN_X) // same as if (BKG.X > -MIN_X)
  52.         BKG.X = -MIN_X;
  53.     if (Map.GetViewportX() > MAX_X) // same as if (BKG.X < -MAX_X)
  54.         BKG.X = -MAX_X;
  55.     if (Map.GetViewportY() < MIN_Y) // same as if (BKG.Y > -MIN_Y)
  56.         BKG.Y = -MIN_Y;
  57.     if (Map.GetViewportY() > MAX_Y) // same as if (BKG.Y < -MAX_Y)
  58.         BKG.Y = -MAX_Y;
  59.    
  60.     // calculating deltas for other GUI controls
  61.     int dx = x0 - Map.GetViewportX();
  62.     int dy = y0 - Map.GetViewportY();
  63.    
  64.     // translating other GUI controls (ie. all except the "background" button)
  65.     for (int i=0; i < Map_GUI.ControlCount; i++)
  66.     {
  67.         if (i==Map_BKG_BUTTON_ID)
  68.             continue;
  69.         Map_GUI.Controls[i].X += dx;
  70.         Map_GUI.Controls[i].Y += dy;
  71.     }
  72. }

GlobalScript:
Code: Adventure Game Studio
  1. int mouse_px = -1; // previous mouse x
  2. int mouse_py = -1; // previous mouse y
  3.  
  4. function repeatedly_execute( )
  5. {
  6.     if (mouse.IsButtonDown(eMouseLeft) && (mouse_px!=-1) && (mouse_py!=-1))
  7.     {
  8.         readonly float DRAG_SPEED_MULTIPLIER = 1.0;
  9.         int dx = FloatToInt( IntToFloat(mouse.x - mouse_px) * DRAG_SPEED_MULTIPLIER );
  10.         int dy = FloatToInt( IntToFloat(mouse.y - mouse_py) * DRAG_SPEED_MULTIPLIER );
  11.         Map.SetViewport(Map.GetViewportX()-dx, Map.GetViewportY()-dy);
  12.     }
  13.    
  14.     mouse_px = mouse.x;
  15.     mouse_py = mouse.y;
  16. }

You can use Map.XXX functions to control your map viewport position.

Setting the DRAG_SPEED_MULTIPLIER thing to "2.0" will make it twice faster to scroll/pan the map.

These "defines" specify corresponding margins. You may want to set them all to 0, if you like.
Code: Adventure Game Studio
  1. #define Map_MARGIN_TOP       (-10)     // top margin
  2. #define Map_MARGIN_BOTTOM    (-10)     // bottom margin
  3. #define Map_MARGIN_LEFT      (-10)     // left margin
  4. #define Map_MARGIN_RIGHT     (-10)     // right margin

See if it works, and let us know how it goes.


(EDIT: cosmetic changes to SetViewport() to make it easier to read the script code, and made DRAG_SPEED_MULTIPLIER a float)
« Last Edit: 03 Sep 2017, 21:26 by Scorpiorus »

Re: Game Map - Best Approach
« Reply #19 on: 03 Sep 2017, 20:45 »
Wow, this is awesome! The map is working like a charm now!

I made a small change in the repeatedly execute so that the map doesn't scroll when it's not visible:

Code: Adventure Game Studio
  1. int mouse_px = -1; // previous mouse x
  2. int mouse_py = -1; // previous mouse y
  3. GUI* g; // Added GUI pointer
  4.  
  5. function repeatedly_execute( )
  6. {
  7.   g = GUI.GetAtScreenXY(mouse.x, mouse.y); // Check if the you are clicking on the map
  8.     if (mouse.IsButtonDown(eMouseLeft) && (mouse_px!=-1) && (mouse_py!=-1) && (g == gMap)) // Pan around when clicking on the gMap
  9.     {
  10.         readonly int DRAG_SPEED_MULTIPLIER = 1;
  11.         int dx = (mouse.x - mouse_px) * DRAG_SPEED_MULTIPLIER;
  12.         int dy = (mouse.y - mouse_py) * DRAG_SPEED_MULTIPLIER;
  13.         Map.SetViewport(Map.GetViewportX()-dx, Map.GetViewportY()-dy);
  14.     }
  15.    
  16.     mouse_px = mouse.x;
  17.     mouse_py = mouse.y;
  18. }

Thank you so much for this! :-D