2026-04-21 01:16:07 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
const SPEED =200.0
|
2026-05-05 01:00:45 +00:00
|
|
|
const JUMP_VELOCITY = -500.0
|
2026-04-28 01:08:27 +00:00
|
|
|
var direction
|
|
|
|
|
enum FaceDirection{LEFT, RIGHT}
|
2026-05-05 01:00:45 +00:00
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
2026-05-12 01:03:27 +00:00
|
|
|
enum player_state{IDLE,RUNNING,JUMPING,FALLING}
|
2026-05-05 01:00:45 +00:00
|
|
|
var current_player_state:player_state = player_state.IDLE
|
2026-05-12 01:03:27 +00:00
|
|
|
var upJump:bool = false
|
2026-05-05 01:00:45 +00:00
|
|
|
|
|
|
|
|
@onready var player_graphic: AnimatedSprite2D = $"player graphic"
|
2026-04-21 01:16:07 +00:00
|
|
|
|
2026-05-05 01:00:45 +00:00
|
|
|
@onready var rightspawn: Marker2D = $rightspawn
|
|
|
|
|
@onready var leftspawn: Marker2D = $leftspawn
|
|
|
|
|
|
|
|
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
|
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
|
|
|
|
var pushTarget
|
|
|
|
|
var pushEnabled:bool=false
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
print(right_cast)
|
|
|
|
|
|
2026-04-21 01:16:07 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2026-04-28 01:08:27 +00:00
|
|
|
handle_input()
|
|
|
|
|
handle_movement(delta)
|
2026-05-05 01:00:45 +00:00
|
|
|
handle_state()
|
|
|
|
|
handle_animation()
|
2026-04-28 01:08:27 +00:00
|
|
|
move_and_slide()
|
|
|
|
|
handle_collisions()
|
|
|
|
|
|
|
|
|
|
func handle_input()->void:
|
2026-05-05 01:00:45 +00:00
|
|
|
if Input.is_action_just_pressed("chuck"):
|
|
|
|
|
print("chuck grenade")
|
|
|
|
|
%SceneManager.makeGrenade(rightspawn.global_transform, 1)
|
|
|
|
|
|
2026-04-28 01:08:27 +00:00
|
|
|
if Input.is_action_just_pressed("ui_accept"):
|
2026-04-21 01:16:07 +00:00
|
|
|
velocity.y = JUMP_VELOCITY
|
2026-05-05 01:00:45 +00:00
|
|
|
if Input.is_action_just_pressed("magic push") and pushEnabled:
|
|
|
|
|
print("I want to shove")
|
|
|
|
|
var shoveDirection:int
|
|
|
|
|
match facing:
|
|
|
|
|
FaceDirection.RIGHT:
|
|
|
|
|
shoveDirection = 1
|
|
|
|
|
FaceDirection.LEFT:
|
|
|
|
|
shoveDirection = -1
|
|
|
|
|
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
|
2026-04-21 01:16:07 +00:00
|
|
|
|
2026-05-12 01:03:27 +00:00
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
|
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
upJump = true
|
|
|
|
|
current_player_state = player_state.JUMPING
|
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
2026-04-28 01:08:27 +00:00
|
|
|
direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
|
if direction<0:
|
|
|
|
|
facing = FaceDirection.LEFT
|
2026-05-05 01:00:45 +00:00
|
|
|
player_graphic.flip_h = true
|
2026-04-28 01:08:27 +00:00
|
|
|
if direction>0:
|
|
|
|
|
facing = FaceDirection.RIGHT
|
2026-05-05 01:00:45 +00:00
|
|
|
player_graphic.flip_h = false
|
2026-05-12 01:03:27 +00:00
|
|
|
|
|
|
|
|
|
2026-04-28 01:08:27 +00:00
|
|
|
|
|
|
|
|
func handle_movement(delta)->void:
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
|
|
|
|
# Handle jump.
|
2026-05-05 01:00:45 +00:00
|
|
|
if direction:
|
2026-04-21 01:16:07 +00:00
|
|
|
velocity.x = direction * SPEED
|
|
|
|
|
else:
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2026-05-05 01:00:45 +00:00
|
|
|
|
2026-04-28 01:08:27 +00:00
|
|
|
func handle_collisions()->void:
|
2026-05-05 01:00:45 +00:00
|
|
|
if right_cast.is_colliding() && facing==FaceDirection.RIGHT:
|
|
|
|
|
var collider = right_cast.get_collider()
|
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
|
pushTarget = collider
|
|
|
|
|
pushEnabled = true
|
|
|
|
|
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
|
|
|
|
|
var collider = left_cast.get_collider()
|
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
|
pushTarget = collider
|
|
|
|
|
pushEnabled = true
|
2026-04-21 01:16:07 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
|
var c = get_slide_collision(i)
|
2026-04-28 01:08:27 +00:00
|
|
|
if c.get_collider() is RigidBody2D:
|
|
|
|
|
#deliver the impact
|
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
2026-05-05 01:00:45 +00:00
|
|
|
|
|
|
|
|
func handle_state()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
player_state.IDLE when velocity.x !=0:
|
|
|
|
|
print("runnnnnn")
|
|
|
|
|
current_player_state = player_state.RUNNING
|
|
|
|
|
player_state.RUNNING when velocity.x ==0:
|
|
|
|
|
current_player_state = player_state.IDLE
|
2026-05-12 01:03:27 +00:00
|
|
|
player_state.JUMPING when velocity.y >0:
|
|
|
|
|
current_player_state = player_state.FALLING
|
|
|
|
|
player_state.FALLING when is_on_floor():
|
|
|
|
|
current_player_state = player_state.IDLE
|
|
|
|
|
|
2026-05-05 01:00:45 +00:00
|
|
|
|
|
|
|
|
func handle_animation()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
player_state.IDLE:
|
|
|
|
|
player_graphic.play("idle")
|
|
|
|
|
player_state.RUNNING:
|
|
|
|
|
player_graphic.play("run")
|
2026-05-12 01:03:27 +00:00
|
|
|
player_state.JUMPING:
|
|
|
|
|
if upJump:
|
|
|
|
|
player_graphic.play("jump")
|
|
|
|
|
player_state.FALLING:
|
|
|
|
|
player_graphic.play("fall")
|
|
|
|
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
player_state.JUMPING:
|
|
|
|
|
upJump = false
|