JanuaryGame/GodotProject/scripts/player.gd

124 lines
3.1 KiB
GDScript

extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@export var BUMP_FORCE = 90
@onready var animation: AnimatedSprite2D = $AnimatedSprite2D
# Shove attack
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
var faceleft = false
var pushTarget
var pushRightEnabled = false
var pushLeftEnabled = false
@export var PUSH_FORCE = 700
# Bullet attack variables
@onready var right_target: Node2D = $RightTarget
@onready var left_target: Node2D = $LeftTarget
var bullet = preload("res://scenes/bullet.tscn")
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var isJumping = false
var living = true
var animPlaying = "idle"
signal deathAnimationComplete
func hurtPlayer(_amt):
if _amt < 100:
animPlaying = "hurt"
animation.play(animPlaying)
func killPlayer():
print("player character is killed")
if living:
living = false
animPlaying = "death"
animation.play(animPlaying)
func _physics_process(delta):
if living and not animPlaying == "hurt":
if not is_on_floor():
velocity.y += gravity * delta
else:
isJumping = false
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
animation.play("jump")
isJumping = true
# shove attack
if Input.is_action_just_pressed("shove"):
if pushRightEnabled && faceleft == false:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
if pushLeftEnabled && faceleft:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if direction<0:
if not isJumping:
animation.play("run")
faceleft = true
animation.flip_h = true
if direction>0:
if not isJumping:
animation.play("run")
faceleft = false
animation.flip_h = false
else:
if not isJumping:
animation.play("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
# shoot
if Input.is_action_just_pressed("shoot"):
# make bullet
#owner.add_child(myBullet)
#myBullet.transform = right_target.global_transform
if faceleft == true:
var myBullet = %SceneManager.makeBullet(left_target.global_transform,-700)
if faceleft == false:
var myBullet = %SceneManager.makeBullet(right_target.global_transform,700)
move_and_slide()
if right_ray.is_colliding():
if not faceleft:
var collider = right_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushRightEnabled = true
else:
pushRightEnabled = false
if left_ray.is_colliding():
if faceleft:
var collider = left_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = 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 == "death":
deathAnimationComplete.emit()