103 lines
2.8 KiB
GDScript
103 lines
2.8 KiB
GDScript
class_name Player extends CharacterBody2D
|
|
@onready var right_cast = $RightCast
|
|
@onready var left_cast = $LeftCast
|
|
@onready var right_spawn = $RightSpawn
|
|
@onready var left_spawn = $LeftSpawn
|
|
@onready var player_graphic = $PlayerGraphic
|
|
|
|
const SPEED = 300.0
|
|
const JUMP_VELOCITY = -400.0
|
|
var direction
|
|
enum FaceDirection{LEFT, RIGHT}
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
|
enum State{IDLE,RUNNING}
|
|
var currentState:State = State.IDLE
|
|
|
|
var pushTarget:RigidBody2D
|
|
var pushEnabled:bool = false
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
handle_input()
|
|
handle_movement(delta)
|
|
handle_state()
|
|
handle_animation()
|
|
move_and_slide()
|
|
handle_collisions()
|
|
|
|
func handle_input()->void:
|
|
if Input.is_action_just_pressed("Chuck"):
|
|
print("Chuck the chicken (inside joke)")
|
|
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
|
|
if Input.is_action_just_pressed("Force Push") && pushEnabled:
|
|
if not pushTarget is RigidBody2D:
|
|
return
|
|
#what direction to shove
|
|
var shoveDirection:int
|
|
match facing:
|
|
FaceDirection.RIGHT:
|
|
shoveDirection =1
|
|
FaceDirection.LEFT:
|
|
shoveDirection =-1
|
|
pushTarget.apply_central_impulse(Vector2(shoveDirection,-1)* 1000)
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
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.
|
|
direction = Input.get_axis("ui_left", "ui_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_movement(delta)->void:
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
func handle_collisions()->void:
|
|
if right_cast.is_colliding() and facing==FaceDirection.RIGHT:
|
|
print("Right is working")
|
|
pushEnabled = true
|
|
var collider = right_cast.get_collider()
|
|
if collider is RigidBody2D:
|
|
pushTarget = collider
|
|
|
|
if left_cast.is_colliding() and facing==FaceDirection.LEFT:
|
|
print("Left is working")
|
|
pushEnabled = true
|
|
var collider = left_cast.get_collider()
|
|
if collider is RigidBody2D:
|
|
pushTarget = collider
|
|
|
|
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
|
pushEnabled = false
|
|
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)
|
|
|
|
|
|
|
|
func handle_state():
|
|
match currentState:
|
|
State.IDLE when velocity.x != 0:
|
|
currentState = State.RUNNING
|
|
State.RUNNING when velocity.x == 0:
|
|
currentState = State.IDLE
|
|
func handle_animation():
|
|
match currentState:
|
|
State.IDLE:
|
|
player_graphic.play("idle")
|
|
State.RUNNING:
|
|
player_graphic.play("Run")
|