122 lines
3.4 KiB
GDScript
122 lines
3.4 KiB
GDScript
extends CharacterBody2D
|
|
|
|
|
|
@export var SPEED = 300.0
|
|
@export var JUMP_VELOCITY = -300.0
|
|
@export var BUMP_FORCE = 80.0
|
|
@export var PUSH_FORCE = 200
|
|
|
|
@onready var right_ray = $RightRay
|
|
@onready var left_ray = $LeftRay
|
|
@onready var marker_right = $MarkerRight
|
|
@onready var marker_left = $MarkerLeft
|
|
|
|
@onready var animated_sprite = $AnimatedSprite2D
|
|
|
|
signal deathComplete
|
|
|
|
var currentPlaying = "idle"
|
|
|
|
var faceLeft = false
|
|
var pushLeftEnabled = false
|
|
var pushRightEnabled = false
|
|
var pushTarget
|
|
var living = true
|
|
|
|
var bullet = preload("res://scenes/bullet.tscn")
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
|
|
func die():
|
|
print("kill the player")
|
|
currentPlaying = "death"
|
|
animated_sprite.play(currentPlaying)
|
|
living = false
|
|
|
|
func _physics_process(delta):
|
|
if living:
|
|
if not is_on_floor():
|
|
velocity.y += gravity * delta
|
|
|
|
# Handle jump.
|
|
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
|
velocity.y = JUMP_VELOCITY
|
|
# Hand force push (melee attack)
|
|
if Input.is_action_just_pressed("push") && pushRightEnabled && faceLeft == false:
|
|
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 5)
|
|
pushRightEnabled = false
|
|
if Input.is_action_just_pressed("push") && pushLeftEnabled:
|
|
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 5)
|
|
pushLeftEnabled = false
|
|
|
|
if Input.is_action_just_pressed("shoot"):
|
|
#make a bullet
|
|
var myBullet = bullet.instantiate()
|
|
#set bullet speed?
|
|
|
|
#put the bullet at the Marker
|
|
owner.add_child(myBullet)
|
|
if not faceLeft:
|
|
# facing right
|
|
myBullet.transform = marker_right.global_transform
|
|
else:
|
|
# facing left
|
|
myBullet.transform = marker_left.global_transform
|
|
myBullet.setSpeed(-750)
|
|
|
|
# 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")
|
|
|
|
# In this example we have replaced the faceLeft code to also have
|
|
# the character's sprite flip depending what direction they are facing
|
|
if direction:
|
|
faceLeft = true if direction<0 else false
|
|
animated_sprite.flip_h = true if direction < 0 else false
|
|
velocity.x = direction * SPEED
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
if is_on_floor():
|
|
if direction==0:
|
|
currentPlaying = "idle"
|
|
animated_sprite.play(currentPlaying)
|
|
else:
|
|
currentPlaying="run"
|
|
animated_sprite.play(currentPlaying)
|
|
else:
|
|
currentPlaying = "jump"
|
|
animated_sprite.play(currentPlaying)
|
|
|
|
move_and_slide()
|
|
if right_ray.is_colliding():
|
|
var collider = right_ray.get_collider()
|
|
#what did we hit?
|
|
if collider is Node:
|
|
if collider.is_in_group("boxes"):
|
|
pushRightEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushRightEnabled = false
|
|
|
|
if left_ray.is_colliding():
|
|
var collider = left_ray.get_collider()
|
|
if collider is Node:
|
|
if collider.is_in_group("boxes"):
|
|
pushLeftEnabled = true
|
|
pushTarget = collider
|
|
else:
|
|
pushLeftEnabled = false
|
|
|
|
#loop through the collisions
|
|
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_FORCE)
|
|
|
|
|
|
func _on_animation_finished():
|
|
if currentPlaying == "death":
|
|
print("Animation is Finished is death")
|
|
deathComplete.emit()
|