ft: switch to click movement
This commit is contained in:
@@ -3,6 +3,7 @@ class_name FoodMolecular
|
||||
|
||||
@onready var collision: Area2D = $Collision
|
||||
@onready var sprite: AnimatedSprite2D = $Collision/Sprite
|
||||
@onready var player = get_tree().get_first_node_in_group("player") # FIXME: this is not v efficient; global resource? or at least per Stage.
|
||||
|
||||
func _ready() -> void:
|
||||
sprite.play()
|
||||
@@ -30,3 +31,10 @@ func apply_effect(consumer: Node2D) -> void:
|
||||
if consumer.has_method("collect_resource"): # TODO: Define a "consumer" group instead?
|
||||
consumer.collect_resource(val) # val is from parent (default 1), override if required
|
||||
# TODO: *Some cool effect here*
|
||||
|
||||
|
||||
# FIXME: position tracking inaccurate?
|
||||
func _on_collision_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if player:
|
||||
player.target = self.collision
|
||||
|
||||
@@ -53,9 +53,6 @@ radius = 5.0
|
||||
|
||||
[node name="FoodMol" type="Node2D" unique_id=742430243]
|
||||
script = ExtResource("1_0vfbj")
|
||||
val = null
|
||||
food_name = null
|
||||
flow_carry_speed = null
|
||||
metadata/_custom_type_script = "uid://cayxffay17o0u"
|
||||
|
||||
[node name="Collision" type="Area2D" parent="." unique_id=927700818]
|
||||
@@ -78,3 +75,4 @@ script = ExtResource("3_8lhj0")
|
||||
metadata/_custom_type_script = "uid://bvbc0n0pslq7p"
|
||||
|
||||
[connection signal="body_entered" from="Collision" to="." method="_on_body_entered"]
|
||||
[connection signal="input_event" from="Collision" to="." method="_on_collision_input_event"]
|
||||
|
||||
@@ -23,6 +23,7 @@ var healthPoints: int = 100
|
||||
var isAlive: bool = true
|
||||
@onready var invulnerable_cooldown_timer: Timer = $InvulnerableCooldownTimer
|
||||
var hasiframes: bool = false # only used for iframe after collision for now
|
||||
var target = Vector2.ZERO # either vector2 or Node2D
|
||||
|
||||
func _ready() -> void:
|
||||
var screen_size = get_viewport_rect().size
|
||||
@@ -35,31 +36,37 @@ func _ready() -> void:
|
||||
sprite.play("default")
|
||||
|
||||
|
||||
func _input(event):
|
||||
# TODO: only does clicks/taps; accept mouse drags
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
target = get_global_mouse_position()
|
||||
|
||||
func _physics_process(delta):
|
||||
var pos
|
||||
if typeof(target) == TYPE_VECTOR2:
|
||||
pos = target
|
||||
elif "position" in target:
|
||||
pos = target.position
|
||||
else:
|
||||
return
|
||||
var dir: Vector2 = position.direction_to(pos)
|
||||
|
||||
if position.distance_to(pos) > 2:
|
||||
self.desired_rotation = atan2(dir.y, dir.x)
|
||||
move_and_collide(speed * dir * delta)
|
||||
position = GameManager.get_boundaried_position(position)
|
||||
|
||||
|
||||
func _process(delta):
|
||||
if isAlive:
|
||||
velocity = Vector2.ZERO
|
||||
if Input.is_action_pressed("move_right"):
|
||||
velocity.x += 1
|
||||
if Input.is_action_pressed("move_left"):
|
||||
velocity.x -= 1
|
||||
if Input.is_action_pressed("move_down"):
|
||||
velocity.y += 1
|
||||
if Input.is_action_pressed("move_up"):
|
||||
velocity.y -= 1
|
||||
if Input.is_action_pressed("try_attack"):
|
||||
try_attack()
|
||||
|
||||
|
||||
if not velocity.is_zero_approx():
|
||||
self.desired_rotation = atan2(velocity.y, velocity.x)
|
||||
|
||||
# smoothly rotate
|
||||
if self.rotation != self.desired_rotation:
|
||||
self.rotation = lerp_angle(self.rotation, self.desired_rotation, clampf(4 * delta, 0, 1))
|
||||
|
||||
move_and_collide(speed * velocity * delta)
|
||||
position = GameManager.get_boundaried_position(position)
|
||||
|
||||
|
||||
func try_attack() -> void:
|
||||
if not can_attack:
|
||||
return
|
||||
|
||||
@@ -69,3 +69,9 @@ func _on_sight_body_entered(body: Node2D) -> void:
|
||||
|
||||
func _on_attack_cooldown_timer_timeout() -> void:
|
||||
can_attack = true
|
||||
|
||||
|
||||
func _on_collision_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if player:
|
||||
player.target = self.collision
|
||||
|
||||
@@ -101,6 +101,7 @@ metadata/_custom_type_script = "uid://dgfimmq53whll"
|
||||
scale = Vector2(0.8, 0.8)
|
||||
collision_layer = 4
|
||||
collision_mask = 3
|
||||
input_pickable = true
|
||||
script = ExtResource("2_7qt2q")
|
||||
|
||||
[node name="Sprite" type="AnimatedSprite2D" parent="Collision" unique_id=410999609]
|
||||
@@ -154,6 +155,7 @@ script = ExtResource("8_7qt2q")
|
||||
wait_time = 0.5
|
||||
one_shot = true
|
||||
|
||||
[connection signal="input_event" from="Collision" to="." method="_on_collision_input_event"]
|
||||
[connection signal="body_entered" from="Collision/Sight" to="." method="_on_sight_body_entered"]
|
||||
[connection signal="timeout" from="StateMachine/Idle/Timer" to="StateMachine/Idle" method="_on_timer_timeout"]
|
||||
[connection signal="timeout" from="StateMachine/RandomMovement/Timer" to="StateMachine/RandomMovement" method="_on_timer_timeout"]
|
||||
|
||||
@@ -42,3 +42,9 @@ func become_injured() -> void:
|
||||
func _on_sight_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("predator") or (health < maxHealth and body.is_in_group("player")):
|
||||
fsm.transition_to_next_state(fsm.States.FLEEING, {"threat": body})
|
||||
|
||||
|
||||
func _on_collision_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
|
||||
if player:
|
||||
player.target = self.collision
|
||||
|
||||
@@ -59,6 +59,7 @@ maxHealth = 20
|
||||
scale = Vector2(0.2, 0.2)
|
||||
collision_layer = 2
|
||||
collision_mask = 5
|
||||
input_pickable = true
|
||||
script = ExtResource("3_teu34")
|
||||
|
||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Collision" index="0" unique_id=1471691984]
|
||||
@@ -109,6 +110,7 @@ script = ExtResource("12_ubfhk")
|
||||
[node name="Timer" type="Timer" parent="StateMachine/Idle" index="0" unique_id=1050348256]
|
||||
one_shot = true
|
||||
|
||||
[connection signal="input_event" from="Collision" to="." method="_on_collision_input_event"]
|
||||
[connection signal="body_entered" from="Collision/Sight" to="." method="_on_sight_body_entered"]
|
||||
[connection signal="timeout" from="StateMachine/RandomMovement/Timer" to="StateMachine/RandomMovement" method="_on_timer_timeout"]
|
||||
[connection signal="timeout" from="StateMachine/Idle/Timer" to="StateMachine/Idle" method="_on_timer_timeout"]
|
||||
|
||||
Reference in New Issue
Block a user