102 lines
2.9 KiB
GDScript
102 lines
2.9 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@onready var label: Label = $Label
|
|
|
|
@onready var rightRay: RayCast2D = $RightCast
|
|
@onready var leftRay: RayCast2D = $LeftCast
|
|
@onready var right_target: Marker2D = $RightTarget
|
|
@onready var left_target: Marker2D = $LeftTarget
|
|
|
|
|
|
var pushRightEnabled:=false
|
|
var pushLeftEnabled := false
|
|
var pushTarget
|
|
var pushTimer:int = 0
|
|
|
|
signal forcePushSignal(body,direction)
|
|
signal shootSignal(targetPosition, speed)
|
|
#track facing direction
|
|
enum FaceDirection {LEFT,RIGHT}
|
|
var facing:FaceDirection = FaceDirection.RIGHT
|
|
|
|
const SPEED = 300.0
|
|
const JUMP_VELOCITY = -400.0
|
|
|
|
|
|
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
|
|
|
|
# Handle shove
|
|
if Input.is_action_just_pressed("shove"):
|
|
pushTimer = Time.get_ticks_msec() #record start of build
|
|
|
|
#Handle shove
|
|
if Input.is_action_just_released("shove"):
|
|
var pushForce = Time.get_ticks_msec() - pushTimer
|
|
#do a maximium force value
|
|
if pushForce>4000:
|
|
pushForce = 4000
|
|
if pushRightEnabled and facing==FaceDirection.RIGHT:
|
|
label.text = "shove"
|
|
forcePushSignal.emit(pushTarget, Vector2(1,0)*100*(pushForce/100))
|
|
if pushLeftEnabled and facing==FaceDirection.LEFT:
|
|
forcePushSignal.emit(pushTarget, Vector2(-1,0)*100*(pushForce/100))
|
|
label.text = "shove"
|
|
# Handle 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)
|
|
shootSignal.emit(right_target.global_transform, 700)
|
|
FaceDirection.LEFT:
|
|
print("Shoot to the left")
|
|
#%SceneManager.makeBullet(left_target.global_transform, -700)
|
|
shootSignal.emit(left_target.global_transform, -700)
|
|
|
|
# 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.RIGHT
|
|
else:
|
|
facing = FaceDirection.LEFT
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
move_and_slide()
|
|
#a loop
|
|
for i in get_slide_collision_count():
|
|
var c = get_slide_collision(i)
|
|
#is this a bonkable thing?
|
|
if c.get_collider() is RigidBody2D:
|
|
forcePushSignal.emit(c.get_collider(), -c.get_normal() * 100)
|
|
if rightRay.is_colliding():
|
|
|
|
#get the thing causing the collision
|
|
var collider = rightRay.get_collider()
|
|
if collider is Node:
|
|
pushRightEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushRightEnabled = false
|
|
|
|
if leftRay.is_colliding():
|
|
|
|
#get the thing causing the collision
|
|
var collider = leftRay.get_collider()
|
|
if collider is Node:
|
|
pushLeftEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushLeftEnabled = false
|