class_name AlienBeam extends Node3D ## The Alien Ray's beam (#18): a glowing green ray from the gun's muzzle straight out for length m, ## cut short at the first wall, with a green glow at the muzzle and a flare where a wall stops ## it. A WeaponModels held effect: it sits at the muzzle facing the way the scarecrow's model does ## (-Z), so it follows the model's yaw, which is all the partner's machine knows of the other ## scarecrow's aim. set_lit() shows it and hides it. ## While lit it is laid out again every frame (rebuild): trace() runs the same wall test as the ## damage does (ScarecrowCombat.beam_path), from the scarecrow's center through the muzzle and on, ## so its end is where the damage stops, on either machine (the walls are the same static farm on ## both, so the partner's machine needs nothing sent for it). ## The beam's parts, as shares of the ray's hit width (alien_ray_width): a white-hot core and a ## green glow around it, both unshaded and brighter than white (HDR), so the beam still burns ## through the night look's dimmed picture, and unfogged. const CORE := 0.22 const GLOW := 0.75 const CORE_COLOR := Color(0.85, 2.2, 1.0) const GLOW_COLOR := Color(0.25, 1.6, 0.45, 0.45) ## The glow's width pulses this much (share), this fast (Hz), so the beam hums. const PULSE := 0.18 const PULSE_HZ := 9.0 const LIGHT_COLOR := Color(0.4, 1.0, 0.5) const LIGHT_ENERGY := 1.8 const LIGHT_RANGE := 3.2 const FLARE_SIZE := 0.5 const WORLD_LAYER := 1 ## The ray it was built for (m): how far it reaches past the muzzle, and how wide it hits. var length: float var width: float var lit := false ## The beam as last laid out (world space): from the muzzle to where it ends, and whether a wall ## stopped it (at the barrel: nothing is drawn). var start := Vector3.ZERO var end := Vector3.ZERO var blocked := false var _core: MeshInstance3D var _glow: MeshInstance3D var _flare: MeshInstance3D var _light: OmniLight3D var _ray := PhysicsRayQueryParameters3D.new() var _time := 0.0 func _init(p_length: float = 20.0, p_width: float = 0.5) -> void: length = p_length width = p_width name = "AlienBeam" _ray.collision_mask = WORLD_LAYER _ray.collide_with_areas = false _core = _part("Core", _cylinder(), _material(CORE_COLOR, false)) _glow = _part("Glow", _cylinder(), _material(GLOW_COLOR, true)) var ball := SphereMesh.new() ball.radius = 0.5 ball.height = 1.0 ball.radial_segments = 12 ball.rings = 6 _flare = _part("Flare", ball, _material(GLOW_COLOR, true)) _light = OmniLight3D.new() _light.name = "Glow" _light.light_color = LIGHT_COLOR _light.light_energy = LIGHT_ENERGY _light.omni_range = LIGHT_RANGE _light.shadow_enabled = false add_child(_light) set_lit(false) ## Shows the beam (laid out now and every frame after), or hides it. func set_lit(on: bool) -> void: lit = on _light.visible = on if on and is_inside_tree(): rebuild(global_transform) else: for mi: MeshInstance3D in [_core, _glow, _flare]: mi.visible = false ## Lays the beam out from a transform of this node (its global one, or the interpolated one each ## frame): the muzzle is its origin, the aim its -Z (level), and the scarecrow's center the point ## at the muzzle's height straight above the model's origin. Returns how long it is (m). func rebuild(xform: Transform3D) -> float: var fwd := -xform.basis.z fwd.y = 0.0 fwd = fwd.normalized() if fwd.length() > 1e-4 else Vector3.FORWARD var center := xform * Vector3(-position.x, 0.0, -position.z) # (this node has no turn of its own) var space := get_world_3d().direct_space_state if is_inside_tree() else null var path := trace(space, _ray, center, xform.origin, fwd, length) start = xform.origin end = path[path.size() - 1] blocked = path.size() < 3 or end.distance_to(start) < length - 1e-3 var shown := lit and path.size() == 3 var span := end - start var span_len := span.length() if shown else 0.0 _core.visible = shown and span_len > 1e-3 _glow.visible = _core.visible _flare.visible = _core.visible and blocked if _core.visible: var pulse := 1.0 + PULSE * sin(TAU * PULSE_HZ * _time) _lay(_core, start, fwd, span_len, width * CORE) _lay(_glow, start, fwd, span_len, width * GLOW * pulse) _flare.global_transform = Transform3D(Basis.IDENTITY.scaled(Vector3.ONE * FLARE_SIZE * pulse), end) return span_len ## The beam's path, world space at the gun's height: from center (the scarecrow's) through muzzle, ## then along dir (unit, level) for length m, cut at the first wall (space: the farm's physics, ## query: a ray on the world layer; null space: no walls). [center, muzzle, end], or, when a ## wall stands between the center and the muzzle (a barrel poking through it), [center, the hit]. ## Shared with ScarecrowCombat, so the damage and the beam drawn end at the same wall. static func trace(space: PhysicsDirectSpaceState3D, query: PhysicsRayQueryParameters3D, center: Vector3, muzzle: Vector3, dir: Vector3, p_length: float) -> PackedVector3Array: var path := PackedVector3Array([center]) for to: Vector3 in [muzzle, muzzle + dir * p_length]: if space != null: query.from = path[path.size() - 1] query.to = to var hit := space.intersect_ray(query) if not hit.is_empty(): path.append(hit["position"]) return path path.append(to) return path func _process(delta: float) -> void: if not lit: return _time = fmod(_time + delta, 1.0) rebuild(get_global_transform_interpolated()) ## A part of the beam, placed in world space (top_level) each frame, drawn as it's placed. func _part(part_name: String, mesh: Mesh, mat: Material) -> MeshInstance3D: var mi := MeshInstance3D.new() mi.name = part_name mi.mesh = mesh mi.material_override = mat mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF mi.top_level = true mi.physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF add_child(mi) return mi ## A cylinder 1 m long (along Y) and 1 m across: _lay stretches it. static func _cylinder() -> CylinderMesh: var c := CylinderMesh.new() c.top_radius = 0.5 c.bottom_radius = 0.5 c.height = 1.0 c.radial_segments = 10 c.rings = 1 c.cap_top = false c.cap_bottom = false return c ## Stretches a unit cylinder from `from` along dir (unit, level) for span m, thick m across. static func _lay(mi: MeshInstance3D, from: Vector3, dir: Vector3, span: float, thick: float) -> void: var side := dir.cross(Vector3.UP) mi.global_transform = Transform3D(Basis(side * thick, dir * span, Vector3.UP * thick), from + dir * span * 0.5) static func _material(color: Color, blend: bool) -> StandardMaterial3D: var mat := StandardMaterial3D.new() mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED mat.albedo_color = color mat.emission_enabled = true mat.emission = Color(color.r, color.g, color.b) mat.disable_fog = true mat.cull_mode = BaseMaterial3D.CULL_DISABLED if blend: mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA mat.render_priority = 1 return mat