godot-webrtc-mplayer-testing/scripts/objects/player.gd
2021-12-08 16:35:37 -06:00

126 lines
3.1 KiB
GDScript

extends KinematicBody2D
export var weight = 8
export var max_speed = 80 # ~5 tiles/sec at top speed
export var friction = 500
export var speed = 20
export var health = 100 setget set_health
export var max_health = 100 setget set_max_health
export var mana = 50 setget set_mana
export var max_mana = 100 setget set_max_mana
enum PlayerClass { WIZARD }
onready var viewport = get_viewport()
onready var sprite = $sprite
onready var collider = $collider
onready var name_displayer = $player_name
onready var display_name = name
onready var player_class = PlayerClass.WIZARD
onready var skills = []
# {
# "name": "fireball",
# "cooldown": 1,
# "cast_time": 0,
# "mana_cost": 25,
# },
# {
# name: "crate",
# cooldown: 0.5,
# cast_time: 0.5,
# mana_cost: 12,
# },
# {
# name: "pillar",
# mana_cost: 50,
# cooldown: 3,
# cast_time: 3,
# },
#]
remotesync var acceleration = Vector2.ZERO
remotesync var velocity = Vector2.ZERO
func set_health(x):
health = x
$bars/health_bar.value = x
maybe_hide_health_bar()
func set_max_health(x):
max_health = x
$bars/health_bar.max_value = x
maybe_hide_health_bar()
func set_mana(x):
mana = x
$bars/mana_bar.value = x
maybe_hide_mana_bar()
func set_max_mana(x):
max_mana = x
$bars/mana_bar.max_value = x
maybe_hide_mana_bar()
func maybe_hide_mana_bar():
if mana >= max_mana:
$bars/mana_bar.hide()
else:
$bars/mana_bar.show()
func maybe_hide_health_bar():
if health >= max_health:
$bars/health_bar.hide()
else:
$bars/health_bar.show()
func _ready():
# rset_config("acceleration", MultiplayerAPI.RPC_MODE_REMOTESYNC)
# rset_config("velocity", MultiplayerAPI.RPC_MODE_REMOTESYNC)
rset_config("position", MultiplayerAPI.RPC_MODE_SLAVE)
set_display_name(name)
func set_display_name(i_name):
display_name = i_name
if len(get_tree().get_network_connected_peers()) > 1:
name_displayer.text = display_name
else:
name_displayer.text = ""
static func int_input_action(action):
return int(Input.is_action_pressed(action))
func _physics_process(delta):
self.mana += (10*delta)
self.health += (1*delta)
_process_input()
_process_animation()
rset("velocity", move_and_slide((velocity + acceleration).clamped(max_speed), Vector2.ZERO, false, 4, 0.785398, false).move_toward(Vector2.ZERO, friction * delta))
for index in get_slide_count():
var collision = get_slide_collision(index)
if collision.collider is RigidBody2D:
collision.collider.apply_central_impulse(-collision.normal * weight)
if is_network_master():
rset_unreliable("position", position)
func _process_input():
if is_network_master():
var iv = Vector2()
# probably want to implement a "whichever one was pressed last" so that
# players move as fast as possible without having to worry about switching
# key frame perfectly
iv.x = int_input_action("ui_right") - int_input_action("ui_left")
iv.y = int_input_action("ui_down") - int_input_action("ui_up")
rset("acceleration", iv.normalized() * speed)
func _process_animation():
if velocity == Vector2.ZERO:
sprite.animation = "idle"
else:
sprite.animation = "run"
if velocity.x < 0:
sprite.flip_h = true
elif velocity.x > 0:
sprite.flip_h = false