139 lines
3.4 KiB
GDScript
139 lines
3.4 KiB
GDScript
extends CharacterBody2D
|
|
|
|
|
|
const SPEED = 300.0
|
|
const JUMP_VELOCITY = -500.0
|
|
const BUMP_FORCE = 50
|
|
const PUSHFORCE = 750
|
|
|
|
@onready var right_ray: RayCast2D = $RightRay
|
|
@onready var left_ray: RayCast2D = $LeftRay
|
|
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var key: Sprite2D = $Key
|
|
|
|
|
|
var faceLeft = false
|
|
|
|
var pushTarget
|
|
var pushLefEnabled = false
|
|
var pushRightEnabled = false
|
|
var isJumping = false
|
|
|
|
var animPlaying = "idle"
|
|
var living = true
|
|
|
|
signal playerDead
|
|
|
|
@onready var right_target: Node2D = $RightTarget
|
|
@onready var left_target: Node2D = $LeftTarget
|
|
|
|
func _ready() -> void:
|
|
keyDrop()
|
|
|
|
func hurtPlayer(amt):
|
|
animPlaying = "hurt"
|
|
playerSprite.play("Hurt")
|
|
|
|
func killPlayer():
|
|
if living:
|
|
living = false
|
|
print("MORRIDO")
|
|
animPlaying = "dead"
|
|
playerSprite.play("Dead")
|
|
|
|
func keyCollected():
|
|
print("Player knows key")
|
|
key.visible = true
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if living and not animPlaying =="hurt":
|
|
# Add the gravity.
|
|
if not is_on_floor():
|
|
velocity += get_gravity() * delta
|
|
|
|
# Handle jump.
|
|
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.
|
|
var direction := Input.get_axis("ui_left", "ui_right")
|
|
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
|
|
print("Shove a box")
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSHFORCE * 2)
|
|
pushRightEnabled = false
|
|
|
|
if Input.is_action_just_pressed("shove") && faceLeft == true && pushLefEnabled == true:
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE * 2)
|
|
pushLefEnabled = false
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
print("I'm an owl: Shoot shoot")
|
|
var myBullet = %SceneManager.bulletFactory()
|
|
if not faceLeft:
|
|
myBullet.transform = right_target.global_transform
|
|
myBullet.setSpeed(750)
|
|
if faceLeft == true:
|
|
myBullet.transform = left_target.global_transform
|
|
myBullet.setSpeed(-750)
|
|
|
|
if direction:
|
|
velocity.x = direction * SPEED
|
|
if direction >0:
|
|
faceLeft = false
|
|
if direction <0:
|
|
faceLeft = true
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
if faceLeft:
|
|
playerSprite.flip_h = true
|
|
else:
|
|
playerSprite.flip_h = false
|
|
|
|
if is_on_floor():
|
|
isJumping = false
|
|
if direction == 0:
|
|
playerSprite.play("Idle")
|
|
else:
|
|
playerSprite.play("Run")
|
|
else:
|
|
if not isJumping:
|
|
playerSprite.play("Jump")
|
|
isJumping = true
|
|
|
|
move_and_slide()
|
|
if right_ray.is_colliding():
|
|
if not faceLeft:
|
|
var collider = right_ray.get_collider()
|
|
if collider is Node:
|
|
if collider.is_in_group("box"):
|
|
pushTarget = collider
|
|
pushRightEnabled = true
|
|
else:
|
|
pushRightEnabled = false
|
|
|
|
if left_ray.is_colliding():
|
|
if faceLeft:
|
|
var collider = left_ray.get_collider()
|
|
if collider is Node:
|
|
if collider.is_in_group("box"):
|
|
pushTarget = collider
|
|
pushLefEnabled = true
|
|
else:
|
|
pushLefEnabled = false
|
|
|
|
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() * BUMP_FORCE)
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
|
if animPlaying == "hurt":
|
|
animPlaying ="idle"
|
|
if animPlaying == "dead":
|
|
### emit a custom signal
|
|
playerDead.emit()
|
|
|