-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
195 lines (167 loc) · 6.68 KB
/
world.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import random
import pickle
import pygame
from pygame.locals import *
from Box2D import *
from Polygon import Polygon
from debug import draw_body
class Chunk(object):
underground_texture = None
def __init__(self, world, chunk_pos):
self.world = world
self.pos = (chunk_pos[0] * 51.2, chunk_pos[1] * 51.2)
self.chunk_pos = (chunk_pos[0] % 32, chunk_pos[1])
self.texture = None
self.polygon = None
self.body = None
self.entities = []
self.things_to_blit = []
texture_filename = "data/world/%i_%i.tga" % self.chunk_pos
if os.path.exists(texture_filename):
self.texture = pygame.image.load(texture_filename)
self.texture.set_colorkey((255, 0, 255))
elif chunk_pos[1] <= 1:
self.texture = Chunk.underground_texture
data_filename = "data/world/%i_%i.dat" % self.chunk_pos
if os.path.exists(data_filename):
with open(data_filename) as fin:
self.polygon, self.entities = pickle.load(fin)
elif chunk_pos[1] <= 1:
self.polygon = Polygon((
(-25.6, -25.6),
(-25.6, 25.6),
(25.6, 25.6),
(25.6, -25.6)
))
self.load_body()
def load_body(self):
if self.body:
self.world.b2world.DestroyBody(self.body)
self.body = None
if not self.polygon:
return
shapes = []
for strip in self.polygon.triStrip():
for i in range(len(strip) - 2):
try:
shapes.append(b2PolygonShape(vertices=strip[i:i+3]))
except:
pass
self.body = self.world.b2world.CreateStaticBody(
position=(self.pos[0] + 25.6, self.pos[1] - 25.6),
shapes=shapes
)
def update(self, delta, player_pos):
for entity in self.entities:
entity.update(delta, self.pos, player_pos)
def render(self, screen, camera):
pos = camera.screen_pos(self.pos)
if self.texture:
screen.blit(self.texture, pos)
#if self.body:
# draw_body(self.body, screen, camera)
def unload(self):
if self.body:
self.world.b2world.DestroyBody(self.body)
for texture, pos in self.things_to_blit:
if self.texture == Chunk.underground_texture:
self.texture = Chunk.underground_texture.copy()
self.texture.set_colorkey((255, 0, 255))
pos = (
(pos[0] - self.pos[0]) * 10.0 - texture.get_width() / 2,
(self.pos[1] - pos[1]) * 10.0 - texture.get_height() / 2
)
self.texture.blit(texture, pos)
if self.texture:
pygame.image.save(
self.texture, "data/world/%i_%i.tga" % self.chunk_pos
)
with open("data/world/%i_%i.dat" % self.chunk_pos, "w") as fout:
pickle.dump((self.polygon, self.entities), fout)
def carve(self, body):
if not self.polygon:
return
if self.texture == Chunk.underground_texture:
self.texture = Chunk.underground_texture.copy()
self.texture.set_colorkey((255, 0, 255))
for fixture in body.fixtures:
shape = fixture.shape
vertices = [tuple(body.transform * x) for x in shape.vertices]
vertices = [
(x - self.pos[0] - 25.6, y - self.pos[1] + 25.6)
for (x, y) in vertices
]
polygon = Polygon(vertices)
self.polygon -= polygon
if self.texture:
draw_vertices = [
((x + 25.6) * 10, 512 - (y + 25.6) * 10) for (x, y) in vertices
]
pygame.draw.polygon(self.texture, (255, 0, 255), draw_vertices)
#self.load_body()
def blit(self, texture, pos, special_flags=0):
self.things_to_blit.append((texture, pos))
def add_entity(self, entity):
entity.pos = (entity.pos[0] - self.pos[0], entity.pos[1] - self.pos[1])
self.entities.append(entity)
class World(object):
def __init__(self):
self.b2world = b2World(gravity=(0, -10.0), doSleep=True)
self.center = (0, 0)
self.center_chunk = None
self.chunks = {}
Chunk.underground_texture = pygame.Surface((512, 512))
Chunk.underground_texture.set_colorkey((255, 0, 255))
for y in range(512):
for x in range(512):
a = random.uniform(112.0, 128.0)
Chunk.underground_texture.set_at((x, y), (a, a, a))
def update(self, delta):
center_chunk = (int(self.center[0] / 51.2), int(self.center[1] / 51.2))
center_chunk = (
center_chunk[0] - 1 if self.center[0] < 0.0 else center_chunk[0],
center_chunk[1] if self.center[1] < 0.0 else center_chunk[1] + 1
)
if center_chunk != self.center_chunk:
self.center_chunk = center_chunk
wanted = set()
for x in range(-1, 2):
for y in range(-1, 2):
chunk_pos = (center_chunk[0] + x, center_chunk[1] + y)
wanted.add((center_chunk[0] + x, center_chunk[1] + y))
for chunk_pos, chunk in self.chunks.items():
if not chunk_pos in wanted:
chunk.unload()
del self.chunks[chunk_pos]
for chunk_pos in wanted:
if not chunk_pos in self.chunks:
self.chunks[chunk_pos] = Chunk(self, chunk_pos)
self.b2world.Step(delta, 8, 8)
self.b2world.ClearForces()
for chunk in self.chunks.values():
chunk.update(delta, self.center)
def render(self, screen, camera):
for chunk in self.chunks.values():
chunk.render(screen, camera)
for chunk in self.chunks.values():
for entity in chunk.entities:
entity.render(screen, camera, chunk.pos)
def carve(self, body):
for chunk in self.chunks.values():
chunk.carve(body)
def blit(self, texture, pos, special_flags=0):
for chunk in self.chunks.values():
chunk.blit(texture, pos, special_flags)
def unload(self):
for chunk in self.chunks.values():
chunk.unload()
self.chunks = {}
self.center_chunk = None
def add_entity(self, entity, pos):
chunk_pos = (int(pos[0] / 51.2), int(pos[1] / 51.2))
chunk_pos = (
chunk_pos[0] - 1 if pos[0] < 0.0 else chunk_pos[0],
chunk_pos[1] if pos[1] < 0.0 else chunk_pos[1] + 1
)
self.chunks[chunk_pos].add_entity(entity)