2024-08-27 01:18:54 +00:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
2024-09-10 01:02:01 +00:00
|
|
|
const PUSH_FORCE = 700
|
|
|
|
|
|
|
|
var faceLeft = false
|
2024-10-01 23:56:32 +00:00
|
|
|
var living = true
|
2024-10-08 00:36:58 +00:00
|
|
|
var animPlaying="jump"
|
|
|
|
signal playerDead
|
2024-10-01 23:56:32 +00:00
|
|
|
|
2024-09-10 01:02:01 +00:00
|
|
|
# can i push right or left
|
|
|
|
var pushLeftEnabled = false
|
|
|
|
var pushRightEnabled = false
|
2024-10-08 00:36:58 +00:00
|
|
|
var punchLeftEnabled = false
|
|
|
|
var punchRightEnabled = false
|
|
|
|
|
2024-09-10 01:02:01 +00:00
|
|
|
@onready var right_ray: RayCast2D = $RightRay
|
|
|
|
@onready var left_ray: RayCast2D = $LeftRay
|
|
|
|
@onready var marker_right: Node2D = $MarkerRight
|
|
|
|
@onready var marker_left: Node2D = $MarkerLeft
|
2024-09-17 01:06:36 +00:00
|
|
|
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
|
2024-09-10 01:02:01 +00:00
|
|
|
|
|
|
|
var pushTarget
|
2024-10-08 00:36:58 +00:00
|
|
|
var punchTarget:Enemy
|
2024-09-10 01:02:01 +00:00
|
|
|
|
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
2024-08-27 01:18:54 +00:00
|
|
|
|
2024-10-01 10:04:10 +00:00
|
|
|
func killPlayer():
|
2024-10-08 00:56:53 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
if living:
|
|
|
|
living = false
|
|
|
|
#play death animation
|
2024-10-08 00:36:58 +00:00
|
|
|
animPlaying="death"
|
|
|
|
playerSprite.play(animPlaying)
|
|
|
|
func hurtPlayer(currentHealth):
|
2024-10-08 00:56:53 +00:00
|
|
|
|
2024-10-08 00:36:58 +00:00
|
|
|
animPlaying="hurt"
|
|
|
|
playerSprite.play(animPlaying)
|
2024-10-01 10:04:10 +00:00
|
|
|
|
2024-08-27 01:18:54 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2024-10-08 00:36:58 +00:00
|
|
|
if living and not animPlaying=="hurt":
|
2024-10-01 23:56:32 +00:00
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity += get_gravity() * delta
|
2024-08-27 01:18:54 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
# Handle jump.
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
|
|
velocity.y = JUMP_VELOCITY
|
2024-10-08 00:36:58 +00:00
|
|
|
if Input.is_action_just_pressed("Shove"):
|
|
|
|
animPlaying="punch"
|
|
|
|
playerSprite.play(animPlaying)
|
|
|
|
if punchLeftEnabled && faceLeft:
|
|
|
|
punchTarget.queue_free()
|
|
|
|
punchLeftEnabled = false
|
|
|
|
if punchRightEnabled && not faceLeft:
|
|
|
|
punchTarget.queue_free()
|
|
|
|
punchRightEnabled = false
|
2024-10-01 23:56:32 +00:00
|
|
|
|
2024-10-08 00:36:58 +00:00
|
|
|
if pushLeftEnabled && faceLeft:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
|
|
|
|
pushLeftEnabled = false
|
|
|
|
|
|
|
|
if pushRightEnabled && not faceLeft:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
|
|
|
|
pushRightEnabled=false
|
2024-09-17 01:06:36 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
if faceLeft:
|
|
|
|
# mybullet.setSpeed(-700)
|
|
|
|
#mybullet.transform = marker_left.global_transform
|
|
|
|
%SceneManager.placeBullet(-700, marker_left.global_transform)
|
|
|
|
else:
|
|
|
|
#mybullet.transform = marker_right.global_transform
|
|
|
|
%SceneManager.placeBullet(700, marker_right.global_transform)
|
2024-09-17 01:06:36 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
# 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 <0:
|
|
|
|
faceLeft = true
|
|
|
|
playerSprite.flip_h = true
|
|
|
|
if direction >0:
|
|
|
|
faceLeft = false
|
|
|
|
playerSprite.flip_h = false
|
|
|
|
|
|
|
|
if direction:
|
|
|
|
velocity.x = direction * SPEED
|
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2024-09-10 01:02:01 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
if is_on_floor():
|
2024-10-08 00:36:58 +00:00
|
|
|
if not animPlaying == "punch":
|
|
|
|
if direction == 0:
|
|
|
|
playerSprite.play("idle")
|
|
|
|
else:
|
|
|
|
playerSprite.play("run")
|
2024-09-10 01:02:01 +00:00
|
|
|
else:
|
2024-10-01 23:56:32 +00:00
|
|
|
playerSprite.play("jump")
|
|
|
|
|
|
|
|
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()* 100)
|
2024-09-10 01:02:01 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
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
|
2024-10-08 00:36:58 +00:00
|
|
|
if collider is Enemy:
|
2024-10-08 00:56:53 +00:00
|
|
|
|
2024-10-08 00:36:58 +00:00
|
|
|
punchTarget = collider
|
|
|
|
punchLeftEnabled = true
|
2024-09-17 01:06:36 +00:00
|
|
|
else:
|
2024-10-01 23:56:32 +00:00
|
|
|
#do something else
|
|
|
|
pushLeftEnabled = false
|
2024-10-08 00:36:58 +00:00
|
|
|
punchLeftEnabled = false
|
2024-09-17 01:06:36 +00:00
|
|
|
|
2024-10-01 23:56:32 +00:00
|
|
|
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
|
2024-10-08 00:36:58 +00:00
|
|
|
if collider is Enemy:
|
2024-10-08 00:56:53 +00:00
|
|
|
|
2024-10-08 00:36:58 +00:00
|
|
|
punchTarget = collider
|
|
|
|
punchRightEnabled = true
|
2024-10-01 23:56:32 +00:00
|
|
|
else:
|
|
|
|
pushRightEnabled = false
|
2024-10-08 00:36:58 +00:00
|
|
|
punchRightEnabled = false
|
|
|
|
|
|
|
|
|
|
|
|
func _on_animation_finished() -> void:
|
2024-10-08 00:56:53 +00:00
|
|
|
|
2024-10-08 00:36:58 +00:00
|
|
|
if animPlaying=="hurt":
|
|
|
|
animPlaying="idle"
|
|
|
|
if animPlaying=="death":
|
|
|
|
#send a signal
|
|
|
|
playerDead.emit()
|
|
|
|
if animPlaying=="punch":
|
|
|
|
animPlaying="idle"
|