- Home /
Building a Chessboard
G'day guys.
I'm beginning a Turn-Based project, where 'units' will move on a grid, similar to Chess. I understand the importance of the basic grid system being as efficient as possible, and I've been trying different techniques. I've thought a multidimensional array populated with 'cubes', each being one 'square' on the 'playing board', would be very useful, but I'm not sure how efficient it is.
I've already built a basic 'State Engine', 'Strategy Camera' and Selection system, but the core of a Turn Based Strategy is the playing field.
I'd love to hear people's suggestions for the 'best-practice' way to go about this.
Cheers,
EDIT: To be more specific:
I'm trying to build the system for movement - when a unit is 'selected', the game keeps track of where he is and examines the 'cubes' around him, seeing if they are empty and moving the unit to their x and z positions if within range, etc. A multidimensional array of cubes mirroring a multidimensional array that uses Intergers to mark 'squares' as occupied or vacant is the only approach that occurs to me. I am hoping to find out if there is a better system, perhaps using only 1 multidimensional array for both the cubes and the 'units' positioned above (or 'on top of') them, maybe replacing the cube gameobject in the array for their corresponding row/column.
Just to clarify, I'm not actually making chess. $$anonymous$$y eventual goal is something akin to tabletop warga$$anonymous$$g, albeit much simpler. I'm aware of existing Chess projects, and unfortunately the systems they use aren't useful here.
Answer by testure · Jun 30, 2011 at 06:15 AM
I'm not quite sure what you mean by 'efficient' in this case, because we're talking about hypothetical cubes. that's 12 triangles per grid space if you're keeping the bottom. Even if your grid was insanely large, it would take a lot of tiles to even come close to pushing the limits of a modern PC or even smartphone. Your real performance gains are going to come from combining static meshes and materials to reduce draw calls.
Other than the specific question about efficiency, I don't really see what you're asking for here. Best practices for what? Building an entire strategy game? something more specific?
You're right, I should have been more specific. I'll edit when I get a chance. I'm trying to build the system for movement - when a unit is 'selected', the game keeps track of where he is and exa$$anonymous$$es the 'cubes' around him, seeing if they are empty and moving the unit to their x and z positions if within range, etc. A multidimensional array of cubes mirroring a multidimensional array that uses Intergers to mark 'squares' as occupied or vacant is the only approach that occurs to me. I am hoping to find out if there is a better system, perhaps using only 1 multidimensional array for both the cubes and the 'units' positioned above (or 'on top of') them, maybe replacing the cube gameobject in the array for their corresponding row/column.
Answer by aJackson · Jun 30, 2011 at 06:51 AM
I'm working on something similar as well. I made an empty game object, attached a box collider to it, then scaled that collider to make a very thin cube. Then I attached a script to it and made it a prefab (this acts as a tile use can use to lay out the grid), the script handles the grid snapping when placing units.
To place units just use OnMouseOver(), OnMouseExit(), and OnMouseDown() to figure out which tile your over (make sure anything between the mouse and tile is set to the "ignore Ray Cast" layer) and reference that tiles transform to position the new unit.
// for example, this would go on the tile to handle snapping for initial placement:
// you need to have a reference of the unit being placed
void OnMouseOver()
{
if (tileIsPasable)
{
Unit.GetComponent<unitScript>().snapping = true;
Unit.transform.position = this.transform.position;
}
}
void OnMouseExit()
{
if (tileIsPasable)
{
Unit.GetComponent<unitScript>().snapping = false;
}
}
void OnMouseDown()
{
if (tileIsPasable)
{
Unit.GetComponent<unitScript>().snapping = true;
Unit.transform.position = this.transform.position;
Unit.GetComponent<unitScript>().state = UnitScript.State.idle;
}
}
// then this bit would be on the unit,
//it attaches the unit to the mouse when it is not being snapped to he grid
if(snapping == false){
Vector3 screenPos = Input.mousePosition;
screenPos.z = 40f;
Vector3 worldPos = Camera.mainCamera.ScreenToWorldPoint(screenPos);
transform.position = worldPos;
}
To move the unit just use the target location's tile transform to figure out the direction and distance and use lerp or something and a transition to move the unit to the desired location.
Not sure if that's what you needed but hopefully it helps.