class_name FarmProjectiles extends Node3D ## Fixed-capacity pooled shots with shared instanced visuals, forked from the prototype's ## ProjectileSystem for the farm (GameConfig, EnemySystem): the corn shooter's kernels and the ## automated defenses' shots. Shots are zero-width rays drawn as a corn kernel streaking ## horizontally at combat height. Each physics tick a shot's whole ## segment is tested against the farm's collision (one physics ray on the world layer) and ## against enemy hit proxies (EnemySystem.sweep_hit); the nearest hit wins, so a building ## shields a thief behind it. No piercing: a hit damages once and frees the shot. Shots also ## expire, and leave when they fly out of the farm. Exhaustion skips the shot and is counted; ## the pool never grows. const WORLD_LAYER := 1 ## A shot struck an enemy (its type) or the farm (enemy_type -1), for sound and effects. signal hit(at: Vector3, enemy_type: int) var capacity: int = 0 var active_count: int = 0 # Diagnostics. var exhausted: int = 0 var fired: int = 0 var hits_enemy: int = 0 var hits_world: int = 0 var blocked_at_muzzle: int = 0 var _px := PackedFloat32Array() var _pz := PackedFloat32Array() var _dx := PackedFloat32Array() var _dz := PackedFloat32Array() var _life := PackedFloat32Array() ## Per-shot speed (m/s): cfg.projectile_speed unless fire() was given its own (Task #7's Potato ## Cannon and Shurikens fly at their own speeds). var _speed := PackedFloat32Array() var _damage := PackedInt32Array() ## The avatar slot that fired each shot (EnemySystem.attacker when it was fired; -1 for the ## automated defenses'), credited with what it kills (co-op Alien Goop). var _owner := PackedInt32Array() var _cfg: GameConfig var _enemies: EnemySystem var _bounds: Rect2 var _renderer: HordeRenderer var _ray := PhysicsRayQueryParameters3D.new() func setup(cfg: GameConfig, enemies: EnemySystem, bounds: Rect2, shooter: RID) -> void: _cfg = cfg _enemies = enemies _bounds = bounds capacity = cfg.projectile_capacity for a: PackedFloat32Array in [_px, _pz, _dx, _dz, _life, _speed]: a.resize(capacity) a.fill(0.0) _damage.resize(capacity) _owner.resize(capacity) _ray.collision_mask = WORLD_LAYER _ray.collide_with_areas = false _ray.collide_with_bodies = true _ray.hit_from_inside = true _ray.exclude = [shooter] var mesh := BoxMesh.new() mesh.size = Vector3(0.13, 0.11, 0.34) var mat := StandardMaterial3D.new() mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED mat.albedo_color = Color(1.0, 0.82, 0.18) _renderer = HordeRenderer.new() _renderer.name = "ShotRenderer" add_child(_renderer) var aabb := AABB(Vector3(bounds.position.x, 0.0, bounds.position.y), Vector3(bounds.size.x, 4.0, bounds.size.y)) _renderer.setup(mesh, mat, capacity, cfg.combat_height, aabb) func clear() -> void: active_count = 0 _renderer.clear() _renderer.commit(0) ## Fires from the weapon origin (scarecrow center at combat height) through the muzzle. The ## origin -> muzzle segment is tested first, so a barrel poking through a wall resolves there ## instead of spawning the shot on the far side. Returns false if the shot was skipped. ## speed/lifetime (<= 0: cfg.projectile_speed / cfg.projectile_lifetime) let a weapon's shot fly at ## its own pace (Task #7: the Potato Cannon's slow heavy shot and Shurikens), with every other ## caller unchanged. func fire(origin: Vector3, muzzle: Vector3, dir: Vector3, damage: int = 1, speed: float = -1.0, lifetime: float = -1.0) -> bool: if active_count >= capacity: exhausted += 1 return false fired += 1 var by := _enemies.attacker if _resolve_segment(origin.x, origin.z, muzzle.x, muzzle.z, false, damage, by): blocked_at_muzzle += 1 return false var d := active_count active_count += 1 _px[d] = muzzle.x _pz[d] = muzzle.z _dx[d] = dir.x _dz[d] = dir.z _life[d] = lifetime if lifetime > 0.0 else _cfg.projectile_lifetime _damage[d] = damage _owner[d] = by _speed[d] = speed if speed > 0.0 else _cfg.projectile_speed _renderer.write_new(d, muzzle.x, muzzle.z, dir.x, dir.z, 0.0) return true func step(dt: float) -> void: var i := 0 while i < active_count: var step_len := _speed[i] * dt var x0 := _px[i] var z0 := _pz[i] var x1 := x0 + _dx[i] * step_len var z1 := z0 + _dz[i] * step_len if _resolve_segment(x0, z0, x1, z1, true, _damage[i], _owner[i]): _release(i) continue _px[i] = x1 _pz[i] = z1 _life[i] -= dt if _life[i] <= 0.0 or not _bounds.has_point(Vector2(x1, z1)): _release(i) continue i += 1 ## Co-op host: every shot in flight as [x, z, dx, dz] (at most max_shots). func replica_record(max_shots: int) -> PackedFloat32Array: var out := PackedFloat32Array() for i in mini(active_count, max_shots): out.append_array([_px[i], _pz[i], _dx[i], _dz[i]]) return out ## Co-op partner: the host's shots, which then fly on here until the next update. func apply_replica(rec: PackedFloat32Array) -> void: active_count = mini(rec.size() / 4, capacity) for i in active_count: _px[i] = rec[i * 4] _pz[i] = rec[i * 4 + 1] _dx[i] = rec[i * 4 + 2] _dz[i] = rec[i * 4 + 3] _life[i] = _cfg.projectile_lifetime func advance_replica(dt: float) -> void: var step_len := _cfg.projectile_speed * dt for i in active_count: _px[i] += _dx[i] * step_len _pz[i] += _dz[i] * step_len func commit_render() -> void: _renderer.write_poses(active_count, _px, _pz, _dx, _dz) _renderer.commit(active_count) ## Resolves the nearest world/enemy hit along a segment. Returns true if something was hit ## (damage is applied for enemy hits, credited to the shooter's avatar slot by). use_motion ## selects the relative-motion enemy test. func _resolve_segment(x0: float, z0: float, x1: float, z1: float, use_motion: bool, damage: int, by: int) -> bool: var h := _cfg.combat_height var world_t := 2.0 _ray.from = Vector3(x0, h, z0) _ray.to = Vector3(x1, h, z1) var res := get_world_3d().direct_space_state.intersect_ray(_ray) if not res.is_empty(): var seg := Vector2(x1 - x0, z1 - z0).length() var p: Vector3 = res["position"] world_t = Vector2(p.x - x0, p.z - z0).length() / maxf(seg, 1e-6) var e := _enemies.sweep_hit(x0, z0, x1, z1, use_motion) if e >= 0 and _enemies.hit_t <= world_t: var t := _enemies.type[e] _enemies.apply_hit(e, damage, by) hits_enemy += 1 hit.emit(Vector3(lerpf(x0, x1, _enemies.hit_t), h, lerpf(z0, z1, _enemies.hit_t)), t) return true if world_t <= 1.0: hits_world += 1 hit.emit(Vector3(lerpf(x0, x1, world_t), h, lerpf(z0, z1, world_t)), -1) return true return false func _release(i: int) -> void: var last := active_count - 1 if i != last: _px[i] = _px[last] _pz[i] = _pz[last] _dx[i] = _dx[last] _dz[i] = _dz[last] _life[i] = _life[last] _damage[i] = _damage[last] _speed[i] = _speed[last] _owner[i] = _owner[last] _renderer.move_slot(last, i) active_count -= 1