Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TileMap.h"
- #define PTM_RATIO 40.0f
- using namespace cocos2d;
- TileMap::TileMap()
- {
- }
- TileMap::~TileMap()
- {
- }
- //Initializes the TileMap with the given map file and its layers.
- //fileName is the name of the map, for example Map.tmx
- bool TileMap::init(cocos2d::CCLayer* l, char* fileName, char* collisionLayer)
- {
- layer = l;
- b2Vec2 gravity = b2Vec2(0.0f, -9.8f);
- bool doSleep = true;
- world = new b2World(gravity);//, doSleep);
- m_debugDraw = new GLESDebugDraw();
- world->SetDebugDraw(m_debugDraw);
- uint32 flags = 0;
- flags += m_debugDraw->e_shapeBit;//b2DebugDraw:: e_shapeBit;
- m_debugDraw->SetFlags(flags);
- addScrollingBackgroundWithTileMap(fileName);
- drawCollisionTiles();
- /*Ab hier alles in Player auslagern*/
- CCSprite *sprite = CCSprite::spriteWithFile("Player.png");
- sprite->setPosition(ccp(100.0f, 180.0f));
- layer->addChild(sprite);
- b2BodyDef playerBodyDef;
- playerBodyDef.type = b2_dynamicBody;
- playerBodyDef.position.Set(sprite->getPosition().x/PTM_RATIO, sprite->getPosition().y/PTM_RATIO);
- playerBodyDef.userData = sprite;
- playerBody = world->CreateBody(&playerBodyDef);
- b2CircleShape circleShape;
- circleShape.m_radius = 1.0f;
- b2FixtureDef fixtureDef;
- fixtureDef.shape = &circleShape;
- fixtureDef.density = 1.0f;
- fixtureDef.friction = 0.0f;
- fixtureDef.restitution = 0.0f;
- playerBody->CreateFixture(&fixtureDef);
- b2Vec2 impulse = b2Vec2(10, 0);
- playerBody->ApplyLinearImpulse(impulse, playerBody->GetWorldCenter());
- /*bis hier*/
- return true;
- }
- void TileMap::addScrollingBackgroundWithTileMap(char* fileName)
- {
- _tileMap = CCTMXTiledMap::tiledMapWithTMXFile(fileName);
- _tileMap->setAnchorPoint(ccp(0, 0));
- layer->addChild(_tileMap);
- }
- void TileMap::update(float dt)
- {
- int32 velocityIterations = 8;
- int32 positionIterations = 1;
- // Instruct the world to perform a single step of simulation. It is
- // generally best to keep the time step and iterations fixed.
- world->Step(dt, velocityIterations, positionIterations);
- //Iterate over the bodies in the physics world
- for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
- if (b->GetUserData() != NULL) {
- //Synchronize the AtlasSprites position and rotation with the corresponding body
- CCSprite *myActor = (CCSprite*)b->GetUserData();
- myActor->setPosition(CCPointMake( b->GetPosition().x * PTM_RATIO,
- b->GetPosition().y * PTM_RATIO));
- myActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
- }
- }
- b2Vec2 pos = playerBody->GetPosition();
- CCPoint newPos = ccp(-1 * pos.x * PTM_RATIO + 50, this->layer->getPositionY() * PTM_RATIO);
- layer->setPosition(newPos);
- //draw();
- }
- void TileMap::makeBox2dObjAt(CCPoint _point, CCPoint _size, bool d, long rotation, long friction, long dens, long rest, int boxID)
- {
- // Define the dynamic body.
- //Set up a 1m squared box in the physics world
- b2BodyDef bodyDef;
- // bodyDef.angle = r;
- if(d)
- bodyDef.type = b2_dynamicBody;
- bodyDef.position.Set(_point.x/PTM_RATIO, _point.y/PTM_RATIO);
- bodyDef.userData = NULL;
- //bodyDef.userData = sprite;
- b2Body *body = world->CreateBody(&bodyDef);
- // Define another box shape for our dynamic body.
- b2PolygonShape dynamicBox;
- dynamicBox.SetAsBox(_size.x/2/PTM_RATIO, _size.y/2/PTM_RATIO);
- // Define the dynamic body fixture.
- b2FixtureDef fixtureDef;
- fixtureDef.shape = &dynamicBox;
- fixtureDef.density = dens;
- fixtureDef.friction = friction;
- fixtureDef.restitution = rest;
- body->CreateFixture(&fixtureDef);
- }
- void TileMap::drawCollisionTiles()
- {
- CCTMXObjectGroup *collisionObjects = _tileMap->objectGroupNamed("CollisionOL");
- CCDictionary *objPoint;
- int x, y, w, h;
- for(int i = 0; i < collisionObjects->getObjects()->count(); i++)
- {
- objPoint = (CCDictionary*) collisionObjects->getObjects()->objectAtIndex(i);
- x = objPoint->valueForKey("x")->intValue();
- y = objPoint->valueForKey("y")->intValue();
- w = objPoint->valueForKey("width")->intValue();
- h = objPoint->valueForKey("height")->intValue();
- if(y < 0)
- y = 0;
- CCPoint _point = ccp(x+w/2,y+h/2);
- CCPoint _size = ccp(w,h);
- //_point = ccpAdd(_point, ccp(_size.x/2, _size.y/2));
- makeBox2dObjAt(_point, _size, false, 0, 0.0f, 0.0f, 0, -1);
- }
- }
- void TileMap::draw()
- {
- glDisable(GL_TEXTURE_2D);
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- world->DrawDebugData();
- // restore default GL states
- glEnable(GL_TEXTURE_2D);
- glEnableClientState(GL_COLOR_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- }
Add Comment
Please, Sign In to add comment