-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_controls.gd
186 lines (143 loc) · 5.33 KB
/
player_controls.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
extends Node3D
@onready var gc = $GroundChecker
@onready var camera = $PhantomCamera3D
@onready var cam_height = camera.position.y
@onready var trajectory_visualizer : MeshInstance3D = $TrajectoryVisualizer
@onready var spawn_point: Marker3D = $SpawnPoint
@export var ball_scene: PackedScene
@onready var goal: Node3D = $"../HoleGoal"
@onready var anim = $Spatial/kiki1/AnimationPlayer
var ball_power: float
var is_aiming = false
var ready_to_aim = true
var moving_feet = false
#Club definition
var clubs = {
"Driver": { "up_vector": Vector3(0, 1.5, 0), "max_speed": 50.0 },
"Iron": { "up_vector": Vector3(0, 1.2, 0), "max_speed": 40.0 },
"Putter": { "up_vector": Vector3(0, 0, 0), "max_speed": 20.0 },
}
# Default selected club
var current_club = "Driver"
signal ball_shot
signal player_moved
signal club_changed
signal Anim_Hit
# cam_height is the initial height of the camera, as a variable in case a character needs to be filmed higher
# Called when the node enters the scene tree for the first time.
func _ready():
anim.play("Idle")
func switch_club(new_club_name):
if clubs.has(new_club_name):
current_club = new_club_name
club_changed.emit(current_club)
print("Switched to:", current_club)
else:
print("Club not found:", new_club_name)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
#draw_trajectory(Vector3(spawn_point.global_transform.basis.z + Vector3.UP * 0.5).normalized(), 30)
# Rotate the character and orient the camera
# First rotate the player node then angle and move the camera node (child of player)
if Input.is_action_pressed("left") and is_aiming == false:
self.rotation.y += 0.02
moving_feet = true
if Input.is_action_pressed("right") and is_aiming == false:
self.rotation.y -= 0.02
moving_feet = true
if Input.is_action_pressed("up") and is_aiming == false:
camera.rotation.x -= 0.0089
camera.position.y += 0.31
if Input.is_action_pressed("down") and is_aiming == false:
camera.rotation.x += 0.0089
camera.position.y -= 0.31
# Clamp all the rotation so that it doesn't loop around
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-30.0), deg_to_rad(0))
camera.position.y = clamp(camera.position.y, cam_height, 20)
# Check if cam is in default orientation before shooting
if camera.position.y != cam_height or camera.rotation.x != 0:
ready_to_aim = false
else:
ready_to_aim = true
func _input(event):
if event.is_action_pressed("switch_to_driver"):
switch_club("Driver")
elif event.is_action_pressed("switch_to_iron"):
switch_club("Iron")
elif event.is_action_pressed("switch_to_putter"):
switch_club("Putter")
elif event.is_action_pressed("left"):
anim.play("SideStep")
elif event.is_action_pressed("right"):
anim.play("SideStep")
if Input.is_action_just_released("left"):
anim.stop(false)
anim.play("Idle")
if Input.is_action_just_released("right"):
anim.stop(false)
anim.play("Idle")
if Input.is_action_pressed("action") and ready_to_aim == false:
camera.rotation.x = 0
camera.position.y = cam_height
func _on_ui_on_aiming():
is_aiming = true
ready_to_aim = false
func _on_ui_pangya(current_zone, shot_power):
anim.play("Shot")
ball_power = shot_power
print(current_zone)
print(ball_power)
func spawn_ball():
if ball_scene:
var ball = ball_scene.instantiate()
ball.transform.origin = spawn_point.global_transform.origin #get spawnpoint origin
# Get current Club properties
var club = clubs.get(current_club, null)
var up_vector = club["up_vector"]
var max_speed = club["max_speed"]
# Calculate launch direction
var forward_direction = spawn_point.global_transform.basis.z #get Forward axis
var upward_component = Vector3.UP # Add some upward motion
ball.launch_direction = (forward_direction + club.up_vector).normalized()
ball.launch_speed = (max_speed * ball_power) / 100 # Adjust speed as needed
#Set ball velocity
ball.linear_velocity = ball.launch_direction * ball.launch_speed
# Add Ball to the scene
get_tree().current_scene.add_child(ball)
print("Ball launched with", current_club, "at speed:", ball.launch_speed)
ball_shot.emit(ball)
ball.timeout.connect(_on_timeout)
#func draw_trajectory(launch_direction: Vector3, ball_power: float = 1000, gravity: float = -9.8): #default ( vector3, 100, -9.8
#if not trajectory_visualizer:
#return
## Calculate trajectory points
#var points = []
#var position = Vector3.ZERO
#var velocity = launch_direction.normalized() * ball_power
#var time_step = 0.2 # Smaller value = smoother curve
#
#for i in range(50): # Simulate for 50 steps
#position += velocity * time_step
#velocity.y += gravity * time_step
#points.append(position)
#if position.y < 0:
#break # Stop when the arc hits the ground
## Generate the trajectory mesh
#var surface_tool = SurfaceTool.new()
#surface_tool.begin(Mesh.PRIMITIVE_LINE_STRIP)
#for point in points:
#surface_tool.add_vertex(point)
## Finish the mesh and assign it to the visualizer
#var mesh = surface_tool.commit()
#trajectory_visualizer.mesh = mesh
func _on_timeout(ball_position):
print("TIMEOUT")
print(self.global_position - ball_position)
self.position = ball_position
player_moved.emit()
self.look_at(self.position * goal.position)
func _on_animation_player_animation_finished(Shot):
anim.play("Idle")
func _on_anim_hit():
spawn_ball()
$ShootingSound.playing = true