Shared.actors := [] Array optimization and Actor.tag = ""

This commit is contained in:
Harmony Honey 2022-08-01 14:20:32 -04:00
parent 53ee2cb831
commit fa5bdcbb71
16 changed files with 54 additions and 68 deletions

View file

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

View file

@ -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="."]

View file

@ -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="."]

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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="."]

View file

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

View file

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

View file

@ -33,6 +33,8 @@ var is_in_game := false
var sfx_volume = 10
var music_volume = 10
var actors := []
func _ready():
print("Shared._ready(): ")

View file

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

View file

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