public CollisionDirection CheckCollision(PhysicsAble entity) { Entity e = this; Vector2 position = entity.GetPhysicsPosition() + entity.GetPhysicsVelocity(); Vector2 entitySize = entity.GetPhysicsSize(); if (entitySize == Vector2.Zero) { return CollisionDirection.NONE; } int half_x_bounds = (int)entitySize.X / 2; int half_y_bounds = (int)entitySize.Y / 2; Rectangle checkingBounds = new Rectangle((int)position.X - half_x_bounds, (int)position.Y - half_y_bounds, (int)entitySize.X, (int)entitySize.Y); bool left = leftRect.Intersects(checkingBounds); bool right = rightRect.Intersects(checkingBounds); bool top = topRect.Intersects(checkingBounds); bool bottom = bottomRect.Intersects(checkingBounds); if (left && top) { return CollisionDirection.TOPLEFT; } else if (right && top) { return CollisionDirection.BOTTOMRIGHT; } else if (bottom && right) { return CollisionDirection.BOTTOMRIGHT; } else if (bottom && left) { return CollisionDirection.BOTTOMLEFT; } else if (top) { return CollisionDirection.TOP; } else if (bottom) { return CollisionDirection.BOTTOM; } else if (left) { return CollisionDirection.LEFT; } else if (right) { return CollisionDirection.RIGHT; } else { return CollisionDirection.NONE; } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { if (entity is Level) { velocity = Vector2.Zero; } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { // Die if you hit a wall if (entity is DoctorWall) return; if (entity is Level) { destroy(); } else if (entity is Enemy) { if (lastHit != entity) { if (bounces == 3) { destroy(); return; } // Do damage Enemy e = (Enemy)entity; e.applyDamage(10); // Bounce lastHit = entity; Entity closestEnemy = getClosestEnemy(); Vector2 newVelocity = Vector2.Zero; if (closestEnemy == null) { Random r = new Random(); newVelocity = new Vector2(r.Next(0, 10) - 5, r.Next(0, 10) - 5); } else { newVelocity = getClosestEnemy().worldPosition - this.worldPosition; } newVelocity.Normalize(); velocity = newVelocity * 8; bounces++; } } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { if (entity is Bullet) { bulletRef = (Bullet)entity; bulletRef.attach(this); } else if (entity is Level) { velocity = Vector2.Zero; } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { // Die if you hit a wall if (entity is Level || entity is DoctorWall) { destroy(); } else if (entity is PlayerCharacter) // Hurt players { // Do damage PlayerCharacter e = (PlayerCharacter)entity; e.applyDamage(1); destroy(); } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { // Die if you hit a wall if (entity is Level || entity is DoctorWall) { destroy(); context.bulletExists = false; } else if (entity is Enemy) { if (hits.Contains(entity)) return; Enemy e = (Enemy)entity; e.applyDamage(payload); hits.Add(entity); } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { if (entity is PlayerCharacter) { PlayerCharacter pc = (PlayerCharacter)entity; if (pc is Tank && pc.attacking) { return; } pc.applyDamage(1); pc.velocity = velocity * 2; velocity = Vector2.Zero; } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { if (entity is DoctorWall) return; // Die if you hit a wall if (entity is Level) { destroy(); } else if (entity is Enemy) { // Do damage Enemy e = (Enemy)entity; e.applyDamage(1); destroy(); } else { base.HandleCollision(cd, entity); } }
public override void HandleCollision(CollisionDirection cd, PhysicsAble entity) { // Die if you hit a wall if (entity is DoctorWall) return; if (entity is Level) { destroy(); } else if (entity is Enemy) { if (!hits.Contains(entity)) { Enemy e = (Enemy)entity; e.applyDamage(4); hits.Add(e); } } else { base.HandleCollision(cd, entity); } }
public CollisionDirection CheckCollision(PhysicsAble entity) { // Assuming for now position and bounds defined in pixel space, this should be easy to switch out if needed Vector2 position = entity.GetPhysicsPosition() + entity.GetPhysicsVelocity(); Vector2 size = entity.GetPhysicsSize(); float half_scaled_bg_w = levelSize.X / 2; float half_scaled_bg_h = levelSize.Y / 2; Vector2 levelPosition = this.worldPosition - new Vector2(half_scaled_bg_w, half_scaled_bg_h); Vector2 characterInLevel = position - levelPosition; // Define region of pixels under the bounds int left = Math.Max(0, (int)Math.Floor((characterInLevel.X - size.X / 2) / levelSize.X * bitmapWidth)); int width = (int)Math.Ceiling(size.X / levelSize.X * bitmapWidth); int top = Math.Max(0, (int)Math.Floor((characterInLevel.Y - size.Y / 2) / levelSize.Y * bitmapHeight)); int height = (int)Math.Ceiling(size.Y / levelSize.Y * bitmapHeight); bool hitTop = false; bool hitLeft = false; bool hitRight = false; bool hitBottom = false; for (int x = left; x < left + width; x++) { for (int y = top; y < top + height; y++) { int index = y * bitmapWidth + x; // TODO: shouldn't get too far ahead of myself if (index < bitmap.Length && bitmap[index]) { if (x - left <= width / 2) hitLeft = true; if (x - left >= width / 2) hitRight = true; if (y - top <= height / 2) hitTop = true; if (y - top >= height / 2) hitBottom = true; } // Possible: delete if out of bounds } } if (hitLeft && hitTop) { return CollisionDirection.TOPLEFT; } else if (hitRight && hitTop) { return CollisionDirection.BOTTOMRIGHT; } else if (hitBottom && hitRight) { return CollisionDirection.BOTTOMRIGHT; } else if (hitBottom && hitLeft) { return CollisionDirection.BOTTOMLEFT; } else if (hitTop) { return CollisionDirection.TOP; } else if (hitBottom) { return CollisionDirection.BOTTOM; } else if (hitLeft) { return CollisionDirection.LEFT; } else if (hitRight) { return CollisionDirection.RIGHT; } else { return CollisionDirection.NONE; } }
public void HandleCollision(CollisionDirection cd, PhysicsAble entity) { }
public virtual void HandleCollision(CollisionDirection cd, PhysicsAble entity) { if (entity is Level || entity is DoctorWall) { velocity = Vector2.Zero; } }