public static CCPoint positionForNode(CCNode node, CCPoint unitPos) { LHScene scene = ((LHNodeProtocol)node).getScene(); CCSize designSize = scene.designResolutionSize(); CCPoint offset = scene.designOffset(); CCPoint designPos = new CCPoint(); if (node.Parent == null || node.Parent == scene || node.Parent == scene.getGameWorldNode() || node.Parent == scene.getUINode() || node.Parent == scene.getBackUINode() ) { designPos = new CCPoint(designSize.Width * unitPos.X, (designSize.Height - designSize.Height * unitPos.Y)); designPos.X += offset.X; designPos.Y += offset.Y; } else { designPos = new CCPoint(designSize.Width * unitPos.X, node.Parent.ContentSize.Height - designSize.Height * unitPos.Y); CCNode p = node.Parent; designPos.X += p.ContentSize.Width * 0.5f; designPos.Y -= p.ContentSize.Height * 0.5f; } return(designPos); }
CCAffineTransform absoluteTransform() { CCAffineTransform transform = CCAffineTransform.Identity; LHScene scene = ((LHNodeProtocol)_node).getScene(); b2Vec2 b2Pos = _body.Position; CCPoint globalPos = new CCPoint(b2Pos.x, b2Pos.y); //[scene pointFromMeters:b2Pos]; transform = CCAffineTransform.Translate(transform, globalPos.X, globalPos.Y); transform = CCAffineTransform.Rotate(transform, _body.Angle); transform = CCAffineTransform.Translate(transform, -_node.ContentSize.Width * 0.5f, -_node.ContentSize.Height * 0.5f); return(transform); }
public static LHSprite nodeWithDictionary(PlistDictionary dict, CCNode prnt) { LHScene scene = ((LHNodeProtocol)prnt).getScene(); string imageFileName = dict ["imageFileName"].AsString; string relativeImagePath = dict ["relativeImagePath"].AsString; // string imagePath = LHUtils.imagePathWithFilename (imageFileName, relativeImagePath, scene.currentDeviceSuffix (false)); // string imageDevPath = LHUtils.imagePathWithFilename (imageFileName, relativeImagePath, scene.currentDeviceSuffix (true)); string imagePath = LHUtils.imagePathWithFilename(imageFileName, "", scene.currentDeviceSuffix(false)); string imageDevPath = LHUtils.imagePathWithFilename(imageFileName, "", scene.currentDeviceSuffix(true)); CCTexture2D texture = scene.textureWithImagePath(imagePath); CCSpriteFrame spriteFrame = null; string imageFilePath = imageDevPath; string spriteFrameName = dict["spriteName"].AsString; if (spriteFrameName != null) { LHSprite.cacheSpriteFramesInfo(imageDevPath, scene); spriteFrame = CCSpriteFrameCache.SharedSpriteFrameCache [spriteFrameName]; } else { //spriteFrame = [texture createSpriteFrame]; } LHSprite spr = new LHSprite(spriteFrame, dict, prnt, spriteFrameName, imageDevPath); return(spr); }
static void cacheSpriteFramesInfo(string imageDevPath, LHScene scene) { string sceneRelative = scene.relativePath; string curDevSuffix = scene.currentDeviceSuffix(true); string atlasName = LHUtils.stripExtension(imageDevPath); // string atlasName = Path.GetFileNameWithoutExtension(imageDevPath); string atlasPlist = atlasName + ".plist"; // string atlasPlist = Path.ChangeExtension (atlasName, "plist"); string sceneSuf = sceneRelative + curDevSuffix; // atlasPlist = sceneSuf + atlasPlist; // Debug.WriteLine ("atlasPlist"); // Debug.WriteLine (atlasPlist); // string sceneSuf = Path.Combine (sceneRelative, curDevSuffix); // atlasPlist = Path.Combine (sceneSuf, atlasPlist); CCSpriteFrameCache cache = CCSpriteFrameCache.SharedSpriteFrameCache; cache.AddSpriteFrames(atlasPlist); atlasName = sceneRelative + atlasName; // atlasName = Path.Combine (sceneRelative, atlasName); if (false == scene.hasEditorBodyInfoForImageFilePath(atlasName)) { string path = CCFileUtils.FullPathFromRelativePath(atlasPlist); PlistDocument document = CCContentManager.SharedContentManager.Load <PlistDocument>(path); PlistDictionary dict = document.Root.AsDictionary; PlistDictionary framesDict = dict ["frames"].AsDictionary; foreach (var pair in framesDict) { string sprName = pair.Key; PlistDictionary frmInfo = pair.Value.AsDictionary; if (null != frmInfo) { PlistDictionary bodyInfo = frmInfo ["body"].AsDictionary; if (null != bodyInfo) { Debug.WriteLine("CACHING BODY " + sprName + " atlas " + atlasName + " body " + bodyInfo); scene.setEditorBodyInfoForSpriteName(sprName, atlasName, bodyInfo); } } } } }
public void loadPhysicsInfoFromDictionary(PlistDictionary dict, CCNode nd) { _node = nd; if (null != dict) { int shapeType = dict ["shape"].AsInt; int type = dict ["type"].AsInt; LHScene scene = ((LHNodeProtocol)_node).getScene(); b2World world = scene.getBox2dWorld(); var bodyDef = new b2BodyDef(); bodyDef.type = (b2BodyType)type; CCPoint position = _node.Parent.ConvertToWorldspace(_node.Position); // bodyDef.position = scene.metersFromPoint (position); bodyDef.position = new b2Vec2(position.X, position.Y); float angle = CCNodeTransforms.GlobalXAngleFromLocalAngle(_node, _node.RotationX); bodyDef.angle = LHUtils.LH_DEGREES_TO_RADIANS(angle); bodyDef.userData = _node; _body = world.CreateBody(bodyDef); _body.UserData = _node; Debug.WriteLine("BODY:" + _body); _body.SetFixedRotation(dict ["fixedRotation"].AsBool); //_body->SetGravityScale _body.SetSleepingAllowed(dict ["allowSleep"].AsBool); _body.SetBullet(dict ["bullet"].AsBool); _body.AngularDamping = dict ["angularDamping"].AsFloat; _body.AngularVelocity = -360.0f * dict ["angularVelocity"].AsFloat; _body.LinearDamping = dict ["linearDamping"].AsFloat; CCPoint linearVel = CCPoint.Parse(dict ["linearVelocity"].AsString); _body.LinearVelocity = new b2Vec2(linearVel.X, linearVel.Y); CCSize size = _node.ContentSize; // size.Width = scene.metersFromValue (size.Width); // size.Height = scene.metersFromValue (size.Height); CCPoint scale = new CCPoint(_node.ScaleX, _node.ScaleY); scale = CCNodeTransforms.ConvertToWorldScale(_node, scale); previousScale = scale; size.Width *= scale.X; size.Height *= scale.Y; PlistDictionary fixInfo = dict ["genericFixture"].AsDictionary; Debug.WriteLine("FIX INFO " + fixInfo); Debug.WriteLine("SHAPE TYPE " + shapeType); if (shapeType == 0) //RECTANGLE { LHBodyShape shape = new LHBodyShape(); shape.createRectangleWithDictionary(fixInfo, _body, _node, scene, size); _subShapes.Add(shape); } else if (shapeType == 1) //CIRCLE { LHBodyShape shape = new LHBodyShape(); shape.createCircleWithDictionary(fixInfo, _body, _node, scene, size); _subShapes.Add(shape); } else if (shapeType == 4) //oval { PlistArray shapePoints = dict ["ovalShape"].AsArray; if (shapePoints != null) { LHBodyShape shape = new LHBodyShape(); shape.createShapeWithDictionary(fixInfo, shapePoints, _body, _node, scene, scale); _subShapes.Add(shape); } } else if (shapeType == 5) //traced { String fixUUID = dict ["fixtureUUID"].AsString; Debug.WriteLine("TRACED " + fixUUID); PlistArray shapePoints = scene.tracedFixturesWithUUID(fixUUID); Debug.WriteLine("RETURNS " + shapePoints); if (shapePoints == null) { //CHECK IN ASSET //LHAsset asset = _node.assetParent; /// } if (shapePoints != null) { LHBodyShape shape = new LHBodyShape(); Debug.WriteLine("WE HAVE A TRACED SHAPE"); shape.createShapeWithDictionary(fixInfo, shapePoints, _body, _node, scene, scale); _subShapes.Add(shape); } } else if (shapeType == 6) //editor { LHSprite sprite = (LHSprite)_node; if (sprite != null && sprite.GetType() == typeof(LHSprite)) { String imageFile = sprite.getImageFilePath(); imageFile = LHUtils.stripExtension(imageFile); Debug.WriteLine("WE HAVE AN EDITOR SHAPE for sprite " + sprite + " node " + _node + " tst " + imageFile); PlistDictionary bodyInfo = scene.getEditorBodyInfoForSpriteName(sprite.getSpriteFrameName(), imageFile); Debug.WriteLine("WE HAVE BODY INFO " + bodyInfo); if (bodyInfo != null) { PlistArray fixturesInfo = bodyInfo ["shapes"].AsArray; for (int i = 0; i < fixturesInfo.Count; ++i) { PlistDictionary shapeInfo = fixturesInfo [i].AsDictionary; Debug.WriteLine("SHAPE INFO " + shapeInfo); LHBodyShape shape = new LHBodyShape(); shape.createEditorWithDictionary(shapeInfo, _body, _node, scene, scale); _subShapes.Add(shape); } } } } // if (dict.ContainsKey ("alpha")) { // _node.Opacity = (byte)dict ["alpha"].AsFloat; // } } }
public void createRectangleWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCSize size) { _shapeID = dict ["shapeID"].AsInt; _shapeName = dict ["name"].AsString; b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(size.Width * 0.5f, size.Height * 0.5f); b2FixtureDef fixture = new b2FixtureDef(); LHSetupb2FixtureWithInfo(fixture, dict); fixture.userData = this; fixture.shape = shape; body.CreateFixture(fixture); }
public void createEditorWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCPoint scale) { _shapeID = dict ["shapeID"].AsInt; _shapeName = dict ["name"].AsString; int flipx = scale.X < 0 ? -1 : 1; int flipy = scale.Y < 0 ? -1 : 1; PlistArray fixtures = dict ["points"].AsArray; if (fixtures != null) { for (int f = 0; f < fixtures.Count; ++f) { PlistArray fixPoints = fixtures [f].AsArray; int count = fixPoints.Count; if (count > 2) { b2Vec2[] verts = new b2Vec2[count]; b2PolygonShape shapeDef = new b2PolygonShape(); int i = 0; for (int j = count - 1; j >= 0; --j) { int idx = (flipx < 0 && flipy >= 0) || (flipx >= 0 && flipy < 0) ? count - i - 1 : i; String pointStr = fixPoints [j].AsString; CCPoint point = CCPoint.Parse(pointStr); point.X *= scale.X; point.Y *= scale.Y; point.Y = -point.Y; b2Vec2 vec = new b2Vec2(point.X, point.Y); verts [idx] = vec; ++i; } if (LHValidateCentroid(verts, count)) { shapeDef.Set(verts, count); b2FixtureDef fixture = new b2FixtureDef(); LHSetupb2FixtureWithInfo(fixture, dict); fixture.userData = this; fixture.shape = shapeDef; body.CreateFixture(fixture); } } } } else { float radius = dict ["radius"].AsFloat; String centerStr = dict ["center"].AsString; CCPoint point = CCPoint.Parse(centerStr); radius *= scale.X; point.X *= scale.X; point.Y *= scale.Y; point.Y = -point.Y; b2CircleShape shape = new b2CircleShape(); shape.Radius = radius; shape.Position = new b2Vec2(point.X, point.Y); b2FixtureDef fixture = new b2FixtureDef(); LHSetupb2FixtureWithInfo(fixture, dict); fixture.userData = this; fixture.shape = shape; body.CreateFixture(fixture); } }
public void createCircleWithDictionary(PlistDictionary dict, b2Body body, CCNode node, LHScene scene, CCSize size) { _shapeID = dict ["shapeID"].AsInt; _shapeName = dict ["name"].AsString; b2CircleShape shape = new b2CircleShape(); shape.Radius = size.Width * 0.5f; b2FixtureDef fixture = new b2FixtureDef(); LHSetupb2FixtureWithInfo(fixture, dict); fixture.userData = this; fixture.shape = shape; body.CreateFixture(fixture); }
public static CCNode createLHNodeWithDictionary(PlistDictionary childInfo, CCNode prnt) { string nodeType = childInfo["nodeType"].AsString; LHScene scene = ((LHNodeProtocol)prnt).getScene(); string subclassNodeType = childInfo["subclassNodeType"].AsString; if (subclassNodeType != null && subclassNodeType.Length > 0) { //TODO handle subclasses //this will not work as we do not have the class included in the api // Class classObj = [scene createNodeObjectForSubclassWithName:subclassNodeType superTypeName:nodeType]; // if(classObj){ // return [classObj nodeWithDictionary:childInfo parent:prnt]; // } // else{ // NSLog(@"\n\nWARNING: Expected a class of type %@ subclassed from %@, but nothing was returned. Check your \"createNodeObjectForSubclassWithName:superTypeName:\" method and make sure you return a valid Class.\n\n", subclassNodeType, nodeType); // } } if (nodeType == "LHGameWorldNode") { LHGameWorldNode pNode = LHGameWorldNode.nodeWithDictionary(childInfo, prnt); pNode.ContentSize = scene._designResolutionSize; // #if LH_DEBUG // [pNode setDebugDraw:YES]; // #endif return(pNode); } else if (nodeType == "LHBackUINode") { LHBackUINode pNode = LHBackUINode.nodeWithDictionary(childInfo, prnt); return(pNode); } else if (nodeType == "LHUINode") { LHUINode pNode = LHUINode.nodeWithDictionary(childInfo, prnt); return(pNode); } else if (nodeType == "LHSprite") { LHSprite spr = LHSprite.nodeWithDictionary(childInfo, prnt); return(spr); } // else if([nodeType isEqualToString:@"LHNode"]) // { // LHNode* nd = [LHNode nodeWithDictionary:childInfo // parent:prnt]; // return nd; // } // else if([nodeType isEqualToString:@"LHBezier"]) // { // LHBezier* bez = [LHBezier nodeWithDictionary:childInfo // parent:prnt]; // return bez; // } // else if([nodeType isEqualToString:@"LHTexturedShape"]) // { // LHShape* sp = [LHShape nodeWithDictionary:childInfo // parent:prnt]; // return sp; // } // else if([nodeType isEqualToString:@"LHWaves"]) // { // LHWater* wt = [LHWater nodeWithDictionary:childInfo // parent:prnt]; // return wt; // } // else if([nodeType isEqualToString:@"LHAreaGravity"]) // { // LHGravityArea* gv = [LHGravityArea nodeWithDictionary:childInfo // parent:prnt]; // return gv; // } // else if([nodeType isEqualToString:@"LHParallax"]) // { // LHParallax* pr = [LHParallax nodeWithDictionary:childInfo // parent:prnt]; // return pr; // } // else if([nodeType isEqualToString:@"LHParallaxLayer"]) // { // LHParallaxLayer* lh = [LHParallaxLayer nodeWithDictionary:childInfo // parent:prnt]; // return lh; // } // else if([nodeType isEqualToString:@"LHAsset"]) // { // LHAsset* as = [LHAsset nodeWithDictionary:childInfo // parent:prnt]; // return as; // } // else if([nodeType isEqualToString:@"LHCamera"]) // { // LHCamera* cm = [LHCamera nodeWithDictionary:childInfo // parent:prnt]; // return cm; // } // else if([nodeType isEqualToString:@"LHRopeJoint"]) // { // LHRopeJointNode* jt = [LHRopeJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHWeldJoint"]) // { // LHWeldJointNode* jt = [LHWeldJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHRevoluteJoint"]){ // // LHRevoluteJointNode* jt = [LHRevoluteJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHDistanceJoint"]){ // // LHDistanceJointNode* jt = [LHDistanceJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // // } // else if([nodeType isEqualToString:@"LHPulleyJoint"]){ // // LHPulleyJointNode* jt = [LHPulleyJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHPrismaticJoint"]){ // // LHPrismaticJointNode* jt = [LHPrismaticJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHWheelJoint"]){ // // LHWheelJointNode* jt = [LHWheelJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // else if([nodeType isEqualToString:@"LHGearJoint"]){ // // LHGearJointNode* jt = [LHGearJointNode nodeWithDictionary:childInfo // parent:prnt]; // return jt; // } // // // else{ // NSLog(@"UNKNOWN NODE TYPE %@", nodeType); // } return(null); }