What's the best way to manage a grid based game in GS?

Hi everyone, I'm Jim, very much a GS newbie but loving it so far.

So, I've got a query for y'all. My first GS project is going to be a grid based tower defence game, essentially a bit like Plants Vs Zombies, i.e. enemies approach from the right along 1 of 6 horizontal paths, and the player has to fight them off by positioning various weapons and the such like in their path to defeat them.

The whole game area is basically a grid of 10 by 6 squares. By following a few basic tutorials I easily managed to get enemies spawning on a regular basis, randomly along the 6 lanes, that was achieved very easily using a table and some basic maths. I've now started looking at the player's side of things and this is where I'm a bit unsure.

So the player basically selects the type of weapon they want to deploy, then they click/touch on the grid square to place said weapon there. The weapon then detects if an enemy in present on the particular horizontal path it's positioned on, and if there is then it shoots at that enemy. At this stage I'm not worried about the shooting or the collision detection, I'm more interested in the management of the grid system. I started on a little experiment, by using the x,y co-ords of a touch I'm able to ascertain if the player is clicking somewhere within the grid, and I started putting together a string of commands to calculate which square in the grid they'd be in and to make a weapon appear there. Using commands alone it is very labour intensive calculating the correct position to perfectly place a weapon in the grid, so what would be the most efficient way of doing this?

I've seen a couple of complicated tutorials where the dimensions and status of each square in the grid were stored in tables, and I've also seen one where the developer has mapped the entire grid with an actor for each square, presumably each square then has attributes that can be set, such as 'is there a weapon already deployed here?'

Just wondering if anyone has done anything like this previously and can offer some advice, I know using the wrong type of grid management system will mess me up massively when I come to add more advanced features later on.

ta

Jim

Best Answer

  • tatiangtatiang Posts: 11,949
    Accepted Answer

    I started putting together a string of commands to calculate which square in the grid they'd be in and to make a weapon appear there. Using commands alone it is very labour intensive calculating the correct position to perfectly place a weapon in the grid, so what would be the most efficient way of doing this?

    It's hard to know because it sounds like you found a method but it's not clear why it's not efficient.

    I've also seen one where the developer has mapped the entire grid with an actor for each square, presumably each square then has attributes that can be set, such as 'is there a weapon already deployed here?'

    This is how I did it in my Ants vs. Bees tutorial (videos #11 to 19).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Answers

  • ArmellineArmelline Member, PRO Posts: 5,366

    I'm on mobile at the moment but if you look at my signature you'll see a link to my code snippets. Amongst them is a template for spawning squares in the grid location a touch falls in. It's not exactly what you're after but it might give you some ideas.

  • mysticjimmysticjim Member Posts: 30

    @tatiang said:

    It's hard to know because it sounds like you found a method but it's not clear why it's not efficient.

    To clarify why I think my way of doing things is looking inefficient here is some background;

    My whole game screen is made up of 40x40px squares - it's 8 high and 12 wide. The first 2 columns of squares are where the player selects a weapon, and the top and bottom rows are for title and score information, so the actual live playing area is 6 squares by 10.

    Using commands alone the first thing I wanted to ascertain is has the user actually touched the screen within the game area, so I check if the touch occurred >80px in the x co-ord, >40 px in the y and <280px in y. I couldn't find a comand that lets you combine co-ords together, i.e. has the touch occurred between these two points, so I used three 'if' commands - and that works fine.

    so, having figured out that a touch has occurred in the game area then next I need to know exactly which of the 60 possible squares has been touched. Without getting into the business of figuring out whether there was already something deployed on that square already (figured I get to that later) I thought I'd see how many commands it would take to figure out the location in the x and y, then calculate which square that would be.

    I started by putting in some commands to figure out the x position, again I've not been able to find a single command to ask if the touch occurred between a range, so it I added a command to ask if the touch occurred >80px and then another to ask if it occurred <120px. If if did then I know the touch occurred in the first column, then I have a do command to set the x co-ord attribute of where I want my weapon to spawn as exactly 100 (100 is the x co-ord right in the centre of that column.)

    I stopped at that point and considered the following;

    So, 2 commands and a do instruction can calculate if the x co-ord of the touch is in the first column. With 10 squares that means I'd need to repeat that step another 9 times, adjusting the x co-ord range each time in order be able to calculate the correct x co-ord of the touch for whole grid. And then I'd need to do something similar for the y co-ords. I'd be looking at somewhere in the region of 60 odd commands, that surely can't be the most efficient way of doing things? So that's why I asked question!

  • mysticjimmysticjim Member Posts: 30

    I'll have a good look through both of your suggestions and see if I can adapt them to my game and let you know. Thanks for your input :)

  • mysticjimmysticjim Member Posts: 30

    @Armelline said:
    I'm on mobile at the moment but if you look at my signature you'll see a link to my code snippets. Amongst them is a template for spawning squares in the grid location a touch falls in. It's not exactly what you're after but it might give you some ideas.

    Hi, this looks exactly like what I need- but it's for Mac, as you'll note, I'm posting in the GS PC section. Do you have a PC compatible version of that?

  • ArmellineArmelline Member, PRO Posts: 5,366

    You should be able to open the Mac version on PC, but I'm afraid I have no experience doing that. A quick search of the forums should bring up an answer though!

  • mysticjimmysticjim Member Posts: 30

    Hi Armelline, I'll look into it, but for now I think I've managed to mimic the square system as proposed by tatian, although I expect to be putting in another question very soon about collision detection!

Sign In or Register to comment.