1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-21 15:44:21 +00:00
This commit is contained in:
Steffo 2024-02-16 04:26:01 +01:00
parent 2f1ef9a4d6
commit bf0578ddcc
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
14 changed files with 144 additions and 22 deletions

View file

@ -4,3 +4,4 @@
- S: putt.wav by 170129 -- https://freesound.org/s/408260/ -- License: Creative Commons 0
- S: bonk.wav by el_boss -- https://freesound.org/s/628636/ -- License: Creative Commons 0
- S: hole.wav by inbeeld -- https://freesound.org/s/21878/ -- License: Creative Commons 0

View file

@ -14,15 +14,27 @@ class_name GolfBall
@export var collision_volume_db: Curve
## The velocity at which the maximum volume of [member collision_volume_db] is played.
@export var collision_volume_max_velocity: float
## Emitted when the ball enters the hole.
signal entered_hole(strokes: int)
## How many strokes have been performed so far.
var strokes: int = 0
## Whether the ball has entered the hole.
var in_hole: bool = false
@onready var putt_controller: PuttController = $"PuttController"
@onready var hole_controller: HoleController = $"HoleController"
@onready var hole_sound: AudioStreamPlayer2D = $"HoleSound"
func _on_putt(putt_vector: Vector2):
strokes += 1
velocity += putt_vector
func do_movement(movement: Vector2):
func do_movement(delta: float):
# How much the body should move in this physics step.
var movement = velocity * delta
# How many times the body collided in the current physics step.
var bounces: int = 0
# While the body should still move some space, and it isn't stuck in a perpetual loop of bouncing...
@ -40,7 +52,6 @@ func do_movement(movement: Vector2):
if bounces == 1:
# Determine with how much speed the body collided
var collision_velocity = -collision_normal.dot(velocity)
print(collision_velocity)
# Create a new sound instance
var collision_sound_instance: AudioStreamPlayer2D = collision_sound.instantiate()
# Set the sound volume based on the relative collision velocity
@ -58,9 +69,32 @@ func do_movement(movement: Vector2):
velocity *= physics_bounce_coefficient
func _physics_process(delta):
do_movement(velocity * delta)
var new_velocity_length = max(0.0, velocity.length() - physics_friction)
func apply_friction(delta):
var new_velocity_length = max(0.0, velocity.length() - physics_friction * delta)
velocity = velocity.normalized() * new_velocity_length
if new_velocity_length == 0.0:
func check_has_stopped() -> bool:
return velocity.length() == 0.0
func check_has_entered_hole() -> bool:
return check_has_stopped() and hole_controller.over_hole
func enter_hole():
in_hole = true
visible = false
hole_sound.play()
entered_hole.emit(strokes)
print("[GolfBall] Entered hole in: ", strokes)
func _physics_process(delta):
if not in_hole:
do_movement(delta)
apply_friction(delta)
if check_has_entered_hole():
enter_hole()
if check_has_stopped():
putt_controller.can_putt = true

View file

@ -1,9 +1,11 @@
[gd_scene load_steps=7 format=3 uid="uid://ca06elq8io5wu"]
[gd_scene load_steps=10 format=3 uid="uid://ca06elq8io5wu"]
[ext_resource type="Script" path="res://scenes/GolfBall.gd" id="1_cnerg"]
[ext_resource type="Script" path="res://scenes/golf_ball.gd" id="1_1uswk"]
[ext_resource type="Texture2D" uid="uid://bxyy3bwt7a5l2" path="res://sprites/circle.svg" id="2_mqlag"]
[ext_resource type="PackedScene" uid="uid://cu03w8wxnkffb" path="res://scenes/putt_controller.tscn" id="3_rx2v7"]
[ext_resource type="PackedScene" uid="uid://cj15ash4r3i4v" path="res://scenes/collision_sound.tscn" id="4_6m2yp"]
[ext_resource type="PackedScene" uid="uid://bf0a04t4b1wil" path="res://scenes/hole_controller.tscn" id="5_tdklt"]
[ext_resource type="AudioStream" uid="uid://crfybmbv6dbs4" path="res://sounds/hole.wav" id="6_08dpq"]
[sub_resource type="Curve" id="Curve_v81ms"]
min_value = -20.0
@ -14,12 +16,14 @@ point_count = 2
[sub_resource type="CircleShape2D" id="CircleShape2D_3vk3q"]
radius = 8.0
[sub_resource type="CircleShape2D" id="CircleShape2D_aigrf"]
radius = 1.41421
[node name="GolfBall" type="CharacterBody2D"]
position = Vector2(146, 142)
motion_mode = 1
wall_min_slide_angle = 0.0
script = ExtResource("1_cnerg")
physics_friction = 1.0
script = ExtResource("1_1uswk")
physics_friction = 60.0
physics_max_bounces = 4.0
physics_bounce_coefficient = 0.65
collision_sound = ExtResource("4_6m2yp")
@ -31,7 +35,17 @@ texture = ExtResource("2_mqlag")
[node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_3vk3q")
debug_color = Color(0, 0.6, 0.701961, 0.419608)
[node name="PuttController" parent="." instance=ExtResource("3_rx2v7")]
[node name="HoleController" parent="." instance=ExtResource("5_tdklt")]
[node name="Shape" type="CollisionShape2D" parent="HoleController"]
shape = SubResource("CircleShape2D_aigrf")
debug_color = Color(0, 0.701961, 0.141176, 0.419608)
[node name="HoleSound" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("6_08dpq")
[connection signal="putt" from="PuttController" to="." method="_on_putt"]

2
scenes/golf_hole.gd Normal file
View file

@ -0,0 +1,2 @@
extends Area2D
class_name GolfHole

19
scenes/golf_hole.tscn Normal file
View file

@ -0,0 +1,19 @@
[gd_scene load_steps=4 format=3 uid="uid://df7t70153a3bb"]
[ext_resource type="Script" path="res://scenes/golf_hole.gd" id="1_7ng0k"]
[ext_resource type="Texture2D" uid="uid://bxyy3bwt7a5l2" path="res://sprites/circle.svg" id="1_dw1g4"]
[sub_resource type="CircleShape2D" id="CircleShape2D_c8ayt"]
radius = 8.0
[node name="GolfHole" type="Area2D"]
scale = Vector2(1.05, 1.05)
script = ExtResource("1_7ng0k")
[node name="Circle" type="Sprite2D" parent="."]
modulate = Color(1, 1, 1, 0.25098)
texture = ExtResource("1_dw1g4")
[node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_c8ayt")
debug_color = Color(0, 0.701961, 0.141176, 0.419608)

15
scenes/hole_controller.gd Normal file
View file

@ -0,0 +1,15 @@
extends Area2D
class_name HoleController
var over_hole: bool = false
func _on_area_entered(area: Area2D):
if area is GolfHole:
over_hole = true
func _on_area_exited(area: Area2D):
if area is GolfHole:
over_hole = false

View file

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://bf0a04t4b1wil"]
[ext_resource type="Script" path="res://scenes/hole_controller.gd" id="1_df7lg"]
[node name="HoleController" type="Area2D"]
script = ExtResource("1_df7lg")
[connection signal="area_entered" from="." to="." method="_on_area_entered"]
[connection signal="area_exited" from="." to="." method="_on_area_exited"]

View file

@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://b8f5t76nfdu5h"]
[gd_scene load_steps=4 format=3 uid="uid://b8f5t76nfdu5h"]
[ext_resource type="PackedScene" uid="uid://ca06elq8io5wu" path="res://scenes/golf_ball.tscn" id="1_5j6ud"]
[ext_resource type="TileSet" uid="uid://17esn8g8xqik" path="res://tiles/hole_tile_set.tres" id="1_rm16w"]
[ext_resource type="PackedScene" uid="uid://df7t70153a3bb" path="res://scenes/golf_hole.tscn" id="3_dn06g"]
[node name="Main" type="Node"]
@ -13,3 +14,6 @@ metadata/_edit_lock_ = true
[node name="GolfBall" parent="." instance=ExtResource("1_5j6ud")]
position = Vector2(340, 229)
[node name="GolfHole" parent="." instance=ExtResource("3_dn06g")]
position = Vector2(544, 333)

View file

@ -35,12 +35,12 @@ func _input(event: InputEvent):
if not is_putting:
start_putt(event.position)
else:
push_warning("[PuttController] Attempted to start putt while another was in progress.")
push_warning("Attempted to start putt while another was in progress.")
else:
if is_putting:
end_putt(event.position)
else:
push_warning("[PuttController] Attempted to end putt while none was in progress.")
push_warning("Attempted to end putt while none was in progress.")
if is_putting:
update_putt_ghost(compute_putt(event.position, putt_start_point))
if event is InputEventMouseMotion:
@ -63,19 +63,16 @@ func compute_putt(start_position: Vector2, end_position: Vector2) -> Vector2:
func start_putt(start_position: Vector2):
print("[PuttController] Starting putt at: ", start_position)
visible = true
is_putting = true
putt_start_point = start_position
func end_putt(end_position: Vector2):
print("[PuttController] Ending putt at: ", end_position)
visible = false
is_putting = false
can_putt = false
var putt_vector = compute_putt(putt_start_point, end_position)
print("[PuttController] Putt is: ", putt_vector)
putt.emit(putt_vector)
play_putt_sound(putt_vector)

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://cu03w8wxnkffb"]
[ext_resource type="Script" path="res://scenes/PuttController.gd" id="1_ldg2x"]
[ext_resource type="Script" path="res://scenes/putt_controller.gd" id="1_rb6v1"]
[ext_resource type="Texture2D" uid="uid://dvr4j6164rphg" path="res://sprites/square.svg" id="2_eblkx"]
[ext_resource type="AudioStream" uid="uid://clm7wapsnjjlp" path="res://sounds/putt.wav" id="3_0eren"]
@ -13,7 +13,7 @@ point_count = 2
[node name="PuttController" type="Node2D"]
visible = false
modulate = Color(1, 1, 1, 0.25098)
script = ExtResource("1_ldg2x")
script = ExtResource("1_rb6v1")
putt_max_impulse = 333.0
putt_drag_scale = 1.0
putt_ghost_scale = 0.3

View file

@ -13,7 +13,7 @@ dest_files=["res://.godot/imported/bonk.wav-3ec20bb198e37143595fed23d3ff5d79.sam
[params]
force/8_bit=false
force/mono=false
force/mono=true
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false

3
sounds/hole.wav Normal file
View file

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:66eec7449d8d70ff4dbb1d06099792340d13bdbaecf7ed831dc3fd5d0614dee0
size 147770

24
sounds/hole.wav.import Normal file
View file

@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://crfybmbv6dbs4"
path="res://.godot/imported/hole.wav-dd9300b81c964cd47886e68b30fe1f15.sample"
[deps]
source_file="res://sounds/hole.wav"
dest_files=["res://.godot/imported/hole.wav-dd9300b81c964cd47886e68b30fe1f15.sample"]
[params]
force/8_bit=false
force/mono=true
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=0

View file

@ -13,7 +13,7 @@ dest_files=["res://.godot/imported/putt.wav-1940451c4ca6b080b45b073564e36160.sam
[params]
force/8_bit=false
force/mono=false
force/mono=true
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false