2025-01-07 01:57:46 +00:00
|
|
|
extends CharacterBody2D
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED = 300.0
|
|
|
|
const JUMP_VELOCITY = -400.0
|
2025-01-14 00:30:08 +00:00
|
|
|
const BUMP_FORCE = 50
|
2025-01-07 01:57:46 +00:00
|
|
|
|
2025-01-14 00:30:08 +00:00
|
|
|
# Shove Attack Variables
|
|
|
|
@onready var right_ray: RayCast2D = $RightRay
|
|
|
|
@onready var left_ray: RayCast2D = $LeftRay
|
|
|
|
var faceLeft = false
|
|
|
|
var pushTarget
|
|
|
|
var pushRightEnabled = false
|
|
|
|
var pushLeftEnabled = false
|
|
|
|
@export var PUSH_FORCE = 700
|
2025-01-21 01:44:44 +00:00
|
|
|
@onready var animation: AnimatedSprite2D = $AnimatedSprite2D
|
2025-02-10 16:30:11 +00:00
|
|
|
@onready var teleportZone: Area2D = $"../Area2D"
|
2025-01-07 01:57:46 +00:00
|
|
|
|
2025-01-14 01:58:53 +00:00
|
|
|
# Bullet Attack Variables
|
|
|
|
@onready var right_target: Node2D = $RightTarget
|
|
|
|
@onready var left_target: Node2D = $LeftTarget
|
|
|
|
|
2025-01-21 01:44:44 +00:00
|
|
|
var isJumping = false
|
2025-02-10 16:30:11 +00:00
|
|
|
var safeTeleport = false
|
2025-01-07 01:57:46 +00:00
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity += get_gravity() * delta
|
2025-01-21 01:44:44 +00:00
|
|
|
else:
|
|
|
|
isJumping = false
|
2025-01-07 01:57:46 +00:00
|
|
|
# Handle jump.
|
2025-01-14 00:30:08 +00:00
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
2025-01-07 01:57:46 +00:00
|
|
|
velocity.y = JUMP_VELOCITY
|
2025-01-21 01:44:44 +00:00
|
|
|
animation.play("jump")
|
|
|
|
isJumping = true
|
2025-02-10 16:30:11 +00:00
|
|
|
# Hover
|
|
|
|
if not is_on_floor() and Input.is_action_pressed("hover"):
|
|
|
|
velocity.x = 0
|
|
|
|
velocity.y = 0
|
2025-01-14 00:30:08 +00:00
|
|
|
# Shove Attack
|
|
|
|
if Input.is_action_just_pressed("shove"):
|
|
|
|
if pushRightEnabled && faceLeft == false:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
|
|
|
if pushLeftEnabled && faceLeft == true:
|
|
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
|
2025-02-10 16:30:11 +00:00
|
|
|
# Teleport
|
|
|
|
if Input.is_action_just_pressed("teleport"):
|
|
|
|
while not safeTeleport:
|
|
|
|
teleportZone.position = Vector2(position.x+randf_range(-300,300),position.y+randf_range(-300,300))
|
|
|
|
if teleportZone.has_overlapping_bodies():
|
|
|
|
safeTeleport = false
|
|
|
|
else:
|
|
|
|
safeTeleport = true
|
|
|
|
safeTeleport= false
|
|
|
|
position = Vector2(teleportZone.position.x,teleportZone.position.y)
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-14 01:58:53 +00:00
|
|
|
# Handle Shooting
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
|
|
#print("I want to shoot")
|
|
|
|
#make a new bullet
|
|
|
|
|
2025-02-10 16:30:11 +00:00
|
|
|
|
2025-01-21 01:44:44 +00:00
|
|
|
|
2025-01-14 01:58:53 +00:00
|
|
|
if faceLeft == true:
|
2025-01-21 01:44:44 +00:00
|
|
|
var myBullet = %SceneManager.makeBullet(left_target.global_transform,-700)
|
|
|
|
#myBullet.transform = left_target.global_transform
|
|
|
|
#myBullet.setSpeed(-700)
|
|
|
|
if faceLeft == false:
|
|
|
|
var myBullet = %SceneManager.makeBullet(right_target.global_transform,700)
|
2025-01-07 01:57:46 +00:00
|
|
|
# Get the input direction and handle the movement/deceleration.
|
|
|
|
# As good practice, you should replace UI actions with custom gameplay actions.
|
2025-01-14 00:30:08 +00:00
|
|
|
var direction := Input.get_axis("left", "right")
|
2025-01-07 01:57:46 +00:00
|
|
|
if direction:
|
|
|
|
velocity.x = direction * SPEED
|
2025-01-14 00:30:08 +00:00
|
|
|
if direction <0:
|
2025-01-21 01:44:44 +00:00
|
|
|
if not isJumping:
|
|
|
|
animation.play("run")
|
2025-01-14 00:30:08 +00:00
|
|
|
faceLeft = true
|
2025-01-21 01:44:44 +00:00
|
|
|
animation.flip_h = true
|
2025-01-14 00:30:08 +00:00
|
|
|
if direction >0:
|
2025-01-21 01:44:44 +00:00
|
|
|
if not isJumping:
|
|
|
|
animation.play("run")
|
2025-01-14 00:30:08 +00:00
|
|
|
faceLeft = false
|
2025-01-21 01:44:44 +00:00
|
|
|
animation.flip_h = false
|
2025-01-07 01:57:46 +00:00
|
|
|
else:
|
2025-01-21 01:44:44 +00:00
|
|
|
if not isJumping:
|
|
|
|
animation.play("idle")
|
2025-01-07 01:57:46 +00:00
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
|
|
|
|
move_and_slide()
|
2025-01-14 00:30:08 +00:00
|
|
|
if right_ray.is_colliding():
|
|
|
|
# print("Right ray contact!")
|
|
|
|
if not faceLeft:
|
|
|
|
var collider = right_ray.get_collider()
|
|
|
|
if collider is RigidBody2D:
|
|
|
|
pushTarget = collider
|
|
|
|
pushRightEnabled = true
|
|
|
|
else:
|
|
|
|
pushRightEnabled = false
|
|
|
|
|
|
|
|
if left_ray.is_colliding():
|
|
|
|
if faceLeft:
|
|
|
|
var collider = left_ray.get_collider()
|
|
|
|
if collider is RigidBody2D:
|
|
|
|
pushTarget = collider
|
|
|
|
pushLeftEnabled = true
|
|
|
|
else:
|
|
|
|
pushLeftEnabled = false
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-07 01:57:46 +00:00
|
|
|
for i in get_slide_collision_count():
|
|
|
|
var c = get_slide_collision(i)
|
|
|
|
if c.get_collider() is RigidBody2D:
|
2025-01-14 00:30:08 +00:00
|
|
|
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
|