Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit d104b81

Browse files
authored
Revert "Move UI lib into a separate repo (#64)" (#65)
This reverts commit 14b8b76.
1 parent 14b8b76 commit d104b81

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1684
-81
lines changed

assets/assets.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package assets
2+
3+
import (
4+
"image"
5+
_ "image/jpeg"
6+
_ "image/png"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"candy/audio"
12+
)
13+
14+
var imageExtensions = map[string]struct{}{"jpg": {}, "png": {}}
15+
var audioExtensions = map[string]struct{}{"mp3": {}, "wav": {}}
16+
17+
type Assets struct {
18+
imageMap map[string]image.Image
19+
audioMap map[string]audio.Audio
20+
}
21+
22+
func (a Assets) GetImage(imageName string) image.Image {
23+
return a.imageMap[convertPath(imageName)]
24+
}
25+
26+
func (a Assets) GetAudio(audioName string) audio.Audio {
27+
return a.audioMap[convertPath(audioName)]
28+
}
29+
30+
func convertPath(path string) string {
31+
parts := strings.Split(path, "/")
32+
return filepath.Join(parts...)
33+
}
34+
35+
func LoadAssets(assetRootDir string) (Assets, error) {
36+
imageAssets, err := loadImages(assetRootDir)
37+
if err != nil {
38+
return Assets{}, err
39+
}
40+
audioAssets, err := loadAudios(assetRootDir)
41+
return Assets{
42+
imageMap: imageAssets,
43+
audioMap: audioAssets,
44+
}, err
45+
}
46+
47+
func loadAudios(assetRootDir string) (map[string]audio.Audio, error) {
48+
audioAssets := make(map[string]audio.Audio)
49+
err := lisFiles(assetRootDir, func(path string, ext string, rel string) error {
50+
if _, ok := audioExtensions[ext]; !ok {
51+
return nil
52+
}
53+
file, err := os.Open(path)
54+
if err != nil {
55+
return err
56+
}
57+
bp, err := audio.NewAudio(file, ext)
58+
if err != nil {
59+
return err
60+
}
61+
audioAssets[rel] = bp
62+
return nil
63+
})
64+
return audioAssets, err
65+
}
66+
67+
func loadImages(assetRootDir string) (map[string]image.Image, error) {
68+
imageAssets := make(map[string]image.Image)
69+
err := lisFiles(assetRootDir, func(path string, ext string, rel string) error {
70+
if _, ok := imageExtensions[ext]; !ok {
71+
return nil
72+
}
73+
file, err := os.Open(path)
74+
defer file.Close()
75+
if err != nil {
76+
return err
77+
}
78+
img, _, err := image.Decode(file)
79+
if err != nil {
80+
return err
81+
}
82+
imageAssets[rel] = img
83+
return nil
84+
})
85+
return imageAssets, err
86+
}
87+
88+
func lisFiles(rootDir string, processFile func(path string, ext string, rel string) error) error {
89+
return filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
90+
if info.IsDir() {
91+
return nil
92+
}
93+
parts := strings.Split(info.Name(), ".")
94+
if len(parts) == 0 {
95+
return nil
96+
}
97+
ext := parts[len(parts)-1]
98+
rel, err := filepath.Rel(rootDir, path)
99+
return processFile(path, ext, rel)
100+
})
101+
}

audio/audio.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package audio
2+
3+
import (
4+
"os"
5+
)
6+
7+
type Audio interface {
8+
Play()
9+
Stop()
10+
}
11+
12+
func NewAudio(file *os.File, extension string) (Audio, error) {
13+
return newBeep(file, extension)
14+
}

audio/beep.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package audio
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/faiface/beep"
8+
"github.com/faiface/beep/mp3"
9+
"github.com/faiface/beep/speaker"
10+
"github.com/faiface/beep/wav"
11+
)
12+
13+
const mp3Extension = "mp3"
14+
const wavExtension = "wav"
15+
16+
type audioDecoder = func(f *os.File) (s beep.StreamSeekCloser, format beep.Format, err error)
17+
18+
var beepDecoders = map[string]audioDecoder{
19+
mp3Extension: func(f *os.File) (s beep.StreamSeekCloser, format beep.Format, err error) {
20+
return mp3.Decode(f)
21+
},
22+
wavExtension: func(f *os.File) (s beep.StreamSeekCloser, format beep.Format, err error) {
23+
return wav.Decode(f)
24+
}}
25+
26+
var _ Audio = (*Beep)(nil)
27+
28+
type Beep struct {
29+
streamer beep.StreamSeekCloser
30+
format beep.Format
31+
}
32+
33+
func (b Beep) Play() {
34+
err := speaker.Init(b.format.SampleRate, b.format.SampleRate.N(time.Second/10))
35+
if err != nil {
36+
return
37+
}
38+
speaker.Play(b.streamer)
39+
}
40+
41+
func (b Beep) Stop() {
42+
b.streamer.Close()
43+
}
44+
45+
func newBeep(file *os.File, extension string) (Beep, error) {
46+
streamer, format, err := beepDecoders[extension](file)
47+
if err != nil {
48+
return Beep{}, err
49+
}
50+
return Beep{
51+
streamer: streamer,
52+
format: format,
53+
}, nil
54+
}

game/backpack.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55

66
"candy/game/gameitem"
77
"candy/game/square"
8-
9-
"github.com/teamyapp/ui/graphics"
8+
"candy/graphics"
109

1110
"golang.org/x/image/font/basicfont"
1211
)

game/candy/candy.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
"candy/game/cell"
88
"candy/game/gameitem"
99
"candy/game/square"
10-
11-
"github.com/teamyapp/ui/graphics"
10+
"candy/graphics"
1211
)
1312

1413
var _ square.Square = (*Candy)(nil)

game/candy/exploding.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"candy/game/cell"
77
"candy/game/direction"
88
"candy/game/square"
9-
10-
"github.com/teamyapp/ui/graphics"
9+
"candy/graphics"
1110
)
1211

1312
const explodingTimeShort = 250 * time.Millisecond

game/candy/explosion.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package candy
22

33
import (
44
"candy/game/square"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
var explosionCenter = graphics.Bound{

game/candy/melting.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"time"
55

66
"candy/game/cell"
7-
8-
"github.com/teamyapp/ui/graphics"
7+
"candy/graphics"
98
)
109

1110
const meltingTime = 5 * time.Second

game/candy/state.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"time"
55

66
"candy/game/cell"
7-
8-
"github.com/teamyapp/ui/graphics"
7+
"candy/graphics"
98
)
109

1110
type state interface {

game/gameitem/gameitem.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package gameitem
22

33
import (
4+
"candy/graphics"
45
"candy/pubsub"
5-
6-
"github.com/teamyapp/ui/graphics"
76
)
87

98
type Type int

game/gamemap/map.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import (
66
"sync"
77
"time"
88

9+
"candy/assets"
910
"candy/game/candy"
1011
"candy/game/cell"
1112
"candy/game/gameitem"
1213
"candy/game/player"
1314
"candy/game/square"
15+
"candy/graphics"
1416
"candy/pubsub"
15-
16-
"github.com/teamyapp/ui/assets"
17-
"github.com/teamyapp/ui/graphics"
1817
)
1918

2019
const defaultRows = 12

game/marker/marker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package marker
33
import (
44
"time"
55

6-
"github.com/teamyapp/ui/graphics"
6+
"candy/graphics"
77
)
88

99
const YOffset = 5

game/player/draw.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package player
22

33
import (
44
"candy/game/direction"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
func draw(

game/player/jelly.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package player
33
import (
44
"time"
55

6-
"github.com/teamyapp/ui/graphics"
6+
"candy/graphics"
77
)
88

99
var JellyImageDuration = (180 * time.Millisecond).Nanoseconds()

game/player/player.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import (
66
"candy/game/direction"
77
"candy/game/marker"
88
"candy/game/square"
9+
"candy/graphics"
10+
"candy/input"
911
"candy/pubsub"
10-
11-
"github.com/teamyapp/ui/graphics"
12-
"github.com/teamyapp/ui/input"
1312
)
1413

1514
const spriteWidth = 48

game/player/standing.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package player
33
import (
44
"candy/game/direction"
55
"candy/game/square"
6+
"candy/input"
67
"candy/pubsub"
7-
8-
"github.com/teamyapp/ui/input"
98
)
109

1110
var _ state = (*standingState)(nil)

game/player/state.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55

66
"candy/game/direction"
77
"candy/game/square"
8+
"candy/graphics"
9+
"candy/input"
810
"candy/pubsub"
9-
10-
"github.com/teamyapp/ui/graphics"
11-
"github.com/teamyapp/ui/input"
1211
)
1312

1413
type state interface {

game/player/trapped.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"time"
55

66
"candy/game/direction"
7-
8-
"github.com/teamyapp/ui/graphics"
9-
"github.com/teamyapp/ui/input"
7+
"candy/graphics"
8+
"candy/input"
109
)
1110

1211
var _ state = (*trappedState)(nil)

game/player/walking.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"time"
55

66
"candy/game/direction"
7+
"candy/input"
78
"candy/pubsub"
8-
9-
"github.com/teamyapp/ui/input"
109
)
1110

1211
var nanoPerStep = (100 * time.Millisecond).Nanoseconds()

game/rightbar.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package game
22

33
import (
44
"candy/game/player"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
const playerStatusBarLeft = 56

game/square/square.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package square
22

33
import (
44
"candy/game/gameitem"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
const Width = 60

game/square/tile.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package square
22

33
import (
44
"candy/game/gameitem"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
const revealItemXOffset = 16

game/square/tilestate.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package square
22

33
import (
44
"candy/game/gameitem"
5-
6-
"github.com/teamyapp/ui/graphics"
5+
"candy/graphics"
76
)
87

98
type tileState interface {

0 commit comments

Comments
 (0)