Skip to content

Commit c06467d

Browse files
committed
explosion animation & injured
1. add explosion animation 2. hero could be hurt by bubbles
1 parent 309dc18 commit c06467d

File tree

9 files changed

+537
-15
lines changed

9 files changed

+537
-15
lines changed

BombingAdventure/Classes/AppDelegate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool AppDelegate::applicationDidFinishLaunching() {
8787
director->runWithScene(scene);
8888

8989
// background music preloaded
90-
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("music&effect/HelloMusic");
90+
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("music&effect/HelloMusic.mp3");
9191

9292
return true;
9393
}

BombingAdventure/Classes/Bomb.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,32 @@ void Bomb::bombUpdate(float delta) {
1919

2020
currentTime += delta;
2121

22-
if (currentTime >= endTime) {
23-
isCounting = false;
22+
if (currentTime >= endTime && currentTime < endTime + 0.5f) {
23+
exploded = true;
24+
removeAllChildren();
25+
}
26+
27+
if (currentTime >= endTime + 0.5f) {
28+
isCounting = false;
29+
removeAllChildren();
2430
this->unschedule(schedule_selector(Bomb::bombUpdate));
25-
this->removeAllChildren();
26-
this->removeFromParent();
31+
removeFromParent();
2732
}
2833
}
2934

3035
void Bomb::startCounting(float time) {
3136
endTime = time;
3237
currentTime = 0;
3338
isCounting = true;
39+
exploded = false;
3440
}
3541

3642
int Bomb::getPower()
3743
{
3844
return power;
3945
}
4046

41-
bool Bomb::bombIsCounting()
47+
bool Bomb::bombIsExploded()
4248
{
43-
return isCounting;
49+
return exploded;
4450
}
45-

BombingAdventure/Classes/Bomb.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ class Bomb : public Entity {
5959

6060
int getPower();
6161

62-
bool bombIsCounting();
62+
bool bombIsExploded();
63+
64+
6365

6466
private:
6567

6668
int power; /* The power of the bomb */
6769
float currentTime; /* Current Time of timing */
6870
float endTime; /* The End Time set for explosion */
6971
bool isCounting; /* Whether counting is being processed. */
70-
72+
bool exploded;
7173
};
7274

7375

BombingAdventure/Classes/GameScene.cpp

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "GameScene.h"
2+
#include "HelloWorldScene.h"
23

34
//using namespace std;
45
USING_NS_CC;
@@ -78,7 +79,7 @@ bool GameScene::init() {
7879
float x = spawnPoint["x"].asFloat();
7980
float y = spawnPoint["y"].asFloat();
8081
hero = Player::create();
81-
hero->setPosition(Vec2(x+20, y+70));
82+
hero->setPosition(Vec2(x+20, y+70));
8283
this->addChild(hero, 100, 200);
8384

8485

@@ -164,6 +165,10 @@ bool GameScene::init() {
164165

165166
void GameScene::update(float delta) {
166167

168+
if (!hero->is_alive()) {
169+
game_over();
170+
}
171+
167172
/* Following part updates the movement of player */
168173
float position_x = hero->getPositionX();
169174
float position_y = hero->getPositionY();
@@ -176,7 +181,7 @@ void GameScene::update(float delta) {
176181
makeMove(Vec2(position_x, position_y));
177182

178183
/* Bombs blow up destructable bricks */
179-
while (!current_bombs.empty() && !current_bombs.front()->bombIsCounting()) {
184+
while (!current_bombs.empty() && current_bombs.front()->bombIsExploded()) {
180185
Bomb * bomb = current_bombs.front();
181186
bomb_explode(bomb);
182187
current_bombs.erase(0);
@@ -207,7 +212,6 @@ void GameScene::bomb_explode(Bomb *bomb)
207212
int firstGid_of_brk = bricks->getTileSet()->_firstGid;
208213

209214
for (; l_range < power && bomb_tile_coord.x - l_range - 1 >= 0; l_range++) {
210-
211215
int GID_brk = bricks->getTileGIDAt(Vec2(bomb_tile_coord.x - l_range - 1, bomb_tile_coord.y));
212216
if (GID_brk - firstGid_of_brk >= 0){
213217
int GID_des = destructable->getTileGIDAt(Vec2(bomb_tile_coord.x - l_range - 1, bomb_tile_coord.y));
@@ -265,12 +269,59 @@ void GameScene::bomb_explode(Bomb *bomb)
265269
}
266270
}
267271

272+
hero->bomb_vs_man(bomb_tile_coord, l_range, r_range, u_range, d_range);
273+
268274
/* Now we have l_range, r_value, u_value, d_value, which indicate the explosion
269275
* range of the bomb in the four directions. What you need to do now is to
270276
* display the animation effect of bombs.
271277
* Please add your code here...
272278
*/
273279

280+
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Explosion.plist");
281+
Sprite *wave = Sprite::createWithSpriteFrameName("ExplosionCenter_01.png");
282+
bomb->addChild(wave);
283+
wave->setPosition(Vec2(TILE_SIZE.width/2, TILE_SIZE.height/2));
284+
285+
for (int i = 0; i < l_range; i++) {
286+
Sprite *l_wave;
287+
if (i = l_range - 1) {
288+
l_wave = Sprite::createWithSpriteFrameName("ExplosionLEFT_01.png");
289+
} else {
290+
l_wave = Sprite::createWithSpriteFrameName("ExplosionLEFT_02.png");
291+
}
292+
bomb->addChild(l_wave);
293+
l_wave->setPosition(Vec2((-i - 1) * TILE_SIZE.width + TILE_SIZE.width / 2, TILE_SIZE.height / 2));
294+
}
295+
for (int i = 0; i < r_range; i++) {
296+
Sprite *r_wave;
297+
if (i = r_range - 1) {
298+
r_wave = Sprite::createWithSpriteFrameName("ExplosionRIGHT_01.png");
299+
} else {
300+
r_wave = Sprite::createWithSpriteFrameName("ExplosionRIGHT_02.png");
301+
}
302+
bomb->addChild(r_wave);
303+
r_wave->setPosition(Vec2((i + 1) * TILE_SIZE.width + TILE_SIZE.width / 2, TILE_SIZE.height / 2));
304+
}
305+
for (int i = 0; i < d_range; i++) {
306+
Sprite *d_wave;
307+
if (i = d_range - 1) {
308+
d_wave = Sprite::createWithSpriteFrameName("ExplosionDOWN_01.png");
309+
} else {
310+
d_wave = Sprite::createWithSpriteFrameName("ExplosionDOWN_02.png");
311+
}
312+
bomb->addChild(d_wave);
313+
d_wave->setPosition(Vec2(TILE_SIZE.width / 2, (- i - 1) * TILE_SIZE.height + TILE_SIZE.height / 2));
314+
}
315+
for (int i = 0; i < u_range; i++) {
316+
Sprite *u_wave;
317+
if (i = u_range - 1) {
318+
u_wave = Sprite::createWithSpriteFrameName("ExplosionUP_01.png");
319+
} else {
320+
u_wave = Sprite::createWithSpriteFrameName("ExplosionUP_02.png");
321+
}
322+
u_wave->setPosition(Vec2(TILE_SIZE.width / 2, (i + 1) * TILE_SIZE.height + TILE_SIZE.height / 2));
323+
bomb->addChild(u_wave);
324+
}
274325
}
275326

276327
bool GameScene::isOutOfMap(Vec2 pos)
@@ -318,7 +369,7 @@ bool GameScene::collideWithBrick(cocos2d::Vec2 targetPos)
318369
bool GameScene::collideWithBubble(Vec2 playerPos, Vec2 targetPos)
319370
{
320371
Vec2 target_tileCoord = tileCoordFromPosition(targetPos);
321-
Vec2 now_tileCoord = tileCoordFromPosition(playerPos);
372+
Vec2 now_tileCoord = tileCoordFromPosition(Vec2(playerPos.x, playerPos.y));
322373

323374
if (now_tileCoord == target_tileCoord) return false;
324375

@@ -363,4 +414,9 @@ Vec2 GameScene::tileCoordFromPosition(Vec2 position)
363414
int x = position.x / map->getTileSize().width;
364415
int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height;
365416
return Vec2(x, y);
366-
}
417+
}
418+
419+
void GameScene::game_over()
420+
{
421+
Director::getInstance()->replaceScene(CCTransitionMoveInR::create(0.4f, HelloWorld::createScene()));
422+
}

BombingAdventure/Classes/GameScene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class GameScene : public cocos2d::Layer
4141

4242
void makeMove(cocos2d::Vec2 position);
4343

44+
void game_over();
45+
4446

4547
/* macro for creating layer */
4648
CREATE_FUNC(GameScene);

BombingAdventure/Classes/Player.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,29 @@ Player::Player() {
2020
this->setAnchorPoint(Vec2(0.5,0.5));
2121
this->_contentSize = getContentSize();
2222
}
23+
2324
Player::~Player() {
2425
/* Not yet */
2526
}
27+
2628
bool Player::init() {
2729
return true;
2830
}
31+
2932
bool Player::can_set_bomb() {
3033
if (get_sprite() != NULL) {
3134
return num_present_bombs < num_max_available_bombs;
3235
}
3336
return false;
3437
}
38+
3539
bool Player::is_alive() {
3640
if (get_sprite() != NULL) {
3741
return HP > 0;
3842
}
3943
return false;
4044
}
45+
4146
float Player::get_moving_speed() {
4247
if (get_sprite() == NULL) return -1;
4348
return moving_speed;
@@ -47,10 +52,12 @@ int Player::get_HP() {
4752
if (get_sprite() == NULL) return -1;
4853
return HP;
4954
}
55+
5056
int Player::get_num_available_bombs() {
5157
if (get_sprite() == NULL) return -1;
5258
return num_max_available_bombs - num_present_bombs;
5359
}
60+
5461
Bomb* Player::set_bomb() {
5562
if (get_sprite() == NULL) return NULL;
5663
Bomb * bomb = Bomb::create();
@@ -71,6 +78,7 @@ Bomb* Player::set_bomb() {
7178

7279
return bomb;
7380
}
81+
7482
void Player::pick_item(Item & item) {
7583
if (get_sprite() == NULL) return; /* Avoid strange things happened :) */
7684
int item_id = item.get_item_id();
@@ -96,6 +104,22 @@ void Player::pick_item(Item & item) {
96104
log("Now my speed is %f", moving_speed);
97105
item.is_picked = true;
98106
}
107+
108+
void Player::bomb_vs_man(Vec2 bomb_tile_coord, int l, int r, int u, int d)
109+
{
110+
Vec2 player_tile_coord = tileCoordFromPosition(this->getPosition());
111+
for (int i = bomb_tile_coord.x - l; i <= bomb_tile_coord.x + r; i++) {
112+
if (player_tile_coord == Vec2(i, bomb_tile_coord.y)) {
113+
this->injured();
114+
}
115+
}
116+
for (int j = bomb_tile_coord.y - d; j <= bomb_tile_coord.y + u; j++) {
117+
if (player_tile_coord == Vec2(bomb_tile_coord.x, j)) {
118+
this->injured();
119+
}
120+
}
121+
}
122+
99123
void Player::injured(int deduct_HP) {
100124
if (get_sprite() != NULL) {
101125
HP -= deduct_HP;

BombingAdventure/Classes/Player.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Player : public Entity {
4646
*/
4747
void pick_item(Item & item);
4848

49+
void bomb_vs_man(Vec2 bomb_tile_coord, int l, int r, int u, int d);
50+
4951
/* Method: injured
5052
* Usage: Player->injured(deduct_HP);
5153
* ----------------------------------

0 commit comments

Comments
 (0)