Lazy Robots - Block selection

April 09, 2012

So after creating the basic building blocks of the levels, I started working on selecting boxes by clicking on them. Which would seem relatively straightforward, right? Like a couple lines of code...

But then complications arose. First of all, I wanted a shift-click option as well that would highlight all contiguous blocks at once, so that was somewhat complex - writing a function to take coordinates from an array, look at all the nearest neighbor spaces of that coordinate and feed those back into the array under certain conditions so the function could be run recursively until all contiguous blocks were found.

The goal in the end is really ease of control. So shift-click just seemed like an obvious implementation, as well as the ability to deselect specific boxes, say, if you wanted to move all the boxes of a specific group except one (and shift-clicking a selected box would deselect all boxes).

But then I started to have major problems with having multiple groups of non-contiguous blocks selected at the same time. So, first of all, every time you select any block, you've gotta check if any other boxes are selected, and, if so, are they adjacent to the box you're selecting? (If not, deselect them.) But the real ass-kicker was what if deselecting an individual box split up a large group of boxes into two separate groups.

Now we've gotta look at all the neighboring boxes of the deselected square, including diagonals, and see if there are multiple groupings of selected boxes. Something like:
XOO
XDX
OOX
where D is the deselected box, and Os are selected boxes. But that doesn't necessarily mean two separate groups because the selected boxes could connect up farther away from the center, like:
XXXOO
XXOOO
XXDXO
XOOXO
OOOOO
So for N groupings of deselected boxes around the original deselected box, I investigate N-1 groups by storing all their coordinates in an array, and then starting with the first coordinate (moving clockwise around the D), I attempt to move around the border of a group of selected boxes counter-clockwise, always keeping track of the coordinate I am in and the coordinate I came from. If you move around the border of group, eventually you are going to get back to where you started. And here's the thing: if I enter the original D square by moving from a coordinate that was in the original set of deselected boxes I started in, then there wasn't a break in the group of selected boxes, but if I move in from a coordinate corresponding to another set, then the group of selected boxes I traveled around, then that group is separate from the other selected boxes neighboring the original D and I've split up a group by deselecting that box. In that case, I just deselect everything as punishment to the play from making me write that ridiculous code.

Here's a movie demonstration (first screens!). It might be a little clearer. Maybe not.
And I'm sorry there aren't cute lazy robots yet. Just boxes...


Leave a comment

Comments will be approved before showing up.


Also in Blog

Forge War Kickstarter: A Retrospective

November 29, 2020

Read More

One crazy month
One crazy month

April 30, 2020

I’m sure I’m not alone in the fact that this last month and a half as been quite crazy. I remember about mid-March when the pandemic really started getting serious in the US, and I decided to still go to Piranha Pig Con (pictured above), and then visit my wife’s family out in Virginia.

Read More

The launch has shifted, and that's okay
The launch has shifted, and that's okay

March 20, 2020

I don’t have to explain to you how chaotic this last week has been, and I’m sure you don’t need any more statistics at this point. What I’d like to do, though, is briefly explain how Cephalofair Games has been affected by recent events, and what we will be doing moving forward.

Read More