diff --git a/metroidvania/Scenes/Player/Player.gd b/metroidvania/Scenes/Player/Player.gd index 2908cfe..d6940f1 100644 --- a/metroidvania/Scenes/Player/Player.gd +++ b/metroidvania/Scenes/Player/Player.gd @@ -11,9 +11,17 @@ export (int) var max_speed = 64 export (float) var friction = 0.25 export (int) var gravity = 200 export (int) var jump_force = 128 +export (int) var wall_slide_speed = 48 +export (int) var max_wall_slide_speed = 128 export (int) var max_slope = 46 export (int) var bullet_speed = 250 +enum { + MOVE, + WALL_SLIDE +} + +var state = MOVE var invincible = false setget set_invincible var motion = Vector2.ZERO var snap_vector = Vector2.ZERO @@ -37,15 +45,33 @@ func _ready(): func _physics_process(delta): just_jumped = false - var input_vector = get_input_vector() - apply_horizontal_force(input_vector, delta) - apply_friction(input_vector) - update_snap_vector() - jump_check() - apply_gravity(delta) - update_animations(input_vector) - move() + + match state: + MOVE: + var input_vector = get_input_vector() + apply_horizontal_force(input_vector, delta) + apply_friction(input_vector) + update_snap_vector() + jump_check() + apply_gravity(delta) + update_animations(input_vector) + move() + + wall_slide_check() + WALL_SLIDE: + animation.play("WallSlide") + + var wall_axis = get_wall_axis() + if wall_axis != 0: + sprite.scale.x = wall_axis + + wall_slide_jump_check(wall_axis) + wall_slide_drop_check(delta) + wall_slide_fast_slide_check(delta) + move() + wall_detach_check(wall_axis) + if Input.is_action_pressed("fire") and fireBulletTimer.time_left == 0: fire_bullet() @@ -144,11 +170,45 @@ func move(): if is_on_floor() and get_floor_velocity().length() == 0 and abs(motion.x) < 1: position.x = last_position.x +func wall_slide_check(): + if !is_on_floor() and is_on_wall(): + state = WALL_SLIDE + double_jump = true + +func get_wall_axis(): + var is_right_wall = test_move(transform, Vector2.RIGHT) + var is_left_wall = test_move(transform, Vector2.LEFT) + return int(is_left_wall) - int(is_right_wall) + +func wall_slide_jump_check(wall_axis): + if Input.is_action_just_pressed("ui_select"): + motion.x = wall_axis * max_speed + motion.y = -jump_force/1.25 + state = MOVE + +func wall_slide_drop_check(delta): + if Input.is_action_just_pressed("ui_right"): + motion.x = acceleration * delta + state = MOVE + + if Input.is_action_just_pressed("ui_left"): + motion.x = -acceleration * delta + state = MOVE + +func wall_slide_fast_slide_check(delta): + var max_slide_speed = wall_slide_speed + if Input.is_action_just_pressed("ui_down"): + max_slide_speed = max_wall_slide_speed + motion.y = min(motion.y + gravity * delta, max_wall_slide_speed) + +func wall_detach_check(wall_axis): + if wall_axis == 0 or is_on_floor(): + state = MOVE + func _on_Hurtbox_hit(damage): if not invincible: PlayerStats.health -= damage blinkAnimator.play("Blink") - func _on_died(): queue_free() diff --git a/metroidvania/Scenes/Player/Player.tscn b/metroidvania/Scenes/Player/Player.tscn index edf6ea7..4576426 100644 --- a/metroidvania/Scenes/Player/Player.tscn +++ b/metroidvania/Scenes/Player/Player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://Assets/Player/Player.png" type="Texture" id=1] [ext_resource path="res://Scenes/Player/Player.gd" type="Script" id=2] @@ -74,6 +74,22 @@ tracks/1/keys = { } ] } +[sub_resource type="Animation" id=5] +resource_name = "WallSlide" +length = 0.1 +tracks/0/type = "value" +tracks/0/path = NodePath("Sprite:frame") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 1, +"values": [ 10 ] +} + [sub_resource type="Animation" id=4] resource_name = "Blink" length = 0.9 @@ -115,7 +131,7 @@ script = ExtResource( 2 ) position = Vector2( 0, -12 ) texture = ExtResource( 1 ) hframes = 12 -frame = 4 +frame = 11 [node name="PlayerGun" parent="Sprite" instance=ExtResource( 4 )] show_behind_parent = true @@ -129,6 +145,7 @@ shape = ExtResource( 6 ) anims/Idle = SubResource( 1 ) anims/Jump = SubResource( 2 ) anims/Run = SubResource( 3 ) +anims/WallSlide = SubResource( 5 ) [node name="BlinkAnimator" type="AnimationPlayer" parent="."] anims/Blink = SubResource( 4 )