## The client's picture of one match: sky, sun, terrain, sea, points of interest, props, ## built pieces and everything that moves, all drawn from one Simulation, with the ## third-person camera on a player's shoulder. It only reads the simulation. ## ## A match gets a new Simulation, so it gets a new MatchView. In play the simulation is ## the client's own copy of the world, which ClientMirror fills with what the server ## has sent; captures and tests also point it at a server's or a replay's. class_name MatchView extends Node3D var sim: Simulation var terrain: TerrainView var water: WaterView var pois: PoiView var props: PropView var structures: StructureView var entities: EntityView var camera: Camera3D var rig: CameraRig = CameraRig.new() ## The woodcut print pass over the camera (Look.add_print_pass). var print_pass: MeshInstance3D var sun: DirectionalLight3D ## The sky, light, haze, rain and murk of the moment (A6). var weather: WeatherView ## Flashes, tracers, dust, blasts and spray (A7). var effects: EffectsView ## Applies the player's graphics settings: the sun's shadows and the print pass. func apply_settings(settings: GameSettings) -> void: var distance: float = settings.shadow_distance() if sun != null: sun.shadow_enabled = distance > 0.0 sun.directional_shadow_max_distance = maxf(distance, 1.0) var splits: Dictionary = {1: DirectionalLight3D.SHADOW_ORTHOGONAL, 2: DirectionalLight3D.SHADOW_PARALLEL_2_SPLITS, 4: DirectionalLight3D.SHADOW_PARALLEL_4_SPLITS} sun.directional_shadow_mode = splits.get(settings.shadow_splits(), DirectionalLight3D.SHADOW_PARALLEL_4_SPLITS) if print_pass != null: print_pass.visible = settings.print_pass if effects != null: effects.budget = settings.particle_budget() ## Builds the scene for `simulation`. `local_id` is the player this client plays, or 0. func setup(simulation: Simulation, local_id: int = 0) -> void: sim = simulation var world: WorldEnvironment = WorldEnvironment.new() world.environment = Look.environment() add_child(world) sun = Look.sun() add_child(sun) camera = Camera3D.new() camera.near = CameraRig.NEAR camera.far = 2500.0 camera.fov = CameraRig.FOV add_child(camera) camera.make_current() print_pass = Look.add_print_pass(camera) if sim.island != null: terrain = TerrainView.new() terrain.setup(sim.island) add_child(terrain) water = WaterView.new() add_child(water) water.set_ground(sim.ground) weather = WeatherView.new() weather.name = "Weather" add_child(weather) weather.setup(world.environment, sun, water) pois = PoiView.new() pois.setup(sim.pois) add_child(pois) props = PropView.new() props.setup(sim.props) add_child(props) structures = StructureView.new() structures.setup(sim.structures) add_child(structures) entities = EntityView.new() entities.local_id = local_id entities.setup(sim) add_child(entities) effects = EffectsView.new() effects.muzzle_of = muzzle_of add_child(effects) sync() ## Matches everything drawn to the simulation as it is now. func sync() -> void: water.follow(sim) weather.apply(Weather.at(sim.tide, sim.tick), camera.transform.origin) Look.set_sea_level(print_pass, water.level) props.sync() structures.sync() entities.sync() effects.sync(sim, camera.transform.origin) ## Where the weapon in a player's drawn hands has its muzzle (its model's +Z end), or ## Vector3.INF when they have no figure showing a held item. func muzzle_of(entity_id: int) -> Vector3: var figure: FigureRig = entities.figure_of(entity_id) if entities != null else null if figure == null or not figure.visible or not figure.is_inside_tree(): return Vector3.INF var held: MeshInstance3D = figure.held() if held == null or held.mesh == null or not held.is_visible_in_tree(): return Vector3.INF var box: AABB = held.mesh.get_aabb() return held.global_transform * Vector3(box.get_center().x, box.get_center().y, box.end.z) ## Puts the camera on a player's shoulder, looking where they look. `pitch` replaces ## their own pitch when given, in radians, for views such as looking down from a glide, ## and `look_yaw` their yaw, for the local player, whose look is newer than any command. func follow(entity_id: int, pitch: float = NAN, look_yaw: float = NAN) -> bool: var player: SimEntity = sim.entities.find(entity_id) if player == null: return false var yaw: float = look_yaw if not is_nan(look_yaw) else float(player.yaw) / PlayerCommand.YAW_STEPS * TAU var look_pitch: float = pitch if not is_nan(pitch) else float(player.pitch) / PlayerCommand.PITCH_MAX * (PI * 0.5) # Where the figure is drawn: on the barge's deck that is its own place, not the # one point the simulation carries every passenger at. var feet: Vector3 = player.position var figure: Node3D = entities.figure_of(entity_id) if figure != null and player.drop_state == Drop.ABOARD: feet = figure.position camera.transform = rig.solve(feet, yaw, look_pitch, player.crouching, sim.ground) camera.fov = rig.fov() # Pines between the lens and the player are hidden (H11). var height: float = Movement.body_height(player) var lines: Array[Vector3] = [feet + Vector3(0.0, height * 0.6, 0.0), feet + Vector3(0.0, height, 0.0)] props.clear_view(sim.ground, camera.transform.origin, lines) _focus() return true ## Frames a player from outside: the camera stands `distance` metres off at `height` ## above their feet and looks at them, from the first of eight directions that has a ## clear line and no prop around the lens. With `beyond` given, it starts on the far ## side of the player from that point, so the point is in the picture behind them. func frame(entity_id: int, distance: float = 6.0, height: float = 2.6, beyond: Vector3 = Vector3.INF) -> bool: var player: SimEntity = sim.entities.find(entity_id) if player == null: return false var chest: Vector3 = player.position + Vector3(0.0, 1.2, 0.0) var first: float = float(player.yaw) / PlayerCommand.YAW_STEPS * TAU + PI if beyond != Vector3.INF: first = atan2(player.position.x - beyond.x, player.position.z - beyond.z) var eye: Vector3 = chest for turn: int in 8: # Straight on first, then alternately to each side. var angle: float = first + (turn + 1) / 2 * (PI / 4.0) * (1.0 if turn % 2 == 1 else -1.0) eye = player.position + Vector3(sin(angle) * distance, height, cos(angle) * distance) eye.y = maxf(eye.y, sim.ground.terrain_height(eye.x, eye.z) + 1.0) var feet: Vector3 = Vector3(eye.x, sim.ground.terrain_height(eye.x, eye.z), eye.z) if sim.ground.raycast(chest, eye) >= 1.0 and sim.props.near(feet, 5.0).is_empty(): break look_from(eye, chest if beyond == Vector3.INF else chest.lerp(beyond, 0.35)) return true ## Puts the camera at `eye`, looking at `target`. func look_from(eye: Vector3, target: Vector3) -> void: camera.transform = Transform3D(Basis.IDENTITY, eye).looking_at(target, Vector3.UP) camera.fov = CameraRig.FOV var none: Array[Vector3] = [] props.clear_view(sim.ground, eye, none) _focus() ## The whole island from high over its south-west, as the lobby shows it. func overview() -> void: var size: float = Tuning.WORLD_SIZE_METRES look_from(Vector3(size * 0.2, 300.0, size * 0.92), Vector3(size * 0.5, 0.0, size * 0.47)) ## Builds every terrain chunk for the current camera at once. For loading and captures. func settle() -> void: if terrain != null: terrain.build_all_now() func _focus() -> void: if terrain != null: terrain.focus = camera.position