def renderplatform(self, screen, looker=None): looker = looker or camera if self.landing_hack: return if not self.platsurface: x, y, z = self.x, self.y, self.z if self.size == 1: z0, z1, z2, z3 = terrain.ihcorners(x, y) p0,p1,p2,p3,p4,p5,p6 = [looker.screenpos(a,b,c) for a,b,c in ((x-1,y,z), (x,y-1,z), (x+1,y,z), (x,y+1,z), (x-1,y,z0), (x,y-1,z1), (x+1,y,z2))] leftps = p0, p4, p5, p1 rightps = p1, p5, p6, p2 topps = p0, p1, p2, p3 elif self.size == 2: z0, z1, z2, z3, z4 = [terrain.iheight(x+dx,y+dy) for dx,dy in [(-2,0),(-1,-1),(0,-2),(1,-1),(2,0)]] b0,b1,b2,b3,b4,t0,t1,t2,t3 = [looker.screenpos(x+a,y+b,c) for a,b,c in [(-2,0,z0), (-1,-1,z1), (0,-2,z2), (1,-1,z3), (2,0,z4), (-2,0,z),(0,-2,z),(2,0,z),(0,2,z)]] leftps = t0, b0, b1, b2, t1 rightps = t1, b2, b3, b4, t2 topps = t0, t1, t2, t3 xs, ys, = zip(*(leftps + rightps + topps)) x0, y0 = min(xs), min(ys) s = pygame.Surface((max(xs) - x0 + 1, max(ys) - y0 + 1)).convert_alpha() s.fill((0,0,0,0)) pygame.draw.polygon(s, (70,70,70), [(x-x0,y-y0) for x,y in leftps]) # left panel pygame.draw.polygon(s, (30,30,30), [(x-x0,y-y0) for x,y in rightps]) # right panel pygame.draw.polygon(s, (50,50,50), [(x-x0,y-y0) for x,y in topps]) # top panel self.platsurface = s, x0+looker.x0//1, y0+looker.y0//1 s, x0, y0 = self.platsurface screen.blit(s, (x0-looker.x0//1, y0-looker.y0//1))
def __init__(self, color, bradii, sector): self.color = color self.tiles = set() for building, radius in bradii: x0, y0 = building.x, building.y + building.size - 1 self.tiles |= set((x0 + dx, y0 + dy) for dx, dy in ctiles(radius)) def insector(sx, sy): return (int(sx // 60), int(sy // 60)) == sector self.tiles = sorted([tile for tile in self.tiles if insector(*terrain.toiModel(*tile))]) self.segs = [] for x,y in self.tiles: ls = [((x+a,y+b),(x+c,y+d)) for a,b,c,d in [(0,1,-1,0),(0,1,1,0),(-1,0,0,-1),(1,0,0,-1)]] # order kind of matters here so be careful for line in ls: if line in self.segs: self.segs.remove(line) else: self.segs.append(line) segs = set(self.segs) self.lines = [] while segs: s0 = min(segs) segs.remove(s0) ps = [s0[0], s0[1]] found = True while found: tofind = ps[-1] found = False for p0,p1 in segs: if p0 == tofind: ps.append(p1) segs.remove((p0,p1)) found = True break elif p1 == tofind: ps.append(p0) segs.remove((p0,p1)) found = True break self.lines.append(ps) self.surfs = [] for line in self.lines: ps = [camera.camera0.screenpos(x,y,terrain.iheight(x,y)) for x,y in line] xs, ys = zip(*ps) x0, y0, x1, y1 = min(xs), min(ys), max(xs), max(ys) s = pygame.Surface((x1-x0+1,y1-y0+1)).convert_alpha() s.fill((0,0,0,0)) pygame.draw.lines(s, self.color, False, [(x-x0,y-y0) for x,y in ps]) self.surfs.append((x0,y0,s)) self.xmin = min(x for x,y,s in self.surfs) - 10 self.xmax = max(x+s.get_width() for x,y,s in self.surfs) + 10 self.ymin = min(y for x,y,s in self.surfs) - 10 self.ymax = max(y+s.get_height() for x,y,s in self.surfs) + 10 self.tiles = set(self.tiles)
def setheight(self, height): if self.size == 1: ps = (-1, 0), (0, -1), (1, 0), (0, 1) elif self.size == 2: ps = (-2, 0), (-1, -1), (0, -2), (1, -1), (2, 0), (1, 1), (0, 2), (-1, 1) self.z = max( terrain.iheight(self.x + ax, self.y + ay) for ax, ay in ps) + height
def renderplatform(self, screen, looker=None): looker = looker or camera if self.landing_hack: return if not self.platsurface: x, y, z = self.x, self.y, self.z if self.size == 1: z0, z1, z2, z3 = terrain.ihcorners(x, y) p0, p1, p2, p3, p4, p5, p6 = [ looker.screenpos(a, b, c) for a, b, c in ((x - 1, y, z), (x, y - 1, z), (x + 1, y, z), (x, y + 1, z), (x - 1, y, z0), (x, y - 1, z1), (x + 1, y, z2)) ] leftps = p0, p4, p5, p1 rightps = p1, p5, p6, p2 topps = p0, p1, p2, p3 elif self.size == 2: z0, z1, z2, z3, z4 = [ terrain.iheight(x + dx, y + dy) for dx, dy in [(-2, 0), (-1, -1), (0, -2), (1, -1), (2, 0)] ] b0, b1, b2, b3, b4, t0, t1, t2, t3 = [ looker.screenpos(x + a, y + b, c) for a, b, c in [(-2, 0, z0), (-1, -1, z1), ( 0, -2, z2), (1, -1, z3), (2, 0, z4), (-2, 0, z), (0, -2, z), (2, 0, z), (0, 2, z)] ] leftps = t0, b0, b1, b2, t1 rightps = t1, b2, b3, b4, t2 topps = t0, t1, t2, t3 xs, ys, = zip(*(leftps + rightps + topps)) x0, y0 = min(xs), min(ys) s = pygame.Surface( (max(xs) - x0 + 1, max(ys) - y0 + 1)).convert_alpha() s.fill((0, 0, 0, 0)) pygame.draw.polygon(s, (70, 70, 70), [(x - x0, y - y0) for x, y in leftps]) # left panel pygame.draw.polygon(s, (30, 30, 30), [(x - x0, y - y0) for x, y in rightps]) # right panel pygame.draw.polygon(s, (50, 50, 50), [(x - x0, y - y0) for x, y in topps]) # top panel self.platsurface = s, x0 + looker.x0 // 1, y0 + looker.y0 // 1 s, x0, y0 = self.platsurface screen.blit(s, (x0 - looker.x0 // 1, y0 - looker.y0 // 1))
def render(self, surf, looker = None): looker = looker or camera if not looker.rectvisible(self.xmin, self.ymin, self.xmax, self.ymax): return if looker is camera: for x, y, s in self.surfs: surf.blit(s, (x - camera.x0//1, y - camera.y0//1)) return for line in self.lines: ps = [looker.screenpos(x,y,terrain.iheight(x,y)) for x,y in line] pygame.draw.lines(surf, self.color, False, ps) return
def render(self, surf, looker=None): looker = looker or camera if not looker.rectvisible(self.xmin, self.ymin, self.xmax, self.ymax): return if looker is camera: for x, y, s in self.surfs: surf.blit(s, (x - camera.x0 // 1, y - camera.y0 // 1)) return for line in self.lines: ps = [ looker.screenpos(x, y, terrain.iheight(x, y)) for x, y in line ] pygame.draw.lines(surf, self.color, False, ps) return
def drawfadinggrid(surf, xc, yc, r=None, looker=None): r = settings.cursorgridsize if r is None else r looker = looker or camera x0, y0 = terrain.nearesttile(xc, yc) s = pygame.Surface(surf.get_size()).convert_alpha() s.fill((0,0,0,0)) hs, ps, alphas = {}, {}, {} for dx in range(-r, r+2): for dy in range(-r, r+2): x, y = x0 - 1 + dx + dy, y0 + dx - dy h = terrain.iheight(x, y) hs[(dx,dy)] = h ps[(dx,dy)] = looker.screenpos(x, y, h) alphas[(dx,dy)] = max(int(50 * (1 - math.sqrt(((x-xc)**2 + (y-yc)**2) / (r+1)**2))), 0) for dx in range(-r, r+1): for dy in range(-r, r+2): if hs[(dx,dy)] or hs[(dx+1,dy)]: a = alphas[(dx,dy)] + alphas[(dx+1,dy)] pygame.draw.line(s, (255,255,255,a), ps[(dx,dy)], ps[(dx+1,dy)]) if hs[(dy,dx)] or hs[(dy,dx+1)]: a = alphas[(dy,dx)] + alphas[(dy,dx+1)] pygame.draw.line(s, (255,255,255,a), ps[(dy,dx)], ps[(dy,dx+1)]) surf.blit(s, (0, 0))
def setheight(self, height): if self.size == 1: ps = (-1,0),(0,-1),(1,0),(0,1) elif self.size == 2: ps = (-2,0),(-1,-1),(0,-2),(1,-1),(2,0),(1,1),(0,2),(-1,1) self.z = max(terrain.iheight(self.x+ax, self.y+ay) for ax, ay in ps) + height
def __init__(self, color, bradii, sector): self.color = color self.tiles = set() for building, radius in bradii: x0, y0 = building.x, building.y + building.size - 1 self.tiles |= set((x0 + dx, y0 + dy) for dx, dy in ctiles(radius)) def insector(sx, sy): return (int(sx // 60), int(sy // 60)) == sector self.tiles = sorted([ tile for tile in self.tiles if insector(*terrain.toiModel(*tile)) ]) self.segs = [] for x, y in self.tiles: ls = [((x + a, y + b), (x + c, y + d)) for a, b, c, d in [(0, 1, -1, 0), (0, 1, 1, 0), (-1, 0, 0, -1), (1, 0, 0, -1)] ] # order kind of matters here so be careful for line in ls: if line in self.segs: self.segs.remove(line) else: self.segs.append(line) segs = set(self.segs) self.lines = [] while segs: s0 = min(segs) segs.remove(s0) ps = [s0[0], s0[1]] found = True while found: tofind = ps[-1] found = False for p0, p1 in segs: if p0 == tofind: ps.append(p1) segs.remove((p0, p1)) found = True break elif p1 == tofind: ps.append(p0) segs.remove((p0, p1)) found = True break self.lines.append(ps) self.surfs = [] for line in self.lines: ps = [ camera.camera0.screenpos(x, y, terrain.iheight(x, y)) for x, y in line ] xs, ys = zip(*ps) x0, y0, x1, y1 = min(xs), min(ys), max(xs), max(ys) s = pygame.Surface((x1 - x0 + 1, y1 - y0 + 1)).convert_alpha() s.fill((0, 0, 0, 0)) pygame.draw.lines(s, self.color, False, [(x - x0, y - y0) for x, y in ps]) self.surfs.append((x0, y0, s)) self.xmin = min(x for x, y, s in self.surfs) - 10 self.xmax = max(x + s.get_width() for x, y, s in self.surfs) + 10 self.ymin = min(y for x, y, s in self.surfs) - 10 self.ymax = max(y + s.get_height() for x, y, s in self.surfs) + 10 self.tiles = set(self.tiles)