/// <summary> /// Checks for relevant collisions between sprites and calls the appropriate methods to act on them /// </summary> public static void CheckCollisions() { List<Collision> collisions = new List<Collision>(); foreach (Item item in ItemManager.levelItems) { foreach (Player player in PlayerManager.players) { Collision c = new Collision(item, player); if (c.isColliding) { collisions.Add(c); } } } foreach (Collision c in collisions) { ItemManager.Collect(c.prop, c.actor as Item); } foreach (Player player in PlayerManager.players) { int playerBlockX = (int)Math.Floor(player.position.X / Block.WIDTH); int playerBlockY = (int)Math.Floor(player.position.Y / Block.HEIGHT); foreach (Block block in Level.GetInstance().MapSection(playerBlockY, playerBlockY + 1, playerBlockX, playerBlockX + 1)) //foreach (Block block in game.getLevel().getMapSection(0, 100, 0, 100)) { Collision c = new Collision(block, player); if (c.isColliding) { block.Collide(c); } } } }
/// <summary> /// Handles a collision with the block /// </summary> /// <param name="collision">Collision to handle</param> public void Collide(Collision collision) { if (collision.isColliding) { if (onStep) { if (collision.isOverlapping) { applyEffects(collision.prop); isBeingSteppedOn = true; } else { isBeingSteppedOn = false; } } else { applyEffects(collision.prop); } //determines passibility Sprite prop = collision.prop; // if the passability condition is met, dont check passability if (passabilityCondition != null && !passabilityCondition.CheckCondition()) { for (int edge = RIGHT; edge <= TOP; edge++) { if (passability[INSIDE, edge] != PASSABLE && collision.edges[edge] == Collision.INTERNAL_COLLISION) { if (edge == RIGHT || edge == LEFT) { prop.position.X = prop.oldPosition.X; } if (edge == BOTTOM || edge == TOP) { prop.position.Y = prop.oldPosition.Y; } } if (passability[OUTSIDE, edge] != PASSABLE && collision.edges[edge] == Collision.EXTERNAL_COLLISION) { if (edge == RIGHT || edge == LEFT) { prop.position.X = prop.oldPosition.X; } if (edge == BOTTOM || edge == TOP) { prop.position.Y = prop.oldPosition.Y; } } } } } }
public void Update(float elapsedTime) { if (Vector2.Distance(enemy.initialPosition, enemy.chasing.position) > enemy.wanderRadius && Vector2.Distance(enemy.position, enemy.chasing.position) > enemy.sightRadius) { enemy.enemyState = new RelaxState(enemy, 0); } Collision c = new Collision(enemy, enemy.chasing); if (c.isColliding) { foreach (Effect effect in enemy.effects) { effect.Apply(enemy.chasing); } enemy.enemyState = new RelaxState(enemy, Enemy.RELAX_TIME_ON_HIT); } enemy.chaseAI.Update(elapsedTime, enemy); }