Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include "TileMap.h"
  2.  
  3. #define PTM_RATIO 40.0f
  4.  
  5. using namespace cocos2d;
  6.  
  7. TileMap::TileMap()
  8. {
  9. }
  10.  
  11. TileMap::~TileMap()
  12. {
  13.  
  14. }
  15.  
  16. //Initializes the TileMap with the given map file and its layers.
  17. //fileName is the name of the map, for example Map.tmx
  18. bool TileMap::init(cocos2d::CCLayer* l, char* fileName, char* collisionLayer)
  19. {
  20. layer = l;
  21.  
  22. b2Vec2 gravity = b2Vec2(0.0f, -9.8f);
  23. bool doSleep = true;
  24. world = new b2World(gravity);//, doSleep);
  25.  
  26. m_debugDraw = new GLESDebugDraw();
  27. world->SetDebugDraw(m_debugDraw);
  28. uint32 flags = 0;
  29. flags += m_debugDraw->e_shapeBit;//b2DebugDraw:: e_shapeBit;
  30. m_debugDraw->SetFlags(flags);
  31.  
  32. addScrollingBackgroundWithTileMap(fileName);
  33. drawCollisionTiles();
  34.  
  35. /*Ab hier alles in Player auslagern*/
  36. CCSprite *sprite = CCSprite::spriteWithFile("Player.png");
  37. sprite->setPosition(ccp(100.0f, 180.0f));
  38.  
  39. layer->addChild(sprite);
  40.  
  41. b2BodyDef playerBodyDef;
  42. playerBodyDef.type = b2_dynamicBody;
  43. playerBodyDef.position.Set(sprite->getPosition().x/PTM_RATIO, sprite->getPosition().y/PTM_RATIO);
  44. playerBodyDef.userData = sprite;
  45.  
  46. playerBody = world->CreateBody(&playerBodyDef);
  47.  
  48. b2CircleShape circleShape;
  49. circleShape.m_radius = 1.0f;
  50.  
  51. b2FixtureDef fixtureDef;
  52. fixtureDef.shape = &circleShape;
  53. fixtureDef.density = 1.0f;
  54. fixtureDef.friction = 0.0f;
  55. fixtureDef.restitution = 0.0f;
  56. playerBody->CreateFixture(&fixtureDef);
  57.  
  58. b2Vec2 impulse = b2Vec2(10, 0);
  59. playerBody->ApplyLinearImpulse(impulse, playerBody->GetWorldCenter());
  60.  
  61. /*bis hier*/
  62.  
  63. return true;
  64. }
  65.  
  66. void TileMap::addScrollingBackgroundWithTileMap(char* fileName)
  67. {
  68. _tileMap = CCTMXTiledMap::tiledMapWithTMXFile(fileName);
  69. _tileMap->setAnchorPoint(ccp(0, 0));
  70. layer->addChild(_tileMap);
  71. }
  72.  
  73.  
  74. void TileMap::update(float dt)
  75. {
  76. int32 velocityIterations = 8;
  77. int32 positionIterations = 1;
  78.  
  79. // Instruct the world to perform a single step of simulation. It is
  80. // generally best to keep the time step and iterations fixed.
  81. world->Step(dt, velocityIterations, positionIterations);
  82.  
  83.  
  84. //Iterate over the bodies in the physics world
  85. for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
  86. if (b->GetUserData() != NULL) {
  87. //Synchronize the AtlasSprites position and rotation with the corresponding body
  88. CCSprite *myActor = (CCSprite*)b->GetUserData();
  89. myActor->setPosition(CCPointMake( b->GetPosition().x * PTM_RATIO,
  90. b->GetPosition().y * PTM_RATIO));
  91. myActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
  92. }
  93. }
  94.  
  95. b2Vec2 pos = playerBody->GetPosition();
  96. CCPoint newPos = ccp(-1 * pos.x * PTM_RATIO + 50, this->layer->getPositionY() * PTM_RATIO);
  97.  
  98. layer->setPosition(newPos);
  99. //draw();
  100.  
  101. }
  102.  
  103. void TileMap::makeBox2dObjAt(CCPoint _point, CCPoint _size, bool d, long rotation, long friction, long dens, long rest, int boxID)
  104. {
  105. // Define the dynamic body.
  106. //Set up a 1m squared box in the physics world
  107. b2BodyDef bodyDef;
  108. // bodyDef.angle = r;
  109.  
  110. if(d)
  111. bodyDef.type = b2_dynamicBody;
  112.  
  113. bodyDef.position.Set(_point.x/PTM_RATIO, _point.y/PTM_RATIO);
  114. bodyDef.userData = NULL;
  115. //bodyDef.userData = sprite;
  116.  
  117. b2Body *body = world->CreateBody(&bodyDef);
  118.  
  119. // Define another box shape for our dynamic body.
  120. b2PolygonShape dynamicBox;
  121. dynamicBox.SetAsBox(_size.x/2/PTM_RATIO, _size.y/2/PTM_RATIO);
  122.  
  123. // Define the dynamic body fixture.
  124. b2FixtureDef fixtureDef;
  125. fixtureDef.shape = &dynamicBox;
  126. fixtureDef.density = dens;
  127. fixtureDef.friction = friction;
  128. fixtureDef.restitution = rest;
  129. body->CreateFixture(&fixtureDef);
  130. }
  131.  
  132. void TileMap::drawCollisionTiles()
  133. {
  134. CCTMXObjectGroup *collisionObjects = _tileMap->objectGroupNamed("CollisionOL");
  135. CCDictionary *objPoint;
  136.  
  137. int x, y, w, h;
  138.  
  139. for(int i = 0; i < collisionObjects->getObjects()->count(); i++)
  140. {
  141. objPoint = (CCDictionary*) collisionObjects->getObjects()->objectAtIndex(i);
  142. x = objPoint->valueForKey("x")->intValue();
  143. y = objPoint->valueForKey("y")->intValue();
  144. w = objPoint->valueForKey("width")->intValue();
  145. h = objPoint->valueForKey("height")->intValue();
  146.  
  147. if(y < 0)
  148. y = 0;
  149.  
  150. CCPoint _point = ccp(x+w/2,y+h/2);
  151. CCPoint _size = ccp(w,h);
  152. //_point = ccpAdd(_point, ccp(_size.x/2, _size.y/2));
  153.  
  154. makeBox2dObjAt(_point, _size, false, 0, 0.0f, 0.0f, 0, -1);
  155. }
  156. }
  157.  
  158. void TileMap::draw()
  159. {
  160. glDisable(GL_TEXTURE_2D);
  161. glDisableClientState(GL_COLOR_ARRAY);
  162. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  163.  
  164. world->DrawDebugData();
  165.  
  166. // restore default GL states
  167. glEnable(GL_TEXTURE_2D);
  168. glEnableClientState(GL_COLOR_ARRAY);
  169. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  170. }
Add Comment
Please, Sign In to add comment