JuneGame/scripts/character_body_2d.gd
2025-06-10 10:13:01 -04:00

81 lines
2.4 KiB
GDScript

extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $"Left cast"
@onready var right_spawn: Node2D = $RightSpawn
@onready var left_spawn: Node2D = $LeftSpawn
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
var pushTarget
var pushEnable = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_just_pressed("shove") && pushEnable:
print("shove pressed")
if facing == FaceDirection.RIGHT:
pushTarget.apply_central_impulse(Vector2(1,-0.2)*700)
pushEnable = false
if facing == FaceDirection.LEFT:
pushTarget.apply_central_impulse(Vector2(-1,-0.2)*700)
pushEnable = false
if Input.is_action_just_pressed("shoot"):
print("shoot a bullet")
if facing == FaceDirection.RIGHT:
%SceneManager.makeBullet(right_spawn.global_transform, 700)
if facing == FaceDirection.LEFT:
%SceneManager.makeBullet(left_spawn.global_transform,-700)
# Handle jump.x
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 direction:
velocity.x = direction * SPEED
if direction <0:
facing = FaceDirection.LEFT
if direction >0:
facing = FaceDirection.RIGHT
else:
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() * 100 )
if right_cast.is_colliding() && facing == FaceDirection.RIGHT:
print("right cast collision")
# get the thing i am colliding with
var collider = right_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("i can shove this right")
pushTarget = collider
pushEnable = true
if left_cast.is_colliding() && facing == FaceDirection.LEFT:
print("left cast collision")
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D:
print("i can shove this left")
pushTarget = collider
pushEnable = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnable = false