Tim can move now

This commit is contained in:
Waldemar Tomme 2024-01-26 21:01:22 +01:00
parent 702c9563f1
commit 41730b86dd
3 changed files with 47 additions and 2 deletions

18
godot/scenes/Boundary.gd Normal file
View File

@ -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

20
godot/scenes/Tim.gd Normal file
View File

@ -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)

View File

@ -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")