diff --git a/scenes/game.tscn b/scenes/game.tscn index 3072aa6..6c82378 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=2 format=3 uid="uid://cy3dg66pt8abt"] +[gd_scene load_steps=3 format=3 uid="uid://cy3dg66pt8abt"] [ext_resource type="PackedScene" uid="uid://3wumh6kpw3ai" path="res://scenes/player.tscn" id="1_uwrxv"] +[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_uwrxv"] + [node name="Game" type="Node2D"] [node name="Player" parent="." instance=ExtResource("1_uwrxv")] @@ -10,3 +12,9 @@ position = Vector2(33, 49) [node name="Camera2D" type="Camera2D" parent="."] position = Vector2(39, 27) zoom = Vector2(4, 4) + +[node name="StaticBody2D" type="StaticBody2D" parent="."] +position = Vector2(1, 61) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] +shape = SubResource("WorldBoundaryShape2D_uwrxv") diff --git a/scenes/player.tscn b/scenes/player.tscn index 7d6a7d4..190976e 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=8 format=3 uid="uid://3wumh6kpw3ai"] +[gd_scene load_steps=9 format=3 uid="uid://3wumh6kpw3ai"] [ext_resource type="Texture2D" uid="uid://pc081i405fo4" path="res://assets/sprites/knight.png" id="1_3vyb7"] +[ext_resource type="Script" uid="uid://dkpsredq14h2l" path="res://scripts/player.gd" id="1_g2els"] [sub_resource type="AtlasTexture" id="AtlasTexture_g2els"] atlas = ExtResource("1_3vyb7") @@ -42,6 +43,9 @@ animations = [{ radius = 5.0 [node name="Player" type="CharacterBody2D"] +script = ExtResource("1_g2els") +speed = 130.0 +jump_velocity = -300.0 [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] position = Vector2(0, -12) diff --git a/scripts/player.gd b/scripts/player.gd new file mode 100644 index 0000000..1c2529f --- /dev/null +++ b/scripts/player.gd @@ -0,0 +1,25 @@ +extends CharacterBody2D + + +@export var speed = 300.0 +@export var jump_velocity = -400.0 + + +func _physics_process(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * delta + + # Handle jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + velocity.y = jump_velocity + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var direction := Input.get_axis("ui_left", "ui_right") + if direction: + velocity.x = direction * speed + else: + velocity.x = move_toward(velocity.x, 0, speed) + + move_and_slide() diff --git a/scripts/player.gd.uid b/scripts/player.gd.uid new file mode 100644 index 0000000..dc23803 --- /dev/null +++ b/scripts/player.gd.uid @@ -0,0 +1 @@ +uid://dkpsredq14h2l