From 9e2a5136928d2fbf6c8275e0bcf5fc759607a3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Mon, 10 Oct 2022 05:01:02 +0200 Subject: [PATCH] more random movement toward target --- core/battlefield.gd | 13 +++++++++++++ core/battlefield.tscn | 5 ++++- core/unit.gd | 7 +------ tac/units/fighter.gd | 4 ++-- tac/units/fighter.tscn | 2 +- 5 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 core/battlefield.gd diff --git a/core/battlefield.gd b/core/battlefield.gd new file mode 100644 index 0000000..a1ff695 --- /dev/null +++ b/core/battlefield.gd @@ -0,0 +1,13 @@ +extends Node2D + +var _ticker1 := 0.0 + +func _physics_process(delta): + _ticker1 += delta + if _ticker1 > 1.0: + _ticker1 -= 1.0 + ticker("ticker1") + +func ticker(group_name: String): + for node in get_tree().get_nodes_in_group(group_name): + node.tick() diff --git a/core/battlefield.tscn b/core/battlefield.tscn index ead8aca..a5e9640 100644 --- a/core/battlefield.tscn +++ b/core/battlefield.tscn @@ -1,3 +1,6 @@ -[gd_scene format=3 uid="uid://brbtq46uk18gg"] +[gd_scene load_steps=2 format=3 uid="uid://brbtq46uk18gg"] + +[ext_resource type="Script" path="res://core/battlefield.gd" id="1_u7icn"] [node name="battlefield" type="Node2D"] +script = ExtResource("1_u7icn") diff --git a/core/unit.gd b/core/unit.gd index 23eab0e..579abd9 100644 --- a/core/unit.gd +++ b/core/unit.gd @@ -29,12 +29,10 @@ func _compose(): func _physics_process(delta): if not target_reached: - # TODO use position instead of position, once parented to battlefield + # TODO use position instead of global_position, once parented to battlefield global_position += (target_position - global_position).normalized() * 100.0 * delta if global_position == target_position: target_reached = true - - make_decision() # TODO not every tick func move_to(pos: Vector2): if pos != target_position: @@ -48,6 +46,3 @@ func damage(lost_hitpoints: float): func die(): queue_free() - -func make_decision(): - pass diff --git a/tac/units/fighter.gd b/tac/units/fighter.gd index d826a31..e6fe6ec 100644 --- a/tac/units/fighter.gd +++ b/tac/units/fighter.gd @@ -1,6 +1,6 @@ extends Unit -func make_decision(): +func tick(): # go to nearest enemy var enemy: Unit = null var closest := INF @@ -11,5 +11,5 @@ func make_decision(): closest = distance enemy = unit if enemy: - move_to(enemy.global_position) + move_to(enemy.global_position + Vector2.from_angle(randf_range(0.0, PI * 2.0)) * randf_range(100.0, 300.0)) $turret.set_target(enemy) diff --git a/tac/units/fighter.tscn b/tac/units/fighter.tscn index c85f52e..226ebaf 100644 --- a/tac/units/fighter.tscn +++ b/tac/units/fighter.tscn @@ -5,7 +5,7 @@ [ext_resource type="Texture2D" uid="uid://2nhprnyswhyh" path="res://tac/assets/ship_small_1.png" id="2_oablw"] [ext_resource type="PackedScene" uid="uid://byg2bmiy2gia0" path="res://tac/weapons/turret.tscn" id="3_rt3r8"] -[node name="fighter" instance=ExtResource("1_2ka4q")] +[node name="fighter" groups=["ticker1"] instance=ExtResource("1_2ka4q")] script = ExtResource("2_evq3k") hitpoints = 20