Created
April 29, 2011 03:59
-
-
Save Alos/947811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AppController.j | |
* HelixC | |
* | |
* Created by You on April 26, 2011. | |
* Copyright 2011, Your Company All rights reserved. | |
*/ | |
@import <Foundation/CPObject.j> | |
@import "ShipView.j" | |
var b2Vec2 = Box2D.Common.Math.b2Vec2 | |
, b2BodyDef = Box2D.Dynamics.b2BodyDef | |
, b2Body = Box2D.Dynamics.b2Body | |
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef | |
, b2Fixture = Box2D.Dynamics.b2Fixture | |
, b2World = Box2D.Dynamics.b2World | |
, b2MassData = Box2D.Collision.Shapes.b2MassData | |
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape | |
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape | |
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw | |
, b2Point = Box2D.Collision.b2Point | |
; | |
var ZERO_POINT = new b2Point(0, 0) | |
var ZERO_VECTOR = new b2Vec2(0, 0); | |
var MAX_VELOCITY = 10; | |
var timeStep = 1.0 / 100; | |
@implementation AppController : CPObject | |
{ | |
Box2D.Dynamics.b2World world; | |
CPMutableArray arrayOfObjects; | |
CPMutableArray arrayOfShips; | |
CPTimer heartBeatTimer; | |
CPView contentView; | |
} | |
- (void)applicationDidFinishLaunching:(CPNotification)aNotification | |
{ | |
CPLogRegister(CPLogConsole); | |
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask]; | |
contentView = [theWindow contentView]; | |
arrayOfObjects = [[CPMutableArray alloc] init]; | |
arrayOfShips = [[CPMutableArray alloc] init] | |
[self initWorld]; | |
[self createObjects:10]; | |
[self createShips]; | |
CPLog.info("Starting timer!"); | |
heartBeatTimer = [CPTimer scheduledTimerWithTimeInterval:.5 target: self selector:"beat:" userInfo:nil repeats:YES ]; | |
[theWindow orderFront:self]; | |
} | |
- (void) initWorld{ | |
world = new b2World( | |
new b2Vec2(0, 10) //gravity | |
, true //allow sleep | |
); | |
} | |
/** | |
Creates the objects that represent the model | |
*/ | |
- (void) createObjects:(CPNumber)numberOfObjects{ | |
var fixDef = new b2FixtureDef; | |
fixDef.density = 1.0; | |
fixDef.friction = 0.5; | |
fixDef.restitution = 0.2; | |
fixDef.shape = new b2CircleShape( | |
Math.random() + 0.1 //radius | |
); | |
for(var i=0; i<numberOfObjects; i++){ | |
var bodyDef = new b2BodyDef; | |
bodyDef.type = b2Body.b2_dynamicBody; | |
var initialPosition = [self getNewInitialPosition]; | |
CPLog.info("Position #%@: %@, %@",i ,initialPosition.x, initialPosition.y); | |
bodyDef.position.Set(initialPosition); | |
var body = world.CreateBody(bodyDef); | |
body.CreateFixture(fixDef); | |
body.SetBullet(false); | |
var initialForce = [self getNewInitialForce]; | |
CPLog.info("Force #%@: %@, %@",i ,initialForce.x, initialForce.y); | |
body.ApplyForce(initialForce, ZERO_VECTOR); | |
[arrayOfObjects addObject: body]; | |
} | |
debugger; | |
} | |
/** | |
Creates the a ship per model object | |
*/ | |
- (void) createShips{ | |
for(var i = 0; i< [arrayOfObjects count]; i++){ | |
var b = [arrayOfObjects objectAtIndex:i]; | |
var vecPos = b.GetPosition(); | |
var newShip = [[ShipView alloc] initWithPosition: CGPointMake(vecPos.x, vecPos.y)]; | |
[contentView addSubview:newShip]; | |
[arrayOfShips addObject: newShip]; | |
} | |
} | |
/** | |
Gets the initial force | |
*/ | |
- (b2Vec2) getNewInitialForce{ | |
var a = Math.random() * MAX_VELOCITY; | |
var b = Math.random() * MAX_VELOCITY; | |
return new b2Vec2(a * 2, b * 2); | |
} | |
/** | |
Gets the initial force | |
*/ | |
- (b2Vec2) getNewInitialPosition{ | |
var a = Math.random() * 400; | |
var b = Math.random() * 400; | |
return new b2Vec2(a , b); | |
} | |
/** | |
The beat of the game | |
*/ | |
- (void) beat:(CPTimer)theTimer{ | |
debugger; | |
world.Step(timeStep, 10, 10); | |
world.ClearForces(); | |
for(var i = 0; i< [arrayOfObjects count]; i++){ | |
var b = [arrayOfObjects objectAtIndex:i]; | |
[self updateShip: i position:CGPointMake(b.position.x, b.position.y)]; | |
} | |
} | |
/** | |
Stops the heartbeatTimer | |
*/ | |
-(void) stopBeat{ | |
[heartBeatTimer invalidate]; | |
} | |
/** | |
Updates the position of the ships | |
*/ | |
- (void) updateShip:(CPNumber)index position:(CGPoint)aPoint{ | |
CPLog.trace("Moving ship #%@: %@, %@",index, aPoint.x, aPoint.y); | |
var aShip = [arrayOfShips objectAtIndex:index]; | |
[aShip setFrameOrigin:aPoint]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment