diff --git a/scene/demo06_move/Move.tscn b/scene/demo06_move/Move.tscn index 3197407..07dcbf6 100644 --- a/scene/demo06_move/Move.tscn +++ b/scene/demo06_move/Move.tscn @@ -5,7 +5,7 @@ [node name="Node2D" type="Node2D"] -[node name="MoveHero" type="Sprite" parent="."] -position = Vector2( 492.717, 412.746 ) +[node name="HeroMove" type="Sprite" parent="."] +position = Vector2( 552.42, 212.613 ) texture = ExtResource( 2 ) script = ExtResource( 1 ) diff --git a/scene/demo07_collision/Collision.tscn b/scene/demo07_collision/Collision.tscn new file mode 100644 index 0000000..cb2dbd0 --- /dev/null +++ b/scene/demo07_collision/Collision.tscn @@ -0,0 +1,80 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://icon.png" type="Texture" id=1] + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 31.5644, 29.6976 ) + +[sub_resource type="GDScript" id=2] +script/source = "extends KinematicBody2D + + +# How fast the player will move (pixels/sec). +export var speed = 400 +# Size of the game window. +var screen_size + +func _ready(): + screen_size = get_viewport_rect().size + +func _physics_process(delta): + var velocity = Vector2.ZERO # The player's movement vector. + if Input.is_action_pressed(\"move_right\"): + velocity.x += 1 + if Input.is_action_pressed(\"move_left\"): + velocity.x -= 1 + if Input.is_action_pressed(\"move_down\"): + velocity.y += 1 + if Input.is_action_pressed(\"move_up\"): + velocity.y -= 1 + + if velocity.length() <= 0: + return + + velocity = velocity.normalized() * speed + + var info: KinematicCollision2D = move_and_collide(velocity * delta) + if info != null: + print(info.collider.name) +" + +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 30.17, 29.3458 ) + +[node name="Node2D" type="Node2D"] + +[node name="StaticBody2D" type="StaticBody2D" parent="."] +position = Vector2( 64.5824, 547.095 ) + +[node name="Polygon2D" type="Polygon2D" parent="StaticBody2D"] +color = Color( 0.329412, 0.580392, 0.270588, 1 ) +polygon = PoolVector2Array( -23.2672, -184.708, -24.0164, 46.0308, 896.693, 51.2749, 897.443, -184.708, 653.968, 3.3291, 39.6617, 1.83081, 39.6617, -180.214 ) + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="StaticBody2D"] +polygon = PoolVector2Array( -25.5147, -183.959, -26.2638, 45.2817, 895.944, 52.024, 897.443, -184.708, 654.717, 4.07825, 38.1633, -2.66412, 38.9125, -183.21 ) + +[node name="RigidBody2D" type="RigidBody2D" parent="."] +position = Vector2( 880.023, 40.7525 ) +continuous_cd = 1 +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="RigidBody2D"] +texture = ExtResource( 1 ) + +[node name="KinematicBody2D" type="KinematicBody2D" parent="."] +position = Vector2( 549.424, 338.422 ) +script = SubResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="KinematicBody2D"] +shape = SubResource( 3 ) + +[node name="Sprite" type="Sprite" parent="KinematicBody2D"] +texture = ExtResource( 1 ) diff --git a/script/demo07_collision/kinematic_body_2d.gd b/script/demo07_collision/kinematic_body_2d.gd new file mode 100644 index 0000000..f19feec --- /dev/null +++ b/script/demo07_collision/kinematic_body_2d.gd @@ -0,0 +1,30 @@ +extends KinematicBody2D + + +# How fast the player will move (pixels/sec). +export var speed = 400 +# Size of the game window. +var screen_size + +func _ready(): + screen_size = get_viewport_rect().size + +func _physics_process(delta): + var velocity = Vector2.ZERO # The player's movement vector. + if Input.is_action_pressed("move_right"): + velocity.x += 1 + if Input.is_action_pressed("move_left"): + velocity.x -= 1 + if Input.is_action_pressed("move_down"): + velocity.y += 1 + if Input.is_action_pressed("move_up"): + velocity.y -= 1 + + if velocity.length() <= 0: + return + + velocity = velocity.normalized() * speed + + var info: KinematicCollision2D = move_and_collide(velocity * delta) + if info != null: + print(info.collider.name)