/** * @brief Updates this state. */ void Hero::PushingState::update() { State::update(); if (is_moving_grabbed_entity()) { // the hero is pushing an entity and is currently moving it (typically a block) // detect when the hero movement is finished // because the hero has covered 16 pixels, has reached an obstacle or has aligned the entity on the grid PathMovement *movement = (PathMovement*) hero.get_movement(); bool horizontal = pushing_direction4 % 2 == 0; bool has_reached_grid = movement->get_total_distance_covered() > 8 && ((horizontal && hero.is_aligned_to_grid_x()) || (!horizontal && hero.is_aligned_to_grid_y())); if (movement->is_finished() || has_reached_grid) { stop_moving_pushed_entity(); } } else { // the hero is pushing a fixed obstacle // stop pushing if there is no more obstacle if (!hero.is_facing_obstacle()) { hero.set_state(new FreeState(hero)); } // stop pushing if the player changes his direction else if (get_controls().get_wanted_direction8() != pushing_direction4 * 2) { if (get_controls().is_key_pressed(GameControls::ACTION)) { hero.set_state(new GrabbingState(hero)); } else { hero.set_state(new FreeState(hero)); } } // see if the obstacle pushed is an entity that the hero can move else { Detector *facing_entity = hero.get_facing_entity(); if (facing_entity != NULL) { // the obstacle pushed is an entity if (facing_entity->get_type() == BLOCK) { // it can be moved by the hero (TODO dynamic binding) hero.try_snap_to_facing_entity(); } if (facing_entity->moved_by_hero()) { std::string path = " "; path[0] = path[1] = '0' + pushing_direction4 * 2; hero.set_movement(new PathMovement(path, 40, false, false, false)); pushed_entity = facing_entity; } } } } }
/** * @brief Notifies this state that the hero has just failed to change its * position because of obstacles. */ void Hero::PushingState::notify_obstacle_reached() { if (is_moving_grabbed_entity()) { // an obstacle is reached before the 16 pixels are completed //std::cout << "stop moving block: the hero has reached an obstacle\n"; stop_moving_pushed_entity(); } }
/** * @brief Notifies this state that the movement if finished. */ void Hero::PushingState::notify_movement_finished() { if (is_moving_grabbed_entity()) { // the 16 pixels of the path are completed //std::cout << "stop moving block: 16 pixels were completed\n"; stop_moving_pushed_entity(); } }
/** * @brief Notifies the hero that the entity he is pushing cannot move any more because of a collision. */ void Hero::PushingState::notify_grabbed_entity_collision() { // the hero has moved one pixel too much // because he moved before the block, not knowing that the block would not follow him static const Rectangle opposite_dxy[] = { Rectangle(-1, 0), Rectangle( 0, 1), Rectangle( 1, 0), Rectangle( 0,-1) }; // go back one pixel into the opposite direction Rectangle bounding_box = hero.get_bounding_box(); bounding_box.add_xy(opposite_dxy[pushing_direction4]); hero.set_bounding_box(bounding_box); stop_moving_pushed_entity(); }
/** * @brief Notifies this state that the hero has just changed its * position. */ void Hero::PushingState::notify_position_changed() { if (is_moving_grabbed_entity()) { // if the entity has made more than 8 pixels and is aligned on the grid, // we stop the movement PathMovement *movement = (PathMovement*) hero.get_movement(); bool horizontal = pushing_direction4 % 2 == 0; bool has_reached_grid = movement->get_total_distance_covered() > 8 && ((horizontal && pushed_entity->is_aligned_to_grid_x()) || (!horizontal && pushed_entity->is_aligned_to_grid_y())); if (has_reached_grid) { //std::cout << "stop moving block: the entity has reached the grid\n"; stop_moving_pushed_entity(); } } }
/** * @brief Notifies the hero that the entity he is pushing cannot move any more because of a collision. */ void Hero::PushingState::notify_grabbed_entity_collision() { //std::cout << "stop moving block: the block has reached an obstacle\n"; stop_moving_pushed_entity(); }