From fa5bdcbb71bb41c2f7e3ed4db66626548ba36ba1 Mon Sep 17 00:00:00 2001 From: Harmony Honey Date: Mon, 1 Aug 2022 14:20:32 -0400 Subject: [PATCH] Shared.actors := [] Array optimization and Actor.tag = "" --- src/actor/Actor.gd | 45 ++++++++++++++-------------------- src/actor/Box.tscn | 9 +++---- src/actor/Exit.tscn | 6 ++--- src/actor/Passthrough.gd | 2 +- src/actor/Passthrough.tscn | 6 ++--- src/actor/Player.gd | 3 +++ src/actor/Player.tscn | 9 +++---- src/actor/Spike.tscn | 6 ++--- src/actor/Switch.tscn | 6 ++--- src/actor/SwitchBlock.gd | 10 +++----- src/actor/SwitchBlock.tscn | 4 +-- src/actor/SwitchBlockBlue.tscn | 2 ++ src/actor/SwitchBlue.tscn | 1 + src/autoload/Shared.gd | 2 ++ src/fx/Explosion2.gd | 7 +++--- src/menu/select.gd | 4 +-- 16 files changed, 54 insertions(+), 68 deletions(-) diff --git a/src/actor/Actor.gd b/src/actor/Actor.gd index 48f2081..ff353f4 100644 --- a/src/actor/Actor.gd +++ b/src/actor/Actor.gd @@ -13,6 +13,8 @@ var term_vel := 16 var remainder := Vector2.ZERO # movement and collision +export var tag := "actor" +export var is_solid := false export var is_moving := false export var is_colliding := false export var is_using_gravity := false @@ -36,6 +38,12 @@ var time_since_floor := 0 # ignore this actor's solidity var ignore_actor : Actor +func _enter_tree(): + Shared.actors.append(self) + +func _exit_tree(): + Shared.actors.erase(self) + # Called when the node enters the scene tree for the first time. func _ready(): position = position.floor() @@ -61,21 +69,6 @@ func _set_hit_y(value): hitbox_y = value update() -func set_solid(arg := false): - #is_solid = arg - if arg: - add_to_group("solid") - else: - remove_from_group("solid") - -func set_active(arg := false): - #is_active = arg - set_physics_process(arg) - if arg: - add_to_group("actor") - else: - remove_from_group("actor") - # draw hitbox in editor func _draw(): if Engine.editor_hint: @@ -219,8 +212,8 @@ func is_area_solid_tile(x1, y1, width, height): # check area for solid actors func is_area_solid_actor(x, y, width = hitbox_x, height = hitbox_y, ignore = null) -> bool: - for a in get_tree().get_nodes_in_group("solid"): - if a != self and a != ignore and a != ignore_actor: + for a in Shared.actors: + if a.is_solid and a != self and a != ignore and a != ignore_actor: if aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): return true return false @@ -232,21 +225,21 @@ func is_area_solid(x = position.x, y = position.y, width = hitbox_x, height = hi return is_area_solid_actor(x, y, width, height, ignore) # is overlapping any actor? -func is_area_actor(group_name = "actor", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): - for a in get_tree().get_nodes_in_group(group_name if group_name else "actor"): - if a != self and a != ignore and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): +func is_area_actor(_tag = "", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): + for a in Shared.actors: + if a != self and a != ignore and (_tag != "" and a.tag == _tag) and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): return true return false # return array of actors -func check_area_actors(group_name = "actor", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): +func check_area_actors(_tag = "", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): var act = [] - for a in get_tree().get_nodes_in_group(group_name if group_name else "actor"): - if a != self and a != ignore and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): + for a in Shared.actors: + if a != self and a != ignore and (_tag != "" and a.tag == _tag) and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): act.append(a) return act -func check_area_first_actor(group_name = "actor", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): - for a in get_tree().get_nodes_in_group(group_name if group_name else "actor"): - if a != self and a != ignore and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): +func check_area_first_actor(_tag = "", x = position.x, y = position.y, width = hitbox_x, height = hitbox_y, ignore = null): + for a in Shared.actors: + if a != self and a != ignore and (_tag != "" and a.tag == _tag) and aabb(x, y, width, height, a.position.x, a.position.y, a.hitbox_x, a.hitbox_y): return a diff --git a/src/actor/Box.tscn b/src/actor/Box.tscn index 175e533..3aa639f 100644 --- a/src/actor/Box.tscn +++ b/src/actor/Box.tscn @@ -32,18 +32,15 @@ tracks/1/keys = { "values": [ Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ) ] } -[node name="Box" type="Node2D" groups=[ -"actor", -"box", -"solid", -]] +[node name="Box" type="Node2D"] z_index = 4 z_as_relative = false script = ExtResource( 3 ) +tag = "box" +is_solid = true is_moving = true is_colliding = true is_using_gravity = true -is_using_tread = true push_dur = 0.04 [node name="Sprite" type="Sprite" parent="."] diff --git a/src/actor/Exit.tscn b/src/actor/Exit.tscn index 3b0fd9b..7ee8d1b 100644 --- a/src/actor/Exit.tscn +++ b/src/actor/Exit.tscn @@ -34,15 +34,13 @@ tracks/1/keys = { "values": [ Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ) ] } -[node name="Exit" type="Node2D" groups=[ -"actor", -"exit", -]] +[node name="Exit" type="Node2D"] z_index = 10 z_as_relative = false script = ExtResource( 2 ) hitbox_x = 6 hitbox_y = 6 +tag = "exit" dust_speed = 0.1 [node name="Sprite" type="Sprite" parent="."] diff --git a/src/actor/Passthrough.gd b/src/actor/Passthrough.gd index f549339..1bff18a 100644 --- a/src/actor/Passthrough.gd +++ b/src/actor/Passthrough.gd @@ -23,7 +23,7 @@ func _physics_process(delta): is_done = not is_area_solid_actor(position.x, position.y) if is_done: node_sprite.frame += 1 - set_solid(true) + is_solid = true node_audio.play() else: is_hit = is_area_solid_actor(position.x, position.y) diff --git a/src/actor/Passthrough.tscn b/src/actor/Passthrough.tscn index 61b87bc..c0ba5a9 100644 --- a/src/actor/Passthrough.tscn +++ b/src/actor/Passthrough.tscn @@ -4,11 +4,9 @@ [ext_resource path="res://media/image/crate.png" type="Texture" id=2] [ext_resource path="res://media/audio/sfx/hit0.wav" type="AudioStream" id=3] -[node name="Passthrough" type="Node2D" groups=[ -"actor", -"passthrough", -]] +[node name="Passthrough" type="Node2D" groups=["actor", "passthrough"]] script = ExtResource( 1 ) +tag = "passthrough" [node name="Sprite" type="Sprite" parent="."] position = Vector2( 4, 4 ) diff --git a/src/actor/Player.gd b/src/actor/Player.gd index 1193677..d3e96dc 100644 --- a/src/actor/Player.gd +++ b/src/actor/Player.gd @@ -181,6 +181,9 @@ func _physics_process(delta): node_audio_push.volume_db = linear2db(push_fade / 0.2) if push_fade == 0: node_audio_push.stop() + + + node_sprite.position = Vector2(4,-4) + remainder func box_release(sx := 0.0, sy := 0.0): is_pickup = false diff --git a/src/actor/Player.tscn b/src/actor/Player.tscn index 2eb92a5..1f22230 100644 --- a/src/actor/Player.tscn +++ b/src/actor/Player.tscn @@ -174,18 +174,15 @@ tracks/1/keys = { "values": [ Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ), Vector2( 0, 0 ) ] } -[node name="Player" type="Node2D" groups=[ -"actor", -"player", -"solid", -]] +[node name="Player" type="Node2D"] z_index = 15 z_as_relative = false script = ExtResource( 1 ) +tag = "player" +is_solid = true is_moving = true is_colliding = true is_using_gravity = true -is_using_tread = true move_speed = 0.9 jump_speed = 1.4 jump_frames = 16 diff --git a/src/actor/Spike.tscn b/src/actor/Spike.tscn index 2897d29..d525e43 100644 --- a/src/actor/Spike.tscn +++ b/src/actor/Spike.tscn @@ -3,14 +3,12 @@ [ext_resource path="res://media/image/crate_tiles.png" type="Texture" id=1] [ext_resource path="res://src/actor/Actor.gd" type="Script" id=2] -[node name="Spike" type="Node2D" groups=[ -"actor", -"spike", -]] +[node name="Spike" type="Node2D" groups=["actor", "spike"]] z_index = 5 z_as_relative = false script = ExtResource( 2 ) hitbox_y = 3 +tag = "spike" [node name="Sprite" type="Sprite" parent="."] position = Vector2( 4, -1 ) diff --git a/src/actor/Switch.tscn b/src/actor/Switch.tscn index 9c3cc7e..f548c4a 100644 --- a/src/actor/Switch.tscn +++ b/src/actor/Switch.tscn @@ -4,13 +4,13 @@ [ext_resource path="res://src/actor/Switch.gd" type="Script" id=2] [ext_resource path="res://media/audio/sfx/btn3.wav" type="AudioStream" id=3] -[node name="Switch" type="Node2D" groups=[ -"actor", -]] +[node name="Switch" type="Node2D" groups=["actor"]] z_index = -5 z_as_relative = false script = ExtResource( 2 ) hitbox_y = 4 +tag = "" +is_solid = "" [node name="Sprite" type="Sprite" parent="."] position = Vector2( 4, 0 ) diff --git a/src/actor/SwitchBlock.gd b/src/actor/SwitchBlock.gd index ce63fc5..bfee3b2 100644 --- a/src/actor/SwitchBlock.gd +++ b/src/actor/SwitchBlock.gd @@ -20,18 +20,16 @@ func _physics_process(delta): if Engine.editor_hint: return - if is_switch: - if not is_in_group("solid"): - if not is_area_solid_actor(position.x, position.y): - set_solid(true) - node_sprite.frame = frame_on + if is_switch and !is_solid and !is_area_solid_actor(position.x, position.y): + is_solid = true + node_sprite.frame = frame_on func switch_on(): is_switch = true func switch_off(): is_switch = false - set_solid(false) + is_solid = false node_sprite.frame = frame_off diff --git a/src/actor/SwitchBlock.tscn b/src/actor/SwitchBlock.tscn index 2cdd193..68b4edd 100644 --- a/src/actor/SwitchBlock.tscn +++ b/src/actor/SwitchBlock.tscn @@ -3,9 +3,7 @@ [ext_resource path="res://media/image/crate.png" type="Texture" id=1] [ext_resource path="res://src/actor/SwitchBlock.gd" type="Script" id=2] -[node name="SwitchBlock" type="Node2D" groups=[ -"actor", -]] +[node name="SwitchBlock" type="Node2D" groups=["actor"]] script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] diff --git a/src/actor/SwitchBlockBlue.tscn b/src/actor/SwitchBlockBlue.tscn index 96db9e6..b556223 100644 --- a/src/actor/SwitchBlockBlue.tscn +++ b/src/actor/SwitchBlockBlue.tscn @@ -3,6 +3,8 @@ [ext_resource path="res://src/actor/SwitchBlock.tscn" type="PackedScene" id=1] [node name="SwitchBlockBlue" instance=ExtResource( 1 )] +tag = "False" +is_solid = false color = "blue" frame_on = 14 frame_off = 12 diff --git a/src/actor/SwitchBlue.tscn b/src/actor/SwitchBlue.tscn index 5011db5..1fd0292 100644 --- a/src/actor/SwitchBlue.tscn +++ b/src/actor/SwitchBlue.tscn @@ -3,6 +3,7 @@ [ext_resource path="res://src/actor/Switch.tscn" type="PackedScene" id=1] [node name="SwitchBlue" instance=ExtResource( 1 )] +is_solid = false color = "blue" [node name="Sprite" parent="." index="0"] diff --git a/src/autoload/Shared.gd b/src/autoload/Shared.gd index 1e4d242..cf427e5 100644 --- a/src/autoload/Shared.gd +++ b/src/autoload/Shared.gd @@ -33,6 +33,8 @@ var is_in_game := false var sfx_volume = 10 var music_volume = 10 +var actors := [] + func _ready(): print("Shared._ready(): ") diff --git a/src/fx/Explosion2.gd b/src/fx/Explosion2.gd index fd51685..54ab678 100644 --- a/src/fx/Explosion2.gd +++ b/src/fx/Explosion2.gd @@ -9,9 +9,10 @@ export var lerp_speed := 0.1 func _ready(): target_pos = position - for a in get_tree().get_nodes_in_group("exit"): - target_pos = a.position + Vector2(3, 3) - break + for a in Shared.actors: + if a.tag == "exit": + target_pos = a.position + Vector2(3, 3) + break node_audio.pitch_scale = 1 + rand_range(-0.2, 0.2) node_audio.play() diff --git a/src/menu/select.gd b/src/menu/select.gd index ae793b0..7a19717 100644 --- a/src/menu/select.gd +++ b/src/menu/select.gd @@ -64,8 +64,8 @@ func view_scene(port, path): if ResourceLoader.exists(path + ".tscn"): var m = load(path + ".tscn").instance() port.add_child(m) - for i in get_tree().get_nodes_in_group("actor"): - if !i.is_in_group("exit"): + for i in Shared.actors: + if !i.tag == "exit": i.set_physics_process(false) # dont process actors func scroll(arg = 0):