-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
200 lines (122 loc) · 6.45 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
188
189
190
191
192
193
194
195
196
197
198
199
200
import pygame
import sys
from tilemap import Tilemap
from utils import load_images
WIN_WIDTH = 640
WIND_HEIGHT = 480
RENDER_SCALE = 2.0
class Editor:
def __init__(self):
pygame.init()
pygame.display.set_caption('game editor')
self.screen = pygame.display.set_mode((WIN_WIDTH, WIND_HEIGHT))
self.display = pygame.surface.Surface((WIN_WIDTH//2, WIND_HEIGHT//2))
self.running = True
self.movement = [0,0,0,0]
self.scroll = [0,0]
self.clock = pygame.time.Clock()
self.assets = {
'blocks/wood':load_images('blocks/wood'),
'spawner':load_images('blocks/spawner'),
'test':load_images('blocks/test'),
'scenario/carpet':load_images('blocks/scenario')
}
self.tilemap = Tilemap(self, 18)
try:
self.tilemap.load('map.json')
except FileNotFoundError:
pass
self.tile_list = list(self.assets)
self.tile_group = 0
self.tile_variant = 0
self.clicking = False
self.right_clicking = False
self.shift = False
self.ongrind = True
def run(self):
while self.running:
self.display.fill((155,155,155))
self.scroll[0] += (self.movement[0] - self.movement[1]) * 2
self.scroll[1] += (self.movement[2] - self.movement[3]) * 2
render_scroll = (int(self.scroll[0]), int(self.scroll[1]))
self.tilemap.render(self.display, offset=render_scroll)
current_tile_img = self.assets[self.tile_list[self.tile_group]][self.tile_variant].copy()
current_tile_img.set_alpha(100)
mpos = pygame.mouse.get_pos()
mpos = ((mpos[0]/RENDER_SCALE , mpos[1]/RENDER_SCALE))
tile_pos = (int(mpos[0] + self.scroll[0])//self.tilemap.tile_size,int(mpos[1] + self.scroll[1])//self.tilemap.tile_size)
#tile_pos_wrong = (int(mpos[0] + self.scroll[0]//self.tilemap.tile_size),int(mpos[1] + self.scroll[1]//self.tilemap.tile_size))
if self.ongrind:
self.display.blit(current_tile_img, (tile_pos[0] * self.tilemap.tile_size - self.scroll[0] , tile_pos[1] * self.tilemap.tile_size - self.scroll[1]))
else:
self.display.blit(current_tile_img, mpos)
if self.clicking and self.ongrind:
self.tilemap.tilemap[str(tile_pos[0]) + ';' + str(tile_pos[1])] = {"type": self.tile_list[self.tile_group], "variant": self.tile_variant, "pos": tile_pos}
if self.right_clicking:
tile_loc = str(tile_pos[0]) + ';' + str(tile_pos[1])
if tile_loc in self.tilemap.tilemap:
del self.tilemap.tilemap[tile_loc]
for tile in self.tilemap.offgrid_tiles.copy():
tile_img = self.assets[tile['type']][tile['variant']]
tile_r = pygame.Rect(tile["pos"][0] - self.scroll[0], tile["pos"][1] - self.scroll[1],tile_img.get_width(), tile_img.get_height())
if tile_r.collidepoint(mpos):
self.tilemap.offgrid_tiles.remove(tile)
self.display.blit(current_tile_img, (5,5))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit
keys = pygame.key.get_pressed()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.clicking = True
if not self.ongrind:
self.tilemap.offgrid_tiles.append({"type": self.tile_list[self.tile_group], "variant": self.tile_variant, "pos": (mpos[0] + self.scroll[0], mpos[1] + self.scroll[1])})
if event.button == 2:
pass
if event.button == 3:
self.right_clicking = True
if self.shift:
if event.button == 4:
self.tile_variant = (self.tile_variant + 1 ) % len(self.assets[self.tile_list[self.tile_group]])
if event.button == 5:
self.tile_variant = (self.tile_variant - 1 ) % len(self.assets[self.tile_list[self.tile_group]])
else:
if event.button == 4:
self.tile_group = (self.tile_group + 1) % len(self.assets)
if event.button == 5:
self.tile_group = (self.tile_group - 1)% len(self.assets)
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.clicking = False
if event.button == 3:
self.right_clicking = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_g:
self.ongrind = not self.ongrind
if keys[pygame.K_d]:
self.movement[0] = True
elif keys[pygame.K_a]:
self.movement[1] = True
elif keys[pygame.K_w]:
self.movement[3] = True
elif keys[pygame.K_s]:
self.movement[2] = True
elif keys[pygame.K_LSHIFT]:
self.shift = True
elif keys[pygame.K_o]:
self.tilemap.save('map.json')
else:
self.movement[0] = False
self.movement[1] = False
self.movement[2] = False
self.movement[3] = False
self.shift = False
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()),[0,0])
pygame.display.update()
self.clock.tick(60)
e = Editor()
while e.running:
e.run()
pygame.quit()
sys.exit()