2026-03-10 01:04:51 +00:00
|
|
|
class_name Player extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
@onready var right_target: Marker2D = $RightTarget
|
|
|
|
|
@onready var left_target: Marker2D = $LeftTarget
|
2026-03-17 00:58:20 +00:00
|
|
|
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
2026-04-07 13:43:49 +00:00
|
|
|
@onready var right_down_cast: RayCast2D = $RightDownCast
|
|
|
|
|
@onready var left_down_cast: RayCast2D = $LeftDownCast
|
2026-03-03 02:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
|
const BUMP_POWER = 50
|
2026-03-10 01:04:51 +00:00
|
|
|
enum FaceDirection{LEFT, RIGHT}
|
|
|
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
2026-03-17 00:58:20 +00:00
|
|
|
var direction
|
2026-04-07 13:43:49 +00:00
|
|
|
#awarness
|
|
|
|
|
var cliffEdge = false
|
2026-03-17 00:58:20 +00:00
|
|
|
enum PlayerState{IDLE,RUNNING, JUMPING, FALLING}
|
|
|
|
|
var current_player_state:PlayerState = PlayerState.IDLE
|
|
|
|
|
var jumpUp:bool = false
|
2026-03-03 02:00:36 +00:00
|
|
|
|
2026-04-07 13:43:49 +00:00
|
|
|
func _ready() -> void:
|
|
|
|
|
var timer = Timer.new()
|
|
|
|
|
add_child(timer)
|
|
|
|
|
|
|
|
|
|
timer.wait_time = 3.0 # Wait time in seconds
|
|
|
|
|
timer.one_shot = false # Set to false for repeating
|
|
|
|
|
timer.autostart = true # Start automatically on enter
|
|
|
|
|
timer.timeout.connect(handle_ai)
|
|
|
|
|
timer.start()
|
|
|
|
|
|
2026-03-03 02:00:36 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
2026-03-17 00:58:20 +00:00
|
|
|
|
2026-04-07 13:43:49 +00:00
|
|
|
#handle_input()
|
2026-03-17 00:58:20 +00:00
|
|
|
handle_movement(delta)
|
|
|
|
|
handle_state()
|
|
|
|
|
handle_animation()
|
|
|
|
|
move_and_slide()
|
|
|
|
|
handle_collisions()
|
2026-04-07 13:43:49 +00:00
|
|
|
|
|
|
|
|
func handle_ai()->void:
|
|
|
|
|
var ran = randi()%10
|
|
|
|
|
#running, idle
|
|
|
|
|
print("random number "+str(ran))
|
|
|
|
|
direction = 0
|
|
|
|
|
if ran<=4:
|
|
|
|
|
direction = -1
|
|
|
|
|
if ran>=7:
|
|
|
|
|
direction = 1
|
|
|
|
|
|
|
|
|
|
#shooting
|
|
|
|
|
var shootran = randi()%5
|
|
|
|
|
if shootran >3:
|
|
|
|
|
shoot()
|
|
|
|
|
|
|
|
|
|
func jump()->void:
|
|
|
|
|
if is_on_floor():
|
2026-03-17 00:58:20 +00:00
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
current_player_state = PlayerState.JUMPING
|
|
|
|
|
jumpUp = true
|
2026-04-07 13:43:49 +00:00
|
|
|
func shoot()->void:
|
|
|
|
|
match facing:
|
2026-03-10 01:04:51 +00:00
|
|
|
FaceDirection.RIGHT:
|
|
|
|
|
%SceneManager.makeBullet(right_target.global_transform, 700)
|
|
|
|
|
FaceDirection.LEFT:
|
|
|
|
|
%SceneManager.makeBullet(left_target.global_transform, -700)
|
2026-04-07 13:43:49 +00:00
|
|
|
|
|
|
|
|
func handle_input()->void:
|
|
|
|
|
direction = Input.get_axis("ui_left", "ui_right")
|
|
|
|
|
# Handle jump.
|
|
|
|
|
if Input.is_action_just_pressed("ui_accept"):
|
|
|
|
|
jump()
|
|
|
|
|
# Handle Shoot
|
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
|
shoot()
|
2026-03-17 00:58:20 +00:00
|
|
|
func handle_state()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.IDLE when velocity.x !=0:
|
|
|
|
|
#change to running!!
|
|
|
|
|
current_player_state = PlayerState.RUNNING
|
|
|
|
|
PlayerState.RUNNING when velocity.x == 0:
|
|
|
|
|
current_player_state = PlayerState.IDLE
|
|
|
|
|
PlayerState.JUMPING when velocity.y > 0:
|
|
|
|
|
current_player_state = PlayerState.FALLING
|
|
|
|
|
PlayerState.FALLING when is_on_floor():
|
|
|
|
|
if velocity.x ==0:
|
|
|
|
|
current_player_state = PlayerState.IDLE
|
|
|
|
|
else:
|
|
|
|
|
current_player_state = PlayerState.RUNNING
|
|
|
|
|
func handle_animation()->void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.IDLE:
|
|
|
|
|
player_graphic.play("idle")
|
|
|
|
|
PlayerState.RUNNING:
|
|
|
|
|
player_graphic.play("running")
|
|
|
|
|
PlayerState.JUMPING:
|
|
|
|
|
if jumpUp:
|
|
|
|
|
player_graphic.play("jumping")
|
|
|
|
|
PlayerState.FALLING:
|
|
|
|
|
player_graphic.play("falling")
|
|
|
|
|
|
|
|
|
|
func handle_movement(delta)->void:
|
2026-04-07 13:43:49 +00:00
|
|
|
|
2026-03-17 00:58:20 +00:00
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
2026-03-03 02:00:36 +00:00
|
|
|
if direction:
|
|
|
|
|
velocity.x = direction * SPEED
|
2026-03-10 01:04:51 +00:00
|
|
|
if direction <0:
|
|
|
|
|
facing=FaceDirection.LEFT
|
2026-04-07 13:43:49 +00:00
|
|
|
player_graphic.flip_h = true
|
2026-03-10 01:04:51 +00:00
|
|
|
if direction >0:
|
|
|
|
|
facing=FaceDirection.RIGHT
|
2026-04-07 13:43:49 +00:00
|
|
|
player_graphic.flip_h = false
|
2026-03-03 02:00:36 +00:00
|
|
|
else:
|
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
2026-04-07 13:43:49 +00:00
|
|
|
|
2026-03-17 00:58:20 +00:00
|
|
|
func handle_collisions()->void:
|
2026-03-03 02:00:36 +00:00
|
|
|
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)
|
2026-03-17 00:58:20 +00:00
|
|
|
if c.get_collider() is NPCCharacter:
|
|
|
|
|
var myCharacter:NPCCharacter = c.get_collider()
|
2026-04-07 13:43:49 +00:00
|
|
|
var mydirection = -c.get_normal()*BUMP_POWER
|
|
|
|
|
myCharacter.knockBack(mydirection)
|
|
|
|
|
if not right_down_cast.is_colliding() and is_on_floor():
|
|
|
|
|
#on edge of drop on right side
|
|
|
|
|
if velocity.x > 100:
|
|
|
|
|
jump()
|
|
|
|
|
if not left_down_cast.is_colliding() and is_on_floor():
|
|
|
|
|
#on edge of drop on left side
|
|
|
|
|
if velocity.x <-100:
|
|
|
|
|
jump()
|
2026-03-17 00:58:20 +00:00
|
|
|
func _on_animation_finished() -> void:
|
|
|
|
|
match current_player_state:
|
|
|
|
|
PlayerState.JUMPING:
|
|
|
|
|
jumpUp = false
|