extends CharacterBody2D @export var speed:int = 150 @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_cam = $player_cam @onready var player := get_node(".") #Animation @onready var anim_tree = $AnimationTree @onready var anim_mode = anim_tree.get("parameters/playback") #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 _enter_tree(): set_multiplayer_authority(str(name).to_int()) @rpc("any_peer") func set_spawn_location(spawn_position: Vector2): print("RPC position update! from PEER ID: ", player.name, spawn_position) set_position(spawn_position) func _ready(): if not is_multiplayer_authority(): return print("Setting IDLE: ", get_position()) player_cam.make_current() #player_cam.current = true # for mp to set the camera #anim_mode.travel("idle") 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("parameters/DamageOneShot/request", 1) #anim_tree.set(disabled_str, 1) else: pass #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(): pass #anim_tree.set(damage_str, 0) func health_changed_func(amount: int): print("Health DAMAGED!") 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 set_animation_state(): if(velocity != Vector2.ZERO): anim_tree.travel("walk") else: pass #anim_tree.travel("idle") func attack_finished(): #anim_tree.set(atk_str, 0) is_attacking = false func get_input(): if Input.is_action_just_pressed("reset_pos"): print("REsetting player POS: ") set_position(Vector2(-110, 561)) 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 print("Attacking!") #anim_tree.set("parameters/AttackOneShot/active", true) anim_tree.set("parameters/AttackOneShot/request", 1) #anim_tree.set(atk_str, 1) else: player_sprite.flip_h = false anim_tree.set("parameters/AttackOneShot/request", 1) #anim_tree.set(atk_str, 1) #anim_mode.travel("attack") next_attack_time = now + attack_speed 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 Input.is_action_pressed("right") and not is_attacking: player_facing = "right" player_sprite.flip_h = false velocity.x += 1 if Input.is_action_pressed("left") and not is_attacking: player_sprite.flip_h = true player_facing = "left" velocity.x -= 1 if Input.is_action_pressed("down") and not is_attacking: direction = "down" velocity.y += 1 if Input.is_action_pressed("up") and not is_attacking: direction = "up" velocity.y -= 1 velocity = velocity.normalized() * speed if velocity == Vector2.ZERO: anim_tree.set("parameters/walkIdleBlend/blend_amount", 1) else: anim_tree.set("parameters/walkIdleBlend/blend_amount", 0) func _physics_process(delta): if not is_multiplayer_authority(): return # 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_force(-collision.get_normal() * push_power) #collider.applied_force = -collision.get_normal() * push_power #collider.move_object.rpc() #collider.apply_central_impulse() # push back the player when player pushed #player_hit.is_player_hit = true #player_hit.hit_velocity = push_power * .1 if player_hit.is_player_hit: # moves player over time 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