@@ -3,10 +3,9 @@ package game
33import (
44 "bufio"
55 "encoding/csv"
6+ "io/fs"
67 "math"
78 "math/rand"
8- "os"
9- "path/filepath"
109 "strconv"
1110 "strings"
1211 "time"
@@ -20,17 +19,25 @@ type Game struct {
2019 CurrentLevel * Level
2120}
2221
23- // NewGame needs to know how many channels to take in
24- func NewGame (numWindows int ) * Game {
22+ // NewGame needs to know how many channels to take in.
23+ // An optional fs.FS can be passed for testing; if nil, the embedded AssetsFS is used.
24+ func NewGame (numWindows int , filesystems ... fs.FS ) * Game {
25+ var assetFS fs.FS
26+ if len (filesystems ) > 0 && filesystems [0 ] != nil {
27+ assetFS = filesystems [0 ]
28+ } else {
29+ assetFS = AssetsFS
30+ }
31+
2532 levelChans := make ([]chan * Level , numWindows ) // 1 level channel for each window
2633 for i := range levelChans {
2734 levelChans [i ] = make (chan * Level )
2835 }
2936 inputChan := make (chan * Input )
30- levels := loadLevels ()
37+ levels := loadLevels (assetFS )
3138
3239 game := & Game {levelChans , inputChan , levels , nil }
33- game .loadWorldFile () // Load world file
40+ game .loadWorldFile (assetFS ) // Load world file
3441 game .CurrentLevel .lineOfSight () // Draw visible tiles without moving
3542
3643 return game
@@ -392,11 +399,12 @@ func (level *Level) bresenham(start Pos, end Pos) {
392399 }
393400}
394401
395- func (game * Game ) loadWorldFile () {
396- file , err := os .Open ("game/ maps/world.txt" )
402+ func (game * Game ) loadWorldFile (assetFS fs. FS ) {
403+ file , err := assetFS .Open ("maps/world.txt" )
397404 if err != nil {
398405 panic (err )
399406 }
407+ defer file .Close ()
400408 csvReader := csv .NewReader (file )
401409 csvReader .FieldsPerRecord = - 1 // Don't enforce each row to have same num columns
402410 csvReader .TrimLeadingSpace = true
@@ -447,7 +455,7 @@ func (game *Game) loadWorldFile() {
447455}
448456
449457// loadLevels opens and prints a map
450- func loadLevels () map [string ]* Level {
458+ func loadLevels (assetFS fs. FS ) map [string ]* Level {
451459 // Make player
452460 player := & Player {} // Player used to not be a pointer
453461 player .MaxStamina = 2
@@ -462,21 +470,18 @@ func loadLevels() map[string]*Level {
462470 player .PatternRNG = rand .New (rand .NewSource (time .Now ().UnixNano ()))
463471 levels := make (map [string ]* Level )
464472 // Load level
465- filenames , err := filepath . Glob ( "game/ maps/*.map " )
473+ entries , err := fs . ReadDir ( assetFS , " maps" )
466474 if err != nil {
467475 panic (err )
468476 }
469- for _ , filename := range filenames {
470- extIndex := strings .LastIndex (filename , ".map" )
471- lastSlashIndexWindows := strings .LastIndex (filename , "\\ " ) // Windows
472- lastSlashIndexLinux := strings .LastIndex (filename , "/" ) // Linux
473- lastSlashIndex := lastSlashIndexWindows
474- if lastSlashIndexLinux > lastSlashIndexWindows {
475- lastSlashIndex = lastSlashIndexLinux
477+ for _ , entry := range entries {
478+ filename := entry .Name ()
479+ if ! strings .HasSuffix (filename , ".map" ) {
480+ continue
476481 }
477- levelName := filename [ lastSlashIndex + 1 : extIndex ]
482+ levelName := strings . TrimSuffix ( filename , ".map" )
478483 // Open file
479- file , err := os .Open (filename )
484+ file , err := assetFS .Open ("maps/" + filename )
480485 if err != nil {
481486 panic (err )
482487 }
0 commit comments