added state handling, animation, camera; changed resolution
This commit is contained in:
1 parent
c5b67bc07c
commit
14146a476d
4 files changed
+100
-32
No files matched your search
+70
-12
@@ -4,10 +4,14 @@ const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -600.0
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
enum State{IDLE, WALK, JUMP, FALLING}
|
||||
|
||||
@export var BUMP_POWER = 50
|
||||
@export var SHOVE_POWER = 200
|
||||
@export var BUMP_POWER := 50
|
||||
@export var SHOVE_POWER := 200
|
||||
@export var ACCELERATION := 5
|
||||
@export var HARD_GRAVITY := 100
|
||||
|
||||
var current_state: State = State.IDLE
|
||||
var facing : FaceDirection = FaceDirection.RIGHT
|
||||
var direction : float = 0.0
|
||||
var jump_buffer_timer : Timer
|
||||
@@ -18,6 +22,7 @@ var push_enabled : bool = false
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_sprite: AnimatedSprite2D = $PlayerSprite
|
||||
|
||||
func _ready() -> void:
|
||||
jump_buffer_timer = Timer.new()
|
||||
@@ -28,15 +33,17 @@ func _physics_process(delta: float) -> void:
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
update_state()
|
||||
update_animation()
|
||||
|
||||
func handle_input() -> void:
|
||||
|
||||
# Handle jumping.
|
||||
if Input.is_action_just_pressed("jump"):
|
||||
jump_buffer_timer.start(0.1)
|
||||
|
||||
if is_on_floor() and jump_buffer_timer.time_left > 0:
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_state = State.JUMP
|
||||
jump_buffer_timer.stop()
|
||||
|
||||
# Set direction.
|
||||
@@ -55,8 +62,8 @@ func handle_input() -> void:
|
||||
FaceDirection.LEFT:
|
||||
shove_direction = -1
|
||||
push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER)
|
||||
|
||||
# Handle shooting.eee
|
||||
|
||||
# Handle shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("pew-pew")
|
||||
match facing:
|
||||
@@ -66,7 +73,8 @@ func handle_input() -> void:
|
||||
FaceDirection.LEFT:
|
||||
%SceneManager.make_bullet(left_spawn.global_transform, -700)
|
||||
print("shoot left")
|
||||
|
||||
|
||||
# Handle throwing grenades
|
||||
if Input.is_action_just_pressed("throw"):
|
||||
print("grenade!")
|
||||
match facing:
|
||||
@@ -78,15 +86,23 @@ func handle_input() -> void:
|
||||
print("throwing left")
|
||||
|
||||
func handle_movement(delta) -> void:
|
||||
# Left-right movement.
|
||||
# Left-right movement. Use acceleration for smoothing
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
velocity.x = move_toward(velocity.x, SPEED * direction, ACCELERATION)
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
# Add gravity.
|
||||
velocity.x = move_toward(velocity.x, 0, ACCELERATION)
|
||||
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
# Add gravity.
|
||||
if current_state == State.JUMP:
|
||||
# Character is jumping; apply normal gravity
|
||||
velocity += get_gravity() * delta
|
||||
if velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
else:
|
||||
# Character falling; apply hard gravity
|
||||
current_state = State.FALLING
|
||||
velocity += get_gravity() * HARD_GRAVITY * delta
|
||||
|
||||
func handle_collisions() -> void:
|
||||
|
||||
@@ -110,3 +126,45 @@ func handle_collisions() -> void:
|
||||
|
||||
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
||||
push_enabled = false
|
||||
|
||||
func update_state() -> void:
|
||||
match current_state:
|
||||
# If player is moving left or right
|
||||
State.IDLE when velocity.x !=0:
|
||||
current_state = State.WALK
|
||||
|
||||
# If player stops walking, or starts falling
|
||||
State.WALK:
|
||||
# If not moving left or right
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
# If falling
|
||||
if not is_on_floor() and velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
|
||||
# When jump peaks, we start to fall
|
||||
State.JUMP when velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
|
||||
# Player lands, either still or moving
|
||||
State.FALLING when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
else:
|
||||
current_state = State.WALK
|
||||
|
||||
func update_animation() -> void:
|
||||
match facing:
|
||||
FaceDirection.LEFT:
|
||||
player_sprite.flip_h = true
|
||||
FaceDirection.RIGHT:
|
||||
player_sprite.flip_h = false
|
||||
match current_state:
|
||||
State.IDLE:
|
||||
player_sprite.play("idle")
|
||||
State.WALK:
|
||||
player_sprite.play("run")
|
||||
State.JUMP:
|
||||
player_sprite.play("jump")
|
||||
State.FALLING:
|
||||
player_sprite.play("fall")
|
||||
Reference in new issue
Block a user