Furry landscape destruction service

I fixed the limitation in foreground scenery destruction that I talked about yesterday — you know, the one that I probably didn’t need to fix. This results in arbitrarily bedizened scenery being perfectly acceptable to the game’s destruction engine, as below.

I don’t plan to decorate the game world so tastelessly. Still, it feels good to have the option, you know?

A note on implementation: each tile in the tileset can declare itself to be “attached” to collision-layer scenery in one of the four cardinal directions. Then if a block in the right direction is destroyed, it is too. The only complication is adjusting this attachment direction to accommodate the tile being rotated or mirrored. I basically figured this out by brute forcing it, which was definitely the easiest way to go. Here’s what the algorithm looks like using the extensions to the XNA Tiled map reader library that I wrote:

private static string GetTileAttachment(Tile tile, Tileset.TilePropertyList tileProperties) {
    int index = ((int) tile.Position.Y * tile.GetLayer().Width) + (int) tile.Position.X;
    byte flipAndRotate = tile.GetLayer().FlipAndRotate[index];
    bool flipH = (flipAndRotate & Layer.HorizontalFlipDrawFlag) != 0;
    bool flipV = (flipAndRotate & Layer.VerticalFlipDrawFlag) != 0;
    bool flipR = (flipAndRotate & Layer.DiagonallyFlipDrawFlag) != 0;

    string attachment = tileProperties["attached"];
    switch ( attachment ) {
        case "up":
            if ( flipH && flipR ) {
                attachment = "right";
            } else if ( flipV && !flipR ) {
                attachment = "down";
            } else if ( flipR && !flipH ) {
                attachment = "left";
            }
            break;
        case "down":
            if ( flipH && flipR ) {
                attachment = "left";
            } else if ( flipV && !flipR ) {
                attachment = "up";
            } else if ( flipR && !flipH ) {
                attachment = "right";
            }
            break;
        case "left":
            if ( flipH && flipR ) {
                attachment = "up";
            } else if ( flipV && !flipR ) {
                attachment = "right";
            } else if ( flipR && !flipH ) {
                attachment = "down";
            }
            break;
        case "right":
            if ( flipH && flipR ) {
                attachment = "down";
            } else if ( flipV && !flipR ) {
                attachment = "left";
            } else if ( flipR && !flipH ) {
                attachment = "up";
            }
            break;
    }
    return attachment;
}

Being able to use strings as the case labels in switch statements is one of the many small things I appreciate about C# over Java. Having to use Java at work every day seems more like oppression than ever before.

6 thoughts on “Furry landscape destruction service

  1. But what if you want to build a jump table indexed by the value in your switch statement?!? You’re SCREWED, is what happens. Well, unless you’re satisfied using the hash code of the string, but THAT’S not guaranteed to fit in log(n) space in terms of case statements.

    Seriously, though, arbitrary case statements are the slicing to the switch statement’s bread.

  2. It’s gotta be a real pain going back to Java from C#. So many little niceties that you lose. I’m basically numb to it at this point, haven’t written C# in a long time.

    You should throw your Tiled extensions up on GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *