137 lines
3.8 KiB
GDScript
137 lines
3.8 KiB
GDScript
extends KinematicBody2D
|
|
|
|
export var ACCELERATION: int = 1000
|
|
export var MAX_SPEED: int = 120
|
|
export var FRICTION: int = 1000
|
|
|
|
const HEALTH_SLICES: Array = [0, 18, 35, 50, 65, 82, 100]
|
|
var health_index: int = 6
|
|
|
|
var l5_gems: int = 0
|
|
|
|
var hud: CanvasLayer = null
|
|
var velocity: Vector2 = Vector2.ZERO
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var input_vector: Vector2 = Vector2.ZERO
|
|
|
|
input_vector.x = Input.get_action_strength('player_right') \
|
|
- Input.get_action_strength('player_left')
|
|
input_vector.y = Input.get_action_strength('player_down') \
|
|
- Input.get_action_strength('player_up')
|
|
input_vector = input_vector.normalized()
|
|
|
|
if input_vector != Vector2.ZERO:
|
|
$AnimationTree.set('parameters/Idle/blend_position', input_vector)
|
|
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
|
|
set_weapon_position(input_vector)
|
|
else:
|
|
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
|
|
|
|
velocity = move_and_slide(velocity)
|
|
return
|
|
|
|
|
|
func load_hud(node: CanvasLayer) -> void:
|
|
hud = node
|
|
if hud.connect('add_currency', self, 'add_currency') != OK:
|
|
print('ERROR: HUD "add_currency" signal already connected.')
|
|
|
|
hud.update_health(HEALTH_SLICES[health_index])
|
|
hud.update_currency($Inventory.get_currency())
|
|
return
|
|
|
|
func set_weapon_position(pos: Vector2) -> void:
|
|
|
|
if pos[0] < 0: # facing left
|
|
$SwordAnimation.position.x = -8
|
|
$SwordAnimation.position.y = -6
|
|
$SwordAnimation.scale.x = -1
|
|
$SwordAnimation.rotation_degrees = 0
|
|
$SwordAnimation.z_index = 0
|
|
|
|
$JavelinAnimation.position.x = -8
|
|
$JavelinAnimation.position.y = -4
|
|
$JavelinAnimation.scale.x = -1
|
|
$JavelinAnimation.rotation_degrees = 0
|
|
$JavelinAnimation.z_index = 0
|
|
elif pos[0] > 0: # facing right
|
|
$SwordAnimation.position.x = 8
|
|
$SwordAnimation.position.y = -6
|
|
$SwordAnimation.scale.x = 1
|
|
$SwordAnimation.rotation_degrees = 0
|
|
$SwordAnimation.z_index = 0
|
|
|
|
$JavelinAnimation.position.x = 8
|
|
$JavelinAnimation.position.y = -4
|
|
$JavelinAnimation.scale.x = 1
|
|
$JavelinAnimation.rotation_degrees = 0
|
|
$JavelinAnimation.z_index = 0
|
|
elif pos[1] < 0: # facing up
|
|
$SwordAnimation.position.x = -4
|
|
$SwordAnimation.position.y = -12
|
|
$SwordAnimation.scale.x = 1
|
|
$SwordAnimation.rotation_degrees = -90
|
|
$SwordAnimation.z_index = 0
|
|
|
|
$JavelinAnimation.position.x = 0
|
|
$JavelinAnimation.position.y = -10
|
|
$JavelinAnimation.scale.x = 1
|
|
$JavelinAnimation.rotation_degrees = -90
|
|
$JavelinAnimation.z_index = 0
|
|
elif pos[1] > 0: # facing down
|
|
$SwordAnimation.position.x = 4
|
|
$SwordAnimation.position.y = 2
|
|
$SwordAnimation.scale.x = 1
|
|
$SwordAnimation.rotation_degrees = 90
|
|
$SwordAnimation.z_index = 1
|
|
|
|
$JavelinAnimation.position.x = 0
|
|
$JavelinAnimation.position.y = 0
|
|
$JavelinAnimation.scale.x = 1
|
|
$JavelinAnimation.rotation_degrees = 90
|
|
$JavelinAnimation.z_index = 1
|
|
return
|
|
|
|
func add_currency(amount: int) -> void:
|
|
$Inventory.add_currency(amount)
|
|
return
|
|
|
|
|
|
func _on_Inventory_update_currency(amount: int) -> void:
|
|
hud.update_currency(amount)
|
|
return
|
|
|
|
func _on_Hitbox_body_entered(body: Node) -> void:
|
|
if not 'enemies' in body.get_groups():
|
|
return
|
|
|
|
if health_index != 0:
|
|
health_index -= 1
|
|
hud.update_health(HEALTH_SLICES[health_index])
|
|
return
|
|
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event.is_action_pressed('screenshot'):
|
|
var img: Image = get_viewport().get_texture().get_data()
|
|
yield(get_tree(), "idle_frame")
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
img.flip_y()
|
|
|
|
var time: Dictionary = OS.get_datetime_from_unix_time(OS.get_unix_time())
|
|
var time_msecs: int = OS.get_system_time_msecs()
|
|
|
|
if img.save_png('user://Screenshot_%d%d%d_%d.png' % [time.year, time.month, time.day, time_msecs]) != OK:
|
|
print('ERROR: Failed saving screenshot.')
|
|
|
|
if event.is_action_pressed("player_attack"):
|
|
|
|
if hud.weapon == "sword":
|
|
$SwordAnimation/SwordAttack.play("swing")
|
|
elif hud.weapon == "javelin":
|
|
$JavelinAnimation/JavelinAttack.play("swing")
|
|
return
|
|
|