Distance counter and player switching implemented.

This commit is contained in:
Patrick_Pluto 2024-06-13 14:25:37 +02:00
parent d31d027b80
commit 047f64fef6
4 changed files with 44 additions and 6 deletions

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,7 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_kf6qt"]
size = Vector2(20, 18) size = Vector2(20, 18)
[node name="CharacterBody2D" type="CharacterBody2D"] [node name="player" type="CharacterBody2D"]
script = ExtResource("1_oik63") script = ExtResource("1_oik63")
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
@ -15,4 +15,5 @@ texture = ExtResource("1_cujcj")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_kf6qt") shape = SubResource("RectangleShape2D_kf6qt")
[node name="Camera2D" type="Camera2D" parent="."] [node name="camera" type="Camera2D" parent="."]
enabled = false

17
scripts/mapscript.gd Normal file
View file

@ -0,0 +1,17 @@
extends Node2D
var playerAmount=2
var playerIndex=0
var objectName
func _ready():
next()
func next():
if playerIndex >= playerAmount:
playerIndex = 0
objectName= "player%d" % playerIndex
print(objectName)
get_node(objectName).start()
playerIndex += 1

View file

@ -1,11 +1,26 @@
extends CharacterBody2D extends CharacterBody2D
@export var speed = 200 @export var speed = 200
var toCalculate = position
var distanceTo = 0
var active = false
func get_input(): func get_input():
var input_direction = Input.get_vector("left", "right", "up", "down") var input_direction = Input.get_vector("left", "right", "up", "down")
velocity = input_direction * speed velocity = input_direction * speed
func _physics_process(delta): func _physics_process(delta):
get_input() if active:
move_and_slide() get_input()
move_and_slide()
distanceTo = position.distance_to(toCalculate)
if distanceTo > 500 and active:
active = false
$camera.enabled = false
get_parent().next()
func start():
$camera.enabled = true
active = true
toCalculate = position