Cow_Life_Sim_RPG/MiniGames/RingToss/PowerBar.gd

137 lines
3.1 KiB
GDScript

extends ProgressBar
@export var Speed = 60
@export var root:Node
@export var ringnode:Node
@export var ringanim:Node
# nodes
var cross = null
var targ = null
var scoreboard = null
var throwboard = null
var animator = null
var score = 0
var throws = 0
var dir = 1
var Power = 1
var shot = null
var active = false
# Called when the node enters the scene tree for the first time.
func _ready():
self.visible = false
cross = root.find_child("Crosshair")
targ = root.find_child("Target")
animator = targ.find_child("Animate")
scoreboard = root.find_child("Score")
throwboard = root.find_child("Throws")
cross.position.y = 154 # 10% power
ringnode.visible = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var aim = ((100 - get_parent().Power) * (330 - (-330)) / 100) + (-330)
var pow = ((100 - self.Power) * (200 - (-260)) / 100) + (-260)
shot = Vector2(aim, pow)
cross.set_position(shot)
if not active:
return
Power += Speed * delta * dir
if Power >= 100:
dir = -1
elif Power <= 0:
dir = 1
self.value = Power
func _on_button_pressed():
active = false
var hit = HitCheck(shot)
score += hit
#animation based on hit
if hit == 20: animator.play("Center")
elif hit == 10:
animator.position.y += 3
if shot.x > targ.position.x:
if shot.y > targ.position.y:
animator.play("RightB")
else:
animator.play("RightT")
else:
if shot.y > targ.position.y:
animator.play("LeftB")
else:
animator.play("LeftT")
else:
ringnode.set_position(shot)
ringnode.visible = true
ringanim.play("Throw")
scoreboard.set_text("Score: " + str(score))
throws += 1
throwboard.set_text("Throws: " + str(throws) + "/10")
$Button.disabled = true
cross.visible = false
if throws == 10:
# all tosses used, do stuff here idk
%Result.Finish(score)
await get_tree().create_timer(3).timeout
get_node("/root/MainGame/CanvasLayer/MessageZone").visible = true
get_node("/root/MainGame/CanvasLayer/MenuBar").visible = true
get_node("/root/MainGame/CanvasLayer/MinimizeMessageZoneButton").visible = true
root.queue_free()
get_tree().paused = false
await get_tree().create_timer(1).timeout
get_parent().find_child("Button").disabled = false
get_parent().find_child("Button").visible = true
ringnode.visible = false
animator.play("default")
if hit == 10:
animator.position.y = 0
targ.new_pos()
self.Power = 10
self.visible = false
await get_tree().process_frame
# wait till next frame otherwise you get a weird frame where crosshair is visible at the target pos
get_parent().active = true
cross.visible = true
$Button.disabled = false
func HitCheck(shot):
var space_state = get_world_2d().direct_space_state
var query := PhysicsPointQueryParameters2D.new()
query.collide_with_areas = true
query.collide_with_bodies = false
query.position = shot
var result = space_state.intersect_point(query)
var hit = 0
for entry in result:
if entry.collider.is_in_group("Center") and hit < 20: hit = 20
if entry.collider.is_in_group("Outer") and hit == 0: hit = 10
return hit