godot-webrtc-mplayer-testing/scripts/objects/player.gd
2021-12-21 16:40:58 -06:00

155 lines
4.5 KiB
GDScript

extends KinematicBody2D
export(NodePath) onready var sprite = get_node(sprite) as AnimatedSprite
export(NodePath) onready var collider = get_node(collider) as CollisionShape2D
export(NodePath) onready var name_label = get_node(name_label) as Label
export(NodePath) onready var health_bar = get_node(health_bar) as ProgressBar
export(NodePath) onready var mana_bar = get_node(mana_bar) as ProgressBar
export(NodePath) onready var aim_indicator = get_node(aim_indicator) as Sprite
export(NodePath) onready var tile_indicator = get_node(tile_indicator) as Sprite
export var weight := 8
export var max_speed := 64 * 5 # ~5 tiles/sec at top speed
export var friction := 2000
export var speed := 80
export var mana_per_sec := 10
export var health_per_sec := 1
export var health := 100.0 setget set_health
export var max_health := 100.0 setget set_max_health
export var mana := 50.0 setget set_mana
export var max_mana := 1000.0 setget set_max_mana
enum PlayerClass { WIZARD }
onready var viewport = get_viewport()
onready var display_name = Global.client.DEFAULT_DISPLAY_NAME setget set_display_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 target := Vector2(0, 0)
remotesync var acceleration = Vector2.ZERO
remotesync var velocity = Vector2.ZERO
func set_health(x: float):
health = x
update_bars()
func set_max_health(x: float):
max_health = x
update_bars()
func set_mana(x: float):
mana = x
update_bars()
func set_max_mana(x: float):
max_mana = x
update_bars()
func update_bars():
if mana_bar is ProgressBar:
mana_bar.max_value = max_mana
mana_bar.value = mana
if mana >= max_mana:
mana_bar.hide()
else:
mana_bar.show()
if health_bar is ProgressBar:
health_bar.max_value = max_health
health_bar.value = health
if health >= max_health:
health_bar.hide()
else:
health_bar.show()
func _ready():
rset_config("position", MultiplayerAPI.RPC_MODE_SLAVE)
var ns = get_tree().get_network_connected_peers().size()
if ns < 1:
print("Hiding Player Name %s..." % ns)
name_label.hide()
set_display_name(display_name)
if not is_network_master():
tile_indicator.modulate.a = 0.05
aim_indicator.modulate.a = 0.05
# if not is_network_master():
# mana_bar.hi
func _input(event):
if event is InputEventKey:
if event.pressed and event.scancode == KEY_P and event.alt:
position.y += 50
func set_display_name(i_name):
display_name = i_name
if name_label is Label: name_label.text = display_name
static func int_input_action(actions) -> int:
if typeof(actions) == TYPE_ARRAY:
var result = false
for action in actions:
result = result or Input.is_action_pressed(action)
return int(result)
else:
return int(Input.is_action_pressed(actions))
func _physics_process(delta):
# if get_viewport().get_camera() != null: name_label.rect_scale = Vector2(get_viewport().get_camera().zoom)
self.mana += mana_per_sec * delta
self.health += health_per_sec * delta
_process_input()
_process_animation()
aim_indicator.rotation = atan2(target.y, target.x) - PI/2
tile_indicator.global_position = ((target + position + Vector2(32, 32)) / 64).floor() * 64
rset_unreliable("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", "right"]) - int_input_action(["ui_left", "left"])
iv.y = int_input_action(["ui_down", "down"]) - int_input_action(["ui_up", "up"])
rset_unreliable("acceleration", iv.normalized() * speed)
rset_unreliable("position", position)
rset_unreliable("target", get_local_mouse_position())
func _process_animation():
if sprite == null: return
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