💄 Player's orientation now follows its state (climb etc)

main
Ervan Lefevre 2 years ago
parent c88c71ff29
commit 3edebcef32
  1. 34
      nodes/player/player.gd
  2. 10
      nodes/player/player.tscn

@ -33,7 +33,8 @@ var can_grab_obstacles = {
}
@onready var grab_ray = get_node('grab_ray')
@onready var grab_ray = get_node('oriented_container/grab_ray')
@onready var sprite = get_node('oriented_container/sprite')
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@ -98,11 +99,11 @@ func jump_state(_delta):
$fsm.set_next_state("climb")
func on_enter_climb_state():
$sprite.modulate = Color(1, 0, 0)
sprite.modulate = Color(1, 0, 0)
velocity = Vector2(0, 0)
func on_exit_climb_state():
$sprite.modulate = Color(1, 1, 1)
sprite.modulate = Color(1, 1, 1)
func climb_state(_delta):
if is_on_wall():
@ -136,6 +137,8 @@ func after_state(delta):
if can_grab_obstacles[$fsm.current_state] == true:
handle_grab()
handle_sprite_orientation()
func handle_grab():
if Input.is_action_just_pressed('player_action'):
if grabbed_obstacle == null:
@ -152,12 +155,35 @@ func handle_grab():
func do_grab(obstacle):
grabbed_obstacle = obstacle
obstacle.grabbed(self)
obstacle.grabbed($oriented_container)
func drop_obstacle():
grabbed_obstacle.dropped()
grabbed_obstacle = null
func handle_sprite_orientation():
# rotates oriented_container to face
# the direction of the velocity
# if is_on_wall, rotated regarding velocity.y, with up by default
# if is_on_ceiling, rotated regarding velocity.x, with right by default
if is_on_wall_only():
if velocity.y != 0:
$oriented_container.rotation = PI/2 if (velocity.y > 0) else - PI / 2
var wall_position = get_slide_collision(0).get_position().x
var player_position = $oriented_container.get_global_position().x
var should_flip_with_wall_left = (wall_position > player_position) && (velocity.y > 0)
var should_flip_with_wall_right = (wall_position < player_position) && (velocity.y < 0)
sprite.flip_v = should_flip_with_wall_left || should_flip_with_wall_right
elif is_on_ceiling():
if velocity.x != 0:
$oriented_container.rotation = PI if (velocity.x > 0) else -PI
else:
if velocity.x != 0:
$oriented_container.rotation = 0 if (velocity.x > 0) else PI
sprite.flip_v = velocity.x < 0
func die():
$fsm.set_next_state("die")

@ -15,9 +15,6 @@ script = ExtResource("1_exx2i")
script = ExtResource("2_ix1o4")
root = NodePath("..")
[node name="sprite" type="Sprite2D" parent="."]
texture = ExtResource("1_s33dc")
[node name="shape" type="CollisionShape2D" parent="."]
rotation = 1.5708
shape = SubResource("CapsuleShape2D_w17ly")
@ -26,5 +23,10 @@ metadata/_edit_group_ = true
[node name="camera" type="Camera2D" parent="."]
zoom = Vector2(5, 5)
[node name="grab_ray" type="RayCast2D" parent="."]
[node name="oriented_container" type="Node2D" parent="."]
[node name="grab_ray" type="RayCast2D" parent="oriented_container"]
target_position = Vector2(24, 0)
[node name="sprite" type="Sprite2D" parent="oriented_container"]
texture = ExtResource("1_s33dc")

Loading…
Cancel
Save