a game about throwing hammers made for the github game off
Diffstat (limited to 'hammers/hammer.gd')
| -rw-r--r-- | hammers/hammer.gd | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/hammers/hammer.gd b/hammers/hammer.gd index 86db072..53110e3 100644 --- a/hammers/hammer.gd +++ b/hammers/hammer.gd @@ -4,6 +4,14 @@ class_name Hammer @icon("res://assets/hammers/hammer01.png") +@onready var left_cast := $LeftCast as RayCast2D +@onready var right_cast := $RightCast as RayCast2D +@onready var head := $Head as Marker2D +@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 + ## The current velocity var velocity := Vector2.ZERO @@ -22,19 +30,23 @@ var direction := Vector2.ZERO ## The hit enum. enum HITS {_a, _b, _c, PLAYER, ENEMY, NONE} +## These nodes want to have their collision mask set. +@onready var hitmasks = [target_finder, hitbox, left_cast, right_cast, self] + ## Pick which layers to hit. @export var hits: HITS = HITS.PLAYER: set(value): - set_collision_mask_value(hits, false) - target_finder.set_collision_mask_value(hits, false) + for node in hitmasks: + 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 if value == HITS.NONE: return - set_collision_mask_value(hits, true) - target_finder.set_collision_mask_value(hits, true) + for node in hitmasks: + node.set_collision_mask_value(hits, true) target_finder.monitoring = not is_instance_valid(target) - hitbox.collision_mask = target_finder.collision_mask ## The amount of time before gravity kicks in. @export var lifetime := 3.0 @@ -45,38 +57,34 @@ enum HITS {_a, _b, _c, PLAYER, ENEMY, NONE} ## The target var target: Node2D = null -@onready var head := $Head as Marker2D -@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 - - func _ready() -> void: hits = hits +## Lerps direction towards [param to]. +func dirlerp(to: Vector2) -> void: + direction = lerp(direction, to, steer_force * clampf(lifetime, 0, 1)) ## Moves the direction towards the target. func seek() -> void: if is_instance_valid(target): - direction = lerp( - direction, head.global_position.direction_to(target.global_position), steer_force * clampf(lifetime, 0, 1) - ) + dirlerp(head.global_position.direction_to(target.global_position)) elif target_finder.monitoring == false: target = null target_finder.monitoring = true -# func anticrash() -> void: -# var space := get_world_2d().direct_space_state -# var param := PhysicsRayQueryParameters2D.create(head.global_position, head.global_position + (direction * 10), pow(2, 1-1) + pow(2, hits - 1)) -# var result := space.intersect_ray(param) -# if result.is_empty() or result.collider is Player or result.collider is Enemy: -# return -# direction = lerp( -# direction, -# direction.bounce(head.global_position.direction_to(result.position)), -# steer_force * clampf(lifetime, 0, 1) -# ) +## Tries not to crash. +func anticrash() -> void: + var is_wall := func is_wall(ray: RayCast2D) -> bool: + if not ray.is_colliding(): return false + if target and ray.get_collider().get_class() == target.get_class(): return false + return true + + var results: Array[bool] = [is_wall.call(left_cast), is_wall.call(right_cast)] + if results.count(true) == 2: return # resign to our fate + + for i in range(2): + if results[i]: + dirlerp(direction.rotated(steer_force if i == 0 else -steer_force)) ## Highlights this hammer. See also [method unhighlight]. func highlight() -> void: @@ -94,7 +102,7 @@ func _physics_process(delta: float) -> void: velocity.y += grav * delta else: seek() -# anticrash() + anticrash() velocity += (direction * acceleration * delta) if velocity.y < 0: velocity.y = lerpf(velocity.y, 0, .1) # hard to move up @@ -111,6 +119,7 @@ func _on_body_entered(_body: Node2D) -> void: set_collision_layer_value(7, true) global_position += velocity.limit_length(1) # go into the wall a little velocity = Vector2.ZERO + hits = HITS.NONE target = null steer_force = 0.05 lifetime = 3 |