June2026Game/scripts/player.gd

89 lines
2.7 KiB
GDScript

class_name Player extends CharacterBody2D
signal pushSignal(body,direction)
signal shootSignal(markerPosition, speed)
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
@onready var right_bullet_spawn: Marker2D = $RightBulletSpawn
@onready var left_bullet_spawn: Marker2D = $LeftBulletSpawn
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
var pushTimer = 0
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
enum FaceDirection {RIGHT,LEFT}
var facing:FaceDirection = FaceDirection.RIGHT
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_up") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle shove
if Input.is_action_just_pressed("shove"):
print("Shove pressed")
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:
pushSignal.emit(pushTarget, Vector2(1,0) * 100 * (pushForce/100))
if pushLeftEnabled and facing == FaceDirection.LEFT:
pushSignal.emit(pushTarget, Vector2(-1,0) * 100 * (pushForce/100))
# Handle shoot
if Input.is_action_just_pressed("shoot"):
print("Shoot pressed")
match facing:
FaceDirection.RIGHT:
shootSignal.emit(right_bullet_spawn.global_transform, 800)
FaceDirection.LEFT:
shootSignal.emit(left_bullet_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
else:
facing = FaceDirection.LEFT
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
# Collision detection needs to happen AFTER move_and_slide calculation
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
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)
pushSignal.emit(c.get_collider(),-c.get_normal() * 100)