Skip to content

Commit cf2fd63

Browse files
authored
Merge pull request #22 from Omnipotent-Youth/master
explosion animation & injured
2 parents 1ff0534 + c06467d commit cf2fd63

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_NS_CC;
45

@@ -66,7 +67,7 @@ bool GameScene::init() {
6667
float x = spawnPoint["x"].asFloat();
6768
float y = spawnPoint["y"].asFloat();
6869
hero = Player::create();
69-
hero->setPosition(Vec2(x+20, y+70));
70+
hero->setPosition(Vec2(x+20, y+70));
7071
this->addChild(hero, 100, 200);
7172

7273

@@ -158,6 +159,10 @@ bool GameScene::init() {
158159

159160
void GameScene::update(float delta) {
160161

162+
if (!hero->is_alive()) {
163+
game_over();
164+
}
165+
161166
/* Following part updates the movement of player */
162167
float position_x = hero->getPositionX();
163168
float position_y = hero->getPositionY();
@@ -170,7 +175,7 @@ void GameScene::update(float delta) {
170175
makeMove(Vec2(position_x, position_y), hero);
171176

172177
/* Bombs blow up destructable bricks */
173-
while (!current_bombs.empty() && !current_bombs.front()->bombIsCounting()) {
178+
while (!current_bombs.empty() && current_bombs.front()->bombIsExploded()) {
174179
Bomb * bomb = current_bombs.front();
175180
bomb_explode(bomb);
176181
current_bombs.erase(0);
@@ -190,7 +195,6 @@ void GameScene::bomb_explode(Bomb *bomb)
190195
int firstGid_of_brk = bricks->getTileSet()->_firstGid;
191196

192197
for (; l_range < power && bomb_tile_coord.x - l_range - 1 >= 0; l_range++) {
193-
194198
int GID_brk = bricks->getTileGIDAt(Vec2(bomb_tile_coord.x - l_range - 1, bomb_tile_coord.y));
195199
if (GID_brk - firstGid_of_brk >= 0){
196200
int GID_des = destructable->getTileGIDAt(Vec2(bomb_tile_coord.x - l_range - 1, bomb_tile_coord.y));
@@ -248,12 +252,59 @@ void GameScene::bomb_explode(Bomb *bomb)
248252
}
249253
}
250254

255+
hero->bomb_vs_man(bomb_tile_coord, l_range, r_range, u_range, d_range);
256+
251257
/* Now we have l_range, r_value, u_value, d_value, which indicate the explosion
252258
* range of the bomb in the four directions. What you need to do now is to
253259
* display the animation effect of bombs.
254260
* Please add your code here...
255261
*/
256262

263+
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Explosion.plist");
264+
Sprite *wave = Sprite::createWithSpriteFrameName("ExplosionCenter_01.png");
265+
bomb->addChild(wave);
266+
wave->setPosition(Vec2(TILE_SIZE.width/2, TILE_SIZE.height/2));
267+
268+
for (int i = 0; i < l_range; i++) {
269+
Sprite *l_wave;
270+
if (i = l_range - 1) {
271+
l_wave = Sprite::createWithSpriteFrameName("ExplosionLEFT_01.png");
272+
} else {
273+
l_wave = Sprite::createWithSpriteFrameName("ExplosionLEFT_02.png");
274+
}
275+
bomb->addChild(l_wave);
276+
l_wave->setPosition(Vec2((-i - 1) * TILE_SIZE.width + TILE_SIZE.width / 2, TILE_SIZE.height / 2));
277+
}
278+
for (int i = 0; i < r_range; i++) {
279+
Sprite *r_wave;
280+
if (i = r_range - 1) {
281+
r_wave = Sprite::createWithSpriteFrameName("ExplosionRIGHT_01.png");
282+
} else {
283+
r_wave = Sprite::createWithSpriteFrameName("ExplosionRIGHT_02.png");
284+
}
285+
bomb->addChild(r_wave);
286+
r_wave->setPosition(Vec2((i + 1) * TILE_SIZE.width + TILE_SIZE.width / 2, TILE_SIZE.height / 2));
287+
}
288+
for (int i = 0; i < d_range; i++) {
289+
Sprite *d_wave;
290+
if (i = d_range - 1) {
291+
d_wave = Sprite::createWithSpriteFrameName("ExplosionDOWN_01.png");
292+
} else {
293+
d_wave = Sprite::createWithSpriteFrameName("ExplosionDOWN_02.png");
294+
}
295+
bomb->addChild(d_wave);
296+
d_wave->setPosition(Vec2(TILE_SIZE.width / 2, (- i - 1) * TILE_SIZE.height + TILE_SIZE.height / 2));
297+
}
298+
for (int i = 0; i < u_range; i++) {
299+
Sprite *u_wave;
300+
if (i = u_range - 1) {
301+
u_wave = Sprite::createWithSpriteFrameName("ExplosionUP_01.png");
302+
} else {
303+
u_wave = Sprite::createWithSpriteFrameName("ExplosionUP_02.png");
304+
}
305+
u_wave->setPosition(Vec2(TILE_SIZE.width / 2, (i + 1) * TILE_SIZE.height + TILE_SIZE.height / 2));
306+
bomb->addChild(u_wave);
307+
}
257308
}
258309

259310
bool GameScene::isOutOfMap(Vec2 pos)
@@ -301,7 +352,7 @@ bool GameScene::collideWithBrick(cocos2d::Vec2 targetPos)
301352
bool GameScene::collideWithBubble(Vec2 playerPos, Vec2 targetPos)
302353
{
303354
Vec2 target_tileCoord = tileCoordFromPosition(targetPos);
304-
Vec2 now_tileCoord = tileCoordFromPosition(playerPos);
355+
Vec2 now_tileCoord = tileCoordFromPosition(Vec2(playerPos.x, playerPos.y));
305356

306357
if (now_tileCoord == target_tileCoord) return false;
307358

@@ -346,4 +397,9 @@ Vec2 GameScene::tileCoordFromPosition(Vec2 position)
346397
int x = position.x / map->getTileSize().width;
347398
int y = ((map->getMapSize().height * map->getTileSize().height) - position.y) / map->getTileSize().height;
348399
return Vec2(x, y);
349-
}
400+
}
401+
402+
void GameScene::game_over()
403+
{
404+
Director::getInstance()->replaceScene(CCTransitionMoveInR::create(0.4f, HelloWorld::createScene()));
405+
}

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, Player * player);
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;
@@ -52,10 +57,12 @@ int Player::get_HP() {
5257
if (get_sprite() == NULL) return -1;
5358
return HP;
5459
}
60+
5561
int Player::get_num_available_bombs() {
5662
if (get_sprite() == NULL) return -1;
5763
return num_max_available_bombs - num_present_bombs;
5864
}
65+
5966
Bomb* Player::set_bomb() {
6067
if (get_sprite() == NULL) return NULL;
6168
Bomb * bomb = Bomb::create();
@@ -76,6 +83,7 @@ Bomb* Player::set_bomb() {
7683

7784
return bomb;
7885
}
86+
7987
void Player::pick_item(Item & item) {
8088
if (get_sprite() == NULL) return; /* Avoid strange things happened :) */
8189
int item_id = item.get_item_id();
@@ -101,6 +109,22 @@ void Player::pick_item(Item & item) {
101109
log("Now my speed is %f", moving_speed);
102110
item.is_picked = true;
103111
}
112+
113+
void Player::bomb_vs_man(Vec2 bomb_tile_coord, int l, int r, int u, int d)
114+
{
115+
Vec2 player_tile_coord = tileCoordFromPosition(this->getPosition());
116+
for (int i = bomb_tile_coord.x - l; i <= bomb_tile_coord.x + r; i++) {
117+
if (player_tile_coord == Vec2(i, bomb_tile_coord.y)) {
118+
this->injured();
119+
}
120+
}
121+
for (int j = bomb_tile_coord.y - d; j <= bomb_tile_coord.y + u; j++) {
122+
if (player_tile_coord == Vec2(bomb_tile_coord.x, j)) {
123+
this->injured();
124+
}
125+
}
126+
}
127+
104128
void Player::injured(int deduct_HP) {
105129
if (get_sprite() != NULL) {
106130
HP -= deduct_HP;

BombingAdventure/Classes/Player.h

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

51+
void bomb_vs_man(Vec2 bomb_tile_coord, int l, int r, int u, int d);
52+
5153
/* Method: injured
5254
* Usage: Player->injured(deduct_HP);
5355
* ----------------------------------

0 commit comments

Comments
 (0)