Guest User

Untitled

a guest
Mar 10th, 2011
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. //
  2. //  SVGLevelBuilder.h
  3. //
  4. //  Created by Tom Elders on 17/07/2010.
  5. //
  6.  
  7. #import <Foundation/Foundation.h>
  8. #import "SpaceManager.h"
  9.  
  10. @interface SVGLevelBuilder : NSObject {
  11.  
  12.     SpaceManager    * spaceMgr;
  13.     NSMutableString * currentNodeGroup;
  14.     NSMutableString * currentNode;
  15.     NSXMLParser     * parser;
  16.     CGPoint           polyStartPoint;
  17.     NSMutableArray  * landscapeNodes;
  18.    
  19. }
  20.  
  21. @property (nonatomic, retain) SpaceManager * spaceMgr;
  22.  
  23. - (id) levelWithSVGFile:(NSString *)fileName inSpace:(SpaceManager *)pSpace;
  24.  
  25. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
  26. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
  27. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
  28.  
  29. - (void) buildLandscapeNode:(NSString *)pointData;
  30.  
  31. @end
  32.  
  33.  
  34. //
  35. //  SVGLevelBuilder.m
  36. //
  37. //  Created by Tom Elders on 17/07/2010.
  38. //
  39.  
  40. #import "SVGLevelBuilder.h"
  41.  
  42.  
  43. @implementation SVGLevelBuilder
  44.  
  45. @synthesize spaceMgr;
  46.  
  47. - (id) levelWithSVGFile:(NSString *)fileName inSpace:(SpaceManager *)pSpace
  48. {
  49.  
  50.     if ( (self = [super init]) ) {
  51.        
  52.         NSLog(@"SVG Level Builder Initiated.");
  53.        
  54.         spaceMgr = pSpace;
  55.        
  56.         // crate the parser
  57.         NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"svg"];
  58.        
  59.         parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
  60.         [parser setDelegate:self];
  61.        
  62.         [parser parse];
  63.        
  64.     }
  65.    
  66.     return self;
  67.    
  68. }
  69.  
  70. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  71. {
  72.    
  73.     NSLog(@"Found Element -----> %@", [attributeDict valueForKey:@"id"]);
  74.    
  75.     // Determine which group we're in
  76.     if ( [(NSString *) [attributeDict valueForKey:@"id"] isEqualToString:@"Landscape"] ) {
  77.         currentNodeGroup = [NSMutableString stringWithString:[attributeDict valueForKey:@"id"]];
  78.     }else{
  79.         NSLog(@"Current Node Group: %@", (NSString *)[attributeDict valueForKey:@"id"]);
  80.         // do nothing
  81.     }
  82.    
  83.     // Build Landscape
  84.     if([elementName isEqualToString:@"polygon"] && [currentNodeGroup isEqualToString:@"Landscape"]){
  85.         [self buildLandscapeNode:(NSString *)[attributeDict objectForKey:@"points"]];
  86.     }
  87.    
  88.    
  89.    
  90. }
  91.  
  92. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  93. {
  94.    
  95. }
  96.  
  97.  
  98. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  99. {
  100.  
  101. }
  102.  
  103. - (void) buildLandscapeNode:(NSString *)pointData
  104. {
  105.     NSLog(@"Point data: %@", pointData);
  106.     NSArray *points = [pointData componentsSeparatedByString:@" "];
  107.    
  108.     for(int i = 0; i < [points count] - 1; i++){
  109.        
  110.         NSArray *coords = [(NSString *)[points objectAtIndex:i] componentsSeparatedByString:@","];
  111.         // get the XY data as floats
  112.         float xVal = [(NSString *)[coords objectAtIndex:0] floatValue];
  113.         float yVal = 320 - [(NSString *)[coords objectAtIndex:1] floatValue];
  114.        
  115.         if (i == 0) {
  116.             polyStartPoint = ccp(xVal, yVal);
  117.         }else{
  118.             CGPoint polyEndPoint = ccp(xVal, yVal);
  119.             cpShape *segment = [spaceMgr addSegmentAtWorldAnchor:polyStartPoint toWorldAnchor:polyEndPoint mass:STATIC_MASS radius:1];
  120.             segment->e = 0.8;
  121.             segment->u = 1;
  122.             NSLog(@"SEGMENT CREATED AT %f,%f -- %f,%f", polyStartPoint.x, polyStartPoint.y, polyEndPoint.x, polyEndPoint.y);
  123.             polyStartPoint = polyEndPoint;
  124.         }
  125.                
  126.     }
  127. }
  128.  
  129.  
  130. @end
Advertisement
Add Comment
Please, Sign In to add comment