169 lines
6.3 KiB
GDScript
169 lines
6.3 KiB
GDScript
class_name CoopHud
|
|
extends Control
|
|
## The co-op part of the HUD (Main adds it to the farm's HUD layer):
|
|
## * the partner panel, under your own portrait and health: their name in their color (P1 gold, P2 blue),
|
|
## their health pips, what they're doing (down until morning, patching, gathering, fear
|
|
## ready), a speaker mark while their voice comes through, and the ping; plus your
|
|
## microphone (on / muted, [M])
|
|
## * an arrow at the screen edge toward the partner when they're off screen, with the distance
|
|
## * the lobby's status while hosting with nobody in yet ("Waiting for a friend to join ...")
|
|
## Both scarecrows also wear name tags (PlayerCharacter.set_tag).
|
|
|
|
const PANEL := Rect2(22.0, 150.0, 336.0, 104.0) # from the top left
|
|
const PIP := Vector2(14.0, 9.0)
|
|
const ARROW_MARGIN := 60.0
|
|
|
|
var sim: FarmSim
|
|
var session: CoopSession
|
|
var lobby: CoopLobby
|
|
|
|
var _name: Label
|
|
var _state: Label
|
|
var _mic: Label
|
|
var _status: Label
|
|
var _arrow_label: Label
|
|
var _arrow_at := Vector2.ZERO
|
|
var _arrow_dir := Vector2.ZERO
|
|
var _arrow_on := false
|
|
var _ping: float = -1.0
|
|
var _ping_wait: float = 0.0
|
|
var _pinging := false
|
|
|
|
|
|
func setup(p_sim: FarmSim, p_session: CoopSession, p_lobby: CoopLobby) -> void:
|
|
sim = p_sim
|
|
session = p_session
|
|
lobby = p_lobby
|
|
|
|
|
|
func _ready() -> void:
|
|
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
_name = _label(22)
|
|
_state = _label(18)
|
|
_mic = _label(16)
|
|
_status = _label(20)
|
|
_status.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
_status.add_theme_color_override("font_color", Avatar.COLORS[1])
|
|
_arrow_label = _label(17)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
queue_redraw()
|
|
_ping_wait -= delta
|
|
if _ping_wait <= 0.0 and not _pinging and lobby != null and lobby.gdsync != null and lobby.other_id >= 0 and lobby.gdsync.is_active():
|
|
_ping_wait = 2.0
|
|
_measure_ping()
|
|
var active := session != null and session.is_active() and sim != null and sim.partner != null
|
|
var other: Avatar = null
|
|
if active:
|
|
other = sim.avatars[0] if session.is_client() else sim.partner
|
|
for l: Label in [_name, _state, _mic]:
|
|
l.visible = other != null
|
|
var waiting := lobby != null and lobby.stage == CoopLobby.Stage.HOSTING and not active
|
|
_status.visible = waiting and lobby.status != ""
|
|
_status.text = "Co-op: " + lobby.status if lobby != null else ""
|
|
_status.position = Vector2((size.x - 900.0) * 0.5, 64.0)
|
|
_status.size = Vector2(900.0, 30.0)
|
|
_arrow_on = false
|
|
_arrow_label.visible = false
|
|
if other == null:
|
|
return
|
|
var base := PANEL.position
|
|
_name.text = other.label()
|
|
_name.add_theme_color_override("font_color", other.color())
|
|
_name.position = base + Vector2(14.0, 6.0)
|
|
var k := other.kit
|
|
var what := ""
|
|
if not k.alive:
|
|
what = "Down until morning"
|
|
elif k.patching:
|
|
what = "Patching up"
|
|
elif other.interactions.gathering != null:
|
|
what = "Gathering"
|
|
elif k.fear_left == 0.0:
|
|
what = "Fear ready"
|
|
if session.partner_speaking():
|
|
what = ("%s · talking" % what) if what != "" else "Talking"
|
|
if _ping >= 0.0:
|
|
what += " %d ms" % roundi(_ping * 1000.0 if _ping < 10.0 else _ping)
|
|
_state.text = what
|
|
_state.add_theme_color_override("font_color", Color(1.0, 0.5, 0.42) if not k.alive else Color(0.85, 0.88, 0.82))
|
|
_state.position = base + Vector2(14.0, 58.0)
|
|
_mic.visible = not session.voices.is_empty()
|
|
_mic.text = "Your mic: %s [M]" % ("on" if session.microphone else "muted")
|
|
_mic.position = base + Vector2(14.0, PANEL.size.y + 2.0)
|
|
# The arrow toward the partner when they're off screen.
|
|
var cam := get_viewport().get_camera_3d()
|
|
if cam == null:
|
|
return
|
|
var world := other.body.global_position + Vector3(0.0, 1.2, 0.0)
|
|
var on_screen := cam.unproject_position(world)
|
|
var view := Rect2(Vector2.ZERO, size).grow(-ARROW_MARGIN)
|
|
if not cam.is_position_behind(world) and view.has_point(on_screen):
|
|
return
|
|
var center := size * 0.5
|
|
var dir := (on_screen - center)
|
|
if cam.is_position_behind(world):
|
|
dir = -dir
|
|
if dir.length_squared() < 1.0:
|
|
return
|
|
dir = dir.normalized()
|
|
var t := INF
|
|
var half := view.size * 0.5
|
|
if absf(dir.x) > 1e-4:
|
|
t = minf(t, half.x / absf(dir.x))
|
|
if absf(dir.y) > 1e-4:
|
|
t = minf(t, half.y / absf(dir.y))
|
|
_arrow_at = center + dir * t
|
|
_arrow_dir = dir
|
|
_arrow_on = true
|
|
var meters := sim.local_avatar.position().distance_to(other.position())
|
|
_arrow_label.visible = true
|
|
_arrow_label.text = "%s %d m" % [other.label(), roundi(meters)]
|
|
_arrow_label.add_theme_color_override("font_color", other.color())
|
|
_arrow_label.position = _arrow_at - dir * 46.0 - Vector2(60.0, 10.0)
|
|
|
|
|
|
func _draw() -> void:
|
|
if session == null or not session.is_active() or sim == null or sim.partner == null:
|
|
return
|
|
var other := sim.avatars[0] if session.is_client() else sim.partner
|
|
var base := PANEL.position
|
|
Sketch.box(self, Rect2(base, PANEL.size), Sketch.WASH, Color(0, 0, 0, 0), 0.0, 601.0)
|
|
Sketch.line(self, base + Vector2(3.0, 4.0), base + Vector2(3.0, PANEL.size.y - 4.0), other.color(), 5.0, 602.0)
|
|
var hp := other.kit.hp()
|
|
var top := other.kit.max_hp()
|
|
for i in top:
|
|
var r := Rect2(base + Vector2(14.0 + i * (PIP.x + 4.0), 40.0), PIP)
|
|
draw_rect(r, other.color() if i < hp else Color(0.15, 0.15, 0.15, 0.8))
|
|
if session.partner_speaking():
|
|
var c := base + Vector2(PANEL.size.x - 34.0, 22.0)
|
|
draw_rect(Rect2(c + Vector2(-8.0, -5.0), Vector2(6.0, 10.0)), other.color())
|
|
draw_colored_polygon(PackedVector2Array([c + Vector2(-2.0, -5.0), c + Vector2(6.0, -11.0), c + Vector2(6.0, 11.0),
|
|
c + Vector2(-2.0, 5.0)]), other.color())
|
|
draw_arc(c + Vector2(6.0, 0.0), 8.0, -0.9, 0.9, 8, other.color(), 2.0)
|
|
draw_arc(c + Vector2(6.0, 0.0), 13.0, -0.9, 0.9, 8, other.color(), 2.0)
|
|
if _arrow_on:
|
|
var tip := _arrow_at + _arrow_dir * 18.0
|
|
var side := Vector2(-_arrow_dir.y, _arrow_dir.x) * 13.0
|
|
var back := _arrow_at - _arrow_dir * 14.0
|
|
draw_colored_polygon(PackedVector2Array([tip, back + side, back - side]), other.color())
|
|
draw_polyline(PackedVector2Array([tip, back + side, back - side, tip]), Color.BLACK, 2.0)
|
|
|
|
|
|
## GD-Sync's ping (it sends a message and waits for the answer).
|
|
func _measure_ping() -> void:
|
|
_pinging = true
|
|
var p: float = await lobby.gdsync.get_client_ping(lobby.other_id)
|
|
_ping = p
|
|
_pinging = false
|
|
|
|
|
|
func _label(font_size: int) -> Label:
|
|
var l := Label.new()
|
|
Sketch.style(l, font_size + 2, Sketch.TEXT, 6)
|
|
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
l.visible = false
|
|
add_child(l)
|
|
return l
|