Are you coding, son?

 Sadly, I've just been coding stuff for my job: very boring websites and no games.  I'm sorry that I lied about coding on the 7th of March 2020.  Things got pretty hectic during the pandemic!  Now, I want to get ready for JS13K games 2020!  So, let's work some more on Daemon to re-familiarize us with how to make games in HTML5!

I haven't been actually coding, but I've been brainstorming on what to do about how many pixels I should try to make each cell on my grid.  I eventually had the thought that it was the wrong question to ask.  What I eventually came up with was a way to force a certain aspect ratio for a canvas that would be used to draw the sections of the map/world as my "camera".  I call it Ratio-Vision!

Ratio-Vision is an object with functions to normalize the display between different screen sizes.  One of the problems I found out with my games was that they would play differently depending on the size of my screen.  Shield Bearer is an obvious example:  the spiders randomly spawn according to the screen width at the time you start the game.  If you resize the window, you can lose site of the Shield Bearer or create an empty void.  I can also enable itch.io's "Full Screen" mode for their embedded player with Ratio-Vision.  I had to disable it for Un-Bee Leap-able: it would break the HUD elements because they were absolute positioned (the pixels of the screen) instead of relative position (percentage of a whole screen).  Now, I can send the HUD elements to Ratio-Vision and have it draw them so that they can be seen on any window size.

The Ratio-Vision uses HTML's onResize Event.  You can set a function to execute whenever someone changes the size of the screen.  So, I created a function in Ratio-Vision to calculate the new height and width of the display (area where stuff will be drawn), as well as a new X and Y position for the display to sit on the screen.  This makes the display automatically add a letterbox or pillarbox when needed.  What that means is I created it to add black lines to the top and bottom of the window if it is too thin for the ratio, and it will add black lines to the left and right if it is too short.  All that is done to make sure the display, or main view of the game, always sits in the middle of the screen.

I managed to get that letterbox or pillarbox to display with a bunch of math.  I have the JavaScript for Ratio-Vision first try to use all of the width of the window, if that doesn't work, it then uses the whole height of the window and sets pillarboxing for the width it cannot use.  It always tries to calculate how much letterboxing it has to do for the display.  I figured if it can use the whole height, then the X position will be calculated as 0.

For Daemon, I have the initial constructor for Ratio-Vision set the ratio as 16 by 9.  I figured this would be a common widescreen format.  However, I have noticed other ratios as well: my computer monitor is one of the old style 16:10 and I have a phone that is 20:9.  That's why I developed Ratio-Vision: to solve the issue with multiple screen ratios.  If someone tries to play Daemon on a very old 4:3 monitor, then they're going to have to enjoy it with letterboxing.

The points value in Ratio-Vision is how many places per one unit of ratio.  It's better if I use an example to explain it:  if I use 100 points and the ratio of 16:9, then I have a display of 1600 by 900 that I can draw stuff on.  If I resize the window, then everything drawn to it should scale with the size of the display.  I currently have a function to draw lines and I used it to draw a whole grid to check the ratio and scale.  You can now try the grid out now!  No matter how you resize the window, you get sixteen boxes across and nine boxes vertically.  I'm hoping to add more features to Ratio-Vision soon: a way to render part of another canvas to the display, add shapes to it, and draw text on it.  Hopefully, it won't be another five months before I start coding again.  Like I said, I want to take part in the JS13K game jam this time!

Check out the latest source code for Daemon!

Comments

Popular posts from this blog

Game Off 2019 Day 4

Hacktoberfest 2019 Day 8

Metroidvania Month 7 Day 6