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 /** 21 * Creates a collision shape. 22 * Params: 23 * position: The position of the bounding box. 24 * shape: The shape of the object in the form of a 1 bit bitmap if any, null otherwise. 25 */ 26 public this(Box position, Bitmap1Bit shape) @nogc @safe pure nothrow { 27 this.position = position; 28 this.shape = shape; 29 //this.id = id; 30 } 31 } 32 alias ObjectMap = TreeMap!(int, CollisionShape); 33 /** 34 * Contains information about an object collision event. 35 */ 36 public class ObjectCollisionEvent { 37 /** 38 * Defines types of object collisions that can happen 39 */ 40 public enum Type : ubyte { 41 None, ///No collision have been occured 42 //BoxCorner, //TODO: Implement 43 BoxOverlap, ///Two boxes are overlapping 44 BoxEdge, ///Two edges are one pixel apart 45 ShapeOverlap, ///Two shapes overlap each other 46 } 47 CollisionShape* shA; ///The object (A) that was tested against other objects 48 CollisionShape* shB; ///The object (B) that was found colliding with the source object 49 int idA; ///ID of object A 50 int idB; ///ID of object B 51 int contextID; ///The context of the collision (e.g. tester ID) 52 Box overlap; ///Overlapping area of the collision 53 Type type; ///Type of the object collision 54 ///default CTOR 55 public this(CollisionShape* shA, CollisionShape* shB, int contextID, Box overlap, Type type) 56 @nogc @safe pure nothrow { 57 this.shA = shA; 58 this.shB = shB; 59 this.contextID = contextID; 60 this.overlap = overlap; 61 this.type = type; 62 } 63 } 64 /** 65 * Contains information about an object to TileLayer collision event. 66 * Custom Bitmap shapes won't be used. 67 */ 68 public class TileCollisionEvent { 69 /** 70 * Defines individual tile collisions. 71 */ 72 public struct CollisionContext { 73 Point position; ///Position of the tile on the map 74 MappingElement data; ///Data of the mapping element read out from the layer 75 } 76 CollisionShape* a; ///Source object 77 int contextID; ///The context of the collision (e.g. layer number) 78 CollisionContext[] topEdge; ///Top edge collisions if any 79 CollisionContext[] bottomEdge; ///Bottom edge collisions if any 80 CollisionContext[] leftEdge; ///Left edge collisions if any 81 CollisionContext[] rightEdge; ///Right edge collisions if any 82 }