2026-04-21 01:13:00 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
|
const JUMP_VELOCITY = -400.0
|
2026-05-05 01:01:53 +00:00
|
|
|
@onready var right_spawn: Marker2D = $RightSpawn
|
|
|
|
|
@onready var left_spawn: Marker2D = $LeftSpawn
|
|
|
|
|
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
|
|
|
|
|
|
|
|
|
var direction
|
|
|
|
|
enum FaceDirection{LEFT,RIGHT}
|
|
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
2026-05-12 01:03:27 +00:00
|
|
|
enum PlayerState{IDLE,RUNNING,JUMPING,FALLING}
|
2026-05-05 01:01:53 +00:00
|
|
|
var current_player_state:PlayerState = PlayerState.IDLE
|
2026-05-12 01:03:27 +00:00
|
|
|
var upjump:bool = false
|
2026-04-21 01:13:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2026-05-05 01:01:53 +00:00
|
|
|
handle_input()
|
|
|
|
|
|
|
|
|
|
handle_state()
|
|
|
|
|
handle_animation()
|
|
|
|
|
|
2026-04-21 01:13:00 +00:00
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
2026-05-05 01:01:53 +00:00
|
|
|
|
2026-04-21 01:13:00 +00:00
|
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
2026-05-05 01:01:53 +00:00
|
|
|
|
2026-04-21 01:13:00 +00:00
|
|
|
if direction:
|
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
|
else:
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
|
|
|
|
|
|
move_and_slide()
|
|
|
|
|
for i in get_slide_collision_count():
|
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
|
if c.get_collider() is RigidBody2D:
|
|
|
|
|
#deliver the impact
|
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
2026-05-05 01:01:53 +00:00
|
|
|
func handle_input()->void:
|
|
|
|
|
if Input.is_action_just_pressed("Chuck"):
|
|
|
|
|
print("chuck a grenade")
|
|
|
|
|
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
|
|
|
velocity.y = JUMP_VELOCITY
|
2026-05-12 01:03:27 +00:00
|
|
|
upjump = true
|
|
|
|
|
current_player_state = PlayerState.JUMPING
|
2026-05-05 01:01:53 +00:00
|
|
|
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
|
|
|
direction = Input.get_axis("left", "right")
|
|
|
|
|
if direction<0:
|
|
|
|
|
facing = FaceDirection.LEFT
|
|
|
|
|
player_graphic.flip_h = true
|
|
|
|
|
if direction>0:
|
|
|
|
|
facing = FaceDirection.RIGHT
|
|
|
|
|
player_graphic.flip_h = false
|
|
|
|
|
|
|
|
|
|
func handle_state()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.IDLE when velocity.x !=0:
|
|
|
|
|
#change to running
|
|
|
|
|
current_player_state = PlayerState.RUNNING
|
|
|
|
|
PlayerState.RUNNING when velocity.x ==0:
|
|
|
|
|
current_player_state = PlayerState.IDLE
|
2026-05-12 01:03:27 +00:00
|
|
|
PlayerState.JUMPING when velocity.y > 0:
|
|
|
|
|
current_player_state = PlayerState.FALLING
|
|
|
|
|
PlayerState.FALLING when is_on_floor():
|
|
|
|
|
if velocity.x ==0:
|
|
|
|
|
current_player_state = PlayerState.IDLE
|
|
|
|
|
else:
|
|
|
|
|
current_player_state=PlayerState.RUNNING
|
|
|
|
|
|
|
|
|
|
|
2026-05-05 01:01:53 +00:00
|
|
|
func handle_animation()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.IDLE:
|
|
|
|
|
player_graphic.play("idle")
|
|
|
|
|
PlayerState.RUNNING:
|
|
|
|
|
player_graphic.play("run")
|
2026-05-12 01:03:27 +00:00
|
|
|
PlayerState.JUMPING:
|
|
|
|
|
if upjump:
|
|
|
|
|
player_graphic.play("jump")
|
|
|
|
|
PlayerState.FALLING:
|
|
|
|
|
player_graphic.play("fall")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.JUMPING:
|
|
|
|
|
upjump = false
|