1 /// contains types used by the engine 2 module ysge.types; 3 4 import std.math; 5 import std.format; 6 7 /// vec2 type with x and y values 8 struct Vec2(T) { 9 T x, y; 10 11 /// initialises with an x and y value 12 this(T px, T py) { 13 x = px; 14 y = py; 15 } 16 17 /// calculates the angle from this vec2 to another vec2 18 double AngleTo(Vec2!T to) { 19 return atan2(cast(float) (to.y - y), cast(float) (to.x - x)); 20 } 21 22 /// casts the vec2 to a vec2 with int values 23 Vec2!int ToIntVec() { 24 return Vec2!int(cast(int) x, cast(int) y); 25 } 26 27 /// casts the vec2 to a vec2 with float values 28 Vec2!float ToFloatVec() { 29 return Vec2!float(cast(float) x, cast(float) y); 30 } 31 32 /// returns the distance from this vec2 to another vec2 33 T DistanceTo(Vec2!T other) { 34 Vec2!T distance; 35 distance.x = abs(other.x - x); 36 distance.y = abs(other.y - y); 37 return cast(T) sqrt( 38 cast(float) ((distance.x * distance.x) + (distance.y * distance.y)) 39 ); 40 } 41 42 /// casts the vec2 to the given type 43 Vec2!T2 CastTo(T2)() { 44 return Vec2!T2( 45 cast(T2) x, 46 cast(T2) y 47 ); 48 } 49 50 /// checks if 2 vec2s have equal values 51 bool Equals(Vec2!T right) { 52 return ( 53 (x == right.x) && 54 (y == right.y) 55 ); 56 } 57 58 /// converts the vec2 to a string 59 string toString() { 60 return format("(%s, %s)", x, y); 61 } 62 }