a game about throwing hammers made for the github game off
improving homing (again)
| -rw-r--r-- | enemys/wrencher.gd | 2 | ||||
| -rw-r--r-- | hammers/hammer.gd | 43 | ||||
| -rw-r--r-- | hammers/hammer_base.tscn | 18 | ||||
| -rw-r--r-- | player/player.gd | 1 |
4 files changed, 43 insertions, 21 deletions
diff --git a/enemys/wrencher.gd b/enemys/wrencher.gd index 4952726..484aa3b 100644 --- a/enemys/wrencher.gd +++ b/enemys/wrencher.gd @@ -46,7 +46,7 @@ func _physics_process(_delta: float) -> void: func fire() -> void: var hammer: Hammer = Utils.get_hammer().instantiate() hammer.global_position = muzzle.global_position - hammer.steer_force = 0.01 # cheat + hammer.steer_force = 0.005 # cheat hammer.target = Globals.player hammer.direction = up_direction bullet_timer = get_tree().create_timer(SHOT_COOLDOWN) diff --git a/hammers/hammer.gd b/hammers/hammer.gd index 2e80ddf..51cb587 100644 --- a/hammers/hammer.gd +++ b/hammers/hammer.gd @@ -6,11 +6,13 @@ class_name Hammer @onready var left_cast := $LeftCast as RayCast2D @onready var right_cast := $RightCast as RayCast2D +@onready var target_cast := $TargetCast as RayCast2D @onready var trail := $Trail as Trail2D @onready var outline_shader := ($Sprite as Sprite2D).material as ShaderMaterial @onready var target_finder := $TargetFinder as Area2D @onready var hitbox := $Hitbox as Hitbox -@onready var hitbox_c := hitbox.get_child(0) +@onready var hitbox_c := hitbox.get_child(0) as CollisionShape2D +@onready var c := $Collision as CollisionShape2D ## The current velocity var velocity := Vector2.ZERO @@ -25,13 +27,13 @@ var direction := Vector2.ZERO @export var top_speed := 3.0 ## The amount it can turn towards its target -@export var steer_force = 0.05 +@export var steer_force = 0.01 ## The hit enum. -enum HITS {_a, _b, _c, PLAYER, ENEMY, NONE} +enum HITS {_a, _b, NONE, PLAYER, ENEMY} -## These nodes want to have their collision mask set. -@onready var hitmasks = [target_finder, hitbox, left_cast, right_cast, self] +## These nodes want to have their collision mask set to the current enemy. +@onready var hitmasks = [target_finder, hitbox, left_cast, right_cast, self, target_cast] ## Pick which layers to hit. @export var hits: HITS = HITS.PLAYER: @@ -40,14 +42,13 @@ enum HITS {_a, _b, _c, PLAYER, ENEMY, NONE} node.set_collision_mask_value(hits, false) hits = value hitbox.monitoring = hits != HITS.NONE - left_cast.enabled = hits != HITS.NONE - right_cast.enabled = hits != HITS.NONE + target_finder.monitoring = not is_instance_valid(target) if value == HITS.NONE: return for node in hitmasks: node.set_collision_mask_value(hits, true) - target_finder.monitoring = not is_instance_valid(target) - hitbox_c.shape.size.x = 6 if hits == HITS.ENEMY else 2 + hitbox_c.shape.size.x = 8 if hits == HITS.ENEMY else 1 + c.shape.size.x = 8 if hits == HITS.ENEMY else 1 ## The amount of time before gravity kicks in. @export var lifetime := 3.0 @@ -68,17 +69,24 @@ func dirlerp(to: Vector2) -> void: ## Moves the direction towards the target. func seek() -> void: if is_instance_valid(target): + target_cast.force_raycast_update() + if is_enemy(target_cast.get_collider()): # course is good, dont touch anything + return dirlerp(global_position.direction_to(target.global_position)) + anticrash() elif target_finder.monitoring == false: target = null target_finder.monitoring = true +func is_enemy(n) -> bool: + if is_instance_valid(target) and is_instance_valid(n) and n.get_class() == target.get_class(): return true + return false + ## Tries not to crash. func anticrash() -> void: var is_wall := func is_wall(ray: RayCast2D) -> bool: - if not ray.is_colliding(): return false - if is_instance_valid(target) and ray.get_collider().get_class() == target.get_class(): return false - return true + ray.force_raycast_update() + return not is_enemy(ray.get_collider()) var results: Array[bool] = [is_wall.call(left_cast), is_wall.call(right_cast)] if results.count(true) == 2: return # resign to our fate @@ -103,7 +111,6 @@ func _physics_process(delta: float) -> void: velocity.y += grav * delta else: seek() - anticrash() velocity += (direction * acceleration * delta) if velocity.y < 0: velocity.y = lerpf(velocity.y, 0, .1) # hard to move up @@ -112,7 +119,7 @@ func _physics_process(delta: float) -> void: rotation = velocity.angle() + PI / 2 # face forward global_position += velocity - +# we crashed func _on_body_entered(_body: Node2D) -> void: trail.emitting = false target_finder.monitoring = false @@ -137,6 +144,9 @@ func throw(p_direction: Vector2) -> void: func _on_target_finder_node_entered(_n: Node2D) -> void: + if target != null: + push_error("Huh.") + return var bods: Array[Node2D] = target_finder.get_overlapping_bodies() + target_finder.get_overlapping_areas() var space = get_world_2d().direct_space_state var current := {closest = null, dist = 0} @@ -147,5 +157,6 @@ func _on_target_finder_node_entered(_n: Node2D) -> void: if current.dist < dist: current.dist = dist current.closest = bod - target = current.closest - target_finder.set_deferred(&"monitoring", false) + if current.closest != null: + target = current.closest + target_finder.set_deferred(&"monitoring", false) diff --git a/hammers/hammer_base.tscn b/hammers/hammer_base.tscn index 9876740..4535773 100644 --- a/hammers/hammer_base.tscn +++ b/hammers/hammer_base.tscn @@ -15,7 +15,7 @@ shader_parameter/line_width = 0.0 shader_parameter/sin_frequency = 0.25 [sub_resource type="RectangleShape2D" id="RectangleShape2D_fffa1"] -size = Vector2(2, 12) +size = Vector2(1, 8) [sub_resource type="RectangleShape2D" id="RectangleShape2D_cbjww"] size = Vector2(6, 12) @@ -29,8 +29,8 @@ collision_layer = 32 script = ExtResource("1_xp22t") [node name="Trail" type="Line2D" parent="." node_paths=PackedStringArray("target")] -position = Vector2(0, -6) z_index = -1 +position = Vector2(0, -6) points = PackedVector2Array(0, 0) width = 3.0 width_curve = ExtResource("4_yvshk") @@ -65,12 +65,22 @@ shape = SubResource("CircleShape2D_jubww") debug_color = Color(0, 0.941176, 0.352941, 0.12549) [node name="LeftCast" type="RayCast2D" parent="."] -position = Vector2(0, -6) +enabled = false target_position = Vector2(-5, -29) +collide_with_areas = true +collide_with_bodies = false [node name="RightCast" type="RayCast2D" parent="."] -position = Vector2(0, -6) +enabled = false target_position = Vector2(5, -29) +collide_with_areas = true +collide_with_bodies = false + +[node name="TargetCast" type="RayCast2D" parent="."] +enabled = false +target_position = Vector2(0, -200) +collide_with_areas = true +collide_with_bodies = false [connection signal="body_entered" from="." to="." method="_on_body_entered"] [connection signal="area_entered" from="TargetFinder" to="." method="_on_target_finder_node_entered"] diff --git a/player/player.gd b/player/player.gd index de4ce55..ae75b9e 100644 --- a/player/player.gd +++ b/player/player.gd @@ -153,6 +153,7 @@ func hammer_highlight() -> void: unhighlight.call() elif not last_highlit in hamms: unhighlight.call() + hamms.sort_custom(func(a: Area2D, b: Area2D) -> bool: return a.global_position.distance_to(global_position) < b.global_position.distance_to(global_position)) last_highlit = hamms[0] hamms[0].highlight() |