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 	Box				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(Box 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,				///No collision have been occured
37 		//BoxCorner,		//TODO: Implement
38 		BoxOverlap,			///Two boxes are overlapping
39 		BoxEdge,			///Two edges are one pixel apart
40 		ShapeOverlap,		///Two shapes overlap each other
41 	}
42 	CollisionShape*	shA;		///The object that was tested against other objects
43 	CollisionShape*	shB;		///The object that was found colliding with other objects
44 	int				idA;		///ID of object A
45 	int				idB;		///ID of object B
46 	int				contextID;	///The context of the collision (e.g. tester ID)
47 	Box				overlap;	///Overlapping area of the collision
48 	Type			type;		///Type of the object collision
49 	///default CTOR
50 	public this(CollisionShape* shA, CollisionShape* shB, int contextID, Box overlap, Type type) 
51 			@nogc @safe pure nothrow {
52 		this.shA = shA;
53 		this.shB = shB;
54 		this.contextID = contextID;
55 		this.overlap = overlap;
56 		this.type = type;
57 	}
58 }
59 /**
60  * Contains information about an object to TileLayer collision event.
61  * Custom Bitmap shapes won't be used.
62  */
63 public class TileCollisionEvent {
64 	/**
65 	 * Defines individual tile collisions.
66 	 */
67 	public struct CollisionContext {
68 		Point				position;	///Position of the tile on the map
69 		MappingElement		data;		///Data of the mapping element read out from the layer
70 	}
71 	CollisionShape*		a;			///Source object
72 	int					contextID;	///The context of the collision (e.g. layer number)
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 }