godot-webrtc-mplayer-testing/scripts/objects/player.gd

155 lines
4.5 KiB
GDScript3
Raw Normal View History

2021-12-08 16:35:37 -06:00
extends KinematicBody2D
2021-12-17 17:10:56 -06:00
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
2021-12-21 16:40:58 -06:00
export(NodePath) onready var aim_indicator = get_node(aim_indicator) as Sprite
export(NodePath) onready var tile_indicator = get_node(tile_indicator) as Sprite
2021-12-17 17:10:56 -06:00
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
2021-12-08 16:35:37 -06:00
enum PlayerClass { WIZARD }
onready var viewport = get_viewport()
2021-12-17 17:10:56 -06:00
onready var display_name = Global.client.DEFAULT_DISPLAY_NAME setget set_display_name
2021-12-08 16:35:37 -06:00
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,
# },
#]
2021-12-21 16:40:58 -06:00
remotesync var target := Vector2(0, 0)
2021-12-08 16:35:37 -06:00
remotesync var acceleration = Vector2.ZERO
remotesync var velocity = Vector2.ZERO
2021-12-17 17:10:56 -06:00
func set_health(x: float):
2021-12-08 16:35:37 -06:00
health = x
2021-12-17 17:10:56 -06:00
update_bars()
2021-12-08 16:35:37 -06:00
2021-12-17 17:10:56 -06:00
func set_max_health(x: float):
2021-12-08 16:35:37 -06:00
max_health = x
2021-12-17 17:10:56 -06:00
update_bars()
2021-12-08 16:35:37 -06:00
2021-12-17 17:10:56 -06:00
func set_mana(x: float):
2021-12-08 16:35:37 -06:00
mana = x
2021-12-17 17:10:56 -06:00
update_bars()
2021-12-08 16:35:37 -06:00
2021-12-17 17:10:56 -06:00
func set_max_mana(x: float):
2021-12-08 16:35:37 -06:00
max_mana = x
2021-12-17 17:10:56 -06:00
update_bars()
2021-12-08 16:35:37 -06:00
2021-12-17 17:10:56 -06:00
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()
2021-12-08 16:35:37 -06:00
func _ready():
2021-12-17 17:10:56 -06:00
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)
2021-12-21 16:40:58 -06:00
if not is_network_master():
tile_indicator.modulate.a = 0.05
aim_indicator.modulate.a = 0.05
2021-12-17 17:10:56 -06:00
# if not is_network_master():
# mana_bar.hi
2021-12-21 16:40:58 -06:00
func _input(event):
if event is InputEventKey:
if event.pressed and event.scancode == KEY_P and event.alt:
position.y += 50
2021-12-08 16:35:37 -06:00
func set_display_name(i_name):
display_name = i_name
2021-12-17 17:10:56 -06:00
if name_label is Label: name_label.text = display_name
2021-12-08 16:35:37 -06:00
2021-12-15 16:14:36 -06:00
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))
2021-12-08 16:35:37 -06:00
func _physics_process(delta):
2021-12-17 17:10:56 -06:00
# 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
2021-12-08 16:35:37 -06:00
_process_input()
_process_animation()
2021-12-21 16:40:58 -06:00
aim_indicator.rotation = atan2(target.y, target.x) - PI/2
tile_indicator.global_position = ((target + position + Vector2(32, 32)) / 64).floor() * 64
2021-12-17 17:10:56 -06:00
rset_unreliable("velocity", move_and_slide((velocity + acceleration).clamped(max_speed), Vector2.ZERO, false, 4, 0.785398, false).move_toward(Vector2.ZERO, friction * delta))
2021-12-08 16:35:37 -06:00
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)
2021-12-15 16:14:36 -06:00
# if is_network_master():
# rset_unreliable("position", position)
2021-12-08 16:35:37 -06:00
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
2021-12-15 16:14:36 -06:00
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"])
2021-12-17 17:10:56 -06:00
rset_unreliable("acceleration", iv.normalized() * speed)
rset_unreliable("position", position)
2021-12-21 16:40:58 -06:00
rset_unreliable("target", get_local_mouse_position())
2021-12-08 16:35:37 -06:00
func _process_animation():
2021-12-17 17:10:56 -06:00
if sprite == null: return
2021-12-08 16:35:37 -06:00
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