2025-06-10 14:13:01 +00:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
|
|
@onready var left_cast: RayCast2D = $"Left cast"
|
|
|
|
@onready var right_spawn: Node2D = $RightSpawn
|
|
|
|
@onready var left_spawn: Node2D = $LeftSpawn
|
2025-06-17 00:58:16 +00:00
|
|
|
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
2025-06-10 14:13:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum FaceDirection{LEFT, RIGHT}
|
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
|
|
|
|
2025-06-17 00:58:16 +00:00
|
|
|
enum State{IDLE, RUN, JUMP, FALLING}
|
|
|
|
var current_state:State = State.IDLE
|
2025-06-10 14:13:01 +00:00
|
|
|
var pushTarget
|
|
|
|
var pushEnable = false
|
2025-06-17 00:58:16 +00:00
|
|
|
var direction
|
|
|
|
var upJump = false
|
2025-06-10 14:13:01 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2025-06-17 00:58:16 +00:00
|
|
|
#game loop
|
|
|
|
handle_input()
|
|
|
|
#calculate movment
|
|
|
|
handle_movement(delta)
|
|
|
|
#change states
|
|
|
|
update_states()
|
|
|
|
#play animations
|
|
|
|
update_animation()
|
|
|
|
#object raycasts
|
|
|
|
#handle_collisions()
|
2025-06-10 14:13:01 +00:00
|
|
|
# Add the gravity.
|
2025-06-17 00:58:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
move_and_slide()
|
|
|
|
handle_collisions()
|
|
|
|
func handle_input():
|
2025-06-10 14:13:01 +00:00
|
|
|
if Input.is_action_just_pressed("shove") && pushEnable:
|
|
|
|
print("shove pressed")
|
|
|
|
if facing == FaceDirection.RIGHT:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(1,-0.2)*700)
|
|
|
|
pushEnable = false
|
|
|
|
if facing == FaceDirection.LEFT:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(-1,-0.2)*700)
|
|
|
|
pushEnable = false
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
print("shoot a bullet")
|
|
|
|
if facing == FaceDirection.RIGHT:
|
|
|
|
%SceneManager.makeBullet(right_spawn.global_transform, 700)
|
|
|
|
if facing == FaceDirection.LEFT:
|
|
|
|
%SceneManager.makeBullet(left_spawn.global_transform,-700)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Handle jump.x
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
|
|
velocity.y = JUMP_VELOCITY
|
2025-06-17 00:58:16 +00:00
|
|
|
current_state = State.JUMP
|
|
|
|
upJump = true
|
2025-06-10 14:13:01 +00:00
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
2025-06-17 00:58:16 +00:00
|
|
|
direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
|
|
|
|
func handle_collisions():
|
2025-06-10 14:13:01 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
if c.get_collider() is RigidBody2D:
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100 )
|
|
|
|
|
|
|
|
if right_cast.is_colliding() && facing == FaceDirection.RIGHT:
|
|
|
|
print("right cast collision")
|
|
|
|
# get the thing i am colliding with
|
|
|
|
var collider = right_cast.get_collider()
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
print("i can shove this right")
|
|
|
|
pushTarget = collider
|
|
|
|
pushEnable = true
|
|
|
|
if left_cast.is_colliding() && facing == FaceDirection.LEFT:
|
|
|
|
print("left cast collision")
|
|
|
|
var collider = left_cast.get_collider()
|
|
|
|
if collider is Node && collider is RigidBody2D:
|
|
|
|
print("i can shove this left")
|
|
|
|
pushTarget = collider
|
|
|
|
pushEnable = true
|
|
|
|
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
|
|
|
pushEnable = false
|
2025-06-17 00:58:16 +00:00
|
|
|
func handle_movement(delta):
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity += get_gravity() * delta
|
2025-06-10 14:13:01 +00:00
|
|
|
|
2025-06-17 00:58:16 +00:00
|
|
|
if direction:
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
if direction <0:
|
|
|
|
facing = FaceDirection.LEFT
|
|
|
|
player_graphic.flip_h = true
|
|
|
|
if direction >0:
|
|
|
|
facing = FaceDirection.RIGHT
|
|
|
|
player_graphic.flip_h = false
|
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
func update_animation():
|
|
|
|
match current_state:
|
|
|
|
State.IDLE:
|
|
|
|
player_graphic.play("Idle")
|
|
|
|
State.RUN:
|
|
|
|
print("play run animation")
|
|
|
|
player_graphic.play("run")
|
|
|
|
State.JUMP:
|
|
|
|
if upJump:
|
|
|
|
player_graphic.play("Jump")
|
|
|
|
State.FALLING:
|
|
|
|
player_graphic.play("Falling")
|
|
|
|
func update_states():
|
|
|
|
match current_state:
|
|
|
|
#idle when movment in x
|
|
|
|
State.IDLE when velocity.x !=0:
|
|
|
|
current_state = State.RUN
|
|
|
|
State.RUN:
|
|
|
|
if velocity.x ==0:
|
|
|
|
current_state = State.IDLE
|
|
|
|
#jumping when reaching apex
|
|
|
|
State.JUMP when velocity.y > 0:
|
|
|
|
current_state = State.FALLING
|
|
|
|
State.FALLING when is_on_floor():
|
|
|
|
if velocity.x ==0:
|
|
|
|
current_state = State.IDLE
|
|
|
|
else:
|
|
|
|
current_state = State.RUN
|
|
|
|
|
|
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
|
|
match current_state:
|
|
|
|
State.JUMP:
|
|
|
|
upJump = false
|