Added ground rolling functionality by pressing r

This commit is contained in:
Joseph Ferano 2023-10-03 23:27:23 +07:00
parent 9f84017a62
commit 7e035e70c0
3 changed files with 114 additions and 30 deletions

View File

@ -3,12 +3,17 @@ extends CharacterBody2D
var gravity = 800 var gravity = 800
const SPEED = 200.0 const SPEED = 200.0
const JUMP_VELOCITY = -400.0 const JUMP_VELOCITY = -400.0
const roll_speed = 300
var roll_cooldown_time = 0.5
@onready var sprite = $Sprites @onready var sprite = $Sprites
var land_time = 0 var land_time = 0
var jumping = false var roll_cooldown = 0
var roll_time = 0
var roll_dir = 1
var side = 1 var side = 1
var double_jump = false var state = State.Idle
enum State { enum State {
Idle, Idle,
@ -16,7 +21,8 @@ enum State {
Walk, Walk,
Jump, Jump,
AirSpin, AirSpin,
Landing Land,
Roll,
} }
enum JumpState { enum JumpState {
@ -39,7 +45,7 @@ func _play_anim(state: State, velocity: Vector2):
sprite.set_frame_and_progress(1, 0) sprite.set_frame_and_progress(1, 0)
State.AirSpin: State.AirSpin:
pass pass
State.Landing: State.Land:
sprite.play("Land") sprite.play("Land")
func _process_velocity(state: State, delta): func _process_velocity(state: State, delta):
@ -48,31 +54,34 @@ func _process_velocity(state: State, delta):
func _physics_process(delta): func _physics_process(delta):
if not is_on_floor(): if not is_on_floor():
velocity.y += gravity * delta velocity.y += gravity * delta
elif jumping: elif state == State.Jump or state == State.AirSpin:
jumping = false state = State.Land
sprite.play("Land") sprite.play("Land")
double_jump = false
land_time = 0.16 land_time = 0.16
return
if Input.is_action_just_pressed("ui_accept"): if Input.is_action_just_pressed("roll"):
if is_on_floor() and roll_time <= 0 and roll_cooldown <= 0:
state = State.Roll
roll_time = 0.55
roll_dir = -1 if sprite.flip_h else 1
sprite.play("GroundRoll")
if Input.is_action_just_pressed("ui_accept") and state != State.Roll:
if is_on_floor(): if is_on_floor():
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
sprite.play("Jump") sprite.play("Jump")
jumping = true state = State.Jump
sprite.pause() sprite.pause()
sprite.set_frame_and_progress(0, 0) sprite.set_frame_and_progress(0, 0)
elif not double_jump: elif state != State.AirSpin:
sprite.play("AirSpin") sprite.play("AirSpin")
double_jump = true state = State.AirSpin
var boost = 0 if velocity.y > 0 else velocity.y / 3 var boost = 0 if velocity.y > 0 else velocity.y / 3
velocity.y = JUMP_VELOCITY / 1.4 + boost velocity.y = JUMP_VELOCITY / 1.4 + boost
if jumping and not double_jump: if state == State.Roll:
if velocity.y > 50: velocity.x = roll_dir * roll_speed
sprite.set_frame_and_progress(2,0) else:
elif velocity.y > -150:
sprite.set_frame_and_progress(1, 0)
var speed = SPEED var speed = SPEED
if Input.is_key_pressed(KEY_SHIFT): if Input.is_key_pressed(KEY_SHIFT):
speed /= 3 speed /= 3
@ -80,15 +89,30 @@ func _physics_process(delta):
if direction: if direction:
velocity.x = direction * speed velocity.x = direction * speed
else: else:
velocity.x = move_toward(velocity.x, 0, speed) # Decelerate
velocity.x = move_toward(velocity.x, 0, 25)
if direction != 0: if direction != 0:
sprite.flip_h = direction == -1 sprite.flip_h = direction == -1
if land_time >= 0: if land_time >= 0:
land_time -= delta land_time -= delta
if roll_cooldown >= 0:
roll_cooldown -= delta
if roll_time >= 0:
roll_time -= delta
elif state == State.Roll:
roll_cooldown = roll_cooldown_time
state = State.Idle
if state == State.Jump:
if velocity.y > 50:
sprite.set_frame_and_progress(2,0)
elif velocity.y > -150:
sprite.set_frame_and_progress(1, 0)
# If shift left/right then 2 * speed, play run # If shift left/right then 2 * speed, play run
if land_time <= 0 and not jumping: if land_time <= 0 and is_on_floor() and state != State.Jump and state != State.Roll:
if velocity.x != 0: if velocity.x != 0:
if Input.is_key_pressed(KEY_SHIFT): if Input.is_key_pressed(KEY_SHIFT):
sprite.play("Walk") sprite.play("Walk")

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=48 format=3 uid="uid://bjc6dwxaakqdg"] [gd_scene load_steps=56 format=3 uid="uid://bjc6dwxaakqdg"]
[ext_resource type="Script" path="res://Code/Character.gd" id="1_23c20"] [ext_resource type="Script" path="res://Code/Character.gd" id="1_23c20"]
[ext_resource type="Texture2D" uid="uid://wfwjlevruveu" path="res://Import/Sprites/Character Idle 48x48.png" id="2_8h572"] [ext_resource type="Texture2D" uid="uid://wfwjlevruveu" path="res://Import/Sprites/Character Idle 48x48.png" id="2_8h572"]
[ext_resource type="Texture2D" uid="uid://c87b08n23v0kx" path="res://Import/Sprites/player air spin 48x48.png" id="2_gpxo5"] [ext_resource type="Texture2D" uid="uid://c87b08n23v0kx" path="res://Import/Sprites/player air spin 48x48.png" id="2_gpxo5"]
[ext_resource type="Texture2D" uid="uid://clx88ivnf6id6" path="res://Import/Sprites/Player Roll 48x48.png" id="3_4dpty"]
[ext_resource type="Texture2D" uid="uid://bge22c82umdui" path="res://Import/Sprites/PlayerWalk 48x48.png" id="3_xxdqr"] [ext_resource type="Texture2D" uid="uid://bge22c82umdui" path="res://Import/Sprites/PlayerWalk 48x48.png" id="3_xxdqr"]
[ext_resource type="Texture2D" uid="uid://y8syonuki304" path="res://Import/Sprites/run cycle 48x48.png" id="4_rqcgh"] [ext_resource type="Texture2D" uid="uid://y8syonuki304" path="res://Import/Sprites/run cycle 48x48.png" id="4_rqcgh"]
[ext_resource type="Texture2D" uid="uid://8ikuchoh7pyd" path="res://Import/Sprites/player jump 48x48.png" id="5_f2sni"] [ext_resource type="Texture2D" uid="uid://8ikuchoh7pyd" path="res://Import/Sprites/player jump 48x48.png" id="5_f2sni"]
@ -32,6 +33,34 @@ region = Rect2(192, 0, 48, 48)
atlas = ExtResource("2_gpxo5") atlas = ExtResource("2_gpxo5")
region = Rect2(240, 0, 48, 48) region = Rect2(240, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_7tnm4"]
atlas = ExtResource("3_4dpty")
region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_xvlia"]
atlas = ExtResource("3_4dpty")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_uk52w"]
atlas = ExtResource("3_4dpty")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_bnei3"]
atlas = ExtResource("3_4dpty")
region = Rect2(144, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_7iiee"]
atlas = ExtResource("3_4dpty")
region = Rect2(192, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_wuw88"]
atlas = ExtResource("3_4dpty")
region = Rect2(240, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_wh1kj"]
atlas = ExtResource("3_4dpty")
region = Rect2(288, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_t3ih3"] [sub_resource type="AtlasTexture" id="AtlasTexture_t3ih3"]
atlas = ExtResource("2_8h572") atlas = ExtResource("2_8h572")
region = Rect2(0, 0, 48, 48) region = Rect2(0, 0, 48, 48)
@ -187,6 +216,32 @@ animations = [{
}, { }, {
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_7tnm4")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_xvlia")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_uk52w")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_bnei3")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_7iiee")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_wuw88")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_wh1kj")
}],
"loop": false,
"name": &"GroundRoll",
"speed": 12.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_t3ih3") "texture": SubResource("AtlasTexture_t3ih3")
}, { }, {
"duration": 1.0, "duration": 1.0,
@ -317,8 +372,8 @@ script = ExtResource("1_23c20")
texture_filter = 1 texture_filter = 1
position = Vector2(0, -16) position = Vector2(0, -16)
sprite_frames = SubResource("SpriteFrames_gvqqb") sprite_frames = SubResource("SpriteFrames_gvqqb")
animation = &"AirSpin" animation = &"GroundRoll"
frame = 5 frame = 6
frame_progress = 1.0 frame_progress = 1.0
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]

View File

@ -49,3 +49,8 @@ down={
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
] ]
} }
roll={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null)
]
}