Boom

I hooked up the bomb to the game world’s physics model, doing a bit of other cleanup along the way. Here’s what it looks like:

It can destroy certain blocks as well as harm enemies. Unlike other weapons I’ve implemented, it has an area of effect separate from its physical occupation of space — quite a bit larger in fact. Ideally this would be a circular radius of destruction, but I found that a rectangular area works about as well and allows you to very easily query Box2D about the game world, like so:

AABB aabb = new AABB(_body.Position - new Vector2(0, TileLevel.TileSize / 2 - Height / 2), 
                     ExplodeRadius, ExplodeRadius);
_world.QueryAABB(fixture => {
    if ( fixture.GetUserData().IsDestructibleRegion ) {                    
        var hitTile = TileLevel.CurrentLevel.GetTile(fixture.GetUserData().Destruction.Position);
        TileLevel.CurrentLevel.TileHitBy(hitTile, this);
    } else if ( fixture.GetUserData().IsEnemy ) {
        fixture.GetUserData().Enemy.HitBy(this);
    }
    return true;
}, ref aabb);

In case it isn’t obvious, GetUserData() is an extension method I wrote on top of the Fixture and Body classes, which basically just handles a null check and cast to my custom UserData class. Extension methods are one of my favorite additions to Java in C#. It may seem like a small piece of syntactic sugar, but it makes a big difference in readability.

One thing about C# that I actually like less than Java, and which has been bothering me lately, is that a method must be explicitly declared virtual in order for a subclass to override it. This is the exact opposite route chosen by Java, where a method can be overridden by a subclass unless it is declared final. As far as I can tell, there’s no good reason for this choice, except maybe as a hold-over from C++. If you happen to know why the designers of C# thought this was a superior choice, please let me know in the comments!

2 thoughts on “Boom

  1. I think the idea was that sub-class-ability should be a conscious decision, not just something that happens by default. That way you’re forced to think about it. Not my favorite decision, I’d rather have everything be overrideable.

    • As much as I’ve been enjoying C#, there does seem to be a certain dogmatic approach to parts of its design, this being one of them.

Leave a Reply