From c9cf713e62403a869b31e54eb557e57d71a43400 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 11 Apr 2004 13:00:00 +0000 Subject: [PATCH] - moved some collision code into the world class, since it only acts on world data SVN-Revision: 471 --- src/collision.cpp | 86 ++------------------------------------------------- src/collision.h | 6 ++-- src/gameloop.cpp | 10 ------ src/world.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/world.h | 6 ++++ 5 files changed, 103 insertions(+), 97 deletions(-) diff --git a/src/collision.cpp b/src/collision.cpp index de62b7a96..373acd2c3 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -39,9 +39,9 @@ bool collision_object_map(base_type* pbase) int h = (int)pbase->width / 16; if(issolid(pbase->x + 1, pbase->y + 1) || - issolid(pbase->x + pbase->width -1, pbase->y + 1) || - issolid(pbase->x +1, pbase->y + pbase->height -1) || - issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1)) + issolid(pbase->x + pbase->width -1, pbase->y + 1) || + issolid(pbase->x +1, pbase->y + pbase->height -1) || + issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1)) return true; for(int i = 1; i < h; ++i) @@ -203,86 +203,6 @@ void collision_swept_object_map(base_type* old, base_type* current) *old = *current; } -void collision_handler() -{ - // CO_BULLET & CO_BADGUY check - for(unsigned int i = 0; i < world.bullets.size(); ++i) - { - for(unsigned int j = 0; j < world.bad_guys.size(); ++j) - { - if(world.bad_guys[j].dying != DYING_NOT) - continue; - if(rectcollision(&world.bullets[i].base, &world.bad_guys[j].base)) - { - // We have detected a collision and now call the - // collision functions of the collided objects. - // collide with bad_guy first, since bullet_collision will - // delete the bullet - world.bad_guys[j].collision(0, CO_BULLET); - bullet_collision(&world.bullets[i], CO_BADGUY); - break; // bullet is invalid now, so break - } - } - } - - /* CO_BADGUY & CO_BADGUY check */ - for(unsigned int i = 0; i < world.bad_guys.size(); ++i) - { - if(world.bad_guys[i].dying != DYING_NOT) - continue; - - for(unsigned int j = i+1; j < world.bad_guys.size(); ++j) - { - if(j == i || world.bad_guys[j].dying != DYING_NOT) - continue; - - if(rectcollision(&world.bad_guys[i].base, &world.bad_guys[j].base)) - { - // We have detected a collision and now call the - // collision functions of the collided objects. - world.bad_guys[j].collision(&world.bad_guys[i], CO_BADGUY); - world.bad_guys[i].collision(&world.bad_guys[j], CO_BADGUY); - } - } - } - - if(tux.dying != DYING_NOT) return; - - // CO_BADGUY & CO_PLAYER check - for(unsigned int i = 0; i < world.bad_guys.size(); ++i) - { - if(world.bad_guys[i].dying != DYING_NOT) - continue; - - if(rectcollision_offset(&world.bad_guys[i].base,&tux.base,0,0)) - { - // We have detected a collision and now call the collision - // functions of the collided objects. - if (tux.previous_base.y < tux.base.y && - tux.previous_base.y + tux.previous_base.height - < world.bad_guys[i].base.y + world.bad_guys[i].base.height/2) - { - world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH); - } - else - { - tux.collision(&world.bad_guys[i], CO_BADGUY); - } - } - } - - // CO_UPGRADE & CO_PLAYER check - for(unsigned int i = 0; i < world.upgrades.size(); ++i) - { - if(rectcollision(&world.upgrades[i].base, &tux.base)) - { - // We have detected a collision and now call the collision - // functions of the collided objects. - upgrade_collision(&world.upgrades[i], &tux, CO_PLAYER); - } - } -} - Tile* gettile(float x, float y) { diff --git a/src/collision.h b/src/collision.h index fa165d713..deee4b8b5 100644 --- a/src/collision.h +++ b/src/collision.h @@ -15,6 +15,7 @@ #include "type.h" class Tile; +class World; /* Collision objects */ enum @@ -32,13 +33,10 @@ enum CollisionType { bool rectcollision(base_type* one, base_type* two); bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y); + void collision_swept_object_map(base_type* old, base_type* current); bool collision_object_map(base_type* object); -/* Checks for all possible collisions. - And calls the collision_handlers, which the collision_objects provide for this case (or not). */ -void collision_handler(); - /** Return a pointer to the tile at the given x/y coordinates */ Tile* gettile(float x, float y); diff --git a/src/gameloop.cpp b/src/gameloop.cpp index e18a80bfb..62dfa2582 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -432,16 +432,6 @@ GameSession::action() world->action(); - /* update particle systems */ - std::vector::iterator p; - for(p = world->particle_systems.begin(); p != world->particle_systems.end(); ++p) - { - (*p)->simulate(frame_ratio); - } - - /* Handle all possible collisions. */ - collision_handler(); - return -1; } diff --git a/src/world.cpp b/src/world.cpp index cc51e5cba..1cfac5ed9 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -249,6 +249,98 @@ World::action() for (unsigned int i = 0; i < bad_guys.size(); i++) bad_guys[i].action(); + + /* update particle systems */ + std::vector::iterator p; + for(p = particle_systems.begin(); p != particle_systems.end(); ++p) + { + (*p)->simulate(frame_ratio); + } + + /* Handle all possible collisions. */ + collision_handler(); +} + + +void +World::collision_handler() +{ + // CO_BULLET & CO_BADGUY check + for(unsigned int i = 0; i < bullets.size(); ++i) + { + for(unsigned int j = 0; j < bad_guys.size(); ++j) + { + if(bad_guys[j].dying != DYING_NOT) + continue; + if(rectcollision(&bullets[i].base, &bad_guys[j].base)) + { + // We have detected a collision and now call the + // collision functions of the collided objects. + // collide with bad_guy first, since bullet_collision will + // delete the bullet + bad_guys[j].collision(0, CO_BULLET); + bullet_collision(&bullets[i], CO_BADGUY); + break; // bullet is invalid now, so break + } + } + } + + /* CO_BADGUY & CO_BADGUY check */ + for(unsigned int i = 0; i < bad_guys.size(); ++i) + { + if(bad_guys[i].dying != DYING_NOT) + continue; + + for(unsigned int j = i+1; j < bad_guys.size(); ++j) + { + if(j == i || bad_guys[j].dying != DYING_NOT) + continue; + + if(rectcollision(&bad_guys[i].base, &bad_guys[j].base)) + { + // We have detected a collision and now call the + // collision functions of the collided objects. + bad_guys[j].collision(&bad_guys[i], CO_BADGUY); + bad_guys[i].collision(&bad_guys[j], CO_BADGUY); + } + } + } + + if(tux.dying != DYING_NOT) return; + + // CO_BADGUY & CO_PLAYER check + for(unsigned int i = 0; i < bad_guys.size(); ++i) + { + if(bad_guys[i].dying != DYING_NOT) + continue; + + if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0)) + { + // We have detected a collision and now call the collision + // functions of the collided objects. + if (tux.previous_base.y < tux.base.y && + tux.previous_base.y + tux.previous_base.height + < bad_guys[i].base.y + bad_guys[i].base.height/2) + { + bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH); + } + else + { + tux.collision(&bad_guys[i], CO_BADGUY); + } + } + } + + // CO_UPGRADE & CO_PLAYER check + for(unsigned int i = 0; i < upgrades.size(); ++i) + { + if(rectcollision(&upgrades[i].base, &tux.base)) + { + // We have detected a collision and now call the collision + // functions of the collided objects. + upgrade_collision(&upgrades[i], &tux, CO_PLAYER); + } + } } void diff --git a/src/world.h b/src/world.h index 953e739c4..5c53c36f9 100644 --- a/src/world.h +++ b/src/world.h @@ -58,6 +58,12 @@ class World void draw(); void action(); + + /** Checks for all possible collisions. And calls the + collision_handlers, which the collision_objects provide for this + case (or not). */ + void collision_handler(); + void arrays_free(); /** Load data for this level: -- 2.11.0