1 /** 2 Kinematic body 2D node. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.kinematicbody2d; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.physicsbody2d; 24 import godot.kinematiccollision2d; 25 import godot.collisionobject2d; 26 import godot.node2d; 27 import godot.canvasitem; 28 import godot.node; 29 /** 30 Kinematic body 2D node. 31 32 Kinematic bodies are special types of bodies that are meant to be user-controlled. They are not affected by physics at all (to other types of bodies, such a character or a rigid body, these are the same as a static body). They have however, two main uses: 33 Simulated Motion: When these bodies are moved manually, either from code or from an AnimationPlayer (with process mode set to fixed), the physics will automatically compute an estimate of their linear and angular velocity. This makes them very useful for moving platforms or other AnimationPlayer-controlled objects (like a door, a bridge that opens, etc). 34 Kinematic Characters: KinematicBody2D also has an API for moving objects (the $(D moveAndCollide) and $(D moveAndSlide) methods) while performing collision tests. This makes them really useful to implement characters that collide against a world, but that don't require advanced physics. 35 */ 36 @GodotBaseClass struct KinematicBody2D 37 { 38 enum string _GODOT_internal_name = "KinematicBody2D"; 39 public: 40 @nogc nothrow: 41 union { godot_object _godot_object; PhysicsBody2D _GODOT_base; } 42 alias _GODOT_base this; 43 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 44 package(godot) __gshared bool _classBindingInitialized = false; 45 package(godot) static struct _classBinding 46 { 47 __gshared: 48 @GodotName("move_and_collide") GodotMethod!(KinematicCollision2D, Vector2, bool, bool, bool) moveAndCollide; 49 @GodotName("move_and_slide") GodotMethod!(Vector2, Vector2, Vector2, bool, bool, long, double) moveAndSlide; 50 @GodotName("move_and_slide_with_snap") GodotMethod!(Vector2, Vector2, Vector2, Vector2, bool, bool, long, double) moveAndSlideWithSnap; 51 @GodotName("test_move") GodotMethod!(bool, Transform2D, Vector2, bool) testMove; 52 @GodotName("is_on_floor") GodotMethod!(bool) isOnFloor; 53 @GodotName("is_on_ceiling") GodotMethod!(bool) isOnCeiling; 54 @GodotName("is_on_wall") GodotMethod!(bool) isOnWall; 55 @GodotName("get_floor_velocity") GodotMethod!(Vector2) getFloorVelocity; 56 @GodotName("set_safe_margin") GodotMethod!(void, double) setSafeMargin; 57 @GodotName("get_safe_margin") GodotMethod!(double) getSafeMargin; 58 @GodotName("get_slide_count") GodotMethod!(long) getSlideCount; 59 @GodotName("get_slide_collision") GodotMethod!(KinematicCollision2D, long) getSlideCollision; 60 @GodotName("set_sync_to_physics") GodotMethod!(void, bool) setSyncToPhysics; 61 @GodotName("is_sync_to_physics_enabled") GodotMethod!(bool) isSyncToPhysicsEnabled; 62 @GodotName("_direct_state_changed") GodotMethod!(void, GodotObject) _directStateChanged; 63 } 64 bool opEquals(in KinematicBody2D other) const { return _godot_object.ptr is other._godot_object.ptr; } 65 KinematicBody2D opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 66 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 67 mixin baseCasts; 68 static KinematicBody2D _new() 69 { 70 static godot_class_constructor constructor; 71 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("KinematicBody2D"); 72 if(constructor is null) return typeof(this).init; 73 return cast(KinematicBody2D)(constructor()); 74 } 75 @disable new(size_t s); 76 /** 77 Moves the body along the vector `rel_vec`. The body will stop if it collides. Returns a $(D KinematicCollision2D), which contains information about the collision. 78 */ 79 Ref!KinematicCollision2D moveAndCollide(in Vector2 rel_vec, in bool infinite_inertia = true, in bool exclude_raycast_shapes = true, in bool test_only = false) 80 { 81 checkClassBinding!(typeof(this))(); 82 return ptrcall!(KinematicCollision2D)(_classBinding.moveAndCollide, _godot_object, rel_vec, infinite_inertia, exclude_raycast_shapes, test_only); 83 } 84 /** 85 Moves the body along a vector. If the body collides with another, it will slide along the other body rather than stop immediately. If the other body is a `KinematicBody2D` or $(D RigidBody2D), it will also be affected by the motion of the other body. You can use this to make moving or rotating platforms, or to make nodes push other nodes. 86 `linear_velocity` is a value in pixels per second. Unlike in for example $(D moveAndCollide), you should $(I not) multiply it with `delta` — this is done by the method. 87 `floor_normal` is the up direction, used to determine what is a wall and what is a floor or a ceiling. If set to the default value of `Vector2(0, 0)`, everything is considered a wall. This is useful for topdown games. 88 $(I TODO: Update for stop_on_slope argument.) If the body is standing on a slope and the horizontal speed (relative to the floor's speed) goes below `slope_stop_min_velocity`, the body will stop completely. This prevents the body from sliding down slopes when you include gravity in `linear_velocity`. When set to lower values, the body will not be able to stand still on steep slopes. 89 If the body collides, it will change direction a maximum of `max_bounces` times before it stops. 90 `floor_max_angle` is the maximum angle (in radians) where a slope is still considered a floor (or a ceiling), rather than a wall. The default value equals 45 degrees. 91 Returns the movement that remained when the body stopped. To get more detailed information about collisions that occurred, use $(D getSlideCollision). 92 */ 93 Vector2 moveAndSlide(in Vector2 linear_velocity, in Vector2 floor_normal = Vector2(0, 0), in bool infinite_inertia = true, in bool stop_on_slope = false, in long max_bounces = 4, in double floor_max_angle = 0.785398) 94 { 95 checkClassBinding!(typeof(this))(); 96 return ptrcall!(Vector2)(_classBinding.moveAndSlide, _godot_object, linear_velocity, floor_normal, infinite_inertia, stop_on_slope, max_bounces, floor_max_angle); 97 } 98 /** 99 Moves the body while keeping it attached to slopes. Similar to $(D moveAndSlide). 100 As long as the `snap` vector is in contact with the ground, the body will remain attached to the surface. This means you must disable snap in order to jump, for example. You can do this by setting `snap` to `(0, 0)` or by using $(D moveAndSlide) instead. 101 */ 102 Vector2 moveAndSlideWithSnap(in Vector2 linear_velocity, in Vector2 snap, in Vector2 floor_normal = Vector2(0, 0), in bool infinite_inertia = true, in bool stop_on_slope = false, in long max_bounces = 4, in double floor_max_angle = 0.785398) 103 { 104 checkClassBinding!(typeof(this))(); 105 return ptrcall!(Vector2)(_classBinding.moveAndSlideWithSnap, _godot_object, linear_velocity, snap, floor_normal, infinite_inertia, stop_on_slope, max_bounces, floor_max_angle); 106 } 107 /** 108 Checks for collisions without moving the body. Virtually sets the node's position, scale and rotation to that of the given $(D Transform2D), then tries to move the body along the vector `rel_vec`. Returns `true` if a collision would occur. 109 */ 110 bool testMove(in Transform2D from, in Vector2 rel_vec, in bool infinite_inertia) 111 { 112 checkClassBinding!(typeof(this))(); 113 return ptrcall!(bool)(_classBinding.testMove, _godot_object, from, rel_vec, infinite_inertia); 114 } 115 /** 116 Returns `true` if the body is on the floor. Only updates when calling $(D moveAndSlide). 117 */ 118 bool isOnFloor() const 119 { 120 checkClassBinding!(typeof(this))(); 121 return ptrcall!(bool)(_classBinding.isOnFloor, _godot_object); 122 } 123 /** 124 Returns `true` if the body is on the ceiling. Only updates when calling $(D moveAndSlide). 125 */ 126 bool isOnCeiling() const 127 { 128 checkClassBinding!(typeof(this))(); 129 return ptrcall!(bool)(_classBinding.isOnCeiling, _godot_object); 130 } 131 /** 132 Returns `true` if the body is on a wall. Only updates when calling $(D moveAndSlide). 133 */ 134 bool isOnWall() const 135 { 136 checkClassBinding!(typeof(this))(); 137 return ptrcall!(bool)(_classBinding.isOnWall, _godot_object); 138 } 139 /** 140 Returns the velocity of the floor. Only updates when calling $(D moveAndSlide). 141 */ 142 Vector2 getFloorVelocity() const 143 { 144 checkClassBinding!(typeof(this))(); 145 return ptrcall!(Vector2)(_classBinding.getFloorVelocity, _godot_object); 146 } 147 /** 148 149 */ 150 void setSafeMargin(in double pixels) 151 { 152 checkClassBinding!(typeof(this))(); 153 ptrcall!(void)(_classBinding.setSafeMargin, _godot_object, pixels); 154 } 155 /** 156 157 */ 158 double getSafeMargin() const 159 { 160 checkClassBinding!(typeof(this))(); 161 return ptrcall!(double)(_classBinding.getSafeMargin, _godot_object); 162 } 163 /** 164 Returns the number of times the body collided and changed direction during the last call to $(D moveAndSlide). 165 */ 166 long getSlideCount() const 167 { 168 checkClassBinding!(typeof(this))(); 169 return ptrcall!(long)(_classBinding.getSlideCount, _godot_object); 170 } 171 /** 172 Returns a $(D KinematicCollision2D), which contains information about a collision that occurred during the last $(D moveAndSlide) call. Since the body can collide several times in a single call to $(D moveAndSlide), you must specify the index of the collision in the range 0 to ($(D getSlideCount) - 1). 173 */ 174 Ref!KinematicCollision2D getSlideCollision(in long slide_idx) 175 { 176 checkClassBinding!(typeof(this))(); 177 return ptrcall!(KinematicCollision2D)(_classBinding.getSlideCollision, _godot_object, slide_idx); 178 } 179 /** 180 181 */ 182 void setSyncToPhysics(in bool enable) 183 { 184 checkClassBinding!(typeof(this))(); 185 ptrcall!(void)(_classBinding.setSyncToPhysics, _godot_object, enable); 186 } 187 /** 188 189 */ 190 bool isSyncToPhysicsEnabled() const 191 { 192 checkClassBinding!(typeof(this))(); 193 return ptrcall!(bool)(_classBinding.isSyncToPhysicsEnabled, _godot_object); 194 } 195 /** 196 197 */ 198 void _directStateChanged(GodotObject arg0) 199 { 200 Array _GODOT_args = Array.empty_array; 201 _GODOT_args.append(arg0); 202 String _GODOT_method_name = String("_direct_state_changed"); 203 this.callv(_GODOT_method_name, _GODOT_args); 204 } 205 /** 206 If the body is at least this close to another body, this body will consider them to be colliding. 207 */ 208 @property double collisionSafeMargin() 209 { 210 return getSafeMargin(); 211 } 212 /// ditto 213 @property void collisionSafeMargin(double v) 214 { 215 setSafeMargin(v); 216 } 217 /** 218 If `true` the body's movement will be synchronized to the physics frame. This is useful when animating movement via $(D AnimationPlayer), for example on moving platforms. 219 */ 220 @property bool motionSyncToPhysics() 221 { 222 return isSyncToPhysicsEnabled(); 223 } 224 /// ditto 225 @property void motionSyncToPhysics(bool v) 226 { 227 setSyncToPhysics(v); 228 } 229 }