Not NaNo Day 5

My first task for the day was to get chunk rendering working, so that I could draw new chunks as the screen panned. I found a solution for it, but it feels pretty hacky. I’m definitely generating way more chunks than I probably need, but I want to make sure there’s always map on the screen and not a jagged black area where terrain hexes haven’t been generated.

What I’m doing is finding all of the bordering 8 chunks around the one the screen is currently centered on, and if they don’t already exist, generating them. In the future, this’ll need to add enemy spawns, city cores, creeps, etc, but that hasn’t been implemented yet.

cocos2d redraw on scroll.gif
Contrived example showing new chunks being generated on a scroll. Purple hex is the “key” or center hex for the chunk.

One problem that I’m having is that it’s slow. I’ve added a couple of workaroundsto improve speed, but it seems like it’s drawing all of the hexes because it slows down the more I scroll. I thought that cocos2d didn’t draw things outside of the viewport, but this doesn’t seem to be the case.

One thing I noticed was that the rendering batches I was creating were sticking around, so each time things were redrawn, they were all getting drawn, even overlapping terrain hexes. I couldn’t figure out a good way to fix this, so I hackily removed the child BatchNodes from the ScrollingManager subclass I have. Yuck, but it seems to help performance some.

In my quest to make things go faster, I also changed loading of assets. Instead of loading the images every time I’m generating a sprite, I now load the images and convert them to sprites as needed. This didn’t have the speedup I’d hoped for. Setting the chunk size lower seemed to help because huge chunks aren’t being generated, but it still pauses when a chunk is added. I think I’m shelving looking at performance issues for now to move on to more game related stuff. Like getting the starting core back.

Next I revamped adding buildings (again), and now not only can I add the starting core, but also place buildings. This is the start of the all important base-building part of the game.

cocos2d building plop.gif
Building buildings! Right now it takes holding a modifier key to get them to build, but at some point I’ll code a proper menu system.

My next task was to attempt to get buildings to go away, which was unsuccessful. The datastructures properly removed the buildings buildings, but somehow the sprites were still being rendered. Obviously I’m missing something in how cocos2d handles rendering.

So I moved on to actual game things. One of the mechanics is safe areas where enemy creeps can’t spawn, so I implemented tiles being able to be safe and be made safe with a tower that projects a protection field. It feels good to finally be getting out of laying all of the groundwork, though there’s still more groundwork to lay, and into actually implementing the game mechanics.

cocos2d protection tower plop.gif
Protection towers being added to the already protected area around the starting city core. As I can’t figure out how to remove existing sprites, I can’t make the area expand by removing the redundant inner border.

This is the start of the mechanic where you expand by building protection towers and expanding, supplying them with resources and protecting them with weapons. The above animation doesn’t show the networks, because they haven’t been implemented yet. I want to dynamically change the sprites as connections are added or removed, but I can’t yet remove sprites.

I’m also starting to look at the scale of what I’ve done, and I’m wondering if being able to see more hexes at once would be better. Not necessarily from a bigger window perspective, but from a using 48×48 or even 32×32 hexes instead of 64×64 ones.


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 )

Google photo

You are commenting using your Google 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