2026-03-10 01:04:52 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
@onready var right_target: Marker2D = $RightTarget
|
|
|
|
|
@onready var left_target: Marker2D = $LeftTarget
|
2026-03-03 02:00:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
|
const BUMP_POWER = 50
|
2026-03-10 01:04:52 +00:00
|
|
|
enum FaceDirection{LEFT, RIGHT}
|
|
|
|
|
var facing: FaceDirection = FaceDirection.RIGHT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-03 02:00:40 +00:00
|
|
|
func _physics_process(delta):
|
|
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
|
|
|
|
|
2026-03-10 01:04:52 +00:00
|
|
|
#Handler shoot
|
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
|
print("Player wants to shoot")
|
|
|
|
|
match facing:
|
|
|
|
|
FaceDirection.RIGHT:
|
|
|
|
|
print("Shoot to the Right")
|
|
|
|
|
%SceneManager.makeBullet(right_target.global_transform,700)
|
|
|
|
|
FaceDirection.LEFT:
|
|
|
|
|
print("Shoot to the Left")
|
|
|
|
|
%SceneManager.makeBullet(left_target.global_transform,-700)
|
|
|
|
|
|
2026-03-03 02:00:40 +00:00
|
|
|
# Handle jump.
|
|
|
|
|
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
|
2026-03-10 01:04:52 +00:00
|
|
|
if direction <0:
|
|
|
|
|
facing = FaceDirection.LEFT
|
|
|
|
|
if direction >=0:
|
|
|
|
|
facing = FaceDirection.RIGHT
|
2026-03-03 02:00:40 +00:00
|
|
|
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()*BUMP_POWER)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|