JanGodotGame/scripts/charController.gd

98 lines
2.5 KiB
GDScript3
Raw Normal View History

2025-01-07 01:03:00 +00:00
extends CharacterBody2D
const SPEED = 500.0
const JUMP_VELOCITY = -600.0
2025-01-20 02:04:07 +00:00
@export var BUMP_FORCE = 90
@export var PUSH_FORCE = 700
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
2025-01-07 01:03:00 +00:00
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_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
2025-01-20 02:04:07 +00:00
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"):
var mybullet = bullet.instantiate()
print("I will shoot")
if faceLeft:
print("shoot left")
mybullet.set_speed(-700)
mybullet.transform = marker_right.global_transform
else:
print("shoot right")
mybullet.transform = marker_right.global_transform
owner.add_child(mybullet)
2025-01-07 01:03:00 +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")
2025-01-20 02:04:07 +00:00
if direction < 0:
faceLeft = true
if direction > 0:
faceLeft = false
2025-01-07 01:03:00 +00:00
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
2025-01-07 01:34:02 +00:00
for i in (get_slide_collision_count()):
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
2025-01-20 02:04:07 +00:00
c.get_collider().apply_central_impulse(-c.get_normal()*BUMP_FORCE)
pushTarget = false
if left_ray.is_colliding():
print("Left ray collision")
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():
print("Right ray collision")
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("pushables"):
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false