1
Fork 0
mirror of https://github.com/Steffo99/nanogolf.git synced 2024-11-21 23:54:21 +00:00

Add golf ball and related scenes

This commit is contained in:
Steffo 2024-02-15 06:04:26 +01:00
parent 63731c18a3
commit 6a8ada8163
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
16 changed files with 502 additions and 3 deletions

1
.gitattributes vendored
View file

@ -1,2 +1,3 @@
# Normalize EOL for all files that Git considers text files. # Normalize EOL for all files that Git considers text files.
* text=auto eol=lf * text=auto eol=lf
*.wav filter=lfs diff=lfs merge=lfs -text

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
# Godot 4+ specific ignores # Godot 4+ specific ignores
.godot/ .godot/
.build/

6
docs/0 - Crediti.md Normal file
View file

@ -0,0 +1,6 @@
# Crediti
## Suoni
- 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

201
export_presets.cfg Normal file
View file

@ -0,0 +1,201 @@
[preset.0]
name="Android"
platform="Android"
runnable=true
dedicated_server=false
custom_features=""
export_filter="all_resources"
include_filter=""
exclude_filter=""
export_path=".build/Nanogolf.apk"
encryption_include_filters=""
encryption_exclude_filters=""
encrypt_pck=false
encrypt_directory=false
[preset.0.options]
custom_template/debug=""
custom_template/release=""
gradle_build/use_gradle_build=false
gradle_build/export_format=0
gradle_build/min_sdk=""
gradle_build/target_sdk=""
architectures/armeabi-v7a=false
architectures/arm64-v8a=true
architectures/x86=false
architectures/x86_64=false
version/code=1
version/name=""
package/unique_name="eu.steffo.nanogolf"
package/name="Nanogolf"
package/signed=true
package/app_category=2
package/retain_data_on_uninstall=false
package/exclude_from_recents=false
package/show_in_android_tv=false
package/show_in_app_library=true
package/show_as_launcher_app=false
launcher_icons/main_192x192=""
launcher_icons/adaptive_foreground_432x432=""
launcher_icons/adaptive_background_432x432=""
graphics/opengl_debug=false
xr_features/xr_mode=0
screen/immersive_mode=true
screen/support_small=true
screen/support_normal=true
screen/support_large=true
screen/support_xlarge=true
user_data_backup/allow=false
command_line/extra_args=""
apk_expansion/enable=false
apk_expansion/SALT=""
apk_expansion/public_key=""
permissions/custom_permissions=PackedStringArray()
permissions/access_checkin_properties=false
permissions/access_coarse_location=false
permissions/access_fine_location=false
permissions/access_location_extra_commands=false
permissions/access_mock_location=false
permissions/access_network_state=false
permissions/access_surface_flinger=false
permissions/access_wifi_state=false
permissions/account_manager=false
permissions/add_voicemail=false
permissions/authenticate_accounts=false
permissions/battery_stats=false
permissions/bind_accessibility_service=false
permissions/bind_appwidget=false
permissions/bind_device_admin=false
permissions/bind_input_method=false
permissions/bind_nfc_service=false
permissions/bind_notification_listener_service=false
permissions/bind_print_service=false
permissions/bind_remoteviews=false
permissions/bind_text_service=false
permissions/bind_vpn_service=false
permissions/bind_wallpaper=false
permissions/bluetooth=false
permissions/bluetooth_admin=false
permissions/bluetooth_privileged=false
permissions/brick=false
permissions/broadcast_package_removed=false
permissions/broadcast_sms=false
permissions/broadcast_sticky=false
permissions/broadcast_wap_push=false
permissions/call_phone=false
permissions/call_privileged=false
permissions/camera=false
permissions/capture_audio_output=false
permissions/capture_secure_video_output=false
permissions/capture_video_output=false
permissions/change_component_enabled_state=false
permissions/change_configuration=false
permissions/change_network_state=false
permissions/change_wifi_multicast_state=false
permissions/change_wifi_state=false
permissions/clear_app_cache=false
permissions/clear_app_user_data=false
permissions/control_location_updates=false
permissions/delete_cache_files=false
permissions/delete_packages=false
permissions/device_power=false
permissions/diagnostic=false
permissions/disable_keyguard=false
permissions/dump=false
permissions/expand_status_bar=false
permissions/factory_test=false
permissions/flashlight=false
permissions/force_back=false
permissions/get_accounts=false
permissions/get_package_size=false
permissions/get_tasks=false
permissions/get_top_activity_info=false
permissions/global_search=false
permissions/hardware_test=false
permissions/inject_events=false
permissions/install_location_provider=false
permissions/install_packages=false
permissions/install_shortcut=false
permissions/internal_system_window=false
permissions/internet=true
permissions/kill_background_processes=false
permissions/location_hardware=false
permissions/manage_accounts=false
permissions/manage_app_tokens=false
permissions/manage_documents=false
permissions/manage_external_storage=false
permissions/master_clear=false
permissions/media_content_control=false
permissions/modify_audio_settings=false
permissions/modify_phone_state=false
permissions/mount_format_filesystems=false
permissions/mount_unmount_filesystems=false
permissions/nfc=false
permissions/persistent_activity=false
permissions/process_outgoing_calls=false
permissions/read_calendar=false
permissions/read_call_log=false
permissions/read_contacts=false
permissions/read_external_storage=false
permissions/read_frame_buffer=false
permissions/read_history_bookmarks=false
permissions/read_input_state=false
permissions/read_logs=false
permissions/read_phone_state=false
permissions/read_profile=false
permissions/read_sms=false
permissions/read_social_stream=false
permissions/read_sync_settings=false
permissions/read_sync_stats=false
permissions/read_user_dictionary=false
permissions/reboot=false
permissions/receive_boot_completed=false
permissions/receive_mms=false
permissions/receive_sms=false
permissions/receive_wap_push=false
permissions/record_audio=false
permissions/reorder_tasks=false
permissions/restart_packages=false
permissions/send_respond_via_message=false
permissions/send_sms=false
permissions/set_activity_watcher=false
permissions/set_alarm=false
permissions/set_always_finish=false
permissions/set_animation_scale=false
permissions/set_debug_app=false
permissions/set_orientation=false
permissions/set_pointer_speed=false
permissions/set_preferred_applications=false
permissions/set_process_limit=false
permissions/set_time=false
permissions/set_time_zone=false
permissions/set_wallpaper=false
permissions/set_wallpaper_hints=false
permissions/signal_persistent_processes=false
permissions/status_bar=false
permissions/subscribed_feeds_read=false
permissions/subscribed_feeds_write=false
permissions/system_alert_window=false
permissions/transmit_ir=false
permissions/uninstall_shortcut=false
permissions/update_device_stats=false
permissions/use_credentials=false
permissions/use_sip=false
permissions/vibrate=false
permissions/wake_lock=false
permissions/write_apn_settings=false
permissions/write_calendar=false
permissions/write_call_log=false
permissions/write_contacts=false
permissions/write_external_storage=false
permissions/write_gservices=false
permissions/write_history_bookmarks=false
permissions/write_profile=false
permissions/write_secure_settings=false
permissions/write_settings=false
permissions/write_sms=false
permissions/write_social_stream=false
permissions/write_sync_settings=false
permissions/write_user_dictionary=false

View file

@ -1,3 +0,0 @@
[gd_scene format=3 uid="uid://b8f5t76nfdu5h"]
[node name="Main" type="Node"]

View file

@ -11,10 +11,12 @@ config_version=5
[application] [application]
config/name="Nanogolf" config/name="Nanogolf"
run/main_scene="res://scenes/main.tscn"
config/features=PackedStringArray("4.2", "Mobile") config/features=PackedStringArray("4.2", "Mobile")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[rendering] [rendering]
renderer/rendering_method="mobile" renderer/rendering_method="mobile"
textures/vram_compression/import_etc2_astc=true
environment/defaults/default_clear_color=Color(0, 0, 0, 1) environment/defaults/default_clear_color=Color(0, 0, 0, 1)

66
scenes/GolfBall.gd Normal file
View file

@ -0,0 +1,66 @@
extends CharacterBody2D
class_name GolfBall
## Dynamic friction subtracted from the body's velocity on each physics step.
@export var physics_friction: float
## The maximum number of bounces that the collision algorithm will consider in a single physics step.
@export var physics_max_bounces: float
## A multiplier applied to the body's velocity every time it collides with something.
@export var physics_bounce_coefficient: float
## The scene to instantiate to play the collision sound
@export var collision_sound: PackedScene
## Curve mapping relative putt power to putt sound volume.
@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
@onready var putt_controller: PuttController = $"PuttController"
func _on_putt(putt_vector: Vector2):
velocity += putt_vector
func do_movement(movement: Vector2):
# 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...
while movement.length() > 0.0 and bounces < physics_max_bounces:
# Try to move!
var collision: KinematicCollision2D = move_and_collide(movement)
# If the body did not collide and performed its full movement, we're done!
if not collision:
break
# Let's try to handle the collision properly
bounces += 1
# Determine the normal of the collision (the direction the body should be pushed back in)
var collision_normal = collision.get_normal()
# Play the collision sound
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
collision_sound_instance.volume_db = min(0, collision_volume_db.sample(collision_velocity / collision_volume_max_velocity))
# Add the sound to the SceneTree so it starts playing
$"..".add_child(collision_sound_instance)
# Set the sound's global position to the current global position of the body
collision_sound_instance.global_position = global_position
# Change the velocity adequately
velocity = velocity.bounce(collision_normal)
# Reflect the remaining movement
movement = collision.get_remainder().bounce(collision_normal)
# If we collided with something in this step, we need to apply the bounce coefficient
if bounces > 0:
velocity *= physics_bounce_coefficient
func _physics_process(delta):
do_movement(velocity * delta)
var new_velocity_length = max(0.0, velocity.length() - physics_friction)
velocity = velocity.normalized() * new_velocity_length
if new_velocity_length == 0.0:
putt_controller.can_putt = true

84
scenes/PuttController.gd Normal file
View file

@ -0,0 +1,84 @@
extends Node2D
class_name PuttController
## The maximum impulse that a putt can have.
@export var putt_max_impulse: float
## How many game units a pixel of screen mouse movement corresponds to.
@export var putt_drag_scale: float
## Curve mapping relative putt impulse to putt sound volume.
@export var putt_volume: Curve
## Emitted when a putt has happened.
signal putt(putt_vector: Vector2)
@onready var sprite: Sprite2D = $Sprite
@onready var sound: AudioStreamPlayer2D = $Sound
@onready var sprite_texture_width: float = sprite.texture.get_width()
var is_putting: bool = false
var putt_start_point: Vector2
var can_putt: bool = false:
get:
return can_putt
set(value):
can_putt = value
if not value:
is_putting = false
func _input(event: InputEvent):
if can_putt:
if event is InputEventMouseButton:
if event.pressed:
if not is_putting:
start_putt(event.position)
else:
push_warning("[PuttController] 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.")
if is_putting:
update_putt_ghost(compute_putt(event.position, putt_start_point))
if event is InputEventMouseMotion:
if is_putting:
update_putt_ghost(compute_putt(event.position, putt_start_point))
func update_putt_ghost(putt_vector: Vector2):
sprite.rotation = putt_vector.angle()
sprite.scale.x = putt_vector.length() / sprite_texture_width
sprite.position = position - putt_vector * 0.5
func compute_putt(start_position: Vector2, end_position: Vector2) -> Vector2:
var vector: Vector2 = -(end_position - start_position)
vector *= putt_drag_scale
if vector.length() > putt_max_impulse:
vector = vector.normalized() * putt_max_impulse
return vector
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)
func play_putt_sound(putt_vector: Vector2):
var putt_impulse: float = putt_vector.length()
sound.volume_db = putt_volume.sample(putt_impulse / putt_max_impulse)
sound.play()

File diff suppressed because one or more lines are too long

37
scenes/golf_ball.tscn Normal file
View file

@ -0,0 +1,37 @@
[gd_scene load_steps=7 format=3 uid="uid://ca06elq8io5wu"]
[ext_resource type="Script" path="res://scenes/GolfBall.gd" id="1_cnerg"]
[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"]
[sub_resource type="Curve" id="Curve_v81ms"]
min_value = -20.0
max_value = 0.0
_data = [Vector2(0, -20), 0.0, 20.0, 0, 1, Vector2(1, 0), 20.0, 0.0, 1, 0]
point_count = 2
[sub_resource type="CircleShape2D" id="CircleShape2D_3vk3q"]
radius = 8.0
[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
physics_max_bounces = 4.0
physics_bounce_coefficient = 0.4
collision_sound = ExtResource("4_6m2yp")
collision_volume_db = SubResource("Curve_v81ms")
collision_volume_max_velocity = 150.0
[node name="Sprite" type="Sprite2D" parent="."]
texture = ExtResource("2_mqlag")
[node name="Shape" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_3vk3q")
[node name="PuttController" parent="." instance=ExtResource("3_rx2v7")]
[connection signal="putt" from="PuttController" to="." method="_on_putt"]

15
scenes/main.tscn Normal file
View file

@ -0,0 +1,15 @@
[gd_scene load_steps=3 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"]
[node name="Main" type="Node"]
[node name="TileMap" type="TileMap" parent="."]
tile_set = ExtResource("1_rm16w")
format = 2
layer_0/tile_data = PackedInt32Array(131074, 2, 0, 196610, 2, 0, 262146, 2, 0, 327682, 2, 0, 393218, 2, 0, 458754, 2, 0, 524290, 2, 0, 589826, 2, 0, 655362, 2, 0, 720898, 2, 0, 786434, 2, 0, 851970, 2, 0, 917506, 2, 0, 983042, 2, 0, 1048578, 2, 0, 1114114, 2, 0, 1179650, 2, 0, 1245186, 2, 0, 1310722, 2, 0, 1376258, 2, 0, 1441794, 2, 0, 1507330, 2, 0, 1572866, 2, 0, 1638402, 2, 0, 1703938, 2, 0, 1769474, 2, 0, 1769475, 2, 0, 1769476, 2, 0, 1769477, 2, 0, 1769478, 2, 0, 1769479, 2, 0, 1769480, 2, 0, 1769481, 2, 0, 1769482, 2, 0, 1769483, 2, 0, 1769484, 2, 0, 1769485, 2, 0, 1769486, 2, 0, 1769487, 2, 0, 1703951, 2, 0, 1638415, 2, 0, 1572879, 2, 0, 1507343, 2, 0, 1441807, 2, 0, 1376271, 2, 0, 1310735, 2, 0, 1245199, 2, 0, 1179663, 2, 0, 1114127, 2, 0, 1048591, 2, 0, 983055, 2, 0, 917519, 2, 0, 851983, 2, 0, 786447, 2, 0, 720911, 2, 0, 655375, 2, 0, 589839, 2, 0, 524303, 2, 0, 131081, 2, 0, 131080, 2, 0, 131079, 2, 0, 131078, 2, 0, 131077, 2, 0, 131076, 2, 0, 131075, 2, 0, 196618, 1, 1342177280, 262155, 1, 1342177280, 327692, 1, 1342177280, 393229, 1, 1342177280, 458766, 1, 1342177280, 458767, 1, 1610612736, 393230, 1, 1610612736, 327693, 1, 1610612736, 262156, 1, 1610612736, 196619, 1, 1610612736, 131082, 1, 1610612736)
[node name="GolfBall" parent="." instance=ExtResource("1_5j6ud")]
position = Vector2(69, 81)
physics_bounce_coefficient = 0.6

View file

@ -0,0 +1,25 @@
[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="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"]
[sub_resource type="Curve" id="Curve_pq63d"]
min_value = -16.0
max_value = 0.0
_data = [Vector2(0, -16), 0.0, 16.0, 0, 1, Vector2(1, 0), 16.0, 0.0, 1, 0]
point_count = 2
[node name="PuttController" type="Node2D"]
visible = false
modulate = Color(1, 1, 1, 0.25098)
script = ExtResource("1_ldg2x")
putt_max_impulse = 300.0
putt_drag_scale = 1.0
putt_volume = SubResource("Curve_pq63d")
[node name="Sprite" type="Sprite2D" parent="."]
texture = ExtResource("2_eblkx")
[node name="Sound" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("3_0eren")

3
sounds/bonk.wav Normal file
View file

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

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

@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://hdic55cix3xx"
path="res://.godot/imported/bonk.wav-3ec20bb198e37143595fed23d3ff5d79.sample"
[deps]
source_file="res://sounds/bonk.wav"
dest_files=["res://.godot/imported/bonk.wav-3ec20bb198e37143595fed23d3ff5d79.sample"]
[params]
force/8_bit=false
force/mono=false
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

3
sounds/putt.wav Normal file
View file

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:382ca28daf23856c5a92a3557d34092940ccc5a8e6e0e9ff3266ad91a09da9c9
size 148680

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

@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://clm7wapsnjjlp"
path="res://.godot/imported/putt.wav-1940451c4ca6b080b45b073564e36160.sample"
[deps]
source_file="res://sounds/putt.wav"
dest_files=["res://.godot/imported/putt.wav-1940451c4ca6b080b45b073564e36160.sample"]
[params]
force/8_bit=false
force/mono=false
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