| 127 | |
| 128 | |
| 129 | class Model(object): |
| 130 | |
| 131 | def __init__(self): |
| 132 | |
| 133 | # A Batch is a collection of vertex lists for batched rendering. |
| 134 | self.batch = pyglet.graphics.Batch() |
| 135 | |
| 136 | # A TextureGroup manages an OpenGL texture. |
| 137 | self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture()) |
| 138 | |
| 139 | # A mapping from position to the texture of the block at that position. |
| 140 | # This defines all the blocks that are currently in the world. |
| 141 | self.world = {} |
| 142 | |
| 143 | # Same mapping as `world` but only contains blocks that are shown. |
| 144 | self.shown = {} |
| 145 | |
| 146 | # Mapping from position to a pyglet `VertextList` for all shown blocks. |
| 147 | self._shown = {} |
| 148 | |
| 149 | # Mapping from sector to a list of positions inside that sector. |
| 150 | self.sectors = {} |
| 151 | |
| 152 | # Simple function queue implementation. The queue is populated with |
| 153 | # _show_block() and _hide_block() calls |
| 154 | self.queue = deque() |
| 155 | |
| 156 | self._initialize() |
| 157 | |
| 158 | def _initialize(self): |
| 159 | """ Initialize the world by placing all the blocks. |
| 160 | |
| 161 | """ |
| 162 | n = 80 # 1/2 width and height of world |
| 163 | s = 1 # step size |
| 164 | y = 0 # initial y height |
| 165 | for x in xrange(-n, n + 1, s): |
| 166 | for z in xrange(-n, n + 1, s): |
| 167 | # create a layer stone an grass everywhere. |
| 168 | self.add_block((x, y - 2, z), GRASS, immediate=False) |
| 169 | self.add_block((x, y - 3, z), STONE, immediate=False) |
| 170 | if x in (-n, n) or z in (-n, n): |
| 171 | # create outer walls. |
| 172 | for dy in xrange(-2, 3): |
| 173 | self.add_block((x, y + dy, z), STONE, immediate=False) |
| 174 | |
| 175 | # generate the hills randomly |
| 176 | o = n - 10 |
| 177 | for _ in xrange(120): |
| 178 | a = random.randint(-o, o) # x position of the hill |
| 179 | b = random.randint(-o, o) # z position of the hill |
| 180 | c = -1 # base of the hill |
| 181 | h = random.randint(1, 6) # height of the hill |
| 182 | s = random.randint(4, 8) # 2 * s is the side length of the hill |
| 183 | d = 1 # how quickly to taper off the hills |
| 184 | t = random.choice([GRASS, SAND, BRICK]) |
| 185 | for y in xrange(c, c + h): |
| 186 | for x in xrange(a - s, a + s + 1): |