YMTD work continues

30 11 2008

Managed to get a fair bit of time to work on Yet More Tower Defense this weekend so I was able to make a good bit of progress. I cleaned up some of the stub functionality I’d put in for loading levels from file and put in the functionality to choose your level from the menu. There’s also a nice dynamically generated preview of the level that comes with it, thanks to experimenting with RenderTargets and basically rendering to a texture.

Today I put in one of the key components of the in-game hud: the tower pane! This is where the details of the currently selected tower (for upgrades/selling) are displayed, as well as the details of the tower type selected when building new towers. It’s working pretty well already, and save for some better art and shifting around the position of its elements, I’m pretty happy with it. How ’bout a picture?

Tower Pane (wip)

Tower Pane (wip)

Not much to look at, but all of the data gets updated as it should. It also goes a long way to bridging the gap between “prototype” and “functional game”. Not sure how I feel about the font though. Leave a comment if you have an opinion about it.

The more I work on the HUD, the more I want to add features and functionality. I’m restraining myself from getting too carried away, but I think I will implement a way for this pane to transition away when not in use. Tower defense is often about watching events unfold, so why clutter the action?

Next up: gamepad input. I think. Maybe sound. :)

Scale and retain aspect ratio

29 11 2008

After doing a bit of work on my level select menu, I came across the need to resize a texture to fit inside the a preview pane I’d created. However, not all of my preview images are going to be the same size or aspect ratio, nor do I want to enforce that restriction. I do, however, want to scale the images (up or down) to fit the pane without stretching them.

It’s a relatively simple procedure:

public static Vector2 FixAspectRatio(int imageX, int imageY, Vector2 paneDimensions)
            float imageAspectRatio = (float) imageX / imageY;

            float paneImageAspectRatio = paneDimensions.X / paneDimensions.Y;

            if (imageAspectRatio > paneImageAspectRatio)
                paneDimensions.Y = (paneDimensions.X / imageX) * imageY;
                paneDimensions.X = (paneDimensions.Y / imageY) * imageX;

            return paneDimensions;

So given a restricted pane size (here “paneDimensions”) and the size of the image, you’ll get back a scaled size. The scaled size will have the same aspect ratio as the image dimensions. It will also be no bigger than the given paneDimensions in width or height.