1
Fork 0
mirror of https://github.com/Steffo99/hella-farm.git synced 2024-11-23 08:34:22 +00:00

THINGS YAY

This commit is contained in:
Steffo 2024-04-19 04:30:35 +02:00
parent 9739a089d5
commit da0180c5be
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
12 changed files with 102 additions and 27 deletions

View file

@ -1,16 +1,33 @@
@icon("res://behaviours/spawner.svg") @icon("res://behaviours/spawner.svg")
extends Area2D extends Node2D
class_name Spawner class_name Spawner
## Emitted when a new scene is spawned.
signal spawned(entity: Node2D) signal spawned(entity: Node2D)
## Emitted when the [field blocking_tracker] prevents something from spawning.
signal spawn_blocked
## The scene to spawn.
@export var scene: PackedScene @export var scene: PackedScene
## Where the scene should be parented to in the tree.
@export var target: Node2D @export var target: Node2D
## If set, requires the tracker to not be tracking anything before spawning a scene.
@export var blocking_tracker: Tracker
## Spawn [field scene] at [field target] and the position of this node.
func spawn(): func spawn():
if blocking_tracker != null:
if not blocking_tracker.tracking.is_empty():
spawn_blocked.emit()
return # Perhaps enqueue the spawn?
if not target: if not target:
target = MainGame.get_ancestor(self).default_spawn_parent target = MainGame.get_via_group(self).default_spawn_parent
if not target: if not target:
target = self target = self
var entity = scene.instantiate() var entity = scene.instantiate()

View file

@ -2,7 +2,7 @@
extends Area2D extends Area2D
class_name Tracker class_name Tracker
## Abstract base class for [Area2D]s tracking a certain subset of [Node2D]s. ## [Area2D]s tracking a certain subset of [Node2D]s.
signal tracked(body: Node2D) signal tracked(body: Node2D)
@ -11,6 +11,9 @@ signal untracked(body: Node2D)
var tracking: Array = [] var tracking: Array = []
## Start [field tracking] a [Node2D].
##
## Returns whether the body was added to the [field tracking] array.
func track(body: Node2D) -> bool: func track(body: Node2D) -> bool:
var act: bool = not body in tracking var act: bool = not body in tracking
if act: if act:
@ -18,9 +21,12 @@ func track(body: Node2D) -> bool:
tracked.emit(body) tracked.emit(body)
# Handle TrackerTracker # Handle TrackerTracker
for tracker_tracker in body.find_children("*", "TrackerTracker", true, false): for tracker_tracker in body.find_children("*", "TrackerTracker", true, false):
tracker_tracker.track(self) tracker_tracker.track_if_detected(self)
return act return act
## Stop [field tracking] a [Node2D].
##
## Returns whether the body was removed from the [field tracking] array.
func untrack(body: Node2D) -> bool: func untrack(body: Node2D) -> bool:
var act: bool = body in tracking var act: bool = body in tracking
if act: if act:

12
behaviours/tracker.tscn Normal file
View file

@ -0,0 +1,12 @@
[gd_scene load_steps=2 format=3 uid="uid://8jkesanu4hrn"]
[ext_resource type="Script" path="res://behaviours/tracker.gd" id="1_4joji"]
[node name="Tracker" type="Area2D"]
collision_layer = 0
collision_mask = 56
monitorable = false
script = ExtResource("1_4joji")
[connection signal="body_entered" from="." to="." method="track"]
[connection signal="body_exited" from="." to="." method="untrack"]

View file

@ -1,6 +1,24 @@
extends Tracker extends Tracker
class_name TrackerTracker class_name TrackerTracker
## Tracks [Tracker]s tracking the parent. ## Tracks [Tracker]s tracking the parent.
##
## Shape is ignored.
## Start [field tracking] a [Node2D] if it overlaps this [Area2D].
##
## Returns whether the body was added to the [field tracking] array.
func track_if_detected(body: Node2D) -> bool:
if overlaps_body(body):
return track(body)
return false
## Start [field tracking] a [Node2D] if it has a [Tracker] tracking this node or one of its ancestors.
##
## Returns whether the body was added to the [field tracking] array.
func track_if_tracked(body: Node2D) -> bool:
var act: bool = false
for tracker in body.find_children("*", "Tracker", true, false):
for ancestor in Traversal.get_ancestors(self): # Make more efficient, perhaps?
if ancestor in tracker.tracking:
act = track(body) or act
return act

View file

@ -4,7 +4,9 @@
[node name="TrackerTracker" type="Area2D"] [node name="TrackerTracker" type="Area2D"]
collision_layer = 0 collision_layer = 0
collision_mask = 0 collision_mask = 24
monitoring = false
monitorable = false monitorable = false
script = ExtResource("1_7b21r") script = ExtResource("1_7b21r")
[connection signal="body_entered" from="." to="." method="track_if_tracked"]
[connection signal="body_exited" from="." to="." method="untrack"]

View file

@ -9,7 +9,7 @@ func _on_move(movement: Vector2) -> void:
position += movement position += movement
func _on_collected(tag: StringName) -> void: func _on_collected(tag: StringName) -> void:
var game = MainGame.get_ancestor(self) var game = MainGame.get_via_group(self)
# TODO: Perhaps use a dictionary in game to store multiple currencies? # TODO: Perhaps use a dictionary in game to store multiple currencies?
match tag: match tag:

View file

@ -60,7 +60,6 @@ default_priority = 10
alternative_priority = 10 alternative_priority = 10
[node name="ChangeDirectionTimer" parent="MovementWander" instance=ExtResource("10_phvea")] [node name="ChangeDirectionTimer" parent="MovementWander" instance=ExtResource("10_phvea")]
wait_time = 0.001
autostart = true autostart = true
mean_secs = 2.0 mean_secs = 2.0
deviation = 2.0 deviation = 2.0

View file

@ -153,6 +153,10 @@ alternative_priority = 40
[node name="HunterSense" parent="MovementRunFromHunter" instance=ExtResource("14_eqowb")] [node name="HunterSense" parent="MovementRunFromHunter" instance=ExtResource("14_eqowb")]
[node name="Shape" type="CollisionShape2D" parent="MovementRunFromHunter/HunterSense"]
shape = SubResource("CircleShape2D_etpf6")
debug_color = Color(1, 1, 0, 0)
[node name="TargetPicker" parent="MovementRunFromHunter/HunterSense" node_paths=PackedStringArray("tracker") instance=ExtResource("13_jf6em")] [node name="TargetPicker" parent="MovementRunFromHunter/HunterSense" node_paths=PackedStringArray("tracker") instance=ExtResource("13_jf6em")]
tracker = NodePath("..") tracker = NodePath("..")

View file

@ -4,7 +4,7 @@ class_name Cursor
## A [CharacterBody2D] syncing its position with the mouse on each physics timestep. ## A [CharacterBody2D] syncing its position with the mouse on each physics timestep.
@onready var game := MainGame.get_ancestor(self) @onready var game := MainGame.get_via_group(self)
func _physics_process(_delta: float) -> void: func _physics_process(_delta: float) -> void:

View file

@ -2,17 +2,13 @@ extends Node2D
class_name MainGame class_name MainGame
## Get the first possible [MainGame] instance by climbing the scene tree one ancestor at a time.
static func get_ancestor(start: Node) -> MainGame:
var current = start
while current is Node:
if current is MainGame:
return current
current = current.get_parent()
Log.w(start, "MainGame ancestor not found.")
return null
@onready var gold_counter: Counter = $"GoldCounter" @onready var gold_counter: Counter = $"GoldCounter"
@onready var camera: GameCamera = $"GameCamera" @onready var camera: GameCamera = $"GameCamera"
@onready var default_spawn_parent: Node2D = %"DefaultSpawnParent" @onready var default_spawn_parent: Node2D = %"DefaultSpawnParent"
static func get_via_group(node: Node) -> MainGame:
var result = node.get_tree().get_nodes_in_group("game")
if result.is_empty():
return null
return result[0]

File diff suppressed because one or more lines are too long

11
traversal.gd Normal file
View file

@ -0,0 +1,11 @@
class_name Traversal
## Get the first possible [MainGame] instance by climbing the scene tree one ancestor at a time.
static func get_ancestors(start: Node) -> Array[Node]:
var result: Array[Node] = []
var current = start
while current is Node:
result.push_back(current)
current = current.get_parent()
return result