1 module PixelPerfectEngine.collision.common;
2 
3 /*
4  * Copyright (C) 2015-2020, by Laszlo Szeremi under the Boost license.
5  *
6  * Pixel Perfect Engine, collision.common module.
7  */
8 
9 public import PixelPerfectEngine.graphics.common;
10 public import PixelPerfectEngine.graphics.bitmap;
11 public import PixelPerfectEngine.graphics.layers : MappingElement;
12 import collections.treemap;
13 
14 /**
15  * Defines a shape for collision detection.
16  */
17 public struct CollisionShape {
18 	Coordinate		position;	///Position of the shape in the 2D space.
19 	Bitmap1bit		shape;		///The shape defined by a 1 bit bitmap. Null if custom shape isn't needed
20 	///default CTOR
21 	public this(Coordinate position, Bitmap1bit shape) @nogc @safe pure nothrow {
22 		this.position = position;
23 		this.shape = shape;
24 		//this.id = id;
25 	}
26 }
27 alias ObjectMap = TreeMap!(int, CollisionShape);
28 /**
29  * Contains information about an object collision event.
30  */
31 public class ObjectCollisionEvent {
32 	/**
33 	 * Defines types of object collisions that can happen
34 	 */
35 	public enum Type : ubyte {
36 		None,
37 		BoxOverlap,
38 		BoxEdge,
39 		ShapeOverlap,
40 	}
41 	CollisionShape*	shA;		///The object that was tested against other objects
42 	CollisionShape*	shB;		///The object that was found colliding with other objects
43 	int				idA;		///ID of object A
44 	int				idB;		///ID of object B
45 	int				contextID;	///The context of the collision (e.g. tester ID)
46 	Coordinate		overlap;	///Overlapping area of the collision
47 	Type			type;		///Type of the object collision
48 	///default CTOR
49 	public this(CollisionShape* shA, CollisionShape* shB, int contextID, Coordinate overlap, Type type) 
50 			@nogc @safe pure nothrow {
51 		this.shA = shA;
52 		this.shB = shB;
53 		this.contextID = contextID;
54 		this.overlap = overlap;
55 		this.type = type;
56 	}
57 }
58 /**
59  * Contains information about an object to TileLayer collision event.
60  * Custom Bitmap shapes won't be used.
61  */
62 public class TileCollisionEvent {
63 	/**
64 	 * Defines individual tile collisions.
65 	 */
66 	public struct CollisionContext {
67 		Coordinate			position;	///Position of the tile
68 		MappingElement		data;		///Data of the mapping element read out from the layer
69 	}
70 	CollisionShape		a;			///Source object
71 	int					contextID;	///The context of the collision (e.g. layer number)
72 	CollisionContext[]	overlap;	///All overlapping collisions
73 	CollisionContext[]	topEdge;	///Top edge collisions if any
74 	CollisionContext[]	bottomEdge;	///Bottom edge collisions if any
75 	CollisionContext[]	leftEdge;	///Left edge collisions if any
76 	CollisionContext[]	rightEdge;	///Right edge collisions if any
77 }