forked from ravenironwing/zhara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap.py
executable file
·221 lines (197 loc) · 9.04 KB
/
tilemap.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import pygame as pg
import pytmx
from settings import *
from os import path
import pyscroll
import copy
import logging
logger = logging.getLogger('orthographic')
logger.setLevel(logging.ERROR)
class MapData: #Used to keep track of what NPCs, animals and objects move to what maps, and are still alive.
def __init__(self, x, y):
self.x = x
self.y = y
self.npcs = []
self.animals = []
self.moved_npcs = []
self.moved_animals = []
self.moved_vehicles = []
self.items = []
self.vehicles = []
self.breakable = []
self.visited = False
self.tiledata = None
class TiledMap:
def __init__(self, game, filename):
self.game = game
self.filename = filename
tm = pytmx.load_pygame(filename)
ov = pytmx.load_pygame(filename)
self.width = tm.width * tm.tilewidth
self.tiles_wide = tm.width
self.tile_size = tm.tilewidth
self.height = tm.height * tm.tileheight
self.tiles_high = tm.height
self.tmxdata = tm
#self.overlay_data = ov
#self.image = self.make_map()
#self.rect = self.image.get_rect()
#self.rect.center = self.game.player.rect.center
#self.overlay = MapOverlay(tm)
map_data = pyscroll.data.TiledMapData(self.tmxdata)
self.map_layer = pyscroll.BufferedRenderer(map_data, (self.game.screen_width, self.game.screen_height), clamp_camera=True)
#self.overlay = self.generate_over_layer()
self.minimap = MiniMap(tm)
def toggle_visible_layers(self):
if self.game.player_inside:
for layer in self.tmxdata.visible_layers:
if ('roof' in layer.name or 'tree' in layer.name):
if isinstance(layer, pytmx.TiledTileLayer):
layer.visible = False
else:
for layer in self.tmxdata:
if layer.name != None:
if ('roof' in layer.name or 'tree' in layer.name):
if isinstance(layer, pytmx.TiledTileLayer):
layer.visible = True
self.map_layer.redraw_tiles(self.map_layer._buffer)
def redraw(self): # Redraws the map for tile updates
self.map_layer.redraw_tiles(self.map_layer._buffer)
#def generate_under_layer(self):
# for layer in self.tmxdata.visible_layers:
# if isinstance(layer, pytmx.TiledTileLayer):
# if ('roof' in layer.name or 'tree' in layer.name or 'high shadow' in layer.name):
# layer.visible = False
# else:
# layer.visible = True
# map_data = pyscroll.data.TiledMapData(self.tmxdata)
# return pyscroll.BufferedRenderer(map_data, (self.game.screen_width, self.game.screen_height), clamp_camera=True, tall_sprites=1)
#def generate_over_layer(self):
# for layer in self.overlay_data.visible_layers:
# if isinstance(layer, pytmx.TiledTileLayer):
# if ('roof' in layer.name or 'tree' in layer.name or 'high shadow' in layer.name):
# layer.visible = True
# else:
# layer.visible = False
# map_data = pyscroll.data.TiledMapData(self.overlay_data)
# return pyscroll.BufferedRenderer(map_data, (self.game.screen_width, self.game.screen_height), clamp_camera=True, alpha=True, tall_sprites=1)
#def render(self, surface):
# ti = self.tmxdata.get_tile_image_by_gid
# for layer in self.tmxdata.visible_layers:
# if not ('roof' in layer.name or 'tree' in layer.name or 'high shadow' in layer.name):
# if isinstance(layer, pytmx.TiledTileLayer):
# for x, y, gid, in layer:
# tile = ti(gid)
# if tile:
# surface.blit(tile, (x * self.tmxdata.tilewidth,
# y * self.tmxdata.tileheight))
#def test_render(self, surface):
# ti = self.tmxdata.get_tile_image_by_gid
# blank_surface = pg.Surface((self.tile_size, self.tile_size)).convert()
# playerx = int(self.game.player.pos.x / self.tile_size)
# playery = int(self.game.player.pos.y / self.tile_size)
# for layernum, layer in enumerate(self.tmxdata.visible_layers):
# if isinstance(layer, pytmx.TiledTileLayer):
# for j, y in enumerate(range(playery - 5, playery + 5)):
# for i, x in enumerate(range(playerx - 8, playerx + 8)):
# try:
# tile_img = self.tmxdata.get_tile_image(x, y, layernum)
# if tile_img != None:
# surface.blit(tile_img, (i * self.tmxdata.tilewidth, j * self.tmxdata.tileheight))
# except:
# surface.blit(blank_surface, (i * self.tmxdata.tilewidth, j * self.tmxdata.tileheight))
#def update(self):
# self.image = self.make_map()
# self.rect = self.image.get_rect()
# self.rect.center = self.game.player.rect.center
#def make_map(self):
# #temp_surface = pg.Surface((self.width, self.height)).convert()
# temp_surface = pg.Surface((self.tile_size * 16, self.tile_size * 10)).convert()
# self.render(temp_surface)
# return temp_surface
# This section creates the map image that is drawn over the player: for roofs and trees.
"""
class MapOverlay:
def __init__(self, tm):
self.width = tm.width * tm.tilewidth
self.height = tm.height * tm.tileheight
self.tmxdata = tm
self.image = self.make_map()
self.rect = self.image.get_rect()
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
if 'roof' in layer.name or 'tree' in layer.name or 'high shadow' in layer.name:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
surface.blit(tile, (x * self.tmxdata.tilewidth,
y * self.tmxdata.tileheight))
def make_map(self):
temp_surface = pg.Surface((self.width, self.height), pg.SRCALPHA).convert_alpha() # SRCALPHA does per pixel transparency and allows the transparency of this layer to show through on the bellow layers.
self.render(temp_surface)
return temp_surface
"""
class MiniMap:
def __init__(self, tm):
self.mapwidth = tm.width * tm.tilewidth
self.mapheight = tm.height * tm.tileheight
self.tmxdata = tm
self.size = 256
self.ratio = self.size / self.mapwidth
self.tile_size = int(self.size / tm.width)
self.height = self.mapheight * self.ratio
self.image = self.make_map()
self.rect = self.image.get_rect()
def render(self, surface):
ti = self.tmxdata.get_tile_image_by_gid
for layer in self.tmxdata.visible_layers:
if isinstance(layer, pytmx.TiledTileLayer):
for x, y, gid, in layer:
tile = ti(gid)
if tile:
small_tile = pg.transform.scale(tile, (self.tile_size, self.tile_size))
surface.blit(small_tile, (x * self.tile_size,
y * self.tile_size))
mini_surface = surface
return mini_surface
def make_map(self):
temp_surface = pg.Surface((self.size, self.height)).convert()
temp_surface = self.render(temp_surface)
return temp_surface
def resize(self, enlarge = True):
if enlarge:
self.size += 128
if self.size > HEIGHT:
self.size = 128
else:
self.size -= 128
if self.size < 128:
self.size = 128
self.ratio = self.size / self.mapwidth
self.height = self.mapheight * self.ratio
self.tile_size = int(self.size / 64)
self.image = self.make_map()
self.rect = self.image.get_rect()
class Camera():
def __init__(self, game, width, height):
self.camera = self.rect = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
self.x = 0
self.y = 0
self.game = game
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def apply_rect(self, rect):
return rect.move(self.camera.topleft)
def update(self, target): #Centers camera on player. target = player
self.x = -target.rect.centerx + int(self.game.screen_width / 2)
self.y = -target.rect.centery + int(self.game.screen_height / 2)
# limit scrolling to map size
self.x = min(0, self.x) # left
self.y = min(0, self.y) # top
self.x = max(-(self.width - self.game.screen_width), self.x) # right
self.y = max(-(self.height - self.game.screen_height), self.y) # bottom
self.camera = pg.Rect(self.x, self.y, self.width, self.height)