1 module PixelPerfectEngine.collision.boxCollision;
2 
3 /*
4  * Copyright (C) 2015-2018, by Laszlo Szeremi under the Boost license.
5  *
6  * Pixel Perfect Engine, collision.boxCollision module.
7  */
8 
9 import PixelPerfectEngine.graphics.common;
10 import PixelPerfectEngine.system.binarySearchTree;
11 import PixelPerfectEngine.collision;
12 
13 /**
14  * Detects if two boxes have collided.
15  * The boxes can be any shape if needed.
16  */
17 public class BoxCollisionDetector{
18 	public Coordinate[int] objects;
19 	public CollisionListener cl;
20 
21 
22 	public this(){
23 		
24 	}
25 	/**
26 	 * Tests all shapes for each other.
27 	 */
28 	public void testAll(){
29 		foreach(objectA; objects.byKey){
30 			foreach(objectB; objects.byKey){
31 				if(objectA != objectB){
32 					if(areColliding(objects[objectA], objects[objectB])){
33 						cl.spriteCollision(new CollisionEvent(objectA,objectB));
34 					}
35 				}
36 			}
37 		}
38 	}
39 	/**
40 	 * Tests a single shape to every other on the list
41 	 */
42 	public void testSingle(int objectA){
43 		foreach(objectB; objects.byKey){
44 			if(objectA != objectB){
45 				if(areColliding(objects[objectA], objects[objectB])){
46 					cl.spriteCollision(new CollisionEvent(objectA,objectB));
47 				}
48 			}
49 		}
50 	}
51 	/**
52 	 * Tests two boxes together. Returns true on collision.
53 	 */
54 	public bool areColliding(ref Coordinate a, ref Coordinate b){
55 		if (a.bottom < b.top) return false;
56 		if (a.top > b.bottom) return false;
57 		
58 		if (a.right < b.left) return false;
59 		if (a.left > b.right) return false;
60 		return true;
61 	}
62 }