205 lines
5.8 KiB
GDScript
205 lines
5.8 KiB
GDScript
extends CharacterBody2D
|
|
|
|
@export var speed:int = 50
|
|
@export var max_speed:int = 350
|
|
@export var push_power:int = 50 + speed
|
|
@onready var health := max_health
|
|
|
|
@export var max_health := 3
|
|
@export var attack_speed := 2000
|
|
@export var throwback_strength := 2
|
|
@onready var player_sprite = $Sprite2D
|
|
@onready var player := get_node(".")
|
|
#Animation
|
|
@onready var anim_tree = $AnimationTree
|
|
@onready var atk_str := "parameters/attacking/current"
|
|
@onready var mov_str := "parameters/moving/current"
|
|
@onready var damage_str := "parameters/damaged/current"
|
|
@onready var disabled_str := "parameters/disabled/current"
|
|
#Other
|
|
@export var value:int = 0
|
|
|
|
@onready var player_facing:String = "right"
|
|
@onready var next_attack_time := 0
|
|
@onready var is_attacking := false
|
|
|
|
@onready var equipped:Node2D
|
|
@onready var spawned_item:Node2D
|
|
@onready var invulnerability_timer:Timer = get_node("InvulnerableTimer")
|
|
@onready var player_disabled := false
|
|
@onready var player_in_inventory := false
|
|
@onready var is_searching := false
|
|
|
|
@onready var item_hovering := false
|
|
|
|
@onready var rng = RandomNumberGenerator.new()
|
|
|
|
# Get the gravity from the project settings to be synced with RigidBody nodes.
|
|
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
|
|
|
|
@onready var player_hit := {
|
|
"is_player_hit": false,
|
|
"hit_velocity": Vector2(0,0),
|
|
}
|
|
|
|
signal speed_changed
|
|
signal value_changed
|
|
signal health_changed
|
|
signal player_killed
|
|
|
|
func _ready():
|
|
if not is_multiplayer_authority(): return
|
|
|
|
|
|
func value_changed_func(amount: int) -> void:
|
|
value = value + amount
|
|
emit_signal("value_changed", value)
|
|
|
|
func set_disabled(is_disabled:bool):
|
|
player_disabled = is_disabled
|
|
if is_disabled:
|
|
anim_tree.set(disabled_str, 1)
|
|
else:
|
|
anim_tree.set(disabled_str, 0)
|
|
|
|
func speed_changed_func(amount: int) -> void:
|
|
speed = speed + amount
|
|
if speed <= 0:
|
|
health_changed_func(-1)
|
|
speed = 100
|
|
elif speed > max_speed:
|
|
speed = max_speed
|
|
health_changed_func(1)
|
|
emit_signal("speed_changed", speed)
|
|
|
|
# reset our invulnerability animations
|
|
func _on_invulnerable_timer_timeout():
|
|
anim_tree.set(damage_str, 0)
|
|
|
|
func health_changed_func(amount: int):
|
|
if amount < 0:
|
|
if invulnerability_timer.is_stopped():
|
|
invulnerability_timer.start()
|
|
health = health + amount
|
|
if health < 0:
|
|
print("kill!")
|
|
health = max_health
|
|
#TODO: Respawn char
|
|
else:
|
|
print("Setting health frame: ", health)
|
|
anim_tree.set(damage_str, 1)
|
|
else:
|
|
health = health + amount
|
|
emit_signal("health_changed", health)
|
|
|
|
func attack_finished():
|
|
anim_tree.set(atk_str, 0)
|
|
is_attacking = false
|
|
|
|
func get_input():
|
|
if Input.is_action_just_pressed("print_pos"):
|
|
print("PLAYER POSITION!: ", player.get_position())
|
|
if Input.is_action_just_pressed("show_inventory"):
|
|
pass
|
|
#show_inventory()
|
|
|
|
if player_disabled or player_in_inventory:
|
|
return
|
|
|
|
if Input.is_action_just_pressed("speed_up"):
|
|
speed_changed_func(10)
|
|
if Input.is_action_just_pressed("attack"):
|
|
if item_hovering:
|
|
spawned_item = null
|
|
return
|
|
var now = Time.get_ticks_msec()
|
|
if now >= next_attack_time:
|
|
is_attacking = true
|
|
if player_facing == "left":
|
|
player_sprite.flip_h = true
|
|
anim_tree.set(atk_str, 1)
|
|
else:
|
|
player_sprite.flip_h = false
|
|
anim_tree.set(atk_str, 1)
|
|
if equipped:
|
|
pass
|
|
#spawned_item = load(ItemDatabase.Castable_objects[equipped.item_name]).instantiate()
|
|
#var map = get_tree().current_scene
|
|
#map.add_child(spawned_item)
|
|
#inventory_ui.item_cooldown()
|
|
#if equipped.item_clip_size > 0:
|
|
# inventory_ui.set_ammo(-1)
|
|
#spawned_item.use_item(player, spawned_item, map)
|
|
#next_attack_time = now + attack_speed
|
|
velocity = Vector2()
|
|
var direction : String
|
|
# if not input_active:
|
|
# return
|
|
if Input.is_action_pressed("right") and not is_attacking:
|
|
player_facing = "right"
|
|
player_sprite.flip_h = false
|
|
velocity.x += 1
|
|
anim_tree.set(mov_str, 1)
|
|
if Input.is_action_pressed("left") and not is_attacking:
|
|
player_sprite.flip_h = true
|
|
player_facing = "left"
|
|
velocity.x -= 1
|
|
anim_tree.set(mov_str, 1)
|
|
if Input.is_action_pressed("down") and not is_attacking:
|
|
direction = "down"
|
|
velocity.y += 1
|
|
anim_tree.set(mov_str, 1)
|
|
if Input.is_action_pressed("up") and not is_attacking:
|
|
direction = "up"
|
|
velocity.y -= 1
|
|
anim_tree.set(mov_str, 1)
|
|
|
|
|
|
velocity = velocity.normalized() * speed
|
|
if velocity == Vector2.ZERO:
|
|
anim_tree.set(mov_str, 0)
|
|
|
|
func _physics_process(delta):
|
|
# if not is_multiplayer_authority():
|
|
# get_input()
|
|
# move_and_slide()
|
|
# return
|
|
|
|
get_input()
|
|
move_and_slide()
|
|
|
|
for i in get_slide_collision_count():
|
|
var collision = get_slide_collision(i)
|
|
var collider = collision.get_collider()
|
|
if collider.is_in_group("moveable"):
|
|
var normal = collision.get_normal()
|
|
collider.apply_central_impulse(-collision.get_normal() * push_power)
|
|
|
|
if player_hit.is_player_hit:
|
|
velocity = Vector2.ZERO
|
|
set_disabled(true)
|
|
player_hit.is_player_hit = false
|
|
var tween = get_tree().create_tween()
|
|
var new_location = (player_hit.hit_velocity * throwback_strength) * delta
|
|
tween.tween_method(move_player, Vector2(0,0) * delta, new_location, 1)
|
|
await get_tree().create_timer(0.5).timeout
|
|
set_disabled(false)
|
|
# Update sync variables, which will be replicated to everyone else
|
|
#rpc(&'update_movement', velocity)
|
|
# $Networking.sync_position = position
|
|
# $Networking.sync_velocity = velocity
|
|
# $Networking.flipped_v = player_sprite.flip_v
|
|
# $Networking.flipped_h = player_sprite.flip_h
|
|
# $Networking.sync_frame_coords = player_sprite.get_frame_coords()
|
|
|
|
func move_player(c_velocity: Vector2):
|
|
# rpc(&'update_movement', velocity)
|
|
var collided = move_and_collide(c_velocity)
|
|
# if collided:
|
|
# print("Collided again?", collided)
|
|
# var collider = collision.get_collider()
|
|
# if collider.is_in_group("moveable"):
|
|
# var normal = collision.get_normal()
|
|
# collider.apply_central_impulse(-collision.get_normal() * push_power)
|
|
player_hit.is_player_hit = false
|