Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SVGLevelBuilder.h
- //
- // Created by Tom Elders on 17/07/2010.
- //
- #import <Foundation/Foundation.h>
- #import "SpaceManager.h"
- @interface SVGLevelBuilder : NSObject {
- SpaceManager * spaceMgr;
- NSMutableString * currentNodeGroup;
- NSMutableString * currentNode;
- NSXMLParser * parser;
- CGPoint polyStartPoint;
- NSMutableArray * landscapeNodes;
- }
- @property (nonatomic, retain) SpaceManager * spaceMgr;
- - (id) levelWithSVGFile:(NSString *)fileName inSpace:(SpaceManager *)pSpace;
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- - (void) buildLandscapeNode:(NSString *)pointData;
- @end
- //
- // SVGLevelBuilder.m
- //
- // Created by Tom Elders on 17/07/2010.
- //
- #import "SVGLevelBuilder.h"
- @implementation SVGLevelBuilder
- @synthesize spaceMgr;
- - (id) levelWithSVGFile:(NSString *)fileName inSpace:(SpaceManager *)pSpace
- {
- if ( (self = [super init]) ) {
- NSLog(@"SVG Level Builder Initiated.");
- spaceMgr = pSpace;
- // crate the parser
- NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"svg"];
- parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
- [parser setDelegate:self];
- [parser parse];
- }
- return self;
- }
- - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
- {
- NSLog(@"Found Element -----> %@", [attributeDict valueForKey:@"id"]);
- // Determine which group we're in
- if ( [(NSString *) [attributeDict valueForKey:@"id"] isEqualToString:@"Landscape"] ) {
- currentNodeGroup = [NSMutableString stringWithString:[attributeDict valueForKey:@"id"]];
- }else{
- NSLog(@"Current Node Group: %@", (NSString *)[attributeDict valueForKey:@"id"]);
- // do nothing
- }
- // Build Landscape
- if([elementName isEqualToString:@"polygon"] && [currentNodeGroup isEqualToString:@"Landscape"]){
- [self buildLandscapeNode:(NSString *)[attributeDict objectForKey:@"points"]];
- }
- }
- - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- {
- }
- - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
- {
- }
- - (void) buildLandscapeNode:(NSString *)pointData
- {
- NSLog(@"Point data: %@", pointData);
- NSArray *points = [pointData componentsSeparatedByString:@" "];
- for(int i = 0; i < [points count] - 1; i++){
- NSArray *coords = [(NSString *)[points objectAtIndex:i] componentsSeparatedByString:@","];
- // get the XY data as floats
- float xVal = [(NSString *)[coords objectAtIndex:0] floatValue];
- float yVal = 320 - [(NSString *)[coords objectAtIndex:1] floatValue];
- if (i == 0) {
- polyStartPoint = ccp(xVal, yVal);
- }else{
- CGPoint polyEndPoint = ccp(xVal, yVal);
- cpShape *segment = [spaceMgr addSegmentAtWorldAnchor:polyStartPoint toWorldAnchor:polyEndPoint mass:STATIC_MASS radius:1];
- segment->e = 0.8;
- segment->u = 1;
- NSLog(@"SEGMENT CREATED AT %f,%f -- %f,%f", polyStartPoint.x, polyStartPoint.y, polyEndPoint.x, polyEndPoint.y);
- polyStartPoint = polyEndPoint;
- }
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment