mirror of
https://github.com/Steffo99/hella-farm.git
synced 2024-11-25 01:24:23 +00:00
Generalize move_towards_mouse in move_towards
Also create move_towards_target
This commit is contained in:
parent
6a14979de7
commit
8f46ff5022
4 changed files with 67 additions and 38 deletions
44
behaviours/move_towards.gd
Normal file
44
behaviours/move_towards.gd
Normal file
|
@ -0,0 +1,44 @@
|
|||
extends Node2D
|
||||
class_name MoveTowards
|
||||
|
||||
|
||||
signal move(movement: Vector2)
|
||||
signal detached
|
||||
signal captured
|
||||
|
||||
|
||||
@export var speed: float = 100.0
|
||||
@export var can_detach: bool = false
|
||||
|
||||
|
||||
enum State { DETACHED, CAPTURED }
|
||||
|
||||
var state: State = State.DETACHED
|
||||
|
||||
func get_followed_global_position():
|
||||
return Vector2.ZERO # OVERRIDE ME!
|
||||
|
||||
|
||||
func get_followed_mouse_position():
|
||||
var global_followed_position: Vector2 = get_followed_global_position()
|
||||
var relative_followed_position: Vector2 = global_followed_position - global_position
|
||||
return relative_followed_position
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
match state:
|
||||
State.CAPTURED:
|
||||
var relative_followed_position: Vector2 = get_followed_mouse_position()
|
||||
var direction: Vector2 = position.direction_to(relative_followed_position)
|
||||
var actual_speed: float = min(delta * speed, relative_followed_position.length()) # Don't overshoot.
|
||||
var movement: Vector2 = direction * actual_speed
|
||||
move.emit(movement)
|
||||
|
||||
func _on_capture_area_mouse_entered() -> void:
|
||||
state = State.CAPTURED
|
||||
captured.emit()
|
||||
|
||||
func _on_capture_area_mouse_exited() -> void:
|
||||
if can_detach:
|
||||
state = State.DETACHED
|
||||
detached.emit()
|
|
@ -1,43 +1,8 @@
|
|||
extends Node2D
|
||||
extends MoveTowards
|
||||
class_name MoveTowardsMouse
|
||||
|
||||
|
||||
signal move(movement: Vector2)
|
||||
signal detached
|
||||
signal captured
|
||||
|
||||
|
||||
@export var speed: float = 100.0
|
||||
@export var can_detach: bool = false
|
||||
|
||||
@onready var game := MainGame.get_ancestor(self)
|
||||
|
||||
|
||||
enum State { DETACHED, CAPTURED }
|
||||
|
||||
var state: State = State.DETACHED
|
||||
|
||||
|
||||
func get_relative_mouse_position():
|
||||
var global_mouse_position: Vector2 = game.camera.get_global_mouse_position()
|
||||
var relative_mouse_position: Vector2 = global_mouse_position - global_position
|
||||
return relative_mouse_position
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
match state:
|
||||
State.CAPTURED:
|
||||
var relative_mouse_position: Vector2 = get_relative_mouse_position()
|
||||
var direction: Vector2 = position.direction_to(relative_mouse_position)
|
||||
var actual_speed: float = min(delta * speed, relative_mouse_position.length()) # Don't overshoot.
|
||||
var movement: Vector2 = direction * actual_speed
|
||||
move.emit(movement)
|
||||
|
||||
func _on_capture_area_mouse_entered() -> void:
|
||||
state = State.CAPTURED
|
||||
captured.emit()
|
||||
|
||||
func _on_capture_area_mouse_exited() -> void:
|
||||
if can_detach:
|
||||
state = State.DETACHED
|
||||
detached.emit()
|
||||
func get_followed_global_position():
|
||||
return game.camera.get_global_mouse_position()
|
||||
|
|
8
behaviours/move_towards_target.gd
Normal file
8
behaviours/move_towards_target.gd
Normal file
|
@ -0,0 +1,8 @@
|
|||
extends MoveTowards
|
||||
class_name MoveTowardsTarget
|
||||
|
||||
@export var target: Node2D
|
||||
|
||||
|
||||
func get_followed_global_position():
|
||||
return target.global_position
|
12
behaviours/move_towards_target.tscn
Normal file
12
behaviours/move_towards_target.tscn
Normal file
|
@ -0,0 +1,12 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c7b4v6h7a3dy6"]
|
||||
|
||||
[ext_resource type="Script" path="res://behaviours/move_towards_target.gd" id="1_703gy"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbg5kgwxusvxf" path="res://behaviours/hover_detector.tscn" id="2_nq6qb"]
|
||||
|
||||
[node name="MoveTowardsTarget" type="Node2D"]
|
||||
script = ExtResource("1_703gy")
|
||||
|
||||
[node name="CaptureArea" parent="." instance=ExtResource("2_nq6qb")]
|
||||
|
||||
[connection signal="mouse_entered" from="CaptureArea" to="." method="_on_capture_area_mouse_entered"]
|
||||
[connection signal="mouse_exited" from="CaptureArea" to="." method="_on_capture_area_mouse_exited"]
|
Loading…
Reference in a new issue