Not NaNo Day 4

Success! I’m using a modified version of the Python library from RedBlogGames’ excellent page on hexagons (seriously, it’s worth a read if you’ve ever wondered about hexagons and programming) to do a bunch of the annoying math with hexagons, and I went back and read the page to figure out why my hexes weren’t aligning properly. One of the comments was talking about a similar problem, leading me to see that the author had already thought of it and included fixes for it.

cocos2d hex alignment fix.png
Look at all those pixel perfectly aligned hexes!

I had a floating point error in my code. Part of the code calculates the distance from the center of the hex to the corner. Initially, I was using Python’s integer division when calculating this, but it rounds down, even though the value I was rounding was ~36.95. Using the round() function got me 37, which was the result expected, and results in proper alignment of the hexes. This is because the ideal hex doesn’t have perfectly matching integer boundaries, so I’m actually deforming the high slightly from ideal to match the display’s pixels.

I was hoping this would also fix the mouse offset I was having, but I’m still seeing an ~21 pixel vertical offset upwards. I’m going to try to shelve this issue and move on with actually getting more game stuff working, including starting to implement game systems and mechanics.

After fixing the pixel alignment issue, I found the bug causing the buildings to not draw properly. I realized that since transparency was working elsewhere that my issue probably wasn’t with transparency. I had accidentally been drawing the buildings in the terrain layer, instead of the terrain, and then drawing buildings on top of them. Perhaps I should have set a different background colour other than black to help catch the issue.

cocos2d buildings.png
Buildings (placeholders) rendering correctly!

I also had another minor bug where I was adding to my rendering batch, but had the actually rendering being done inside the loop. I wondered why it took several seconds to render the scene before I realized, but it was an easy fix.

I also worked on refactoring the terrain rendering code. I want to be able to render chunks and also not have building stored directly on the hex tile, rather have a building both be associated with a tile and a dictionary of building attached to the whole terrain map. I also switched to “rectangular” chunks because they’re about the same aspect ratio as the viewport is.

cocos2d hex chunk start core.png
Here’s a 15×15 rectangular chunk with the start core showing at the center.

I haven’t gotten more than one chunk added and drawn yet because I got sidetracked adding scrolling so I could see my different chunks. Scrolling itself works, but thouse mouse is offset from where it should be.

cocos2d hex scrolling broken mouse.gif
Sort of working scrolling. The mouse gets more and more offset as things scroll.

Continued in Part 2.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s