Skip to content

Commit 94cae85

Browse files
committed
Add: Monster now can injure the monitored_player.
1 parent a8a2103 commit 94cae85

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

BombingAdventure/Classes/MonsterController.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
bool MonsterController::init() {
88
/* Get current time to initialize a random seed */
9-
timeval now;
10-
gettimeofday(&now, NULL);
11-
auto seed = (unsigned) (now.tv_sec * 1000 + now.tv_usec / 1000);
12-
srand(seed);
9+
// timeval now;
10+
// gettimeofday(&now, NULL);
11+
// auto seed = (unsigned) (now.tv_sec * 1000 + now.tv_usec / 1000);
12+
// srand(seed);
1313

1414
create_monster();
1515
/* Enable update */
@@ -19,12 +19,23 @@ bool MonsterController::init() {
1919
void MonsterController::update(float delta) {
2020
for (Monster* monster: current_monster_vector) {
2121
if (monster->is_alive()) {
22+
2223
Vec2 monster_pos_in_pixel = monster->getPosition();
2324
Vec2 monster_pos_in_tile = monster->tileCoordFromPosition(monster_pos_in_pixel);
2425
if (monster_pos_in_tile.x < 0 || monster_pos_in_tile.x > 40
2526
|| monster_pos_in_tile.y < 0 || monster_pos_in_tile.y > 40) {
2627
monster->reset_position();
2728
}
29+
30+
if (monster->collided_with(monitored_player)) {
31+
monitored_player->injured();
32+
log("The HP of the player is %d", monitored_player->get_HP());
33+
}
34+
} else {
35+
monster->removeAllChildren();
36+
monster->removeFromParent();
37+
current_monster_vector.eraseObject(monster);
38+
log("Monster is removed.");
2839
}
2940
}
3041
}
@@ -40,3 +51,6 @@ void MonsterController::create_monster() {
4051
current_monster_vector.pushBack(monster);
4152
}
4253
}
54+
void MonsterController::bind_player(Player * player) {
55+
monitored_player = player;
56+
}

BombingAdventure/Classes/MonsterController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BOMBING_ADVENTURE_MONSTERCONTROLLER_H
77

88
#include "cocos2d.h"
9+
#include "Player.h"
910
#include "Monster.h"
1011

1112

@@ -29,6 +30,9 @@ class MonsterController : public Node {
2930

3031
/* Override update method */
3132
virtual void update(float delta);
33+
34+
/* */
35+
void bind_player(Player * player);
3236
private:
3337
/* Method: create_monster
3438
* ----------------------
@@ -37,6 +41,7 @@ class MonsterController : public Node {
3741
void create_monster();
3842
/* Store all the Monsters */
3943
Vector<Monster*> current_monster_vector;
44+
Player * monitored_player = NULL;
4045
};
4146

4247
#endif //BOMBING_ADVENTURE_MONSTERCONTROLLER_H

0 commit comments

Comments
 (0)