forked from hajimehoshi/ebiten
/
titlescene.go
104 lines (89 loc) · 2.55 KB
/
titlescene.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package blocks
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"image/color"
)
var imageBackground *ebiten.Image
func init() {
var err error
imageBackground, _, err = ebitenutil.NewImageFromFile("images/blocks/background.png", ebiten.FilterNearest)
if err != nil {
panic(err)
}
}
type titleImageParts struct {
image *ebiten.Image
count int
}
func (t *titleImageParts) Len() int {
w, h := t.image.Size()
return (ScreenWidth/w + 1) * (ScreenHeight/h + 2)
}
func (t *titleImageParts) Dst(i int) (x0, y0, x1, y1 int) {
w, h := t.image.Size()
i, j := i%(ScreenWidth/w+1), i/(ScreenWidth/w+1)-1
dx := (-t.count / 4) % w
dy := (t.count / 4) % h
dstX := i*w + dx
dstY := j*h + dy
return dstX, dstY, dstX + w, dstY + h
}
func (t *titleImageParts) Src(i int) (x0, y0, x1, y1 int) {
w, h := t.image.Size()
return 0, 0, w, h
}
type TitleScene struct {
count int
parts *titleImageParts
}
func NewTitleScene() *TitleScene {
return &TitleScene{
parts: &titleImageParts{imageBackground, 0},
}
}
func (s *TitleScene) Update(state *GameState) error {
s.count++
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
state.SceneManager.GoTo(NewGameScene())
}
return nil
}
func (s *TitleScene) Draw(r *ebiten.Image) error {
if err := s.drawTitleBackground(r, s.count); err != nil {
return err
}
if err := drawLogo(r, "BLOCKS"); err != nil {
return err
}
message := "PRESS SPACE TO START"
x := (ScreenWidth - textWidth(message)) / 2
y := ScreenHeight - 48
return drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
}
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) error {
s.parts.count = c
return r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
ImageParts: s.parts,
})
}
func drawLogo(r *ebiten.Image, str string) error {
scale := 4
textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2
y := 32
return drawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff})
}