extends CharacterBody2D const SPEED = 500.0 const JUMP_VELOCITY = -600.0 @export var BUMP_FORCE = 90 @export var PUSH_FORCE = 700 @onready var animation: AnimatedSprite2D = $AnimatedSprite2D var faceLeft = false var pushLeftEnabled = false var pushRightEnabled = false var pushTarget var bullet = preload("res://scenes/bullet.tscn") @onready var right_ray = $RightRay @onready var left_ray = $LeftRay @onready var marker_right = $MarkerRight @onready var marker_left = $MarkerLeft var isJumping = false var living = true var animPlaying = "playerIdle" signal deathAnimDone func hurtPlayer(_amt): if living: animPlaying = "playerHurt" animation.play(animPlaying) func killPlayer(): if living: living = false print("Oh no") animPlaying = "playerDeath" animation.play(animPlaying) func _physics_process(delta: float) -> void: if living and not animPlaying == "playerHurt": # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta else: isJumping = false # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): isJumping = true velocity.y = JUMP_VELOCITY if pushTarget: if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft: print("shove left") pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 10) pushLeftEnabled = false if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft: print("shove right") pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 15) pushRightEnabled = false if Input.is_action_just_pressed("Shoot"): if faceLeft: var _myBullet = %SceneManager.makeBullet(marker_left.global_transform, -700) else: var _myBullet = %SceneManager.makeBullet(marker_right.global_transform, 700) # 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: if direction < 0: faceLeft = true if direction > 0: faceLeft = false animation.flip_h = faceLeft if not isJumping: animation.play("playerRun") else: animation.play("playerJump") velocity.x = direction * SPEED else: if not isJumping: animation.play("playerIdle") else: animation.play("playerJump") velocity.x = move_toward(velocity.x, 0, SPEED) move_and_slide() 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) pushTarget = false if left_ray.is_colliding(): var collider = left_ray.get_collider() if collider is Node: if collider.is_in_group("pushables"): pushLeftEnabled = true pushTarget = collider else: pushLeftEnabled = false if right_ray.is_colliding(): var collider = right_ray.get_collider() if collider is Node: if collider.is_in_group("pushables"): pushRightEnabled = true pushTarget = collider else: pushRightEnabled = false func _on_animated_finished() -> void: if animPlaying == "playerHurt": animPlaying = "playerIdle" if animPlaying == "playerDeath": # emit death signal deathAnimDone.emit()