diff --git a/godot/scenes/Boundary.gd b/godot/scenes/Boundary.gd new file mode 100644 index 0000000..4fe97d8 --- /dev/null +++ b/godot/scenes/Boundary.gd @@ -0,0 +1,18 @@ +class_name Boundary +extends Node2D + +@export var width = 200 + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass + +func get_most_left_position(): + return global_position.x - width / 2 + +func get_most_right_position(): + return global_position.x + width / 2 diff --git a/godot/scenes/Tim.gd b/godot/scenes/Tim.gd new file mode 100644 index 0000000..1850e91 --- /dev/null +++ b/godot/scenes/Tim.gd @@ -0,0 +1,20 @@ +extends Node2D + +@export var move_speed = 100 +@export var boundary: Boundary + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + if Input.is_action_pressed("move_right"): + position += Vector2.RIGHT * delta * move_speed + if position.x > boundary.get_most_right_position(): + position = Vector2(boundary.get_most_right_position(), position.y) + + if Input.is_action_pressed("move_left"): + position += Vector2.LEFT * delta * move_speed + if position.x < boundary.get_most_left_position(): + position = Vector2(boundary.get_most_left_position(), position.y) diff --git a/godot/scenes/stage.tscn b/godot/scenes/stage.tscn index 0e881fe..6fedda2 100644 --- a/godot/scenes/stage.tscn +++ b/godot/scenes/stage.tscn @@ -1,6 +1,8 @@ -[gd_scene load_steps=3 format=3 uid="uid://dw8tbafwj1rgo"] +[gd_scene load_steps=5 format=3 uid="uid://dw8tbafwj1rgo"] +[ext_resource type="Script" path="res://scenes/Tim.gd" id="1_g3k2b"] [ext_resource type="Texture2D" uid="uid://kq63ictuirhc" path="res://sprites/tim_side.png" id="1_saxit"] +[ext_resource type="Script" path="res://scenes/Boundary.gd" id="2_8p6ir"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_3hx6l"] size = Vector2(600, 200) @@ -11,8 +13,13 @@ size = Vector2(600, 200) texture = SubResource("PlaceholderTexture2D_3hx6l") offset = Vector2(0, -100) -[node name="Tim" type="Node2D" parent="."] +[node name="Tim" type="Node2D" parent="." node_paths=PackedStringArray("boundary")] +script = ExtResource("1_g3k2b") +boundary = NodePath("../Boundary") [node name="Sprite2D" type="Sprite2D" parent="Tim"] texture = ExtResource("1_saxit") offset = Vector2(0, -200) + +[node name="Boundary" type="Node2D" parent="."] +script = ExtResource("2_8p6ir")