开发者

Best way to store terrain state?

开发者 https://www.devze.com 2023-02-15 06:30 出处:网络
I am building a game using canvas. I have terrain that is generated randomly on game initilisation. The terrain is destroyable via the weaponry in the game.

I am building a game using canvas.

I have terrain that is generated randomly on game initilisation. The terrain is destroyable via the weaponry in the game.

Here is an example of开发者_开发技巧 the terrain I want to generate.

When generating this 2D terrain...

  • Is it best to simply store the peaks per x coordinate, or store every pixel that is terrain?
  • When a portion is removed via weapon damage, should I switch that pixel to off and then redraw the terrain, or redraw the terrain whilst checking if each one is on via the weapons damage being stored? Or, upon weapons damage, should I adjust each peak in the terrain state and then redraw?


I think you can safely store the terrain in 2D array. Having array of bools with size of like 3000x1000 may seem scary, but rest assured, traversing such array is still very fast. You can traverse this whole array 10 times and still maintain smooth animation speed.


Yey, Scorched Earth! As far as I remember you could bomb away circle-shaped volumes of terrain even below the original surface (leapfrog!). Terrain will then crumble and fill the gaps. Hence storing only polygonal vertex coordinates of the surface will not suffice. A per-pixel array-approach seems like a better idea.

The redrawing issue remains difficult. I guess it is not feasible to redraw the whole canvas at 30fps. Maybe you can redraw the weapon-affected/crumbled pixels only.


Let's say you split your terrain into subsquares.

Before:

+---------------------------+
|           ______       /  |
|   ___    /      \     /   |
|  /   \__/        \___/    |
|-/                         |
|                           |
|                           |
|                           |
+---------------------------+

After:

+---------------------------+
|    |    | ___|__  |    |  |
|   _|_   |/   |  \ |   /|  |
|  / | \__/    |   \|__/ |  |
|-/--|----------------------|
|    |    |    |    |    |  |
|    |    |    |    |    |  |
|    |    |    |    |    |  |
+---------------------------+

You can have three states for these subsections:

  1. All terrain
  2. All blank
  3. Split up

This should give you a decent optimization without seriously sacrificing readability of code.

0

精彩评论

暂无评论...
验证码 换一张
取 消