mardi 9 novembre 2021

Trying to figure out how to realise formations for a game

First of all English is not my first language, so sorry if something isn't understandable or clear, just let me know!

I hope this is the right place to ask this question, if not please direct me to a better place.

I'm working on a little game in my freetime. Nothing serious, at least not as of now. I'm just using it to improve my coding skills :) I want to make a little 2D topdown RTS with larger groups of units which you can move around in formation. And the goal of this question is to get a bit of a direction, a bit of help as I'm stuck right now and am not sure of how to solve this. I'm not asking for code, but maybe small code examples or ressources where I can find out more would be appreciated :)

For anyone interested, I'm working in the GameMaker Studio 2 engine with the GML Language.

As a reference to what I mean you can look at the Total War game series. Good example would be this video. As you can see in the example, he selected some units and was able to choose the starting point and then dragged the mouse to rotate the formation and make it wider. That's pretty much what I want. I want nothing too complicated. Just simple dragging and having

What I got now

I'm able to have one group selected and put that into a simple formation. But it's locked on the X-Axis. It has also a fixed minWidth and a maxWidth. Here you can see how it looks right now. Please focus only on the Formation itself, ignore the rest.

What I'm struggling with - Problem 1

I'm struggling with mainly two things.

The rotation. My system right now works like the following: Every group of units (those little, colored squares) has a single manager. This manager has an array with all the different units. We then take the distance (distance from mouse click, the start of the formation, to where the mouse is right now) on the X-Axis and calculate how many units fit in this width (how many columns) and clamping that to the min and max amounts. So for example a group of 3 units has a min width of 1 unit and a max width of 2 units (so 1 is behind them in a second row). Then we go through the cols and rows in 2 for loops and yeah i think the rest is clear.

So the first problem would be, how do i change it so that it rotates with the mouse? So I take the distance between starting point and mouse and the rotation, but how do i set the coordinates for each unit in the for loops? Right now it's just a simple x = i*(unitWidth + spacing) in the for loop.

What I'm struggling with - Problem 2

So the second problem would be the "How do I have multiple groups in one formation?". I'm able to calculate the min and max distance of each unit in pixels. With that I can take all of the groups, adding the spacing between the groups and calculating the total width the whole formation would take if every group has there min or the max formation. I then simply set the distance between starting point and mouse to the min or max width (depending on what is closer) and every group then got there specific min or max width in the group and just offset every following group.

This works, but I want it to be dynamic. So I want to get the distance and calculate what group can have how much width in there and distribute that correctly.

Thanks to everyone in advance, I hope it was clear enough. If not, please tell me and I'll try to elaborate better :D

Aucun commentaire:

Enregistrer un commentaire