Skip to content

Commit a8a2103

Browse files
committed
Add: Monster can check whether it collied with player now.
1 parent e661734 commit a8a2103

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

BombingAdventure/Classes/Monster.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ bool Monster::init() {
2424
return true;
2525
}
2626
int Monster::get_new_direction() {
27-
int new_direction = floor(CCRANDOM_0_1() * 4);
27+
int new_direction = (int) floor(CCRANDOM_0_1() * 4);
2828
return new_direction;
2929
}
3030
//void Monster::move_forward(int new_direction) {
31-
// step = CCRANDOM_0_1() * 100;
3231
// switch (new_direction) {
3332
// case MOVE_UP:
3433
//
34+
//
3535
// }
36-
//}
36+
//}
37+
bool Monster::collided_with(Player * player) {
38+
/* Get the bounding box of the player */
39+
Rect player_bounding_box = player->getBoundingBox();
40+
Vec2 monster_position = this->getPosition();
41+
/* Check whether the player and the monster has intersection */
42+
bool is_collided = player_bounding_box.containsPoint(monster_position);
43+
return is_collided;
44+
}

BombingAdventure/Classes/Monster.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ class Monster : public Player {
2828
/* Initiate Method */
2929
virtual bool init();
3030

31+
bool collided_with(Player * player);
32+
3133
/* Method: get_new_direction */
3234
int get_new_direction();
35+
3336
/* Method: move_forward */
3437
// void move_forward(int new_direction);
3538

39+
3640
private:
3741
enum direction { MOVE_UP, MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT };
3842

0 commit comments

Comments
 (0)