Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- import json
- import re
- import sys
- def mirror(wall, axis):
- # Wall points appear in the form 'PoolVector2Array( x1, y1, x2, y2, ..., x99, y99)'
- # Capture each float, noting that whole numbers won't have a decimal.
- matches = re.findall(r" (\d+(?:\.\d+)?)", wall["points"])
- assert (len(matches) % 2) == 0
- # We have a flattened list of points. Even indices are x values. Mirror them
- # across the y-axis, but leave the y values alone.
- mirrored_points = (f"{2*axis*256-float(n)}" if i % 2 == 0 else n for i, n in enumerate(matches))
- new_wall = copy.deepcopy(wall)
- new_wall["points"] = f"PoolVector2Array( {', '.join(mirrored_points)} )"
- return new_wall
- def main():
- if len(sys.argv) != 2:
- print(f"Usage: {sys.argv[0]} map_file", file=sys.stderr)
- sys.exit(1)
- map_file = sys.argv[1]
- with open(map_file, "r") as f:
- d = json.load(f)
- world = d["world"]
- # This assumes the first wall on the first level is the target.
- walls = world["levels"]["0"]["walls"]
- # Mirroring is along the y-axis, even width assumed.
- axis = round(world["width"] / 2)
- new_wall = mirror(walls[0], axis)
- # The next node id is stored as a hex string. Use and update manually for the new wall.
- next_node_id = world["next_node_id"]
- new_wall["node_id"] = next_node_id
- world["next_node_id"] = format(int(next_node_id, 16)+1, "x")
- world["levels"]["0"]["walls"].append(new_wall)
- with open(f"mirrored_{map_file}", "w") as f:
- json.dump(d, f)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement