1. Hey! Guest! The 33rd GMC Jam will take place between May 23rd, 12:00 UTC (Friday noon) and May 27th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML how to check if next move/field is in range?

Discussion in 'Programming' started by Traversal, Mar 14, 2019.

Tags:
  1. Traversal

    Traversal Member

    Joined:
    May 1, 2017
    Posts:
    63
    Thinking about how to check a players move on a given map.
    Map consists of (to keep this sample simple) 5 X 5 rows = 25 fields

    The player should only be able to move to the next field from his starting field
    (but in all directions) and positioned centered in the new field.
    All 25 fields are objects.

    How would you check what fields are allowed?


    [​IMG]
     
    Last edited: Mar 14, 2019
  2. roozilla

    roozilla Member

    Joined:
    Feb 4, 2017
    Posts:
    102
    Think of your room as a grid, your player object will have an x and y bases on that. You can then add logic in your movement code to prevent out of bounds movement. If your map is smaller than your room then youd need to base it off your width and length of thr map. You can figure that out with some conversions of your corner tile objects x and y values if you have the sprite draw at 0, 0
     
  3. Traversal

    Traversal Member

    Joined:
    May 1, 2017
    Posts:
    63
    Had some hope to find a predefined function to handle it.
    Might now check for a fixed radius and check if the click on that field/object is within the range.
    If so, move the player to that object/field.

    [​IMG]
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,711
    You mentioned "click" so I am going to assume mouse-driven movement, not direction keys. One solution would be to check distance between instance player is standing on and clicked instance is less than distance between centers - that is, length of the sprite's side - times two. Distance to all permitted destinations is going to be 1.0 or 1.41 times that distance.

    (This of course has no provisions for blocked grid cells and moving multiple hops will be problematic, but that wasn't the question asked.)
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,969
    In this case you could simply take the max norm of the vector from your current position to the target position (i.e. max(abs(x-mouse_x), abs(y-mouse_y))), divide it by the cell size, round down and compare it to 1, but that's not the best path forward.

    I would advise mapping that playing field onto a 2D array or grid. Then you can just check adjacent indexes for being in range (i.e. both coordinates between 0 and 4, and not occupied) and that would give you the answer of whether a move is allowed. It would also give easy answers to most other questions your project would need to ask of itself in the foreseeable future.

    Anyone who uses instance placement as the main data model for a grid-based strategy game is doing it wrong. 9 times out of 10 it will struggle with movement, waste most of its time checking collisions and scaling distances, its testing of edge cases will be inadequate, and whatever AI it can muster will suck. And I'm telling you this as someone who "accidentally" developed an AI for a grid-based strategy game that only one or two Jam reviewers out of more than 20 could beat.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice