-
Notifications
You must be signed in to change notification settings - Fork 289
Rework and document CharacterSight and PlayerInteraction #2078
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eabf539
Rework and document CharacterSight and PlayerInteraction
manuq 29188a7
champ StoryQuest: Fix champ_stealth scene
manuq 58d2abc
after_the_tremor StoryQuest: Adapt minijuego2 level to player changes
manuq 856a1cd
fixup! Rework and document CharacterSight and PlayerInteraction
manuq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 0 additions & 39 deletions
39
scenes/game_elements/characters/player/components/interact_zone.gd
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
scenes/game_elements/props/character_sight/character_sight.gd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # SPDX-FileCopyrightText: The Threadbare Authors | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
| class_name CharacterSight | ||
| extends Area2D | ||
| ## The area of sight of the character. | ||
| ## | ||
| ## The character sees one [InteractArea] at a time to interact. | ||
| ## This area also faces toward the character current direction. | ||
| ## [br][br] | ||
| ## This script automatically configures the correct [member collision_layer] and | ||
| ## [member collision_mask] values to enable interaction with the player. | ||
|
|
||
| ## Emitted when [member interact_area] changes. | ||
| signal interact_area_changed | ||
|
|
||
| ## The character. | ||
| @export var character: CharacterBody2D | ||
|
|
||
| ## The direction the character is facing. | ||
| ## [br][br] | ||
| ## TODO: Use east/west instead of right/left? | ||
| var is_looking_from_right: bool = false | ||
|
|
||
| ## The area that the character is currently observing. | ||
| var interact_area: InteractArea | ||
|
|
||
|
|
||
| func _ready() -> void: | ||
| if not character and owner is CharacterBody2D: | ||
| character = owner | ||
| collision_layer = 0 | ||
| collision_mask = 0 | ||
| set_collision_mask_value(Enums.CollisionLayers.INTERACTABLE, true) | ||
| area_entered.connect(_on_area_entered) | ||
| area_exited.connect(_on_area_exited) | ||
|
|
||
|
|
||
| func _get_best_interact_area() -> InteractArea: | ||
| if not monitoring: | ||
| return null | ||
| var areas := get_overlapping_areas() | ||
| var best: InteractArea = null | ||
| var best_distance: float = INF | ||
|
|
||
| # TODO: This picks the closest area according to their global position, | ||
| # which could be misleading. An Area2D may have a collision shape | ||
| # that is far away from it's anchor. | ||
| for area in areas: | ||
| var distance := global_position.distance_to(area.global_position) | ||
| if not best or distance < best_distance: | ||
| best_distance = distance | ||
| best = area | ||
|
|
||
| return best | ||
|
|
||
|
|
||
| func _process(_delta: float) -> void: | ||
| if not character: | ||
| return | ||
| # Flip this area according to the character current direction: | ||
| if not is_zero_approx(character.velocity.x): | ||
| if character.velocity.x < 0: | ||
| scale.x = -1 | ||
| else: | ||
| scale.x = 1 | ||
| is_looking_from_right = scale.x < 0 | ||
|
|
||
|
|
||
| func _on_area_entered(_area: Area2D) -> void: | ||
| interact_area = _get_best_interact_area() | ||
| interact_area_changed.emit() | ||
|
|
||
|
|
||
| func _on_area_exited(_area: Area2D) -> void: | ||
| interact_area = _get_best_interact_area() | ||
| interact_area_changed.emit() |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a bit weird for this to live in
propsrather thanplayer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I did it to match interact_area. But also this can potentially be attached to a CharacterBody2D NPC.
Maybe I move it to scenes/game_logic/ ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could work. Honestly I think it's also quite strange that this script (not a scene) lives in
scenes/, like many many others (e.g. area_filler.gd) but I can't bring myself to propose rearranging all the folders in the project again :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't feel the need to address this now, it's just a path, we can change it later. I was just observing that it seems a bit strange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes the whole structure is strange to me as well!