2026-04-21 01:12:58 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
2026-04-28 01:08:43 +00:00
|
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
|
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
2026-05-05 01:00:27 +00:00
|
|
|
@onready var right_spawn: Marker2D = $RightSpawn
|
|
|
|
|
@onready var left_spawn: Marker2D = $LeftSpawn
|
|
|
|
|
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
2026-04-21 01:12:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
|
const JUMP_VELOCITY = -400.0
|
2026-04-28 01:08:43 +00:00
|
|
|
var direction
|
|
|
|
|
enum FaceDirection{LEFT,RIGHT}
|
|
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
2026-05-05 01:00:27 +00:00
|
|
|
enum PlayerState{IDLE,RUNNING}
|
|
|
|
|
var current_player_state:PlayerState = PlayerState.IDLE
|
2026-04-21 01:12:58 +00:00
|
|
|
|
2026-04-28 01:08:43 +00:00
|
|
|
var pushTarget:RigidBody2D
|
|
|
|
|
var pushEnabled:bool = false
|
2026-04-21 01:12:58 +00:00
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2026-04-28 01:08:43 +00:00
|
|
|
handle_input()
|
|
|
|
|
handle_movement(delta)
|
2026-05-05 01:00:27 +00:00
|
|
|
handle_state()
|
|
|
|
|
handle_animation()
|
2026-04-28 01:08:43 +00:00
|
|
|
move_and_slide()
|
|
|
|
|
handle_collisions()
|
|
|
|
|
|
|
|
|
|
func handle_input()->void:
|
2026-05-05 01:00:27 +00:00
|
|
|
if Input.is_action_just_pressed("chuck"):
|
|
|
|
|
print("Chuck a grenade")
|
|
|
|
|
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
|
|
|
|
|
|
2026-04-28 01:08:43 +00:00
|
|
|
if Input.is_action_just_pressed("shove") and pushEnabled:
|
|
|
|
|
print("Shove somethin")
|
|
|
|
|
# determine what direction to shove the thing
|
|
|
|
|
var shoveDirection:int
|
|
|
|
|
match facing:
|
|
|
|
|
FaceDirection.RIGHT:
|
|
|
|
|
shoveDirection = 1
|
|
|
|
|
FaceDirection.LEFT:
|
|
|
|
|
shoveDirection = -1
|
|
|
|
|
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
|
|
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
2026-04-21 01:12:58 +00:00
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
|
|
|
|
|
# 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:43 +00:00
|
|
|
direction = Input.get_axis("left", "right")
|
|
|
|
|
if direction<0:
|
|
|
|
|
facing = FaceDirection.LEFT
|
2026-05-05 01:00:27 +00:00
|
|
|
player_graphic.flip_h = true
|
2026-04-28 01:08:43 +00:00
|
|
|
if direction>0:
|
|
|
|
|
facing = FaceDirection.RIGHT
|
2026-05-05 01:00:27 +00:00
|
|
|
player_graphic.flip_h = false
|
2026-04-28 01:08:43 +00:00
|
|
|
|
|
|
|
|
func handle_movement(delta)->void:
|
|
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
# Handle jump.
|
|
|
|
|
|
2026-04-21 01:12:58 +00:00
|
|
|
if direction:
|
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
|
else:
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2026-04-28 01:08:43 +00:00
|
|
|
|
|
|
|
|
func handle_collisions()->void:
|
|
|
|
|
if right_cast.is_colliding() and facing==FaceDirection.RIGHT:
|
|
|
|
|
print("Right raycast contact!!")
|
|
|
|
|
pushEnabled = true
|
|
|
|
|
var collider = right_cast.get_collider()
|
|
|
|
|
pushTarget = collider
|
|
|
|
|
|
|
|
|
|
if left_cast.is_colliding() and facing==FaceDirection.LEFT:
|
|
|
|
|
print("Left raycast contact!!")
|
|
|
|
|
pushEnabled = true
|
|
|
|
|
var collider = left_cast.get_collider()
|
|
|
|
|
pushTarget = collider
|
|
|
|
|
|
|
|
|
|
if not right_cast.is_colliding() and not left_cast.is_colliding():
|
|
|
|
|
pushEnabled = false
|
|
|
|
|
|
2026-04-21 01:12:58 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
|
if c.get_collider() is RigidBody2D:
|
|
|
|
|
#deliver the impact
|
2026-05-05 01:00:27 +00:00
|
|
|
if not c.get_collider() is Grenade:
|
|
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
func handle_animation()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.IDLE:
|
|
|
|
|
player_graphic.play("idle")
|
|
|
|
|
PlayerState.RUNNING:
|
|
|
|
|
player_graphic.play("run")
|
|
|
|
|
|