-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
187 lines (165 loc) · 7.67 KB
/
editor.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
import sys
import pygame
import pygame.gfxdraw
from scripts.assets import load_tile_assets
from scripts.tilemap import Tilemap
INITIAL_DISPLAY_SIZE = [800, 500]
FPS = 60
class Editor:
def __init__(self, path="map.json"):
self.level_file = path
# display
pygame.init()
pygame.display.set_caption("Level Editor")
self.screen = pygame.display.set_mode(INITIAL_DISPLAY_SIZE, pygame.RESIZABLE)
self.display = pygame.Surface(INITIAL_DISPLAY_SIZE)
self.display_scale = 1
self.clock = pygame.Clock()
self.font = pygame.Font("data/fonts/press-start-2p-latin-400-normal.ttf", 16)
# user inputs & derived states
self.movement = [False, False, False, False]
self.shift = False
self.click = [False, False]
self.ongrid = True
self.tile_type = 0
self.tile_variant = 0
self.scroll = [0, 0]
self.size = [0, 0]
# assets and tilemap
self.tile_assets = load_tile_assets()
self.tile_list = list(self.tile_assets)
self.tilemap = Tilemap(self.tile_assets)
try:
self.tilemap.load(self.level_file)
self.calc_size()
except FileNotFoundError:
pass
def calc_size(self):
p0 = [0, 0]
p1 = [0, 0]
for tile_loc in self.tilemap.tiles:
tile = self.tilemap.tiles[tile_loc]
p0 = [min(p0[0], tile["pos"][0]), min(p0[1], tile["pos"][1])]
p1 = [max(p1[0], tile["pos"][0]), max(p1[1], tile["pos"][1])]
self.size = [p1[0] - p0[0], p1[1] - p0[1]]
def run(self):
running = True
while running:
# camera position
self.scroll[0] += (self.movement[1] - self.movement[0]) * 8
self.scroll[1] += (self.movement[3] - self.movement[2]) * 8
render_offset = (int(self.scroll[0]), int(self.scroll[1]))
# relative mouse position
mpos = pygame.mouse.get_pos()
mpos = (mpos[0] * self.display_scale, mpos[1] * self.display_scale)
# tile placement
if self.click[0] and self.ongrid: # (offgrid in events loop once per click)
self.tilemap.add((mpos[0] + render_offset[0], mpos[1] + render_offset[1]), self.tile_list[self.tile_type], self.tile_variant, self.ongrid)
self.calc_size()
if self.click[1]:
self.tilemap.remove((mpos[0] + render_offset[0], mpos[1] + render_offset[1]), render_offset, self.ongrid)
self.calc_size()
# rendering
self.display.fill((0, 0, 0, 0))
self.render_background()
self.tilemap.render(self.display, render_offset)
self.render_current_tile(mpos)
self.display.blit(self.font.render(f"{self.size[0]}x{self.size[1]}", False, (255, 255, 255)), (8, 8))
self.screen.fill((0, 0, 0, 0))
self.screen.blit(
pygame.transform.scale(self.display, (self.display.get_width() / self.display_scale, self.display.get_height() / self.display_scale)), (0, 0)
)
pygame.display.update()
# user inputs
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.VIDEORESIZE:
self.resize(event.dict["size"])
if event.type == pygame.VIDEOEXPOSE:
self.resize(self.screen.get_size())
if event.type == pygame.KEYDOWN:
if event.key in (pygame.K_LEFT, pygame.K_a):
self.movement[0] = True
if event.key in (pygame.K_RIGHT, pygame.K_d):
self.movement[1] = True
if event.key in (pygame.K_UP, pygame.K_w):
self.movement[2] = True
if event.key in (pygame.K_DOWN, pygame.K_s):
self.movement[3] = True
if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
self.shift = True
if event.key == pygame.K_g:
self.ongrid = not self.ongrid
if event.key == pygame.K_o:
self.tilemap.save(self.level_file)
if event.key == pygame.K_t:
self.tilemap.autotile()
if event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_a):
self.movement[0] = False
if event.key in (pygame.K_RIGHT, pygame.K_d):
self.movement[1] = False
if event.key in (pygame.K_UP, pygame.K_w):
self.movement[2] = False
if event.key in (pygame.K_DOWN, pygame.K_s):
self.movement[3] = False
if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
self.shift = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.click[0] = True
if not self.ongrid:
self.tilemap.add(
(mpos[0] + render_offset[0], mpos[1] + render_offset[1]), self.tile_list[self.tile_type], self.tile_variant, self.ongrid
)
if event.button == 3:
self.click[1] = True
if event.button == 4:
self.update_tile(increment=+1, variant=self.shift)
if event.button == 5:
self.update_tile(increment=-1, variant=self.shift)
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.click[0] = False
if event.button == 3:
self.click[1] = False
self.clock.tick(FPS)
def resize(self, size):
# fixed height, variable width
self.display_scale = INITIAL_DISPLAY_SIZE[1] / size[1]
self.display = pygame.Surface((size[0] * self.display_scale, INITIAL_DISPLAY_SIZE[1]))
def update_tile(self, increment=1, variant=False):
if variant:
self.tile_variant = (self.tile_variant + increment) % len(self.tile_assets[self.tile_list[self.tile_type]])
else:
self.tile_type = (self.tile_type + increment) % len(self.tile_list)
self.tile_variant = 0
def render_current_tile(self, mpos):
tile_img = self.tile_assets[self.tile_list[self.tile_type]][self.tile_variant]
if self.ongrid:
self.display.blit(
tile_img,
(
((mpos[0] + self.scroll[0]) // self.tilemap.tile_size) * self.tilemap.tile_size - self.scroll[0],
((mpos[1] + self.scroll[1]) // self.tilemap.tile_size) * self.tilemap.tile_size - self.scroll[1],
),
)
else:
self.display.blit(tile_img, mpos)
def render_background(self):
pygame.gfxdraw.textured_polygon(
self.display,
[
(0, 0),
(self.display.get_width(), 0),
self.display.get_size(),
(0, self.display.get_height()),
],
self.tile_assets["backgrounds"][self.tilemap.background],
self.tile_assets["backgrounds"][self.tilemap.background].get_width(),
self.tile_assets["backgrounds"][self.tilemap.background].get_height(),
)
if __name__ == "__main__":
file_path = sys.argv[1] if len(sys.argv) > 1 else "map.json"
Editor(file_path).run()