def json_load(self, path, additional_vars={}): import json self.world.GetGroundBody().userData = {"saveid": 0} f = open(path, 'r') worldmodel = json.loads(f.read()) f.close() # clean world for joint in self.world.GetJointList(): self.world.DestroyJoint(joint) for body in self.world.GetBodyList(): if body != self.world.GetGroundBody(): self.world.DestroyBody(body) # load bodys for body in worldmodel['bodylist']: bodyDef = box2d.b2BodyDef() bodyDef.position = body['position'] bodyDef.userData = body['userData'] bodyDef.angle = body['angle'] newBody = self.world.CreateBody(bodyDef) #_logger.debug(newBody) newBody.angularVelocity = body['angularVelocity'] newBody.linearVelocity = body['linearVelocity'] if 'shapes' in body: for shape in body['shapes']: if shape['type'] == 'polygon': polyDef = box2d.b2PolygonDef() polyDef.setVertices(shape['vertices']) polyDef.density = shape['density'] polyDef.restitution = shape['restitution'] polyDef.friction = shape['friction'] newBody.CreateShape(polyDef) if shape['type'] == 'circle': circleDef = box2d.b2CircleDef() circleDef.radius = shape['radius'] circleDef.density = shape['density'] circleDef.restitution = shape['restitution'] circleDef.friction = shape['friction'] circleDef.localPosition = shape['localPosition'] newBody.CreateShape(circleDef) newBody.SetMassFromShapes() for joint in worldmodel['jointlist']: if joint['type'] == 'distance': jointDef = box2d.b2DistanceJointDef() body1 = self.getBodyWithSaveId(joint['body1']) anch1 = joint['anchor1'] body2 = self.getBodyWithSaveId(joint['body2']) anch2 = joint['anchor2'] jointDef.collideConnected = joint['collideConnected'] jointDef.Initialize(body1, body2, anch1, anch2) jointDef.SetUserData(joint['userData']) self.world.CreateJoint(jointDef) if joint['type'] == 'revolute': jointDef = box2d.b2RevoluteJointDef() body1 = self.getBodyWithSaveId(joint['body1']) body2 = self.getBodyWithSaveId(joint['body2']) anchor = joint['anchor'] jointDef.Initialize(body1, body2, anchor) jointDef.SetUserData(joint['userData']) jointDef.enableMotor = joint['enableMotor'] jointDef.motorSpeed = joint['motorSpeed'] jointDef.maxMotorTorque = joint['maxMotorTorque'] self.world.CreateJoint(jointDef) for (k, v) in worldmodel['additional_vars'].items(): additional_vars[k] = v for body in self.world.GetBodyList(): del body.userData['saveid'] # remove temporary data
def __init__(self, game, location, bottomGraphicFile, topGraphicFile): self.jumpTimer = RollCat.initialJumpTimer self.game = game self.world = game.world self.cachedOnGround = False self.jumpState = RollCat.NO_GROUND_TOUCH bodyDef = box2d.b2BodyDef() bodyDef.position.Set(location[0], location[1]+1) #bodyDef.fixedRotation = True body = self.world.CreateBody(bodyDef) shapeDef = box2d.b2PolygonDef() shapeDef.SetAsBox(1, 1) shapeDef.density = 1 shapeDef.restitution = 0.3 shapeDef.friction = 1.0 shape = body.CreateShape(shapeDef) shape.SetUserData(-1) body.SetMassFromShapes() self.torso = body self.torso.SetUserData(ROLLCAT_BODY) bodyDef3 = box2d.b2BodyDef() bodyDef3.position.Set(location[0],location[1]) body3 = self.world.CreateBody(bodyDef3) shapeDef3 = box2d.b2CircleDef() shapeDef3.radius = 0.95 shapeDef3.density = 1 shapeDef3.restitution = 0.1 shapeDef3.friction = 3.0 shape3 = body3.CreateShape(shapeDef3) shape3.SetUserData(-1) #don't need body3.SetMassFromShapes() self.wheel = body3 self.wheel.SetUserData(ROLLCAT_WHEEL) sensorDef = box2d.b2CircleDef() sensorDef.radius = 0.9 sensorDef.isSensor = True sensorDef.localPosition = box2d.b2Vec2(0,-1.1) sensorShape = self.torso.CreateShape(sensorDef) sensorShape.SetUserData(ROLLCAT_JUMP_SENSOR) sensorDef2 = box2d.b2PolygonDef() sensorDef2.SetAsBox(1.1,1.6,box2d.b2Vec2(0,.5),0) sensorDef2.isSensor = True sensorShape2 = self.torso.CreateShape(sensorDef2) sensorShape2.SetUserData(ROLLCAT_WIN_SENSOR) jd = box2d.b2RevoluteJointDef() jd.Initialize(body, body3, box2d.b2Vec2(location[0],location[1])) revJoint = self.world.CreateJoint(jd).getAsType() self.axle = revJoint revJoint.EnableMotor(True) revJoint.SetMotorSpeed(0) revJoint.SetMaxMotorTorque(4000) self.motorSpeed = 0 self.targetAngle = 0 BodySprite(body3,bottomGraphicFile,2, 2, 0, 0, self.game.worldToScreen, self.game.screenToWorld) BodySprite(body,topGraphicFile,2, 2, 0, 0,self.game.worldToScreen, self.game.screenToWorld)