Skip to content
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

Dev Task: Area designation and detection #167

Open
NancokPS2 opened this issue Dec 7, 2023 · 2 comments
Open

Dev Task: Area designation and detection #167

NancokPS2 opened this issue Dec 7, 2023 · 2 comments
Labels
enhancement New feature or request feature request game logic game logic related tickets

Comments

@NancokPS2
Copy link
Contributor

NancokPS2 commented Dec 7, 2023

This could help a lot and could be used trough a component. It can be used to define areas as safe/unsafe or other things like a boss area with special properties. This component could be used both on a character as a detector or in the world as a designator.

Either by using a combo of 2 different components or a single one that can switch between detecting and simply being a designated area.

For a single object, the script could be something like this (leaving as reference for later mostly):

extends Area2D  
class_name AreaDesignationComponent  
  
signal area_detected(type: String)  

enum Modes { DESIGNATOR, DETECTOR }
@export var mode: Modes    

@export var area_type: String

var overlappingTypes: Array[String]    


func _init():  
    collision_mask = J.AREA_LAYER  
    collision_layer = J.AREA_LAYER    
      
    if mode == Modes.DETECTOR:  
        body_entered.connect(_on_body_entered_detector)  
        body_exited.connect(_on_body_exited_detector)

func update_mode():      
    if mode == Modes.DESIGNATOR:  
        monitoreable = true  
        monitoring = false  
    elif mode == Modes.DETECTOR:  
        monitoreable = false  
        monitoring = true  

func detector_get_areas() -> Array[String]:   
    return overlappingTypes
    

func _on_body_entered_detector(body: Node):    
    if body is AreaDesignationComponent and not body.area_type in overlappingTypes:  
        overlappingTypes.append(body.area_type)    
        area_detected.emit(body.area_type)

func _on_body_exited_detector(body: Node):    
    if body is AreaDesignationComponent:  
        overlappingTypes.erase(body.area_type)  

Or this:

extends Area2D  
class_name AreaDesignationComponent  
  
signal area_detected(type: String)  

@export var area_types: Array[String]

var pointParameters := PhysicsPointParameters2D.new()

func _init():  
    collision_mask = J.AREA_LAYER  
    collision_layer = J.AREA_LAYER    
    monitoreable = true  
    monitoring = false     
    pointParameters.collide_with_areas = true  
    pointParameters.collide_with_bodies = false  
    pointParameters.collision_mask = J.AREA_LAYER

static func detector_get_areas(globalPoint: Vector2) -> Array[String]:     
    var directSpace: DirectPhysicsSpace2D = get_world_2d().direct_space  
    var pointParameters := PhysicsPointQueryParameters2D.new()    

    pointParameters.position = globalPoint  
  
    directSpace.intersect_point(pointParameters)
        
    ...
@jonathaneeckhout
Copy link
Owner

What is are some use cases for this component?

@jonathaneeckhout jonathaneeckhout added enhancement New feature or request game logic game logic related tickets feature request labels Dec 28, 2023
@NancokPS2
Copy link
Contributor Author

  • Telling enemies which areas they can and cannot wander towards.
  • Defining zones where the player is considered as "safe" and can do actions that would otherwise be locked.
  • For quests that randomize locations of objectives, an area can be defined instead.
  • Informing players of their current area.
  • Defining where PvP can be performed.

As i proposed before, these areas would be little more than an Area2D with an Array[String]. Each component/script would be in charge of interpreting each String as they see fit.
For example, the code for allowing changing selected skills, would check if the player's current position touches an area with the "safe" tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request game logic game logic related tickets
Projects
None yet
Development

No branches or pull requests

2 participants