## Draws what moves in a match: players with what they hold, the sky barge while it ## crosses, items lying in the world, thrown kegs and sea chests. `sync()` matches everything to the ## simulation as it is now, and `animate()` steps the players' figures. ## ## Players are FigureRigs (A2): jointed islanders in oilskins, animated from what the ## simulation replicates about them, with shots, swings and building played from FX ## records through hear(). Held and lying items are the models of A4 (ItemModels): a ## held one sits with its grip in the figure's right hand and points where its player ## looks. The barge and the sea chests are VesselModels, generated in code. Nothing ## here is ever used for collision. class_name EntityView extends Node3D ## Oilskin colours. A player's is picked by entity ID; the local player wears the first. const OILSKINS: Array[Color] = [ Palette.OILSKIN, Palette.OCHRE_RED, Palette.SEA_PAINT, Palette.OILSKIN_MOSS, Palette.OILSKIN_SLATE, Palette.OILSKIN_NAVY, Palette.TANNED_SAIL, ] const SKIN: Color = Palette.SKIN ## Chest instance colours, multiplied into the chest model's own: an opened chest ## shows darker, its paint dulled. const CHEST_SHUT: Color = Color.WHITE const CHEST_OPEN: Color = Palette.OPENED_DULL ## How far apart passengers stand on the barge's deck, in metres. The simulation ## carries them all at one point; this only spreads them out to be seen. const DECK_SPACING: float = 1.3 const DECK_ROWS: int = 4 ## Items lying in the world are drawn this much larger than life, to be seen. const PICKUP_SCALE: float = 1.4 ## The entity drawn in the brightest oilskin, or 0. var local_id: int = 0 ## Where to draw the barge, when its owner knows better than the simulation's tick: a ## network client, which draws between ticks. Vector3.INF leaves it to the tick. var barge_at: Vector3 = Vector3.INF var _sim: Simulation ## Entity ID -> its FigureRig. var _figures: Dictionary = {} ## Whether _process steps the figures. Captures that pose figures exactly turn it off. var animate_live: bool = true var _barge: Node3D ## Items lying in the world: one MultiMeshInstance3D per item model and tier. var _pickups: Node3D var _pickup_fields: Dictionary = {} var _chests: MultiMeshInstance3D func setup(sim: Simulation) -> void: _sim = sim _barge = _build_barge() _barge.visible = false add_child(_barge) _pickups = Node3D.new() _pickups.name = "Pickups" add_child(_pickups) _chests = _chest_field() add_child(_chests) sync() func figure_count() -> int: return _figures.size() ## The figure drawn for a player, or null. func figure_of(entity_id: int) -> FigureRig: return _figures.get(entity_id) func barge() -> Node3D: return _barge ## Items drawn lying in the world. func pickup_count() -> int: var total: int = 0 for field: MultiMeshInstance3D in _pickup_fields.values(): total += field.multimesh.instance_count return total ## The catalog code of the item a player holds: -1 for the boathook. A full copy of ## the player (the server's, or the client's own) knows its slots; others carry the ## held code the snapshot gave them. static func held_code(entity: SimEntity) -> int: if entity.slot_items.size() > 0: var slot: int = entity.selected - 1 return entity.slot_items[slot] if slot >= 0 and slot < entity.slot_items.size() else -1 return entity.item static func held_tier(entity: SimEntity) -> int: var slot: int = entity.selected - 1 if slot >= 0 and slot < entity.slot_tiers.size(): return entity.slot_tiers[slot] return 1 func sync() -> void: if _sim == null: return var drop: Drop = _sim.drop var crossing: bool = drop != null and not drop.finished var heading: float = 0.0 if drop != null: var along: Vector3 = drop.finish - drop.start heading = atan2(along.x, along.z) _barge.visible = crossing if crossing: _barge.position = barge_at if barge_at != Vector3.INF else drop.barge_position(_sim.tick) _barge.rotation.y = heading var seen: Dictionary = {} var aboard: int = 0 for entity: SimEntity in _sim.entities.entities(): if entity.archetype != SimEntity.ARCHETYPE_PLAYER: continue seen[entity.id] = true var figure: FigureRig = _figures.get(entity.id) if figure == null: figure = _build_figure(entity.id) _figures[entity.id] = figure add_child(figure) figure.drive(entity, 0.0) figure.visible = not entity.eliminated figure.position = entity.position figure.rotation.y = float(entity.yaw) / PlayerCommand.YAW_STEPS * TAU if entity.drop_state == Drop.ABOARD: var column: int = aboard / DECK_ROWS var row: int = aboard % DECK_ROWS var offset: Vector3 = Vector3((row - (DECK_ROWS - 1) * 0.5) * DECK_SPACING, 0.0, (2.5 - column) * DECK_SPACING) figure.position += offset.rotated(Vector3.UP, heading) figure.rotation.y = heading aboard += 1 _hold(figure, entity) for entity_id: int in _figures.keys(): if not seen.has(entity_id): _figures[entity_id].queue_free() _figures.erase(entity_id) _sync_pickups() var chests: MultiMesh = _chests.multimesh chests.instance_count = _sim.loot.chest_positions.size() for index: int in _sim.loot.chest_positions.size(): chests.set_instance_transform(index, Transform3D(Basis.IDENTITY, _sim.loot.chest_positions[index])) chests.set_instance_color(index, CHEST_OPEN if _sim.loot.chest_opened[index] != 0 else CHEST_SHUT) func _process(delta: float) -> void: sync() if animate_live: animate(delta) ## Drives every figure from its player's state and steps its animation by `delta`. func animate(delta: float) -> void: if _sim == null: return for entity_id: int in _figures: var entity: SimEntity = _sim.entities.find(entity_id) var figure: FigureRig = _figures[entity_id] if entity == null or not figure.visible: continue figure.drive(entity, delta) figure.advance(delta) ## Plays the actions FX records show (shots, swings, pieces placed) on the figures of ## the players who made them. func hear(records: Array[Dictionary]) -> void: for record: Dictionary in records: var figure: FigureRig = _figures.get(int(record.get("player", 0))) if figure != null and figure.visible: figure.hear(record) ## Puts the held item's model in the figure's hand; the figure points it where the ## player looks. Nothing is held aboard the barge, falling, gliding, swimming or down. func _hold(figure: FigureRig, entity: SimEntity) -> void: var busy: bool = entity.drop_state != Drop.NONE or entity.swimming or entity.eliminated or entity.downed figure.hand().visible = not busy if busy: return var id: String = ItemModels.id_of(held_code(entity)) var mesh: ArrayMesh = ItemModels.model(id, held_tier(entity)) if figure.held().mesh != mesh: figure.held().mesh = mesh ## Items lying in the world, and thrown charges (B8) in flight or waiting to blow, ## which are drawn with the same models. func _sync_pickups() -> void: var groups: Dictionary = {} var shown: Array[int] = [] shown.append_array(_sim.pickups) for projectile_id: int in _sim.projectiles: var projectile: SimEntity = _sim.entities.find(projectile_id) if projectile != null and Explosives.is_charge(ItemCatalog.get_def(projectile.item)): shown.append(projectile_id) for entity_id: int in shown: var pickup: SimEntity = _sim.entities.find(entity_id) if pickup == null: continue var id: String = ItemModels.id_of(pickup.item) var key: String = "%s@%d" % [id, ItemModels.shown_tier(id, pickup.item_tier)] if not groups.has(key): groups[key] = [] groups[key].append(pickup) for key: String in _pickup_fields.keys(): if not groups.has(key): _pickup_fields[key].multimesh.instance_count = 0 for key: String in groups: var field: MultiMeshInstance3D = _pickup_fields.get(key) if field == null: field = _item_field(key) _pickup_fields[key] = field _pickups.add_child(field) var lying: Array = groups[key] var lift: float = pickup_lift(field.multimesh.mesh) field.multimesh.instance_count = lying.size() for index: int in lying.size(): var pickup: SimEntity = lying[index] # Laid on their side, turned by ID so a pile doesn't line up. var basis: Basis = Basis(Vector3.UP, 0.9 * pickup.id).scaled(Vector3.ONE * PICKUP_SCALE) field.multimesh.set_instance_transform(index, Transform3D(basis, pickup.position + Vector3(0.0, lift, 0.0))) ## How far above the spot an item lying there is placed, so the model's lowest point, ## not its grip, rests on the ground. static func pickup_lift(mesh: Mesh) -> float: return 0.02 - mesh.get_aabb().position.y * PICKUP_SCALE func _item_field(key: String) -> MultiMeshInstance3D: var multimesh: MultiMesh = MultiMesh.new() multimesh.transform_format = MultiMesh.TRANSFORM_3D var mesh: ArrayMesh = ItemModels.model(key.get_slice("@", 0), key.get_slice("@", 1).to_int()) if mesh == null: # Every catalogue item has a model (test_item_models); this is a bug. push_error("EntityView: no model for item %s" % key) mesh = ArrayMesh.new() multimesh.mesh = mesh var field: MultiMeshInstance3D = MultiMeshInstance3D.new() field.name = key.replace("@", "_") field.multimesh = multimesh field.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF return field func _build_figure(entity_id: int) -> FigureRig: var coat: Color = OILSKINS[0] if entity_id == local_id else OILSKINS[1 + entity_id % (OILSKINS.size() - 1)] var figure: FigureRig = FigureRig.new(coat) figure.name = "player_%d" % entity_id return figure ## The barge (VesselModels): a hull hung under a canvas envelope. Its deck is at the ## origin, where the simulation carries the passengers. func _build_barge() -> Node3D: var barge: MeshInstance3D = MeshInstance3D.new() barge.name = "Barge" barge.mesh = VesselModels.barge() return barge func _chest_field() -> MultiMeshInstance3D: var multimesh: MultiMesh = MultiMesh.new() multimesh.transform_format = MultiMesh.TRANSFORM_3D multimesh.use_colors = true multimesh.mesh = VesselModels.chest() var instance: MultiMeshInstance3D = MultiMeshInstance3D.new() instance.name = "Chests" instance.multimesh = multimesh instance.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF return instance