Week 27 packet
Student instructions, tasks, and checks. The answer key is not in this packet. Links stay as text. This page does not embed a clip or run a file.
# Week 27 — Combine agents and generated spaces carefully
## VGC-L131 — Reserve space for both player and guard
**Objective:** Design generation constraints around the actual movement model.
**Time:** 55 minutes
### Instruction
A direct-steering guard cannot navigate arbitrary mazes. Reserve an open patrol lane separate from the required player route, and prevent random walls from appearing there. Keep the guard’s chase area in an open region or constrain the game so walls cannot trap it. Document this limitation rather than naming it navigation AI. A procedural generator should satisfy the needs of its actors; adding random geometry without considering movement is not a complete system design.
### Worked example
A procedural bug report might say seed 17, build alpha2, start at entrance, move right, expected clear corridor, actual blocking wall. The seed makes reproduction practical.
Scaffold C-GRID — A deterministic grid with a guaranteed clear route
Setup: Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer.
```gdscript
extends Node2D
const COLS: int = 8
const ROWS: int = 6
const CELL: int = 40
var cells: Array = []
func _ready() -> void:
var rng := RandomNumberGenerator.new()
rng.seed = 2468
for y in range(ROWS):
var row: Array[int] = []
for x in range(COLS):
var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25
row.append(1 if blocked else 0)
cells.append(row)
queue_redraw()
func _draw() -> void:
for y in range(cells.size()):
for x in range(cells[y].size()):
var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE
draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color)
```
Scaffold C-GUARD-AI — A readable patrol/chase/return state machine
Setup: Create CharacterBody2D Guard with visible circle or polygon and matching collider. Layer 4 mask 2 for walls. Attach script and assign target to your Player instance in Inspector. Level is one open rectangle; this is steering, not navigation around walls. Damage remains a separate sensor/manager event. Draw detection radii on a design diagram before tuning values.
```gdscript
extends CharacterBody2D
enum Mode { PATROL, CHASE, RETURN }
@export var target: Node2D
@export var speed: float = 70.0
var mode: Mode = Mode.PATROL
var home: Vector2
var patrol_target: Vector2
func _ready() -> void:
home = global_position
patrol_target = home + Vector2(80, 0)
func _physics_process(delta: float) -> void:
if not is_instance_valid(target):
velocity = Vector2.ZERO
return
var distance: float = global_position.distance_to(target.global_position)
if mode != Mode.CHASE and distance < 120.0:
mode = Mode.CHASE
elif mode == Mode.CHASE and distance > 180.0:
mode = Mode.RETURN
var destination: Vector2
if mode == Mode.CHASE:
destination = target.global_position
elif mode == Mode.RETURN:
destination = home
if global_position.distance_to(home) <= maxf(3.0, speed * delta):
mode = Mode.PATROL
else:
destination = patrol_target
if global_position.distance_to(patrol_target) <= maxf(3.0, speed * delta):
patrol_target = home if patrol_target != home else home + Vector2(80, 0)
var offset := destination - global_position
velocity = offset.normalized() * minf(speed, offset.length() / maxf(delta, 0.001))
move_and_slide()
```
### Assignment
1. Mark player route, guard lane, spawn and goal as reserved cells.
2. Update generation conditions and test ten seeds.
### Acceptance checks
- All required actor spaces remain open.
- Guard patrol endpoints are not inside walls.
**Submit:** Reserved-space map, generator update and ten-seed checks.
**Question:** Why is an open reserved lane appropriate for this guard?
**Support:** Keep guard patrol in a reserved open lane and the player route guaranteed. Avoid adding maze pathfinding to this project. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
**Optional extension:** Add one optional guard route only if the generator can prove its lane remains clear. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
**Optional free reading:** [Your first 2D game](https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html)
---
## VGC-L132 — Bound enemy and update costs
**Objective:** Prevent runaway creation and unnecessary per-frame work.
**Time:** 55 minutes
### Instruction
Performance starts with sensible limits. Use a fixed small guard count, avoid spawning in _process, and cache stable references with @onready or exported fields. Do not search the entire scene tree every frame for a player that already has a reference. Measure before claiming an optimization helped. For this small project record object counts and use Godot’s monitors while running the same seed. Optimize the actual repeated operation, not a harmless line chosen because it looks complicated.
### Worked example
A procedural bug report might say seed 17, build alpha2, start at entrance, move right, expected clear corridor, actual blocking wall. The seed makes reproduction practical.
Scaffold C-TIMER — Timer-based spawning with a hard cap
Setup: World Node2D root contains Node2D Hazards and Timer SpawnTimer: wait_time 1.2, autostart On, one_shot Off. Hazard scene root Area2D has a CircleShape2D radius 12 and C-HAZARD. Attach script to World and drag Hazard.tscn into hazard_scene Inspector field. Set viewport 640x360. Name Hazards group not needed. This script is a separate lab; merge its _ready into an existing manager rather than declaring a second _ready.
```gdscript
extends Node2D
@export var hazard_scene: PackedScene
@export var spawn_limit: int = 12
func _ready() -> void:
$SpawnTimer.timeout.connect(_spawn)
func _spawn() -> void:
if hazard_scene == null or $Hazards.get_child_count() >= spawn_limit:
return
var hazard := hazard_scene.instantiate() as Area2D
$Hazards.add_child(hazard)
hazard.position = Vector2(randf_range(20.0, 620.0), -20.0)
```
Scaffold C-GUARD-AI — A readable patrol/chase/return state machine
Setup: Create CharacterBody2D Guard with visible circle or polygon and matching collider. Layer 4 mask 2 for walls. Attach script and assign target to your Player instance in Inspector. Level is one open rectangle; this is steering, not navigation around walls. Damage remains a separate sensor/manager event. Draw detection radii on a design diagram before tuning values.
```gdscript
extends CharacterBody2D
enum Mode { PATROL, CHASE, RETURN }
@export var target: Node2D
@export var speed: float = 70.0
var mode: Mode = Mode.PATROL
var home: Vector2
var patrol_target: Vector2
func _ready() -> void:
home = global_position
patrol_target = home + Vector2(80, 0)
func _physics_process(delta: float) -> void:
if not is_instance_valid(target):
velocity = Vector2.ZERO
return
var distance: float = global_position.distance_to(target.global_position)
if mode != Mode.CHASE and distance < 120.0:
mode = Mode.CHASE
elif mode == Mode.CHASE and distance > 180.0:
mode = Mode.RETURN
var destination: Vector2
if mode == Mode.CHASE:
destination = target.global_position
elif mode == Mode.RETURN:
destination = home
if global_position.distance_to(home) <= maxf(3.0, speed * delta):
mode = Mode.PATROL
else:
destination = patrol_target
if global_position.distance_to(patrol_target) <= maxf(3.0, speed * delta):
patrol_target = home if patrol_target != home else home + Vector2(80, 0)
var offset := destination - global_position
velocity = offset.normalized() * minf(speed, offset.length() / maxf(delta, 0.001))
move_and_slide()
```
### Assignment
1. Set explicit caps for guards and generated walls.
2. Record object/node counts during a two-minute run and identify one avoidable repeated lookup.
### Acceptance checks
- Counts stay bounded.
- Any changed lookup preserves behavior on the same seed.
**Submit:** Performance bounds table and before/after observation.
**Question:** Why is a lower line count not proof of faster code?
**Support:** Keep guard patrol in a reserved open lane and the player route guaranteed. Avoid adding maze pathfinding to this project. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
**Optional extension:** Add one optional guard route only if the generator can prove its lane remains clear. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
**Optional free reading:** [Your first 2D game](https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html)
---
## VGC-L133 — Test procedural edge seeds and state transitions
**Objective:** Combine invariant tests with human play observations.
**Time:** 55 minutes
### Instruction
Run a fixed seed set including zero, one, 2468 and several others. Check spawn safety, reserved route, wall count, patrol, chase, return and completion. Automated assertions can check data invariants, but a human still needs to inspect readability and fairness. A reachable route may be visually confusing or place the guard too close to the start. Record both structural pass/fail results and observed design issues. Use the exact seed in every bug report.
### Worked example
A procedural bug report might say seed 17, build alpha2, start at entrance, move right, expected clear corridor, actual blocking wall. The seed makes reproduction practical.
Scaffold C-GRID — A deterministic grid with a guaranteed clear route
Setup: Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer.
```gdscript
extends Node2D
const COLS: int = 8
const ROWS: int = 6
const CELL: int = 40
var cells: Array = []
func _ready() -> void:
var rng := RandomNumberGenerator.new()
rng.seed = 2468
for y in range(ROWS):
var row: Array[int] = []
for x in range(COLS):
var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25
row.append(1 if blocked else 0)
cells.append(row)
queue_redraw()
func _draw() -> void:
for y in range(cells.size()):
for x in range(cells[y].size()):
var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE
draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color)
```
Scaffold C-GUARD-AI — A readable patrol/chase/return state machine
Setup: Create CharacterBody2D Guard with visible circle or polygon and matching collider. Layer 4 mask 2 for walls. Attach script and assign target to your Player instance in Inspector. Level is one open rectangle; this is steering, not navigation around walls. Damage remains a separate sensor/manager event. Draw detection radii on a design diagram before tuning values.
```gdscript
extends CharacterBody2D
enum Mode { PATROL, CHASE, RETURN }
@export var target: Node2D
@export var speed: float = 70.0
var mode: Mode = Mode.PATROL
var home: Vector2
var patrol_target: Vector2
func _ready() -> void:
home = global_position
patrol_target = home + Vector2(80, 0)
func _physics_process(delta: float) -> void:
if not is_instance_valid(target):
velocity = Vector2.ZERO
return
var distance: float = global_position.distance_to(target.global_position)
if mode != Mode.CHASE and distance < 120.0:
mode = Mode.CHASE
elif mode == Mode.CHASE and distance > 180.0:
mode = Mode.RETURN
var destination: Vector2
if mode == Mode.CHASE:
destination = target.global_position
elif mode == Mode.RETURN:
destination = home
if global_position.distance_to(home) <= maxf(3.0, speed * delta):
mode = Mode.PATROL
else:
destination = patrol_target
if global_position.distance_to(patrol_target) <= maxf(3.0, speed * delta):
patrol_target = home if patrol_target != home else home + Vector2(80, 0)
var offset := destination - global_position
velocity = offset.normalized() * minf(speed, offset.length() / maxf(delta, 0.001))
move_and_slide()
```
Scaffold C-TESTS — A tiny pure-function test harness
Setup: Attach to Node TestLab. If using command line, create a separate SceneTree harness rather than running this Node script with --script. Run current TestLab scene in editor; assert reports a failed condition in Debugger. No external test framework or package is needed.
```gdscript
extends Node
func bounded_score(old_score: int, change: int) -> int:
return clampi(old_score + change, 0, 999)
func _ready() -> void:
assert(bounded_score(10, 2) == 12)
assert(bounded_score(0, -3) == 0)
assert(bounded_score(998, 9) == 999)
print("3 score checks passed")
```
### Assignment
1. Test ten named seeds with seven checks each.
2. Choose one problematic seed or inject a reserved-cell fault and repair it.
### Acceptance checks
- Every report includes seed and version.
- Structural tests and play observations are kept distinct.
**Submit:** Seventy-check seed matrix and one repaired case.
**Question:** Can a path-existence assertion prove the game is enjoyable or readable?
**Support:** Keep guard patrol in a reserved open lane and the player route guaranteed. Avoid adding maze pathfinding to this project. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
**Optional extension:** Add one optional guard route only if the generator can prove its lane remains clear. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
**Optional free reading:** [Your first 2D game](https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html)
---
## VGC-L134 — Explain the limits of the AI and generator
**Objective:** Make accurate claims about what the systems can do.
**Time:** 55 minutes
### Instruction
Write a technical note for another beginner. Explain the guard’s three states, two distance thresholds and lack of maze navigation. Explain the generator’s seed, reserved path and limited connectivity guarantee. Show one small code excerpt for each claim and one test result supporting it. Naming limitations is part of engineering quality, not an admission of failure. Avoid marketing terms such as “intelligent” or “infinite worlds” when the artifact is a bounded one-room rule system.
### Worked example
A procedural bug report might say seed 17, build alpha2, start at entrance, move right, expected clear corridor, actual blocking wall. The seed makes reproduction practical.
Scaffold C-GRID — A deterministic grid with a guaranteed clear route
Setup: Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer.
```gdscript
extends Node2D
const COLS: int = 8
const ROWS: int = 6
const CELL: int = 40
var cells: Array = []
func _ready() -> void:
var rng := RandomNumberGenerator.new()
rng.seed = 2468
for y in range(ROWS):
var row: Array[int] = []
for x in range(COLS):
var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25
row.append(1 if blocked else 0)
cells.append(row)
queue_redraw()
func _draw() -> void:
for y in range(cells.size()):
for x in range(cells[y].size()):
var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE
draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color)
```
Scaffold C-GUARD-AI — A readable patrol/chase/return state machine
Setup: Create CharacterBody2D Guard with visible circle or polygon and matching collider. Layer 4 mask 2 for walls. Attach script and assign target to your Player instance in Inspector. Level is one open rectangle; this is steering, not navigation around walls. Damage remains a separate sensor/manager event. Draw detection radii on a design diagram before tuning values.
```gdscript
extends CharacterBody2D
enum Mode { PATROL, CHASE, RETURN }
@export var target: Node2D
@export var speed: float = 70.0
var mode: Mode = Mode.PATROL
var home: Vector2
var patrol_target: Vector2
func _ready() -> void:
home = global_position
patrol_target = home + Vector2(80, 0)
func _physics_process(delta: float) -> void:
if not is_instance_valid(target):
velocity = Vector2.ZERO
return
var distance: float = global_position.distance_to(target.global_position)
if mode != Mode.CHASE and distance < 120.0:
mode = Mode.CHASE
elif mode == Mode.CHASE and distance > 180.0:
mode = Mode.RETURN
var destination: Vector2
if mode == Mode.CHASE:
destination = target.global_position
elif mode == Mode.RETURN:
destination = home
if global_position.distance_to(home) <= maxf(3.0, speed * delta):
mode = Mode.PATROL
else:
destination = patrol_target
if global_position.distance_to(patrol_target) <= maxf(3.0, speed * delta):
patrol_target = home if patrol_target != home else home + Vector2(80, 0)
var offset := destination - global_position
velocity = offset.normalized() * minf(speed, offset.length() / maxf(delta, 0.001))
move_and_slide()
```
### Assignment
1. Write a 200-word note or private narrated diagram with code references.
2. Include two supported capabilities and two limitations.
### Acceptance checks
- Claims match the actual implementation.
- At least one limitation is demonstrated by a test or diagram.
**Submit:** AI/generation explanation and supporting evidence.
**Question:** What does the reserved route prove, and what does it not prove?
**Support:** Keep guard patrol in a reserved open lane and the player route guaranteed. Avoid adding maze pathfinding to this project. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
**Optional extension:** Add one optional guard route only if the generator can prove its lane remains clear. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
**Optional free reading:** [Your first 2D game](https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html)
---
## VGC-L135 — Submit Garden Patrol
**Objective:** Deliver a bounded playable AI/procedural game.
**Time:** 55 minutes
### Instruction
The finished game needs one readable patrol/chase/return guard, a seeded generated layout, a guaranteed required route, a clear goal, safe reset and an explicit actor/object cap. Include your state diagram, reserved-cell map, seed test matrix and limitations note. Keep all execution local; no networking or generated personal data is needed. Restore the project and play one complete seeded round before submitting. Optional recordings supplement the code and reproducible seed evidence.
### Worked example
A procedural bug report might say seed 17, build alpha2, start at entrance, move right, expected clear corridor, actual blocking wall. The seed makes reproduction practical.
Scaffold C-GRID — A deterministic grid with a guaranteed clear route
Setup: Attach to Node2D GridLab. This example draws, not physics, so the assignment adds matching wall collision only after validation. Grid is 8 columns by 6 rows at 40 pixels per cell. Row 0 and column 7 are a guaranteed clear route from (0,0) to (7,5). RNG seed is a nonpersonal integer.
```gdscript
extends Node2D
const COLS: int = 8
const ROWS: int = 6
const CELL: int = 40
var cells: Array = []
func _ready() -> void:
var rng := RandomNumberGenerator.new()
rng.seed = 2468
for y in range(ROWS):
var row: Array[int] = []
for x in range(COLS):
var blocked: bool = y != 0 and x != COLS - 1 and rng.randf() < 0.25
row.append(1 if blocked else 0)
cells.append(row)
queue_redraw()
func _draw() -> void:
for y in range(cells.size()):
for x in range(cells[y].size()):
var color := Color.DIM_GRAY if cells[y][x] == 1 else Color.BEIGE
draw_rect(Rect2(x * CELL, y * CELL, CELL - 2, CELL - 2), color)
```
Scaffold C-GUARD-AI — A readable patrol/chase/return state machine
Setup: Create CharacterBody2D Guard with visible circle or polygon and matching collider. Layer 4 mask 2 for walls. Attach script and assign target to your Player instance in Inspector. Level is one open rectangle; this is steering, not navigation around walls. Damage remains a separate sensor/manager event. Draw detection radii on a design diagram before tuning values.
```gdscript
extends CharacterBody2D
enum Mode { PATROL, CHASE, RETURN }
@export var target: Node2D
@export var speed: float = 70.0
var mode: Mode = Mode.PATROL
var home: Vector2
var patrol_target: Vector2
func _ready() -> void:
home = global_position
patrol_target = home + Vector2(80, 0)
func _physics_process(delta: float) -> void:
if not is_instance_valid(target):
velocity = Vector2.ZERO
return
var distance: float = global_position.distance_to(target.global_position)
if mode != Mode.CHASE and distance < 120.0:
mode = Mode.CHASE
elif mode == Mode.CHASE and distance > 180.0:
mode = Mode.RETURN
var destination: Vector2
if mode == Mode.CHASE:
destination = target.global_position
elif mode == Mode.RETURN:
destination = home
if global_position.distance_to(home) <= maxf(3.0, speed * delta):
mode = Mode.PATROL
else:
destination = patrol_target
if global_position.distance_to(patrol_target) <= maxf(3.0, speed * delta):
patrol_target = home if patrol_target != home else home + Vector2(80, 0)
var offset := destination - global_position
velocity = offset.normalized() * minf(speed, offset.length() / maxf(delta, 0.001))
move_and_slide()
```
### Assignment
1. Package source and design/test evidence.
2. Run one full round and regeneration/reset from the restored artifact.
### Acceptance checks
- Teacher can reproduce a named seed.
- Guard state changes and route invariants match the submitted diagrams.
**Submit:** Garden Patrol source artifact, state diagram, seed matrix and readme.
**Question:** Which information is essential to reproduce a procedural gameplay bug?
**Support:** Keep guard patrol in a reserved open lane and the player route guaranteed. Avoid adding maze pathfinding to this project. Start with the first assigned task and re-run its acceptance test before adding the remaining feature.
**Optional extension:** Add one optional guard route only if the generator can prove its lane remains clear. Document one changed parameter, your prediction, and the observed effect; optional, not required for completion.
**Optional free reading:** [Your first 2D game](https://docs.godotengine.org/en/stable/getting_started/first_2d_game/index.html)
---
