Index: ./Games/Pingus/src/pingu_action.cxx =================================================================== RCS file: /usr/local/cvsroot/Games/Pingus/src/pingu_action.cxx,v retrieving revision 1.10 diff -u -r1.10 pingu_action.cxx --- ./Games/Pingus/src/pingu_action.cxx 20 Oct 2002 18:28:49 -0000 1.10 +++ ./Games/Pingus/src/pingu_action.cxx 28 Oct 2002 01:00:08 -0000 @@ -79,6 +79,26 @@ return false; } +bool +PinguAction::collision_on_walk (int x, int y) +{ + bool collision = false; + int pixel = Groundtype::GP_NOTHING; + + for (int pingu_y = 0; pingu_y <= pingu_height; ++pingu_y) + { + pixel = rel_getpixel(x, y + pingu_y); + + if (pixel != Groundtype::GP_NOTHING && pixel != Groundtype::GP_BRIDGE) + { + collision = true; + break; + } + } + + return collision; +} + void PinguAction::move_with_forces (float x_to_add, float y_to_add) { @@ -121,9 +141,11 @@ y_inc = denominator; } + Vector force_counter = resultant_force; + // Keep moving the Pingu until there is only a fraction left - while (resultant_force.x <= -1 || resultant_force.x >= 1 - || resultant_force.y <= -1 || resultant_force.y >= 1) + while (force_counter.x <= -1 || force_counter.x >= 1 + || force_counter.y <= -1 || force_counter.y >= 1) { x_numerator += x_inc; @@ -134,29 +156,55 @@ x_numerator -= denominator; // Move the Pingu depending on what the direction of the force is - if (resultant_force.x >= 1) + if (force_counter.x >= 1) { - // Check what is to the right of the Pingu - if (rel_getpixel(pingu->direction, 0) != Groundtype::GP_NOTHING - || head_collision_on_walk(pingu->direction, 0)) + // If there is something to the right of the Pingu + if (collision_on_walk(pingu->direction, 0)) { - break; - } + // Make it so that the Pingu won't go right any further. + force_counter.x = 0; + resultant_force.x = 0; + + // Stop Pingu from still going up + if (force_counter.y < 0) + { + force_counter.y = 0; + resultant_force.y = 0; + } - pingu->set_x(pingu->get_x() + 1); - resultant_force.x--; + pingu->set_velocity(resultant_force); + } + else + { + // Move the Pingu right + pingu->set_x(pingu->get_x() + 1); + force_counter.x--; + } } - else if (resultant_force.x <= -1) + else if (force_counter.x <= -1) { - // Check what is to the left of the Pingu - if (rel_getpixel(-(pingu->direction), 0) != Groundtype::GP_NOTHING - || head_collision_on_walk(-(pingu->direction), 0) ) + // If there is something to the left of the Pingu + if (collision_on_walk(-(pingu->direction), 0)) { - break; - } + // Make it so that the Pingu won't go left any further. + force_counter.x = 0; + resultant_force.x = 0; + + // Stop Pingu from still going up + if (force_counter.y < 0) + { + force_counter.y = 0; + resultant_force.y = 0; + } - pingu->set_x(pingu->get_x() - 1); - resultant_force.x++; + pingu->set_velocity(resultant_force); + } + else + { + // Move the Pingu left + pingu->set_x(pingu->get_x() - 1); + force_counter.x++; + } } } @@ -169,21 +217,41 @@ y_numerator -= denominator; // Move the Pingu depending on what the direction of the force is - if (resultant_force.y >= 1) + if (force_counter.y >= 1) { + // If there is something below the Pingu if (rel_getpixel(0, -1) != Groundtype::GP_NOTHING) - break; + { + // Make it so that the Pingu won't go down any further. + force_counter.y = 0; + resultant_force.y = 0; - pingu->set_y(pingu->get_y() + 1); - resultant_force.y--; + pingu->set_velocity(resultant_force); + } + else + { + // Move the Pingu down + pingu->set_y(pingu->get_y() + 1); + force_counter.y--; + } } - else if (resultant_force.y <= -1) + else if (force_counter.y <= -1) { + // If there is something in the way above the Pingu if (head_collision_on_walk(0, 1)) - break; + { + // Make it so that the Pingu won't go up any further. + force_counter.y = 0; + resultant_force.y = 0; - pingu->set_y(pingu->get_y() - 1); - resultant_force.y++; + pingu->set_velocity(resultant_force); + } + else + { + // Move the Pingu up + pingu->set_y(pingu->get_y() - 1); + force_counter.y++; + } } } } Index: ./Games/Pingus/src/pingu_action.hxx =================================================================== RCS file: /usr/local/cvsroot/Games/Pingus/src/pingu_action.hxx,v retrieving revision 1.22 diff -u -r1.22 pingu_action.hxx --- ./Games/Pingus/src/pingu_action.hxx 22 Oct 2002 19:31:11 -0000 1.22 +++ ./Games/Pingus/src/pingu_action.hxx 28 Oct 2002 01:00:08 -0000 @@ -103,13 +103,16 @@ /// True if Pingu in specified position would bang its head if it were walking bool head_collision_on_walk (int x, int y); + /// True if Pingu in specified position would have a collision if it were walking + bool collision_on_walk (int x, int y); + /** Called if the action was successfully applied via request_set_action */ virtual void on_successfull_apply () { } /** Called if the request_set_action failded to apply this action */ virtual void on_failed_apply (Pingu*) { } - /** FIXME: document me */ + /** Move Pingu according to the forces applied to it */ void move_with_forces (float x_to_add, float y_to_add); private: