-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapeditor.py
executable file
·232 lines (192 loc) · 6.27 KB
/
mapeditor.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
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python
# -*- coding: utf-8 -*-
# mapeditor.py
import pygame
import random
from time import clock
from config import *
from gameEngine import *
from mapDisplay import MapTileset, Map
import inspect, os.path
caller = inspect.stack()[-1]
if os.path.basename(caller[1]).lower() != 'wxmapeditor.py':
SCREEN = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
else:
SCREEN = None
print 'Skipping display initialization'
class MapEditor(object):
def __init__(self, screen = SCREEN):
self.map = None
self.currentTileCode = "wwww"
self.dragging = False
self.dragOriginX = 0
self.dragOriginY = 0
if screen:
self.screen = screen
else:
self.screen = pygame.display.set_mode((800,600))
self.currentLayer = "ground"
def open(self, filename):
self.load(filename)
def load(self, filename):
self.filename = filename
self.map = Map(filename)
def save(self, filename):
if not self.map:return
self.map.save(filename)
def new(self, x, y):
self.map = Map("new")
self.map.setSize(x, y)
def random(self):
self.map.setSize(80, 60)
randint = random.randint
#making the ocean
for y in range(self.map.h):
for x in range(self.map.w):
self.drawTile(self.currentLayer, x, y)
#each vulcan generates a small island
vulcans = randint(10,30)
ndirties = randint(1,3)
nroads = randint(1,3)
for n in range(vulcans):
#used to kill slow process
self.starttime = clock()
print "Generating islands %d" % (n+1,)
self.islandspoints = []
x, y = randint(1, self.map.w-1), randint(1, self.map.h-1)
radius = randint(1,30)
self.randomIsland(x, y, radius)
print "Islands %d generated" % (n+1,)
self.toggleTileCode()
#dirty areas generation to fix
for n in range(ndirties):
if len(self.islandspoints) < 1:
break
print "Adding dirty area number", n+1
source = list(self.islandspoints[randint(0, len(self.islandspoints)-1)])
dest = list(self.islandspoints[randint(0, len(self.islandspoints)-1)])
dx, dy = cmp(dest[0] - source[0], 0), cmp(dest[1] - source[1], 0)
while source != dest and 0 < source[0] < self.map.w and 0 < source [1] < self.map.h:
if source[0] != dest[0]:
source[0] = source[0] + dx
if source[1] != dest[1]:
source[1] = source[1] + dy
self.drawTile(self.currentLayer, source[0], source[1])
print "Dirty areas added"
self.toggleTileCode()
#roads generation to fix
#for n in range(nroads):
# if len(self.islandspoints) < 1:
# break
# print "Adding road number", n
# source = list(self.islandspoints[randint(0, len(self.islandspoints)-1)])
# dest = list(self.islandspoints[randint(0, len(self.islandspoints)-1)])
# dx, dy = cmp(dest[0] - source[0], 0), cmp(dest[1] - source[1], 0)
# while source != dest and 0 < source[0] < self.map.w and 0 < source [1] < self.map.h:
# if source[0] != dest[0]:
# source[0] = source[0] + dx
# if source[1] != dest[1]:
# source[1] = source[1] + dy
# self.drawTile(self.currentLayer, source[0], source[1])
#del self.islandspoints
#print "Roads added"
self.toggleTileCode()
#self.toggleTileCode()
def randomIsland(self, x, y, radius):
#killing the process if it takes too much which also add some random shape
if clock() - self.starttime > 5.0:
return
try:
#tree pruning
if radius < 1 or self.map.layers["ground"].getTile(x, y) == "gggg":
return
except:
#skipping positions outside the map
return
self.drawGrass(self.currentLayer, x, y)
if radius > 2:
self.islandspoints.append((x,y))
self.randomIsland(x-1, y, radius-1)
self.randomIsland(x-1, y-1, radius-1)
self.randomIsland(x-1, y+1, radius-1)
self.randomIsland(x+1, y, radius-1)
self.randomIsland(x+1, y-1, radius-1)
self.randomIsland(x+1, y+1, radius-1)
self.randomIsland(x, y-1, radius-1)
self.randomIsland(x, y+1, radius-1)
def setSize(self, x, y):
if not self.map:return
self.map.setSize(x, y)
def getMouseTilePos(self, x, y):
tx = (x+self.map.offsetX)/self.map.tileWidth
ty = (y+self.map.offsetY)/self.map.tileHeight
return tx, ty
def startDrag(self):
if not self.map:return
print "started to drag map"
self.dragging = True
x, y = pygame.mouse.get_pos()
self.dragOriginX = self.map.offsetX + x
self.dragOriginY = self.map.offsetY + y
def stopDrag(self):
print "stopped dragging"
self.dragging = False
def update(self, events = []):
if not self.map:return
x, y = pygame.mouse.get_pos()
if self.dragging:
self.map.setOffset(self.dragOriginX-x, self.dragOriginY-y)
#print "setting map offset : %s %s" % (self.map.offsetX, self.map.offsetY)
tx, ty = self.getMouseTilePos(x, y)
if pygame.mouse.get_pressed()[0]==1:
self.drawTile(self.currentLayer, tx, ty)
elif pygame.mouse.get_pressed()[2]==1:
self.drawGrass(self.currentLayer, tx, ty)
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if pygame.mouse.get_pressed()[1]==1 and not self.dragging:
self.startDrag()
elif event.type == pygame.MOUSEBUTTONUP:
if self.dragging:
self.stopDrag()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.toggleTileCode()
elif event.key == pygame.K_s:
self.save("maps/testmap2.txt")
elif event.key == pygame.K_o:
self.open("maps/testmap2.txt")
elif event.key == pygame.K_r:
self.setSize(40,20)
elif event.key == pygame.K_g:
self.random()
#self.screen.fill((0,0,0))
self.map.blit(self.screen)
pygame.display.update()
def blit(self, dest):
self.map.blit(dest)
def drawTile(self, layerName, x, y):
if not self.map:return
self.map.setTile(layerName, x, y, self.currentTileCode)
self.map.needFullBlit = True
def drawGrass(self, layerName, x, y):
if not self.map:return
self.map.setTile(layerName, x, y, "gggg")
self.map.needFullBlit = True
def setTileCode(self, code):
self.currentTileCode = code
def toggleTileCode(self):
if self.currentTileCode == "wwww":
self.setTileCode("dddd")
elif self.currentTileCode == "dddd":
self.setTileCode("rrrr")
else:
self.setTileCode("wwww")
if __name__ == "__main__":
from utils import KeyHandler
kh = KeyHandler()
m = MapEditor()
m.open("maps/testmap2.txt")
while kh.keyDict[pygame.K_ESCAPE]==0:
events = kh.getEvents()
m.update(events)