-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.py
134 lines (94 loc) · 3.41 KB
/
mapper.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
import pygame, vector, enemy, item, layer, utils
util = utils.Utils()
from pytmx import tmxloader
class Mapper(object):
def __init__(self, game, world, mapName):
self.mapName = mapName
self.world = world
self.drawable = []
self.layers = []
self.tilemap = tmxloader.load_pygame("maps/%s/%s/map.tmx" % (self.world, self.mapName), pixelalpha = True)
self.width = self.tilemap.width * self.tilemap.tilewidth
self.height = self.tilemap.height * self.tilemap.tileheight
self.drawShadow = True
self.gravity = 750
self.gravityAccel = 25
self.bgColor = (82, 246, 255)
self.background = None
self.pPosition = vector.Vec2d(0, 0)
self.pLayer = 0
self.layerCount = -1
self.hasKeyHoles = False
if hasattr(self.tilemap, "shadow"):
self.drawShadow = bool(int(self.tilemap.shadow))
if hasattr(self.tilemap, "gravity"):
self.gravity = int(self.tilemap.gravity)
if hasattr(self.tilemap, "gravityAccel"):
self.gravityAccel = int(self.tilemap.gravityAccel)
if hasattr(self.tilemap, "rgb"):
self.bgColor = tuple([map(int, self.tilemap.rgb.split())])
if hasattr(self.tilemap, "bg"):
tmp = self.tilemap.bg.split(":")
if tmp[0] == "d":
self.background = pygame.image.load("assets/sprites/backgrounds/%s.png" % (tmp[1])).convert_alpha()
elif tmp[0] == "w":
self.background = pygame.image.load("maps/%s/%s.png" % (self.world, tmp[1])).convert_alpha()
elif tmp[0] == "m":
self.background = pygame.image.load("maps/%s/%s/%s.png" % (self.world, self.mapName, tmp[1])).convert_alpha()
self.bgSize = self.background.get_size()
self.bgOffset = (0, 0)
for rawLayer in self.tilemap.getTileLayerOrder():
self.layerCount += 1
if rawLayer.visible:
layer.Layer(self, rawLayer)
self.layerCount += 1
MapText.group = []
for i in self.layers:
MapText.group.append([])
for obj in self.tilemap.getObjects():
if obj.name == "spawn":
self.pPosition.x = obj.x
self.pPosition.y = obj.y
if hasattr(obj, "layer"):
self.pLayer = int(obj.layer)
elif obj.name == "enemy":
eLayer = 0
enemyType = "enemyBlue"
if hasattr(obj, "layer"):
eLayer = int(obj.layer)
if hasattr(obj, "color"):
enemyType = "enemy" + obj.color.capitalize()
enemy.Enemy(game, self, enemyType, vector.Vec2d(obj.x, obj.y), eLayer)
elif obj.name == "item":
iLayer = 0
itemType = "keyYellow"
if hasattr(obj, "layer"):
iLayer = int(obj.layer)
if hasattr(obj, "color"):
itemType = "key" + obj.color.capitalize()
item.Item(game, self, itemType, vector.Vec2d(obj.x, obj.y), iLayer)
elif obj.name == "text":
text = ""
tLayer = 0
if hasattr(obj, "text"):
text = obj.text
if hasattr(obj, "layer"):
tLayer = int(obj.layer)
MapText(game, text, tLayer, vector.Vec2d(obj.x, obj.y), obj.width)
elif obj.name == "title":
tLayer = 0
if hasattr(obj, "layer"):
tLayer = int(obj.layer)
MapText(game, self.world + " | " + self.mapName, tLayer, vector.Vec2d(obj.x, obj.y), obj.width)
def drawBackground(self, game):
if self.background:
game.screen.blit(self.background, self.bgOffset)
class MapText(object):
group = []
def __init__(self, game, text, tLayer, pos, width):
self.text = text
self.pos = pos
MapText.group[tLayer].append(self)
self.surface = util.boxText(self.text, width, game.smallFont)
def draw(self, game):
game.screen.blit(self.surface, self.pos - game.viewport.rect.topleft)