66
77bool 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() {
1919void 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+ }
0 commit comments