All pastes #2056785 Raw Edit

Someone

public text v1 · immutable
#2056785 ·published 2011-05-11 13:14 UTC
rendered paste body
### Game.h:

#import "SpaceManagerCocos2d.h"
#import "GameConfig.h"
#import "cocos2d.h"

typedef struct {
	int form;
	int x;
	int y;
	bool stat;	//fest
	cpShape *shape;
	cpShapeNode *node;
	int a;
	int b;
	int c;
}levelobjs;

@interface Game : CCLayer
{
	SpaceManagerCocos2d *smgr;
	levelobjs levelobj[99];
	
	cpShape *unishape;
	cpShapeNode *uninode;
	
	int curobjtodrop;
	int curobjdropped;
	bool newobjready;
	bool newobjwaiting;
	
	//grafik:
	CCSprite *sfeld;
	CCSprite *sback;
}

+(id) scene;
- (CCNode*) createBlockAt:(cpVect)pt
					width:(int)w
				   height:(int)h
					 mass:(int)mass;
-(void) gamelogic:(ccTime)dt;
-(BOOL) handleCollision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space;
//-(void) objcoll;
-(void) dropcurobj;
-(ccColor3B) rndcolor;
@end



### Game.m:

#import "Game.h"
#import "chipmunk.h"

@interface Game (PublicMethods)
@end

@implementation Game

+(id) scene
{
	CCScene *scene = [CCScene node];
	[scene addChild:[Game node]];
	return scene;
}


- (id) init
{
	[super init];
	
	
	
	
	sback = [CCSprite spriteWithFile:@"back.png"];
	sback.position = ccp(160,240);
	[self addChild:sback];
	
	sfeld = [CCSprite spriteWithFile:@"feld.png"];
	sfeld.position = ccp(30,450);
	[self addChild:sfeld];
	
	
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
	
	levelobj[0].form=rechteck;
	levelobj[0].x=200;
	levelobj[0].y=200;
	levelobj[0].a=50;
	levelobj[0].b=10;
	levelobj[0].stat=1;
	levelobj[1].form=rechteck;
	levelobj[1].x=200;
	levelobj[1].y=150;
	levelobj[1].stat=0;
	levelobj[1].a=50;
	levelobj[1].b=10;
	levelobj[2].form=rechteck;
	levelobj[2].x=200;
	levelobj[2].y=450;
	levelobj[2].stat=0;
	levelobj[2].a=50;
	levelobj[2].b=90;
	
	levelobj[3].form=99999;
	levelobj[3].x=99999;
	levelobj[3].y=99999;
	
	levelobj[4].form=rechteck;
	levelobj[4].x=45;
	levelobj[4].y=435;
	levelobj[4].a=10;
	levelobj[4].b=10;
	
	levelobj[5].form=rechteck;
	levelobj[5].x=45;
	levelobj[5].y=450;
	levelobj[5].a=45;
	levelobj[5].b=10;
	
	levelobj[6].form=rechteck;
	levelobj[6].x=50;
	levelobj[6].y=430;
	levelobj[6].a=50;
	levelobj[6].b=20;
	
	
	//allocate our space manager
	smgr = [[SpaceManagerCocos2d alloc] init];
	smgr.constantDt = 1/55.0; 
	[smgr setGravity:ccp(0,-200)];
	//level einlesen
	for (int i = 0;i!=100000;i++){
		NSLog(@"i:%i form: %i",i,levelobj[i].form);
		if (levelobj[i].form==rechteck) {
			if (levelobj[i].stat==1){
				levelobj[i].shape = [smgr addRectAt:cpv(levelobj[i].x,levelobj[i].y) mass:INFINITY width:levelobj[i].a 
											 height:levelobj[i].b rotation:levelobj[i].c];
				levelobj[curobjtodrop].shape->collision_type=obj_passive;
				levelobj[i].node = [cpShapeNode nodeWithShape:levelobj[i].shape];
				levelobj[i].node.color = ccc3(180, 180, 180);
				[self addChild:levelobj[i].node];
			}
			else{
				levelobj[i].shape = [smgr addRectAt:cpv(levelobj[i].x,levelobj[i].y) mass:levelobj[i].a*levelobj[i].b 
											  width:levelobj[i].a height:levelobj[i].b rotation:levelobj[i].c];
				levelobj[curobjtodrop].shape->collision_type=obj_passive;
				levelobj[i].node = [cpShapeNode nodeWithShape:levelobj[i].shape];
				levelobj[i].node.color = rndcolor;// ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);
				[self addChild:levelobj[i].node];
			}
		}
		else if (levelobj[i].form==0 || levelobj[i].form==99999){
			if(levelobj[i].x==99999 && levelobj[i].y==99999){
				curobjtodrop=i+1;
			}
			i=99999;
		}
	}
	
	//erstes bewegliches obj	
	levelobj[curobjtodrop].shape = [smgr addRectAt:cpv(levelobj[curobjtodrop].x,levelobj[curobjtodrop].y) mass:levelobj[curobjtodrop].a*levelobj[curobjtodrop].b 
											 width:levelobj[curobjtodrop].a height:levelobj[curobjtodrop].b rotation:levelobj[curobjtodrop].c];
	levelobj[curobjtodrop].shape->collision_type=obj_active ;
	levelobj[curobjtodrop].node = [cpShapeNode nodeWithShape:levelobj[curobjtodrop].shape];
	levelobj[curobjtodrop].node.color = rndcolor();// ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);
	[self addChild:levelobj[curobjtodrop].node];
	newobjready=1;
	newobjwaiting=1;
	
	ccColor3B ccolor;
	ccolor=rndcolor;
	
	[smgr addCollisionCallbackBetweenType:obj_passive otherType:obj_active target:self 
								 selector:@selector(handleCollision:arbiter:space:) moments:COLLISION_BEGIN,nil];
	[smgr addCollisionCallbackBetweenType:obj_active otherType:0 target:self 
								 selector:@selector(handleCollision:arbiter:space:) moments:COLLISION_BEGIN,nil];
	///cpSpaceAddCollisionPairFunc(smgr.space,obj_active,obj_passive,&ballCollision ,self );
	//cpSpaceAddCollisionHandler(smgr.space, obj_active,0, objcoll,NULL, NULL, NULL, NULL);
	[smgr start];
	
	//[smgr addSegmentAtWorldAnchor:cpv(72,13) toWorldAnchor:cpv(480,13) mass:STATIC_MASS radius:1];
	//[smgr addSegmentAtWorldAnchor:cpv(72,13) toWorldAnchor:cpv(72,133) mass:STATIC_MASS radius:1];
	[smgr addSegmentAtWorldAnchor:cpv(72,133) toWorldAnchor:cpv(0,133) mass:STATIC_MASS radius:1];
	[self addChild:[smgr createDebugLayer]];

	
	
	
	
	
	self.isTouchEnabled = YES;
	[self schedule: @selector(gamelogic:)];
	return self;
}

- (void) dealloc
{
	[smgr release];
	[super dealloc];
}




- (BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
	CGPoint click = [self convertTouchToNodeSpaceAR: touch];
	click.x+=160;
	click.y+=240;
	if(click.x>90 || click.y<390){
		NSLog(@"touch ignored");
		return NO;
	}
	curobjdropped=0;
	
	NSLog(@"new**: %i  %i",newobjready,(int)newobjwaiting);
	if (newobjready==1 && newobjwaiting==1 ) {
		newobjwaiting=0;
		return YES;	//use this touch
	}
	else {
		return NO;
	}

	//[self createBlockAt:cpv(240,201) width:12 height:40 mass:100];
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	if (curobjdropped==0 && newobjready==1){
		NSLog(@"moving");
		CGPoint click = [self convertTouchToNodeSpaceAR: touch];
		click.x+=160;
		click.y+=240;
		levelobj[curobjtodrop].x=click.x;		
		levelobj[curobjtodrop].y=click.y;
		cpBodySetPos(levelobj[curobjtodrop].shape->body, CGPointMake(click.x, click.y));
		cpBodySetForce(levelobj[curobjtodrop].shape->body, cpv(0,0));
		cpBodySetVel(levelobj[curobjtodrop].shape->body, cpv(0,0));
		cpBodySetAngVel(levelobj[curobjtodrop].shape->body, 0);
		
		cpSpaceRehashShape(smgr.space, levelobj[curobjtodrop].shape);
	}
}


- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{		
	NSLog(@"TouchEnded");
	[self dropcurobj];
	newobjready=1;
}


- (CCNode*) createBlockAt:(cpVect)pt
					width:(int)w
				   height:(int)h
					 mass:(int)mass
{
	cpShape *shape = [smgr addRectAt:pt mass:mass width:w height:h rotation:0];
	cpShapeNode *node = [cpShapeNode nodeWithShape:shape];
	node.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);
	
	[self addChild:node];
	
	return node;
}



-(void) gamelogic:(ccTime)dt{
	if(curobjdropped==0 || newobjready==0 || newobjwaiting==1){
		cpBodySetForce(levelobj[curobjtodrop].shape->body, cpv(0,0));
		cpBodySetVel(levelobj[curobjtodrop].shape->body, cpv(0,0));
		cpBodySetAngVel(levelobj[curobjtodrop].shape->body, 0);
		cpBodySetPos(levelobj[curobjtodrop].shape->body, CGPointMake(levelobj[curobjtodrop].x, levelobj[curobjtodrop].y));
	}
}



-(BOOL) handleCollision:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space{
	NSLog(@"Kollision handl");
	[self dropcurobj];
	return YES;
}

-(void) dropcurobj{
	if (curobjdropped==0){
		NSLog(@"dropping");
		curobjdropped=1;
		levelobj[curobjtodrop].shape->collision_type=obj_passive ;
		//[smgr morphShapeToActive:levelobj[curobjtodrop].shape mass:levelobj[curobjtodrop].a*levelobj[curobjtodrop].b];
		
		//nächstes bewegliches obj
		curobjtodrop+=1;
		levelobj[curobjtodrop].shape = [smgr addRectAt:cpv(levelobj[curobjtodrop].x,levelobj[curobjtodrop].y) mass:levelobj[curobjtodrop].a*levelobj[curobjtodrop].b 
												 width:levelobj[curobjtodrop].a height:levelobj[curobjtodrop].b rotation:levelobj[curobjtodrop].c];
		levelobj[curobjtodrop].shape->collision_type=obj_active ;
		levelobj[curobjtodrop].node = [cpShapeNode nodeWithShape:levelobj[curobjtodrop].shape];
		levelobj[curobjtodrop].node.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);
		[self addChild:levelobj[curobjtodrop].node];
		newobjready=0;
		newobjwaiting=1;
	}
}


-(ccColor3B) rndcolor{
	unsigned char r,g,b;
	int rnd1,rnd2;
	rnd1=rand()%3 + 1; //1-3
	rnd2=rand()%2 + 1; //1-2
	
	if (rnd1==1){
		r=rand()%256;
		if(rnd2==1){
			g=rand()%256;
		}
		if(rnd2==2){
			b=rand()%256;
		}
	}
	if (rnd1==2){
		g=rand()%256;
		if(rnd2==1){
			r=rand()%256;
		}
		if(rnd2==2){
			b=rand()%256;
		}
	}
	if (rnd1==3){
		b=rand()%256;
		if(rnd2==1){
			r=rand()%256;
		}
		if(rnd2==2){
			g=rand()%256;
		}
	}
	ccColor3B color;
	color=ccc3(r, g, b);
	return color;
}





@end