Skip to content

Commit

Permalink
CollideWith function for Entity. Can pass in any collider to collide …
Browse files Browse the repository at this point in the history
…with. (instead of using the one assigned to the entity)
  • Loading branch information
zaphire committed Jun 28, 2011
1 parent d2b2d32 commit bd5698b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Core/Collision.cpp
Expand Up @@ -73,6 +73,28 @@ namespace Monocle
return NULL;
}

Collider* Collision::Collide(Collider *passedCollider, const std::string &tag, CollisionData *collisionData)
{
if (passedCollider != NULL)
{
for (std::list<Collider*>::iterator i = instance->colliders.begin(); i != instance->colliders.end(); ++i)
{
Entity* otherEntity = (*i)->GetEntity();
if (otherEntity != NULL) //&& otherEntity != entity)
{
Collider* otherCollider = otherEntity->GetCollider();
if (otherCollider != NULL && otherEntity->HasTag(tag))
{
if (Collider::Collide(passedCollider, otherCollider, collisionData))
{
return otherCollider;
}
}
}
}
}
}

//OPTION: refactor to add multiple colliders? (consider carefully)
//FORNOW: limit to one collider per entity (simpler, makes more sense)
//LATER: rename these to "Set" or something else?
Expand Down
1 change: 1 addition & 0 deletions Core/Collision.h
Expand Up @@ -40,6 +40,7 @@ namespace Monocle
static void RegisterColliderWithEntity(Collider *collider, Entity *entity);
static void RemoveCollider(Collider *collider);
static Collider* Collide(Entity *entity, const std::string &tag, CollisionData *collisionData=NULL);
static Collider* Collide(Collider *collider, const std::string &tag, CollisionData *collisionData=NULL);

private:
static Collision *instance;
Expand Down
6 changes: 6 additions & 0 deletions Core/Entity.cpp
Expand Up @@ -378,6 +378,12 @@ namespace Monocle
return collider;
}

Collider* Entity::CollideWith(Collider *pCollider, const std::string &tag, CollisionData *collisionData)
{
Collider *collider = Collision::Collide(pCollider, tag, collisionData);
return collider;
}

/*
RectangleCollider* Entity::AddRectangleCollider(float width, float height, const Vector2 &offset)
{
Expand Down
2 changes: 2 additions & 0 deletions Core/Entity.h
Expand Up @@ -102,6 +102,8 @@ namespace Monocle
Collider* Collide(const std::string &tag, CollisionData *collisionData=NULL);
//! Check our collider against all entities that have "tag" - warping us to atPosition first, then back to our original position after
Collider* CollideAt(const std::string &tag, const Vector2& atPosition, CollisionData *collisionData=NULL);
//! Do a circle collision
Collider *CollideWith(Collider *collider, const std::string &tag, CollisionData *collisionData=NULL);

//! Associates this entity with the given tag
void AddTag(const std::string& tag, bool save=false);
Expand Down

0 comments on commit bd5698b

Please sign in to comment.