@@ -58,7 +58,7 @@ bool GameScene::init() {
5858 map->getLayer (" background" )->setGlobalZOrder (-1 );
5959
6060 // "destructable" layer, indicating the tiles which can be blown up by bubbles
61- TMXLayer * destructable = map->getLayer (" destructable" );
61+ destructable = map->getLayer (" destructable" );
6262 destructable->setVisible (false ); /* set it transparent*/
6363
6464 // object layer (probably useless)
@@ -117,8 +117,7 @@ bool GameScene::init() {
117117 case EventKeyboard::KeyCode::KEY_SPACE:
118118 if (hero->can_set_bomb ()) {
119119 Bomb *bomb = hero->set_bomb ();
120- addChild (bomb);
121- bomb->setGlobalZOrder (0 );
120+ current_bombs.pushBack (bomb);
122121 }
123122 break ;
124123 }
@@ -174,6 +173,13 @@ void GameScene::update(float delta) {
174173
175174 makeMove (Vec2 (position_x, position_y));
176175
176+ /* Bombs blow up destructable bricks */
177+ while (!current_bombs.empty () && !current_bombs.front ()->bombIsCounting ()) {
178+ Bomb * bomb = current_bombs.front ();
179+ bomb_explode (bomb);
180+ current_bombs.erase (0 );
181+ }
182+
177183 /* Test pick_item method */
178184 Item * speed_up_item = Item::create ();
179185 speed_up_item->setPosition (Vec2 (500 , 380 ));
@@ -186,14 +192,58 @@ void GameScene::update(float delta) {
186192 }
187193}
188194
189- void GameScene::bomb_explode ()
190- {
191-
192- }
193-
194- Vector<Bomb*> GameScene::get_all_bomb ()
195+ void GameScene::bomb_explode (Bomb *bomb)
195196{
196- return this ->currentBomb ;
197+ int power = bomb->getPower ();
198+ Vec2 bomb_tile_coord = tileCoordFromPosition (bomb->getPosition ());
199+ int firstGid = destructable->getTileSet ()->_firstGid ;
200+
201+ int i = 0 ;
202+ for (; i < power && bomb_tile_coord.x - i - 1 >= 0 ; i++) {
203+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
204+ if (GID - firstGid >= 0 ) {
205+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
206+ if (bomb_tile_coord.y > 0 ) {
207+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y - 1 ));
208+ }
209+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
210+ break ;
211+ }
212+ }
213+ i = 0 ;
214+ for (; i < power && bomb_tile_coord.x + i + 1 < MAP_SIZE.width ; i++) {
215+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
216+ if (GID - firstGid >= 0 ) {
217+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
218+ if (bomb_tile_coord.y > 0 ) {
219+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y - 1 ));
220+ }
221+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
222+ break ;
223+ }
224+ }
225+ int j = 0 ;
226+ for (; j < power && bomb_tile_coord.y + j + 1 < MAP_SIZE.height ; j++) {
227+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
228+ if (GID - firstGid >= 0 ) {
229+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
230+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j));
231+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
232+ break ;
233+ }
234+ }
235+ j = 0 ;
236+ for (; j < power && bomb_tile_coord.y - j - 1 >= 0 ; j++) {
237+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
238+ if (GID - firstGid >= 0 ) {
239+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
240+ if (bomb_tile_coord.y - j - 1 != 0 ) {
241+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 2 ));
242+ }
243+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
244+ break ;
245+ }
246+ }
197247}
198248
199249bool GameScene::isOutOfMap (Vec2 pos)
@@ -237,57 +287,26 @@ bool GameScene::collideWithBubble(cocos2d::Vec2 targetPos)
237287
238288void GameScene::makeMove (Vec2 position)
239289{
240- Vec2 targetPos = position;
241-
242290 // correct the detection deviation caused by the sprite size
243291 Size figSize = hero->getContentSize ();
244-
245- if (y_movement == MOVE_STOP) {
246- switch (x_movement) {
247- case MOVE_LEFT:
248- targetPos.x -= 1 * figSize.width / 3 ;
249- break ;
250- case MOVE_RIGHT:
251- targetPos.x += 1 * figSize.width / 3 ;
252- break ;
253- }
254- }
255- else {
256- switch (y_movement) {
257- case MOVE_UP:
258- if (x_movement == MOVE_STOP) {
259- break ;
260- }
261- else {
262- if (x_movement == MOVE_LEFT) {
263- targetPos.x -= 1 * figSize.width / 3 ;
264- }
265- else {
266- targetPos.x += 1 * figSize.width / 3 ;
267- }
268- break ;
269- }
270- case MOVE_DOWN:
271- targetPos.y -= figSize.height / 2 + 3 ;
272- if (x_movement == MOVE_STOP) {
273- break ;
274- }
275- else {
276- if (x_movement == MOVE_LEFT) {
277- targetPos.x -= 1 * figSize.width / 3 ;
278- }
279- else {
280- targetPos.x += 1 * figSize.width / 3 ;
281- }
282- break ;
283- }
284- }
292+
293+ Vec2 targetPos_down (position.x , position.y - figSize.height / 2 );
294+ Vec2 targetPos_top = position;
295+
296+ switch (x_movement) {
297+ case MOVE_LEFT:
298+ targetPos_down.x -= 1 * figSize.width / 3 ;
299+ targetPos_top.x -= 1 * figSize.width / 3 ;
300+ break ;
301+ case MOVE_RIGHT:
302+ targetPos_down.x += 1 * figSize.width / 3 ;
303+ targetPos_top.x += 1 * figSize.width / 3 ;
304+ break ;
285305 }
286-
287306 // if the target position is out of bound
288- if (isOutOfMap (targetPos )) return ;
307+ if (isOutOfMap (targetPos_down) || isOutOfMap (targetPos_top )) return ;
289308
290- if (collideWithBrick (targetPos ) || collideWithBubble (targetPos )) return ;
309+ if (collideWithBrick (targetPos_down ) || collideWithBrick (targetPos_top) || collideWithBubble (targetPos_down) || collideWithBubble (targetPos_top )) return ;
291310
292311 hero->setPosition (position);
293312}
0 commit comments