|
4 | 4 | from pathlib import Path |
5 | 5 | with open('scratchproject/project.json') as whatsinhere: |
6 | 6 | gamecode = json.load(whatsinhere) |
| 7 | +def make_new_event(): |
| 8 | + global neweventbaby |
| 9 | + neweventbaby = "<event eventtype=\""+str(eventtype)+"\" enumb=\""+str(enumb)+"\">\n <action>\n <libid>1</libid>\n <id>603</id>\n <kind>7</kind>\n <userelative>0</userelative>\n <isquestion>0</isquestion>\n <useapplyto>-1</useapplyto>\n <exetype>2</exetype>\n <functionname></functionname>\n <codestring></codestring>\n <whoName>self</whoName>\n <relative>0</relative>\n <isnot>0</isnot>\n <arguments>\n <argument>\n <kind>1</kind>\n <string>" |
| 10 | +def add_to_script(): |
| 11 | + global writingin |
| 12 | + if writingin == "whenGreenFlag": |
| 13 | + global greenflag |
| 14 | + if greenflag == "": |
| 15 | + global eventtype |
| 16 | + global enumb |
| 17 | + eventtype = 7 |
| 18 | + enumb = 4 |
| 19 | + make_new_event() |
| 20 | + greenflag = neweventbaby |
| 21 | + if greenflag != "": |
| 22 | + global codetoadd |
| 23 | + greenflag += codetoadd |
| 24 | +def convert_block(): |
| 25 | + global codetoadd |
| 26 | + codetoadd = "" |
| 27 | + #gotta figure out how to do this block lmao |
| 28 | + #if currentblock == 'doRepeat': |
| 29 | + # repeatfor = int(AllScripts[codeparse+1]) |
| 30 | + if currentblock == 'whenGreenFlag': |
| 31 | + global writingin |
| 32 | + writingin = "whenGreenFlag" |
| 33 | + if currentblock == 'gotoXy': |
| 34 | + global codeparse |
| 35 | + daX = AllScripts[codeparse+1] |
| 36 | + daX = int(daX)+480 |
| 37 | + daY = AllScripts[codeparse+2] |
| 38 | + daY = int(daY)+352 |
| 39 | + codetoadd = "x = "+str(daX)+";\ny = "+str(daY)+";\n" |
| 40 | + add_to_script() |
7 | 41 | if ("objName" in gamecode): |
8 | 42 | spritemetadata = ""; |
9 | 43 | soundmetadata = ""; |
|
26 | 60 | AllCostumes = gamecode['costumes'] |
27 | 61 | costumeamount = len(AllCostumes) |
28 | 62 | subimage = 0 |
29 | | - thecostume = AllCostumes[i] |
| 63 | + thecostume = AllCostumes[0] |
30 | 64 | rotX = thecostume['rotationCenterX'] |
31 | 65 | rotY = thecostume['rotationCenterY'] |
32 | | - costumeID = thecostume['baseLayerID'] |
33 | | - img = PIL.Image.open("scratchproject/"+str(costumeID)+".png") |
34 | | - wid, hgt = img.size |
35 | | - img.close() |
| 66 | + costumeID = (thecostume['baseLayerID']) |
| 67 | + imagez = PIL.Image.open("scratchproject/"+str(costumeID)+".png") |
| 68 | + wid, hgt = imagez.size |
| 69 | + imagez.close() |
36 | 70 | spritemetadata += " <sprite>sprites\\spr_stage</sprite>\n"; |
37 | 71 | with open('FinishedGame.gmx/sprites/spr_stage.sprite.gmx','x') as f: |
38 | 72 | f.write("<!--This Document is generated by GameMaker, if you edit it by hand then you do so at your own risk!-->\n<sprite>\n <type>0</type>\n <xorig>"+str(rotX)+"</xorig>\n <yorigin>"+str(rotY)+"</yorigin>\n <colkind>1</colkind>\n <coltolerance>0</coltolerance>\n <sepmasks>0</sepmasks>\n <bboxmode>0</bboxmode>\n <bbox_left>0</bbox_left>\n <bbox_right>"+str(wid-1)+"</bbox_right>\n <bbox_top>0</bbox_top>\n <bbox_bottom>"+str(hgt-1)+"</bbox_bottom>\n <HTile>0</HTile>\n <VTile>0</VTile>\n <TextureGroups>\n <TextureGroup0>0</TextureGroup0>\n </TextureGroups>\n <For3D>0</For3D>\n <width>960</width>\n <height>720</height>\n <frames>\n") |
|
57 | 91 | varcode = varname+" = "+str(varvalue)+";\n" |
58 | 92 | with open('FinishedGame.gmx/scripts/establishVariables.gml','a') as f: |
59 | 93 | f.write(varcode) |
60 | | - # Other Sprite Time! |
| 94 | + # Other Sprite Time! |
| 95 | + allobjmetadata = "" |
| 96 | + if ("children" in gamecode): |
61 | 97 | AllObjects = gamecode['children'] |
62 | 98 | ObjectAmount = len(AllObjects) |
63 | 99 | for i in range(ObjectAmount): |
64 | 100 | if 'target' not in AllObjects[i]:#this wont trip up by having a sprite or sound in Scratch be named "target" will it? |
65 | 101 | CurrentObject = AllObjects[i] |
| 102 | + ObjectX = CurrentObject['scratchX'] |
| 103 | + ObjectY = CurrentObject['scratchY'] |
66 | 104 | # Import Sounds |
67 | 105 | if ("sounds" in CurrentObject): |
68 | 106 | print("Importing '"+str(CurrentObject['objName'])+"' sounds...") |
|
110 | 148 | f.write(" </frames>\n</sprite>") |
111 | 149 | # Start to make Object XML |
112 | 150 | objectmetadata = "<!--This Document is generated by GameMaker, if you edit it by hand then you do so at your own risk!-->\n<object>\n <spriteName>spr_"+str(CurrentObject['objName'])+"</spriteName>\n <solid>0</solid>\n <visible>-1</visible>\n <depth>0</depth>\n <persistent>0</persistent>\n <parentName><undefined></parentName>\n <maskName><undefined></maskName>\n <events>\n " |
113 | | - # Game Code, Baby (this stuff doesn't work right now D:) |
114 | | - #greenflag = "" |
115 | | - #if ("scripts" in CurrentObject): |
116 | | - # CurrentScripts = CurrentObject['scripts'] |
117 | | - # ScriptAmount = len(CurrentScripts) |
118 | | - # for i in range (ScriptAmount): |
119 | | - # if ("whenGreenFlag" in CurrentScripts[i]): |
120 | | - # if (greenflag == ""): |
121 | | - # greenflag = "<event eventtype=\"0\" enumb=\"0\">\n <action>\n <libid>1</libid>\n <id>603</id>\n <kind>7</kind>\n <userelative>0</userelative>\n <isquestion>0</isquestion>\n <useapplyto>-1</useapplyto>\n <exetype>2</exetype>\n <functionname></functionname>\n <codestring></codestring>\n <whoName>self</whoName>\n <relative>0</relative>\n <isnot>0</isnot>\n <arguments>\n <argument>\n <kind>1</kind>\n <string>" |
122 | | - # whatodo = CurrentScripts["whenGreenFlag"] |
123 | | - # whatodolength = len(whatodo) |
124 | | - # for e in range (whatodolength): |
125 | | - # if ("gotoX:y:" in whatodo[i]): |
126 | | - # greenflag += "" |
127 | | - #Write Object Metadata |
| 151 | + # Object Code, Baby |
| 152 | + global greenflag |
| 153 | + greenflag = "" |
| 154 | + if ("scripts" in CurrentObject): |
| 155 | + print("Converting '"+str(CurrentObject['objName'])+"' code...") |
| 156 | + AllScripts = json.dumps(CurrentObject['scripts']) |
| 157 | + badchar = "[],:\"" |
| 158 | + for character in badchar: |
| 159 | + AllScripts = AllScripts.replace(character,"") |
| 160 | + AllScripts = AllScripts.split() |
| 161 | + global codeparse |
| 162 | + global codetoadd |
| 163 | + codeparse = 0 |
| 164 | + global eventtype |
| 165 | + global enumb |
| 166 | + eventtype = 0 |
| 167 | + enumb = 0 |
| 168 | + global writingin |
| 169 | + writingin = "" |
| 170 | + ScriptAmount = len(AllScripts) |
| 171 | + for i in range (ScriptAmount): |
| 172 | + currentblock = AllScripts[codeparse] |
| 173 | + convert_block() |
| 174 | + codeparse += 1 |
| 175 | + if greenflag != "": |
| 176 | + greenflag += "</string>\n </argument>\n </arguments>\n </action>\n </event>\n" |
| 177 | + #Write Object Metadata & Write Object Into Room Metadata |
| 178 | + objectmetadata += ""+str(greenflag)+" </events>\n <PhysicsObject>0</PhysicsObject>\n <PhysicsObjectSensor>0</PhysicsObjectSensor>\n <PhysicsObjectShape>0</PhysicsObjectShape>\n <PhysicsObjectDensity>0.5</PhysicsObjectDensity>\n <PhysicsObjectRestitution>0.100000001490116</PhysicsObjectRestitution>\n <PhysicsObjectGroup>0</PhysicsObjectGroup>\n <PhysicsObjectLinearDamping>0.100000001490116</PhysicsObjectLinearDamping>\n <PhysicsObjectAngularDamping>0.100000001490116</PhysicsObjectAngularDamping>\n <PhysicsObjectFriction>0.200000002980232</PhysicsObjectFriction>\n <PhysicsObjectAwake>-1</PhysicsObjectAwake>\n <PhysicsObjectKinematic>0</PhysicsObjectKinematic>\n <PhysicsShapePoints/>\n</object>" |
| 179 | + allobjmetadata += " <object>objects\obj_"+str(CurrentObject['objName'])+"</object>\n" |
128 | 180 | with open('FinishedGame.gmx/objects/obj_'+str(CurrentObject['objName'])+'.object.gmx','a') as f: |
129 | 181 | f.write(str(objectmetadata)) |
130 | | - #Rewrite Main Game Metadata |
| 182 | + with open('FinishedGame.gmx/rooms/rm_stage.room.gmx','a') as f: |
| 183 | + f.write(' <instance objName=\"obj_'+str(CurrentObject['objName'])+'\" x=\"'+str(ObjectX+480)+'\" y=\"'+str(ObjectY+352)+'\" name=\"inst_'+str(CurrentObject['objName'])+'\" locked=\"0\" code=\"\" scaleX=\"1\" scaleY=\"1\" colour=\"4294967295\" rotation=\"0\"/>\n') |
| 184 | + #Rewrite Main Game and Room Metadata |
131 | 185 | with open ('FinishedGame.gmx/FinishedGame.project.gmx','w') as f: |
132 | | - f.write("<!--This Document is generated by GameMaker, if you edit it by hand then you do so at your own risk!-->\n<assets>\n <Configs name=\"configs\">\n <Config>Configs\Default</Config>\n </Configs>\n <NewExtensions/>\n <sounds name=\"sound\">\n"+str(soundmetadata)+" </sounds>\n <sprites name=\"sprites\">\n"+str(spritemetadata)+" </sprites>\n <backgrounds name=\"background\"/>\n <paths name=\"paths\"/>\n <scripts name=\"scripts\">\n <script>scripts\establishVariables.gml</script>\n </scripts>\n <rooms name=\"rooms\">\n <room>rooms\\rm_stage</room>\n </rooms>\n <help>\n <rtf>help.rtf</rtf>\n </help>\n <TutorialState>\n <IsTutorial>0</IsTutorial>\n <TutorialName></TutorialName>\n <TutorialPage>0</TutorialPage>\n </TutorialState>\n</assets>") |
| 186 | + f.write("<!--This Document is generated by GameMaker, if you edit it by hand then you do so at your own risk!-->\n<assets>\n <Configs name=\"configs\">\n <Config>Configs\Default</Config>\n </Configs>\n <NewExtensions/>\n <sounds name=\"sound\">\n"+str(soundmetadata)+" </sounds>\n <sprites name=\"sprites\">\n"+str(spritemetadata)+" </sprites>\n <backgrounds name=\"background\"/>\n <paths name=\"paths\"/>\n <scripts name=\"scripts\">\n <script>scripts\establishVariables.gml</script>\n </scripts>\n <objects name=\"objects\">\n"+str(allobjmetadata)+" </objects>\n <rooms name=\"rooms\">\n <room>rooms\\rm_stage</room>\n </rooms>\n <help>\n <rtf>help.rtf</rtf>\n </help>\n <TutorialState>\n <IsTutorial>0</IsTutorial>\n <TutorialName></TutorialName>\n <TutorialPage>0</TutorialPage>\n </TutorialState>\n</assets>") |
| 187 | + with open('FinishedGame.gmx/rooms/rm_stage.room.gmx','a') as f: |
| 188 | + f.write(' </instances>\n <tiles/>\n <PhysicsWorld>0</PhysicsWorld>\n <PhysicsWorldTop>0</PhysicsWorldTop>\n <PhysicsWorldLeft>0</PhysicsWorldLeft>\n <PhysicsWorldRight>1024</PhysicsWorldRight>\n <PhysicsWorldBottom>768</PhysicsWorldBottom>\n <PhysicsWorldGravityX>0</PhysicsWorldGravityX>\n <PhysicsWorldGravityY>10</PhysicsWorldGravityY>\n <PhysicsWorldPixToMeters>0.100000001490116</PhysicsWorldPixToMeters>\n</room>') |
133 | 189 | print("*DING* Done!") |
134 | 190 | else: |
135 | 191 | print("ERROR: This Scratch project doesn't have a Stage from what we can tell. Is it a valid .sb2 file?") |
|
0 commit comments