-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgen_map.py
136 lines (108 loc) · 3.34 KB
/
gen_map.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
from p5 import *
import json
import sympy
import numpy as np
import argparse
import os
import constants
args = None
FILE = None
map = []
golf_start = None
golf_target = None
f = None # STEP 1 Declare PFont variable
def draw_polygon(poly):
begin_shape()
for v in poly.vertices:
vertex(float(v.x), float(v.y))
end_shape()
def draw_point(p):
point(float(p.x), float(p.y))
def draw_line(l):
line((float(l.points[0].x), float(l.points[0].y)), (float(l.points[1].x), float(l.points[1].y)))
def draw_circle(c):
circle(float(c.center.x), float(c.center.y), float(c.radius))
def setup():
global f, args
size(args.width, args.height)
f = create_font("Arial.ttf", 16) # STEP 2 Create Font
text_font(f, 16)
text_align("CENTER")
def draw():
global map, golf_start, golf_target, f, args
background(102)
fill(0)
text("Press e to save and exit, s for start (red) and t for target (green)", (int(width/2), int(args.height*0.9)))
text("start clicking anywhere to draw map polygon", (int(width/2), int(args.height*0.95)))
stroke(0)
fill(255)
if map:
poly = sympy.geometry.Polygon(*map)
if len(map) == 1:
draw_point(poly)
elif len(map) == 2:
draw_line(poly)
elif len(map) > 2:
draw_polygon(poly)
if golf_start:
sc = sympy.geometry.Circle(golf_start, 10)
fill(255, 0, 0)
draw_circle(sc)
fill(255)
if golf_target:
ec = sympy.geometry.Circle(golf_target, 10)
fill(0, 255, 0)
draw_circle(ec)
fill(255)
def save():
global map, golf_start, golf_target, FILE
map_list = []
for p in map:
tup = (float(p.x), float(p.y))
map_list.append(tup)
save_dict = dict()
save_dict["map"] = map_list
save_dict["start"] = (float(golf_start.x), float(golf_start.y))
save_dict["target"] = (float(golf_target.x), float(golf_target.y))
with open(FILE, "w") as f:
json.dump(save_dict, f)
print("Auto-saved file {}".format(FILE))
def mouse_pressed():
global map, golf_start, golf_target
p = sympy.geometry.Point2D(mouse_x, mouse_y)
print(mouse_x, mouse_y)
map.append(p)
print("New Map", map)
save()
def key_pressed():
global map, golf_start, golf_target
p = sympy.geometry.Point2D(mouse_x, mouse_y)
# print(mouse_x, mouse_y)
if key == "e":
save()
exit()
elif key == "s":
golf_start = p
print("Start assigned")
save()
elif key == "t":
golf_target = p
print("Target assigned")
save()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--file", "-f", default="maps/temp.json", help="Path to export generated map")
parser.add_argument("--width", help="Width", type=int, default=constants.vis_width)
parser.add_argument("--height", help="Height", type=int, default=constants.vis_height)
args = parser.parse_args()
FILE = args.file
golf_start = sympy.geometry.Point2D(args.width*0.2, args.height*0.2)
golf_target = sympy.geometry.Point2D(args.width*0.8, args.height*0.8)
dir = os.path.dirname(FILE)
if dir:
os.makedirs(dir, exist_ok=True)
try:
builtins.title = "Map Generation auto-saving to {}".format(FILE)
except:
pass
run(frame_rate=60)