/ld26

To get this branch, use:
bzr branch /bzr/ld26
13 by Josh C
pull data from svg to place objects into the world
1
#!/usr/bin/python
2
3
# ./svg_levels.py >svg_levels.lua
4
5
import os, glob
6
from lxml import etree
7
from lxml.cssselect import CSSSelector
8
9
print '-- autogenerated by svg_levels.py\n\nsvg_objects = {'
10
11
ns = {'svg': 'http://www.w3.org/2000/svg',
12
      'ink': 'http://www.inkscape.org/namespaces/inkscape'}
14 by Josh C
some transitions in svg
13
38 by Josh C
gate
14
gate = ''
15
13 by Josh C
pull data from svg to place objects into the world
16
for svg in glob.glob('data/*.svg'):
17
    t = etree.parse(svg)
18
    basefile = os.path.basename(svg).split('.')[0]
19
    print '%s = {' % basefile
20
21
    sel = CSSSelector('svg|rect[ink|label="displace"]', ns)
22
    for e in sel(t):
23
        x, y = float(e.get('x')), float(e.get('y'))
24
        w, h = float(e.get('width')), float(e.get('height'))
14 by Josh C
some transitions in svg
25
13 by Josh C
pull data from svg to place objects into the world
26
        print """
27
Displacer:new{
28
  x = %f, y = %f,
29
  width = %f, height = %f
14 by Josh C
some transitions in svg
30
},
13 by Josh C
pull data from svg to place objects into the world
31
""" % (x,y,w,h)
32
14 by Josh C
some transitions in svg
33
    sel = CSSSelector('svg|rect[ink|label="transition"]', ns)
34
    for e in sel(t):
35
        x, y = float(e.get('x')), float(e.get('y'))
36
        w, h = float(e.get('width')), float(e.get('height'))
37
38
        desc = ""
39
        descSel = CSSSelector('svg|desc', ns)
40
        for d in descSel(e):
41
            desc = desc + d.text
42
43
        print """
44
Transition:new{
45
  x = %f, y = %f,
46
  width = %f, height = %f,
47
  %s
48
},
49
""" % (x,y,w,h, desc)
50
19 by Josh C
inventory (and a flower)
51
    sel = CSSSelector('svg|rect[ink|label="flower"]', ns)
52
    for e in sel(t):
53
        x, y = float(e.get('x')), float(e.get('y'))
54
55
        print """
56
Flower:new{
57
  x = %f, y = %f,
58
},
59
""" % (x,y)
60
39 by Josh C
cat
61
    sel = CSSSelector('svg|rect[ink|label="cat"]', ns)
62
    for e in sel(t):
63
        x, y = float(e.get('x')), float(e.get('y'))
64
65
        print """
66
Cat:new{
67
  x = %f, y = %f,
68
},
69
""" % (x,y)
70
34 by Josh C
GET FISH
71
    sel = CSSSelector('svg|rect[ink|label="fish"]', ns)
72
    for e in sel(t):
73
        x, y = float(e.get('x')), float(e.get('y'))
74
        w, h = float(e.get('width')), float(e.get('height'))
75
76
        print """
77
Fish:new{
78
  x = %f, y = %f,
79
  width = %f, height = %f,
80
},
81
""" % (x,y,w,h)
82
36 by Josh C
flag
83
    sel = CSSSelector('svg|rect[ink|label="flag"]', ns)
84
    for e in sel(t):
85
        x, y = float(e.get('x')), float(e.get('y'))
86
        w, h = float(e.get('width')), float(e.get('height'))
87
88
        print """
89
Flag:new{
90
  x = %f, y = %f,
91
  width = %f, height = %f,
92
},
93
""" % (x,y,w,h)
94
41 by Josh C
fairy
95
    sel = CSSSelector('svg|rect[ink|label="fairy"]', ns)
96
    for e in sel(t):
97
        x, y = float(e.get('x')), float(e.get('y'))
98
        w, h = float(e.get('width')), float(e.get('height'))
99
100
        print """
101
Fairy:new{
102
  x = %f, y = %f,
103
  width = %f, height = %f,
104
},
105
""" % (x,y,w,h)
106
38 by Josh C
gate
107
    sel = CSSSelector('svg|rect[ink|label="gatetrigger"]', ns)
108
    for e in sel(t):
109
        x, y = float(e.get('x')), float(e.get('y'))
110
        w, h = float(e.get('width')), float(e.get('height'))
111
112
        print """
113
GateTrigger:new{
114
  x = %f, y = %f,
115
  width = %f, height = %f,
116
},
117
""" % (x,y,w,h)
118
35 by Josh C
troll
119
    sel = CSSSelector('svg|rect[ink|label="troll"]', ns)
120
    for e in sel(t):
121
        x, y = float(e.get('x')), float(e.get('y'))
122
        #w, h = float(e.get('width')), float(e.get('height'))
123
124
        print """
125
Troll:new{
126
  x = %f, y = %f,
127
},
128
""" % (x,y)
129
34 by Josh C
GET FISH
130
13 by Josh C
pull data from svg to place objects into the world
131
    print '},'
132
38 by Josh C
gate
133
    # gate...
134
    sel = CSSSelector('svg|rect[ink|label="gateup"]', ns)
135
    for e in sel(t):
136
        x, y = float(e.get('x')), float(e.get('y'))
137
        w, h = float(e.get('width')), float(e.get('height'))
138
139
        gate = gate + """
140
gateup = Fill:new{
141
  x = %f, y = %f,
142
  width = %f, height = %f,
143
  fill = {108,57,22,255},
144
  gateup = true
145
}
146
""" % (x,y,w,h)
147
148
    sel = CSSSelector('svg|rect[ink|label="gatedown"]', ns)
149
    for e in sel(t):
150
        x, y = float(e.get('x')), float(e.get('y'))
151
        w, h = float(e.get('width')), float(e.get('height'))
152
153
        gate = gate + """
154
gatedown = Fill:new{
155
  x = %f, y = %f,
156
  width = %f, height = %f,
157
  fill = {108,57,22,255},
158
  visible = false
159
}
160
""" % (x,y,w,h)
161
162
13 by Josh C
pull data from svg to place objects into the world
163
print '}'
38 by Josh C
gate
164
165
print gate