forked from lmandres/GGJTeamSpaceBubbles2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapprocess.py
150 lines (126 loc) · 6.65 KB
/
mapprocess.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
import os
import pygame
import settings
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
class Map:
def __init__(self, filename=None, maptilesfolder=None):
map_t = lambda t, x, y : self.getmaptile(os.path.join(maptilesfolder, t), x, y)
self.TILE_WIDTH = settings.TILESIZE
self.TILE_HEIGHT = settings.TILESIZE
self.tileimgs = {
# TODO numerical order 0-9 then Uppercase A-a done alpabetically A-a---Z-z
'0': {'walkable': True, 'tileimg': map_t('dirt1tile.png', 0, 0)},
'1': {'walkable': True, 'tileimg': map_t('junkpile1.png', 0, 0)}, # Used
'2': {'walkable': True, 'tileimg': map_t('junkpile2.png', 0, 0)}, # Used
'3': {'walkable': True, 'tileimg': map_t('junkpile3.png', 0, 0)}, # Used
'4': {'walkable': True, 'tileimg': map_t('junkpile4.png', 0, 0)}, # Used
'5': {'walkable': True, 'tileimg': map_t('junkpile5.png', 0, 0)}, # Used
'6': {'walkable': True, 'tileimg': map_t('junkpile6.png', 0, 0)}, # Used
'A': {'walkable': True, 'tileimg': map_t('ua.png', 1, 0)}, # TODO
'B': {'walkable': False, 'tileimg': map_t('tire-stack.png', 0, 0)},
'b': {'walkable': False, 'tileimg': map_t('tire-stack.png', 1, 0)},
'C': {'walkable': False, 'tileimg': map_t('chainlink.png', 0, 0)}, # chainlink Used
'c': {'walkable': False, 'tileimg': map_t('carpile.png', 1, 0)}, # TODO
'D': {'walkable': True, 'tileimg': map_t('dirtandgrasses.png', 0, 0)}, # Dirt Used
'd': {'walkable': True, 'tileimg': map_t('dirtandgrasses.png', 1, 1)}, # Dirt? Used
'E': {'walkable': True, 'tileimg': map_t('grass1edges.png', 0, 0)}, # grass1edges
'e': {'walkable': True, 'tileimg': map_t('grass1edges.png', 1, 0)}, # grass1edges Used
'F': {'walkable': True, 'tileimg': map_t('grass1edges.png', 1, 1)}, # Used
'f': {'walkable': True, 'tileimg': map_t('grass1edges.png', 0, 1)},
'G': {'walkable': True, 'tileimg': map_t('grasses.png', 1, 0)}, # Used
'g': {'walkable': True, 'tileimg': map_t('dirtandgrasses.png', 0, 1)}, # Used
'J': {'walkable': False, 'tileimg': map_t('tire-stack.png', 2, 0)},
'j': {'walkable': True, 'tileimg': map_t('dirt1tile.png', 0, 0)}, # Used
'M': {'walkable': True, 'tileimg': map_t('door.png', 0, 0)}, # Mob, large (guard)
'm': {'walkable': True, 'tileimg': map_t('door.png', 0, 0)}, # Mob, small (dog)
'P': {'walkable': False, 'tileimg': map_t('carpile.png', 0, 0)}, # Car pile Used
'p': {'walkable': False, 'tileimg': map_t('carpile.png', 1, 0)}, # Car pile Used
'Q': {'walkable': False, 'tileimg': map_t('carpile.png', 0, 1)}, # Car pile Used
'q': {'walkable': False, 'tileimg': map_t('carpile.png', 1, 1)}, # Car pile Used
'S': {'walkable': True, 'tileimg': map_t('door.png', 0, 0)}, # Spawn Used
'T': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 1, 0)}, # Golem
't': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 1, 1)}, # Golem
'U': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 2, 0)}, # Golem
'u': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 2, 1)}, # Golem
'X': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 0, 0)}, # Golem Used
'x': {'walkable': False, 'tileimg': map_t('robotprogress5.png', 0, 1)}, # Golem Used
'y': {'walkable': False, 'tileimg': map_t('Golem_3d_paint.png', 0, 0)}, # Golem part Used
'Z': {'walkable': True, 'tileimg': map_t('door.png', 0, 0)}, # Entrance
'z': {'walkable': True, 'tileimg': map_t('door.png', 0, 0)} # Exit
}
if filename:
self.loadmap(filename)
def getmaptile(self, filename, row, col):
return pygame.image.load(filename).subsurface(
(
self.TILE_WIDTH*col,
self.TILE_HEIGHT*row,
self.TILE_WIDTH,
self.TILE_HEIGHT
)
)
def loadmap(self, fileName):
self.maplist = []
fileIn = open(fileName, "r")
row = -1
for lineIn in fileIn:
row += 1
self.maplist.append([])
line = lineIn.strip()
tile = None
for tileIndex in range(0, len(line), 1):
tile = line[tileIndex]
self.maplist[row].append(tile)
fileIn.close()
def gettilemap(self):
maxRowIndex = len(self.maplist)
maxColIndex = 0
for rowIndex in range(0, len(self.maplist), 1):
if len(self.maplist[rowIndex]) > maxColIndex:
maxColIndex = len(self.maplist[rowIndex])
returnSurface = pygame.Surface(
(
maxColIndex*self.TILE_WIDTH,
maxRowIndex*self.TILE_HEIGHT
)
)
for rowIndex in range(0, len(self.maplist), 1):
for colIndex in range(0, len(self.maplist[rowIndex]), 1):
returnSurface.blit(
self.tileimgs[self.maplist[rowIndex][colIndex]]["tileimg"],
(self.TILE_WIDTH*colIndex, self.TILE_HEIGHT*rowIndex)
)
return returnSurface
def getwallmap(self):
returnList = []
for rowIndex in range(0, len(self.maplist), 1):
for colIndex in range(0, len(self.maplist[rowIndex]), 1):
if not self.tileimgs[self.maplist[rowIndex][colIndex]]["walkable"]:
returnList.append(
(
self.TILE_WIDTH*colIndex,
self.TILE_HEIGHT*rowIndex,
self.TILE_WIDTH,
self.TILE_HEIGHT
)
)
return returnList
class Camera:
def __init__(self, width, height):
self.camera = pygame.Rect(0, 0, width, height)
self.width = width
self.height = height
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):
x = -target.rect.centerx + int(settings.WIDTH / 2)
y = -target.rect.centery + int(settings.HEIGHT / 2)
# limit scrolling to map size
x = min(0, x) # left
y = min(0, y) # top
x = max(-(self.width - settings.WIDTH), x) # right
y = max(-(self.height - settings.HEIGHT), y) # bottom
self.camera = pygame.Rect(x, y, self.width, self.height)