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
const SPEED = 200.0
const JUMP_VELOCITY = -400.0
const roll_speed = 300
var roll_cooldown_time = 0.5
@onready var sprite = $Sprites
var land_time = 0
var jumping = false
var roll_cooldown = 0
var roll_time = 0
var roll_dir = 1
var side = 1
var double_jump = false
var state = State.Idle
enum State {
Idle,
@ -16,7 +21,8 @@ enum State {
Walk,
Jump,
AirSpin,
Landing
Land,
Roll,
}
enum JumpState {
@ -39,7 +45,7 @@ func _play_anim(state: State, velocity: Vector2):
sprite.set_frame_and_progress(1, 0)
State.AirSpin:
pass
State.Landing:
State.Land:
sprite.play("Land")
func _process_velocity(state: State, delta):
@ -48,47 +54,65 @@ func _process_velocity(state: State, delta):
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
elif jumping:
jumping = false
elif state == State.Jump or state == State.AirSpin:
state = State.Land
sprite.play("Land")
double_jump = false
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():
velocity.y = JUMP_VELOCITY
sprite.play("Jump")
jumping = true
state = State.Jump
sprite.pause()
sprite.set_frame_and_progress(0, 0)
elif not double_jump:
elif state != State.AirSpin:
sprite.play("AirSpin")
double_jump = true
state = State.AirSpin
var boost = 0 if velocity.y > 0 else velocity.y / 3
velocity.y = JUMP_VELOCITY / 1.4 + boost
if jumping and not double_jump:
if state == State.Roll:
velocity.x = roll_dir * roll_speed
else:
var speed = SPEED
if Input.is_key_pressed(KEY_SHIFT):
speed /= 3
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * speed
else:
# Decelerate
velocity.x = move_toward(velocity.x, 0, 25)
if direction != 0:
sprite.flip_h = direction == -1
if land_time >= 0:
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)
var speed = SPEED
if Input.is_key_pressed(KEY_SHIFT):
speed /= 3
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * speed
else:
velocity.x = move_toward(velocity.x, 0, speed)
if direction != 0:
sprite.flip_h = direction == -1
if land_time >= 0:
land_time -= delta
# 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 Input.is_key_pressed(KEY_SHIFT):
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="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://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://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"]
@ -32,6 +33,34 @@ region = Rect2(192, 0, 48, 48)
atlas = ExtResource("2_gpxo5")
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"]
atlas = ExtResource("2_8h572")
region = Rect2(0, 0, 48, 48)
@ -187,6 +216,32 @@ animations = [{
}, {
"frames": [{
"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")
}, {
"duration": 1.0,
@ -317,8 +372,8 @@ script = ExtResource("1_23c20")
texture_filter = 1
position = Vector2(0, -16)
sprite_frames = SubResource("SpriteFrames_gvqqb")
animation = &"AirSpin"
frame = 5
animation = &"GroundRoll"
frame = 6
frame_progress = 1.0
[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)
]
}
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)
]
}