| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- """Rectangular copy and collision-checked group placement."""
- import pygame
- from const.window import MAP_AT, MAP_W, MAP_H, GAME_SQUARE
- from game.frame import obstacle
- from game.frame.obstacle_instance import ObstacleInstance
- class Selection:
- def __init__(self, app, read_clipboard):
- self.app = app
- self.read_clipboard = read_clipboard
- self.start = None
- self.end = None
- self.layer = None
- self.group = []
- self.clipboard_text = None
- def cancel(self):
- self.start = self.end = None
- self.group = []
- self.app.blank_map_hint_area()
- def cell(self, pos, clamp=False):
- x, y = pos[0] - MAP_AT[0], pos[1] - MAP_AT[1]
- if not clamp and not (0 <= x < MAP_W and 0 <= y < MAP_H):
- return None
- x = max(0, min(MAP_W - 1, x)) // GAME_SQUARE + self.app.scroll_grid_at[0]
- y = max(0, min(MAP_H - 1, y)) // GAME_SQUARE + self.app.scroll_grid_at[1]
- if not clamp and (x >= self.app.map_size[0] or y >= self.app.map_size[1]):
- return None
- return (min(x, self.app.map_size[0] - 1), min(y, self.app.map_size[1] - 1))
- def handle_event(self, event):
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- cell = self.cell(event.pos)
- if cell is None:
- return False
- if pygame.key.get_mods() & pygame.KMOD_SHIFT:
- self.start = self.end = cell
- self.layer = "obstacle" if self.app.show_obstacle else "floor"
- return True
- if self.group:
- self.app.menu.edit(lambda: self.place(cell))
- self.app.menu.block_pointer = True
- return True
- if self.start is not None:
- if event.type == pygame.MOUSEMOTION:
- self.end = self.cell(event.pos, clamp=True)
- elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
- self.end = self.cell(event.pos, clamp=True)
- self.copy_rectangle(self.start, self.end, self.layer)
- self.start = self.end = None
- self.app.menu.block_pointer = True
- return True
- return False
- def copy_rectangle(self, start, end, layer):
- x0, x1 = sorted((start[0], end[0]))
- y0, y1 = sorted((start[1], end[1]))
- nodes = []
- if layer == "obstacle":
- seen = set()
- for (x, y), instance in self.app.map_obstacles_dict.items():
- if x0 <= x <= x1 and y0 <= y <= y1 and id(instance) not in seen:
- seen.add(id(instance))
- nodes.append(dict(type=instance.obstacle["TYPE"], name=instance.obstacle["NAME"],
- x=instance.x, y=instance.y, layer=layer))
- else:
- for node in self.app.map_json["floor"]:
- for point in node["points"]:
- if x0 <= point["x"] <= x1 and y0 <= point["y"] <= y1:
- nodes.append(dict(type=node["type"], name=node["name"],
- x=point["x"], y=point["y"], layer=layer))
- self.group = nodes
- self.clipboard_text = self.read_clipboard()
- self.app.blank_map_hint_area()
- if nodes:
- anchor_x, anchor_y = min(n["x"] for n in nodes), min(n["y"] for n in nodes)
- for node in nodes:
- node["x"] -= anchor_x
- node["y"] -= anchor_y
- self.app.menu.notify("已复制 %d 个元素;左键放置,Esc 取消" % len(nodes))
- def plan(self, position):
- result, cells = [], set()
- self.conflicts = set()
- valid = bool(self.group)
- moving = getattr(self.app.tools.extra, "sources", []) if self.app.tools.tool == "move" else []
- for node in self.group:
- x, y = position[0] + node["x"], position[1] + node["y"]
- obs = obstacle.get_obstacle(node["type"], node["name"]) if node["layer"] == "obstacle" else None
- width, height = (obs["WIDTH"], obs["HEIGHT"]) if obs else (1, 1)
- if obs and not self.app.show_obstacle:
- valid = False
- for dx in range(width):
- for dy in range(height):
- cell = (x + dx, y + dy)
- if (cell in cells or (obs is not None and cell in self.app.map_obstacles_dict and self.app.map_obstacles_dict[cell] not in moving) or
- not (0 <= cell[0] < self.app.map_size[0] and 0 <= cell[1] < self.app.map_size[1])):
- valid = False
- self.conflicts.add(cell)
- cells.add(cell)
- result.append((node, x, y, obs))
- return valid, result, cells
- def place(self, position):
- valid, nodes, cells = self.plan(position)
- if not valid:
- self.app.menu.notify("无法放置:有障碍物、超出地图或障碍物已隐藏")
- return False
- # The entire footprint is checked before making any change.
- for node, x, y, obs in nodes:
- if obs:
- ObstacleInstance(x, y, self.app.map_obstacles_dict, obs)
- else:
- self.app.remove_elem((x, y), "floor")
- target = next((n for n in self.app.map_json["floor"]
- if n["type"] == node["type"] and n["name"] == node["name"]), None)
- if target is None:
- target = dict(type=node["type"], name=node["name"], points=[])
- self.app.map_json["floor"].append(target)
- target["points"].append(dict(x=x, y=y))
- self.app.redraw_floor_obstacle_trigger = True
- return True
- def draw(self):
- overlay = pygame.Surface((MAP_W, MAP_H), pygame.SRCALPHA)
- scroll_x, scroll_y = self.app.scroll_grid_at
- if self.start is not None:
- x0, x1 = sorted((self.start[0], self.end[0]))
- y0, y1 = sorted((self.start[1], self.end[1]))
- rect = pygame.Rect((x0 - scroll_x) * GAME_SQUARE, (y0 - scroll_y) * GAME_SQUARE,
- (x1 - x0 + 1) * GAME_SQUARE, (y1 - y0 + 1) * GAME_SQUARE)
- pygame.draw.rect(overlay, (70, 155, 255, 55), rect)
- pygame.draw.rect(overlay, (100, 195, 255, 240), rect, 2)
- elif self.group and not self.app.menu.blocks_editing():
- position = self.cell(pygame.mouse.get_pos())
- if position is not None:
- valid, nodes, cells = self.plan(position)
- for x, y in cells:
- color = (255, 65, 65, 130) if (x, y) in self.conflicts else (170, 170, 170, 105)
- rect = pygame.Rect((x - scroll_x) * GAME_SQUARE, (y - scroll_y) * GAME_SQUARE,
- GAME_SQUARE, GAME_SQUARE)
- pygame.draw.rect(overlay, color, rect)
- pygame.draw.rect(overlay, color[:3] + (220,), rect, 1)
- self.app.screen.blit(overlay, MAP_AT)
|