From 1baed58659825332190b4fa38f3a36e2d403da03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20C=2E=20R=C4=83dulescu?= Date: Fri, 7 Feb 2020 12:29:45 +0200 Subject: [PATCH] Review smart agents I made minimal changes, mostly cosmetic like so: - rename KinematicMovementType to MovementType since GSTKinematicBody2DAgent.KinematicMovementType.COLLIDE for example is really more than a mouthful with repeated Kinematic in the name - add optional movement_type parameter to the constructor, otherwise we'd be forced to construct the object and then specify as an aditional step the type of movement if we want something else than the default - rewrote the constructor to yield on ready and removed _on_body_ready - renamed _apply_steering to apply_steering as this is a public method - renamed _on_SceneTree_frame to _on_SceneTree_physics_frame --- project/demos/Arrive/Arriver.gd | 2 +- project/demos/AvoidCollisions/Avoider.gd | 2 +- project/demos/Face/Turret.gd | 2 +- project/demos/FollowPath/PathFollower.gd | 2 +- project/demos/GroupBehaviors/Member.gd | 2 +- project/demos/SeekFlee/Seeker.gd | 2 +- project/src/Agents/GSTKinematicBody2DAgent.gd | 34 ++++++++----------- project/src/Agents/GSTKinematicBodyAgent.gd | 34 ++++++++----------- project/src/Agents/GSTRigidBody2DAgent.gd | 2 +- project/src/Agents/GSTRigidBodyAgent.gd | 2 +- project/src/Agents/GSTSpecializedAgent.gd | 2 +- 11 files changed, 39 insertions(+), 47 deletions(-) diff --git a/project/demos/Arrive/Arriver.gd b/project/demos/Arrive/Arriver.gd index 0d3b81d..dc7e196 100644 --- a/project/demos/Arrive/Arriver.gd +++ b/project/demos/Arrive/Arriver.gd @@ -12,7 +12,7 @@ var _drag := 0.1 func _physics_process(delta: float) -> void: arrive.calculate_steering(_accel) - agent._apply_steering(_accel, delta) + agent.apply_steering(_accel, delta) func setup( diff --git a/project/demos/AvoidCollisions/Avoider.gd b/project/demos/AvoidCollisions/Avoider.gd index dfb8281..894ae7e 100644 --- a/project/demos/AvoidCollisions/Avoider.gd +++ b/project/demos/AvoidCollisions/Avoider.gd @@ -31,7 +31,7 @@ func _physics_process(delta: float) -> void: target.position.y = agent.position.y + _direction.y*_radius priority.calculate_steering(_accel) - agent._apply_steering(_accel, delta) + agent.apply_steering(_accel, delta) func setup( diff --git a/project/demos/Face/Turret.gd b/project/demos/Face/Turret.gd index 8326837..30ca245 100644 --- a/project/demos/Face/Turret.gd +++ b/project/demos/Face/Turret.gd @@ -20,7 +20,7 @@ func _ready() -> void: func _physics_process(delta: float) -> void: face.calculate_steering(_accel) - agent._apply_steering(_accel, delta) + agent.apply_steering(_accel, delta) func _draw() -> void: diff --git a/project/demos/FollowPath/PathFollower.gd b/project/demos/FollowPath/PathFollower.gd index d9b54cb..67dd256 100644 --- a/project/demos/FollowPath/PathFollower.gd +++ b/project/demos/FollowPath/PathFollower.gd @@ -36,7 +36,7 @@ func setup( func _physics_process(delta: float) -> void: if _valid: follow.calculate_steering(_accel) - agent._apply_steering(_accel, delta) + agent.apply_steering(_accel, delta) func _on_Drawer_path_established(points: Array) -> void: diff --git a/project/demos/GroupBehaviors/Member.gd b/project/demos/GroupBehaviors/Member.gd index 183a681..4fe6168 100644 --- a/project/demos/GroupBehaviors/Member.gd +++ b/project/demos/GroupBehaviors/Member.gd @@ -46,7 +46,7 @@ func _draw() -> void: func _physics_process(delta: float) -> void: if blend: blend.calculate_steering(acceleration) - agent._apply_steering(acceleration, delta) + agent.apply_steering(acceleration, delta) func set_neighbors(neighbor: Array) -> void: diff --git a/project/demos/SeekFlee/Seeker.gd b/project/demos/SeekFlee/Seeker.gd index a18d111..896a420 100644 --- a/project/demos/SeekFlee/Seeker.gd +++ b/project/demos/SeekFlee/Seeker.gd @@ -27,4 +27,4 @@ func _physics_process(delta: float) -> void: else: flee.calculate_steering(accel) - agent._apply_steering(accel, delta) + agent.apply_steering(accel, delta) diff --git a/project/src/Agents/GSTKinematicBody2DAgent.gd b/project/src/Agents/GSTKinematicBody2DAgent.gd index 1f18176..4854939 100644 --- a/project/src/Agents/GSTKinematicBody2DAgent.gd +++ b/project/src/Agents/GSTKinematicBody2DAgent.gd @@ -4,7 +4,7 @@ extends GSTSpecializedAgent class_name GSTKinematicBody2DAgent -enum KinematicMovementType { SLIDE, COLLIDE, POSITION } +enum MovementType { SLIDE, COLLIDE, POSITION } # The KinematicBody2D to keep track of @@ -15,30 +15,31 @@ var body: KinematicBody2D setget _set_body # SLIDE uses use move_and_slide # COLLIDE uses move_and_collide # POSITION changes the global_position directly -var kinematic_movement_type: int = KinematicMovementType.SLIDE +var movement_type: int var _last_position: Vector2 -func _init(body: KinematicBody2D) -> void: +func _init(body: KinematicBody2D, movement_type: int = MovementType.SLIDE) -> void: + yield(body, "ready") + self.body = body - if body.is_inside_tree(): - body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame") - else: - body.connect("ready", self, "_on_body_ready") + self.movement_type = movement_type + + body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame") # Moves the agent's `body` by target `acceleration`. # tags: virtual -func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: +func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: _applied_steering = true - match kinematic_movement_type: - KinematicMovementType.COLLIDE: + match movement_type: + MovementType.COLLIDE: _apply_collide_steering(acceleration.linear, delta) - KinematicMovementType.SLIDE: + MovementType.SLIDE: _apply_sliding_steering(acceleration.linear) _: - _apply_normal_steering(acceleration.linear, delta) + _apply_position_steering(acceleration.linear, delta) _apply_orientation_steering(acceleration.angular, delta) @@ -64,7 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void: linear_velocity = velocity -func _apply_normal_steering(accel: Vector3, delta: float) -> void: +func _apply_position_steering(accel: Vector3, delta: float) -> void: var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max) if apply_linear_drag: velocity = velocity.linear_interpolate( @@ -95,12 +96,7 @@ func _set_body(value: KinematicBody2D) -> void: orientation = _last_orientation -func _on_body_ready() -> void: - body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame") - _set_body(body) - - -func _on_SceneTree_frame() -> void: +func _on_SceneTree_physics_frame() -> void: var current_position: Vector2 = body.global_position var current_orientation: float = body.rotation diff --git a/project/src/Agents/GSTKinematicBodyAgent.gd b/project/src/Agents/GSTKinematicBodyAgent.gd index 690c147..3744047 100644 --- a/project/src/Agents/GSTKinematicBodyAgent.gd +++ b/project/src/Agents/GSTKinematicBodyAgent.gd @@ -4,7 +4,7 @@ extends GSTSpecializedAgent class_name GSTKinematicBodyAgent -enum KinematicMovementType { SLIDE, COLLIDE, POSITION } +enum MovementType { SLIDE, COLLIDE, POSITION } # The KinematicBody to keep track of @@ -15,30 +15,31 @@ var body: KinematicBody setget _set_body # SLIDE uses use move_and_slide # COLLIDE uses move_and_collide # POSITION changes the global_position directly -var kinematic_movement_type: int = KinematicMovementType.SLIDE +var movement_type: int var _last_position: Vector3 -func _init(body: KinematicBody) -> void: +func _init(body: KinematicBody, movement_type: int = MovementType.SLIDE) -> void: + yield(body, "ready") + self.body = body - if body.is_inside_tree(): - body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame") - else: - body.connect("ready", self, "_on_body_ready") + self.movement_type = movement_type + + body.get_tree().connect("physics_frame", self, "_on_SceneTree_physics_frame") # Moves the agent's `body` by target `acceleration`. # tags: virtual -func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: +func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: _applied_steering = true - match kinematic_movement_type: - KinematicMovementType.COLLIDE: + match movement_type: + MovementType.COLLIDE: _apply_collide_steering(acceleration.linear, delta) - KinematicMovementType.SLIDE: + MovementType.SLIDE: _apply_sliding_steering(acceleration.linear) _: - _apply_normal_steering(acceleration.linear, delta) + _apply_position_steering(acceleration.linear, delta) _apply_orientation_steering(acceleration.angular, delta) @@ -64,7 +65,7 @@ func _apply_collide_steering(accel: Vector3, delta: float) -> void: linear_velocity = velocity -func _apply_normal_steering(accel: Vector3, delta: float) -> void: +func _apply_position_steering(accel: Vector3, delta: float) -> void: var velocity := GSTUtils.clampedv3(linear_velocity + accel, linear_speed_max) if apply_linear_drag: velocity = velocity.linear_interpolate( @@ -95,12 +96,7 @@ func _set_body(value: KinematicBody) -> void: orientation = _last_orientation -func _on_body_ready() -> void: - body.get_tree().connect("physics_frame", self, "_on_SceneTree_frame") - _set_body(body) - - -func _on_SceneTree_frame() -> void: +func _on_SceneTree_physics_frame() -> void: var current_position: Vector3 = body.global_position var current_orientation: float = body.rotation.y diff --git a/project/src/Agents/GSTRigidBody2DAgent.gd b/project/src/Agents/GSTRigidBody2DAgent.gd index 2222259..bb52652 100644 --- a/project/src/Agents/GSTRigidBody2DAgent.gd +++ b/project/src/Agents/GSTRigidBody2DAgent.gd @@ -20,7 +20,7 @@ func _init(body: RigidBody2D) -> void: # Moves the agent's `body` by target `acceleration`. # tags: virtual -func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: +func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: _applied_steering = true body.apply_central_impulse(GSTUtils.to_vector2(acceleration.linear)) body.apply_torque_impulse(acceleration.angular) diff --git a/project/src/Agents/GSTRigidBodyAgent.gd b/project/src/Agents/GSTRigidBodyAgent.gd index cb5eb3d..499e6c4 100644 --- a/project/src/Agents/GSTRigidBodyAgent.gd +++ b/project/src/Agents/GSTRigidBodyAgent.gd @@ -20,7 +20,7 @@ func _init(body: RigidBody) -> void: # Moves the agent's `body` by target `acceleration`. # tags: virtual -func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: +func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: _applied_steering = true body.apply_central_impulse(acceleration.linear) body.apply_torque_impulse(Vector3.UP * acceleration.angular) diff --git a/project/src/Agents/GSTSpecializedAgent.gd b/project/src/Agents/GSTSpecializedAgent.gd index 9392cd3..55b0bdf 100644 --- a/project/src/Agents/GSTSpecializedAgent.gd +++ b/project/src/Agents/GSTSpecializedAgent.gd @@ -36,5 +36,5 @@ var _applied_steering := false # Moves the agent's body by target `acceleration`. # tags: virtual -func _apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: +func apply_steering(acceleration: GSTTargetAcceleration, delta: float) -> void: pass