@@ -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 }
@@ -167,6 +166,13 @@ void GameScene::update(float delta) {
167166
168167 makeMove (Vec2 (position_x, position_y));
169168
169+ /* Bombs blow up destructable bricks */
170+ while (!current_bombs.empty () && !current_bombs.front ()->bombIsCounting ()) {
171+ Bomb * bomb = current_bombs.front ();
172+ bomb_explode (bomb);
173+ current_bombs.erase (0 );
174+ }
175+
170176 /* Test pick_item method */
171177 Item * speed_up_item = Item::create ();
172178 speed_up_item->setPosition (Vec2 (500 , 380 ));
@@ -180,14 +186,58 @@ void GameScene::update(float delta) {
180186
181187}
182188
183- void GameScene::bomb_explode ()
184- {
185-
186- }
187-
188- Vector<Bomb*> GameScene::get_all_bomb ()
189+ void GameScene::bomb_explode (Bomb *bomb)
189190{
190- return this ->currentBomb ;
191+ int power = bomb->getPower ();
192+ Vec2 bomb_tile_coord = tileCoordFromPosition (bomb->getPosition ());
193+ int firstGid = destructable->getTileSet ()->_firstGid ;
194+
195+ int i = 0 ;
196+ for (; i < power && bomb_tile_coord.x - i - 1 >= 0 ; i++) {
197+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
198+ if (GID - firstGid >= 0 ) {
199+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
200+ if (bomb_tile_coord.y > 0 ) {
201+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y - 1 ));
202+ }
203+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x - i - 1 , bomb_tile_coord.y ));
204+ break ;
205+ }
206+ }
207+ i = 0 ;
208+ for (; i < power && bomb_tile_coord.x + i + 1 < MAP_SIZE.width ; i++) {
209+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
210+ if (GID - firstGid >= 0 ) {
211+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
212+ if (bomb_tile_coord.y > 0 ) {
213+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y - 1 ));
214+ }
215+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x + i + 1 , bomb_tile_coord.y ));
216+ break ;
217+ }
218+ }
219+ int j = 0 ;
220+ for (; j < power && bomb_tile_coord.y + j + 1 < MAP_SIZE.height ; j++) {
221+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
222+ if (GID - firstGid >= 0 ) {
223+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
224+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j));
225+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y + j + 1 ));
226+ break ;
227+ }
228+ }
229+ j = 0 ;
230+ for (; j < power && bomb_tile_coord.y - j - 1 >= 0 ; j++) {
231+ int GID = destructable->getTileGIDAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
232+ if (GID - firstGid >= 0 ) {
233+ map->getLayer (" bricks" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
234+ if (bomb_tile_coord.y - j - 1 != 0 ) {
235+ map->getLayer (" tops" )->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 2 ));
236+ }
237+ destructable->removeTileAt (Vec2 (bomb_tile_coord.x , bomb_tile_coord.y - j - 1 ));
238+ break ;
239+ }
240+ }
191241}
192242
193243bool GameScene::isOutOfMap (Vec2 pos)
@@ -231,57 +281,26 @@ bool GameScene::collideWithBubble(cocos2d::Vec2 targetPos)
231281
232282void GameScene::makeMove (Vec2 position)
233283{
234- Vec2 targetPos = position;
235-
236284 // correct the detection deviation caused by the sprite size
237285 Size figSize = hero->getContentSize ();
238-
239- if (y_movement == MOVE_STOP) {
240- switch (x_movement) {
241- case MOVE_LEFT:
242- targetPos.x -= 1 * figSize.width / 3 ;
243- break ;
244- case MOVE_RIGHT:
245- targetPos.x += 1 * figSize.width / 3 ;
246- break ;
247- }
248- }
249- else {
250- switch (y_movement) {
251- case MOVE_UP:
252- if (x_movement == MOVE_STOP) {
253- break ;
254- }
255- else {
256- if (x_movement == MOVE_LEFT) {
257- targetPos.x -= 1 * figSize.width / 3 ;
258- }
259- else {
260- targetPos.x += 1 * figSize.width / 3 ;
261- }
262- break ;
263- }
264- case MOVE_DOWN:
265- targetPos.y -= figSize.height / 2 + 3 ;
266- if (x_movement == MOVE_STOP) {
267- break ;
268- }
269- else {
270- if (x_movement == MOVE_LEFT) {
271- targetPos.x -= 1 * figSize.width / 3 ;
272- }
273- else {
274- targetPos.x += 1 * figSize.width / 3 ;
275- }
276- break ;
277- }
278- }
286+
287+ Vec2 targetPos_down (position.x , position.y - figSize.height / 2 );
288+ Vec2 targetPos_top = position;
289+
290+ switch (x_movement) {
291+ case MOVE_LEFT:
292+ targetPos_down.x -= 1 * figSize.width / 3 ;
293+ targetPos_top.x -= 1 * figSize.width / 3 ;
294+ break ;
295+ case MOVE_RIGHT:
296+ targetPos_down.x += 1 * figSize.width / 3 ;
297+ targetPos_top.x += 1 * figSize.width / 3 ;
298+ break ;
279299 }
280-
281300 // if the target position is out of bound
282- if (isOutOfMap (targetPos )) return ;
301+ if (isOutOfMap (targetPos_down) || isOutOfMap (targetPos_top )) return ;
283302
284- if (collideWithBrick (targetPos ) || collideWithBubble (targetPos )) return ;
303+ if (collideWithBrick (targetPos_down ) || collideWithBrick (targetPos_top) || collideWithBubble (targetPos_down) || collideWithBubble (targetPos_top )) return ;
285304
286305 hero->setPosition (position);
287306}
0 commit comments