153 lines
4.5 KiB
GDScript
153 lines
4.5 KiB
GDScript
class_name Player extends CharacterBody2D
|
|
signal pushSignal(body, direction)
|
|
signal shootSignal(markerPosition, speed)
|
|
|
|
const SPEED = 300.0
|
|
const JUMP_VELOCITY = -400.0
|
|
@onready var right_cast: RayCast2D = $RightCast
|
|
@onready var left_cast: RayCast2D = $LeftCast
|
|
@onready var right_spawn: Marker2D = $RightSpawn
|
|
@onready var left_spawn: Marker2D = $LeftSpawn
|
|
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
|
|
|
var pushRightEnabled = false
|
|
var pushLeftEnabled = false
|
|
var pushTarget
|
|
var pushTimer = 0
|
|
|
|
enum FaceDirection {RIGHT,LEFT}
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
|
|
|
enum playerStates {IDLE, RUNNING, JUMPING, FALLING, CHARGING, SHOVING}
|
|
var current_player_state:playerStates = playerStates.IDLE
|
|
var upJump = false
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
handle_movement(delta)
|
|
handle_input()
|
|
handle_states()
|
|
handle_animation()
|
|
move_and_slide()
|
|
handle_collisions()
|
|
|
|
func handle_input()->void:
|
|
# Handle jump.
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
current_player_state = playerStates.JUMPING
|
|
upJump = true
|
|
#Handle shove target
|
|
if Input.is_action_just_pressed("shove"):
|
|
print("I want to shove")
|
|
if pushRightEnabled && facing==FaceDirection.RIGHT:
|
|
current_player_state = playerStates.CHARGING
|
|
pushTimer = Time.get_ticks_msec()
|
|
if pushLeftEnabled && facing==FaceDirection.LEFT:
|
|
current_player_state = playerStates.CHARGING
|
|
pushTimer = Time.get_ticks_msec()
|
|
if Input.is_action_just_released("shove"):
|
|
var pushForce = Time.get_ticks_msec() - pushTimer
|
|
if pushForce>4000:
|
|
pushForce=4000
|
|
if pushRightEnabled and facing==FaceDirection.RIGHT:
|
|
#deliver the boom
|
|
pushSignal.emit(pushTarget, Vector2(1,0)*100*(pushForce/100))
|
|
current_player_state = playerStates.SHOVING
|
|
if pushLeftEnabled and facing==FaceDirection.LEFT:
|
|
pushSignal.emit(pushTarget, Vector2(-1,0)*100*(pushForce/100))
|
|
current_player_state = playerStates.SHOVING
|
|
|
|
#Handle Shoot Bullet
|
|
if Input.is_action_just_pressed("shoot"):
|
|
print("Ima shoot a bullet")
|
|
#marker postion, speed
|
|
match facing:
|
|
FaceDirection.RIGHT:
|
|
shootSignal.emit(right_spawn.global_transform, 800)
|
|
FaceDirection.LEFT:
|
|
shootSignal.emit(left_spawn.global_transform, -800)
|
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
|
var direction := Input.get_axis("ui_left", "ui_right")
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
if direction >0:
|
|
facing = FaceDirection.RIGHT
|
|
player_graphic.flip_h = false
|
|
else:
|
|
facing = FaceDirection.LEFT
|
|
player_graphic.flip_h = true
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
func handle_movement(delta)->void:
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
func handle_collisions()->void:
|
|
if right_cast.is_colliding():
|
|
print("Right raycast collision")
|
|
var collider = right_cast.get_collider()
|
|
if collider is Node:
|
|
pushRightEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushRightEnabled = false
|
|
|
|
if left_cast.is_colliding():
|
|
print("Left raycast collision")
|
|
var collider = left_cast.get_collider()
|
|
if collider is Node:
|
|
pushLeftEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushLeftEnabled = false
|
|
#a loop
|
|
for i in get_slide_collision_count():
|
|
var c = get_slide_collision(i)
|
|
#is this bonkable? reverse normal
|
|
if c.get_collider() is RigidBody2D:
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
|
|
|
|
# Exciting new stuff
|
|
func handle_states()->void:
|
|
match current_player_state:
|
|
playerStates.IDLE when velocity.x != 0:
|
|
print("I run")
|
|
current_player_state = playerStates.RUNNING
|
|
playerStates.RUNNING when velocity.x == 0:
|
|
current_player_state = playerStates.IDLE
|
|
playerStates.JUMPING when velocity.y>0:
|
|
current_player_state = playerStates.FALLING
|
|
playerStates.FALLING when is_on_floor():
|
|
if velocity.x == 0:
|
|
current_player_state = playerStates.IDLE
|
|
else:
|
|
current_player_state = playerStates.RUNNING
|
|
|
|
func handle_animation()->void:
|
|
match current_player_state:
|
|
playerStates.RUNNING:
|
|
player_graphic.play("running")
|
|
playerStates.IDLE:
|
|
player_graphic.play("idle")
|
|
playerStates.JUMPING:
|
|
if upJump:
|
|
player_graphic.play("jumping")
|
|
playerStates.FALLING:
|
|
player_graphic.play("falling")
|
|
playerStates.CHARGING:
|
|
player_graphic.play("charging")
|
|
playerStates.SHOVING:
|
|
player_graphic.play("shoving")
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
match current_player_state:
|
|
playerStates.JUMPING:
|
|
upJump = false
|
|
playerStates.SHOVING:
|
|
current_player_state = playerStates.IDLE
|