feat: Room.dark + Item.light flags and Light helper
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package thb.jeanluc.adventure.game;
|
||||
|
||||
import thb.jeanluc.adventure.model.Room;
|
||||
import thb.jeanluc.adventure.model.item.Item;
|
||||
import thb.jeanluc.adventure.model.item.SwitchableItem;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/** Decides whether a room is lit, given dark rooms and active light sources. */
|
||||
public final class Light {
|
||||
|
||||
private Light() {
|
||||
}
|
||||
|
||||
/** A room is lit if it is not dark, or an active light source is carried or present. */
|
||||
public static boolean isLit(GameContext ctx, Room room) {
|
||||
if (!room.isDark()) {
|
||||
return true;
|
||||
}
|
||||
return hasActiveLight(ctx.getPlayer().getInventory().values())
|
||||
|| hasActiveLight(room.getItems().values());
|
||||
}
|
||||
|
||||
/** True if the player carries an active light source (for the HUD). */
|
||||
public static boolean carryingLight(GameContext ctx) {
|
||||
return hasActiveLight(ctx.getPlayer().getInventory().values());
|
||||
}
|
||||
|
||||
private static boolean hasActiveLight(Collection<Item> items) {
|
||||
for (Item it : items) {
|
||||
if (isActiveLight(it)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isActiveLight(Item it) {
|
||||
if (!it.isLight()) {
|
||||
return false;
|
||||
}
|
||||
return !(it instanceof SwitchableItem s) || s.isOn();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user