Skip to content

SimpleCharacter: Don't impose a collision shape #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions addons/block_code/simple_nodes/simple_character/simple_character.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const BlockDefinition = preload("res://addons/block_code/code_generation/block_d
const BlocksCatalog = preload("res://addons/block_code/code_generation/blocks_catalog.gd")
const Types = preload("res://addons/block_code/types/types.gd")

## A texture can be provided for simple setup. If provided, the character will have a collision box
## that matches the size of the texture.
@export var texture: Texture2D:
set = _set_texture

Expand Down Expand Up @@ -44,8 +46,28 @@ func _set_texture(new_texture):


func _texture_updated():
if not texture:
if sprite:
sprite.queue_free()
sprite = null
if collision:
collision.queue_free()
collision = null
return

if not sprite:
sprite = Sprite2D.new()
sprite.name = &"Sprite2D"
add_child(sprite)

if not collision:
collision = CollisionShape2D.new()
collision.name = &"CollisionShape2D"
collision.shape = RectangleShape2D.new()
add_child(collision)

sprite.texture = texture
collision.shape.size = Vector2(100, 100) if texture == null else texture.get_size()
collision.shape.size = texture.get_size()


## Nodes in the "affected_by_gravity" group will receive gravity changes:
Expand All @@ -59,29 +81,9 @@ func _ready():

func simple_setup():
add_to_group("affected_by_gravity", true)

sprite = Sprite2D.new()
sprite.name = &"Sprite2D"
add_child(sprite)

collision = CollisionShape2D.new()
collision.name = &"CollisionShape2D"
collision.shape = RectangleShape2D.new()
add_child(collision)

_texture_updated()


func _exit_tree():
if collision:
collision.queue_free()
collision = null

if sprite:
sprite.queue_free()
sprite = null

Copy link
Contributor

@starnight starnight Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These benefit from add_child().


func get_custom_class():
return "SimpleCharacter"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ func simple_setup():
add_child(right_label)


func _exit_tree():
for label in _score_labels.values():
label.queue_free()
_score_labels.clear()

Copy link
Contributor

@starnight starnight Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This benefits from add_child().


func get_custom_class():
return "SimpleScoring"

Expand Down