99 lines
2.5 KiB
GDScript3
99 lines
2.5 KiB
GDScript3
extends KinematicBody2D
|
|
|
|
enum STATE {IDLE, RUN, JUMP, FALL, HURT}
|
|
enum STATE_SECONDARY {NONE, SHOOT}
|
|
|
|
class StateMachine:
|
|
var primary = STATE.IDLE
|
|
var secondary = STATE_SECONDARY.NONE
|
|
|
|
onready var player_state = StateMachine.new()
|
|
onready var player_last_state = StateMachine.new()
|
|
|
|
# Declare member variables here. Examples:
|
|
# var a = 2
|
|
# var b = "text"
|
|
onready var gravity = 60*9.8
|
|
|
|
onready var speed = Vector2((30*5), (gravity * 1/2))
|
|
#onready var gravity = ProjectSettings.get("physics/2d/default_gravity")
|
|
|
|
onready var _animated_sprite = $AnimatedSprite
|
|
|
|
var velocity = Vector2.ZERO
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready():
|
|
pass # Replace with function body.
|
|
|
|
|
|
func get_direction():
|
|
return Vector2(
|
|
Input.get_axis("ui_left", "ui_right"),
|
|
-1 if is_on_floor() and Input.is_action_just_pressed("ui_accept") else 0
|
|
)
|
|
|
|
func calculate_move_velocity(
|
|
linear_velocity,
|
|
direction,
|
|
speed
|
|
):
|
|
var velocity = linear_velocity
|
|
velocity.x = speed.x * direction.x
|
|
if direction.y != 0.0:
|
|
velocity.y = speed.y * direction.y
|
|
return velocity
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _physics_process(delta):
|
|
var direction = get_direction()
|
|
# if not is_on_floor():
|
|
# velocity.y += gravity * delta
|
|
#
|
|
# # Handle jump.
|
|
# if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
# velocity.y += (gravity * -1) * delta
|
|
#
|
|
# # Get the input direction and handle the movement/deceleration.
|
|
# # As good practice, you should replace UI actions with custom gameplay actions.
|
|
# var direction = Input.get_axis("ui_left", "ui_right")
|
|
# if direction:
|
|
# velocity.x = direction * SPEED
|
|
# else:
|
|
# velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
velocity = calculate_move_velocity(velocity, direction, speed)
|
|
move_and_slide(velocity, Vector2(0, -1))
|
|
# Apply Gravity
|
|
velocity.y += gravity * delta
|
|
|
|
#Animation
|
|
|
|
if direction.x > 0 and _animated_sprite.flip_h != true:
|
|
_animated_sprite.flip_h = true
|
|
if direction.x < 0 and _animated_sprite.flip_h != false:
|
|
_animated_sprite.flip_h = false
|
|
|
|
|
|
player_state.primary = STATE.IDLE
|
|
|
|
match player_state.primary:
|
|
STATE.IDLE:
|
|
if direction.y == 0:
|
|
player_state.primary = STATE.FALL
|
|
STATE.FALL:
|
|
print("Fall")
|
|
STATE.RUN:
|
|
print("Idle")
|
|
|
|
if not is_on_floor():
|
|
if _animated_sprite.animation != "jump":
|
|
_animated_sprite.play("jump")
|
|
else:
|
|
if direction and _animated_sprite.animation != "run":
|
|
print(_animated_sprite.animation, direction)
|
|
_animated_sprite.play("run")
|
|
elif not direction:
|
|
_animated_sprite.play("idle")
|