tenchu/Godot/Code/Character.gd

151 lines
3.4 KiB
GDScript

extends CharacterBody2D
var gravity = 900
var gravity_mid = 500
var gravity_fall = 1300
const SPEED = 200.0
const JUMP_VELOCITY = -400.0
const roll_speed = 300
var roll_cooldown_time = 0.5
var colshape_stand = 30
@onready var sprite = $Sprites
var land_time = 0
var roll_cooldown = 0
var roll_time = 0
var roll_dir = 1
var side = 1
var state = State.Idle
enum State {
Idle,
Run,
Walk,
Jump,
AirSpin,
Land,
Attack,
Roll,
}
func _physics_process(delta):
if Input.is_action_just_pressed("attack"):
if is_on_floor():
state = State.Attack
velocity = Vector2.ZERO
sprite.play("Attack1")
if state == State.Attack:
await (sprite as AnimatedSprite2D).animation_finished
state = State.Idle
else:
if not is_on_floor():
if velocity.y > 50:
velocity.y += gravity_fall * delta
elif velocity.y < -50:
velocity.y += gravity * delta
else:
velocity.y += gravity_mid * delta
elif state == State.Jump or state == State.AirSpin:
state = State.Land
sprite.play("Land")
land_time = 0.16
return
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
$CollisionShape2D.shape.size.y = colshape_stand / 2
$CollisionShape2D.position.y = colshape_stand / -4
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")
state = State.Jump
sprite.pause()
sprite.set_frame_and_progress(0, 0)
elif state != State.AirSpin:
sprite.play("AirSpin")
state = State.AirSpin
var boost = 0 if velocity.y > 0 else velocity.y / 3
velocity.y = JUMP_VELOCITY / 1.4 + boost
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:
$CollisionShape2D.shape.size.y = colshape_stand
$CollisionShape2D.position.y = colshape_stand / -2
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 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")
else:
sprite.play("Run")
else:
sprite.play("Idle")
move_and_slide()
# Maybe later
enum JumpState {
}
func _play_anim(state: State, velocity: Vector2):
match state:
State.Idle:
sprite.play("Idle")
State.Run:
sprite.play("Run")
State.Walk:
sprite.play("Walk")
State.Jump:
sprite.pause()
sprite.set_frame_and_progress(0, 0)
if velocity.y > 50:
sprite.set_frame_and_progress(2,0)
elif velocity.y > -150:
sprite.set_frame_and_progress(1, 0)
State.AirSpin:
pass
State.Land:
sprite.play("Land")
func _process_velocity(state: State, delta):
pass