ebiten/example/blocks/titlescene.go

84 lines
2.3 KiB
Go
Raw Normal View History

2014-12-09 15:16:04 +01:00
/*
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.
*/
2014-12-08 17:35:35 +01:00
package blocks
2013-12-18 10:05:28 +01:00
import (
2014-12-09 14:09:22 +01:00
"github.com/hajimehoshi/ebiten"
2013-12-18 10:05:28 +01:00
"image/color"
)
2013-12-18 19:21:25 +01:00
func init() {
2014-12-08 17:35:35 +01:00
texturePaths["background"] = "images/blocks/background.png"
2013-12-18 19:21:25 +01:00
}
2013-12-18 10:05:28 +01:00
type TitleScene struct {
count int
}
func NewTitleScene() *TitleScene {
return &TitleScene{}
}
2013-12-18 19:21:25 +01:00
func (s *TitleScene) Update(state *GameState) {
2013-12-18 10:05:28 +01:00
s.count++
2014-12-09 14:09:22 +01:00
if state.Input.StateForKey(ebiten.KeySpace) == 1 {
2013-12-18 19:21:25 +01:00
state.SceneManager.GoTo(NewGameScene())
}
2013-12-18 10:05:28 +01:00
}
2014-12-09 14:09:22 +01:00
func (s *TitleScene) Draw(context ebiten.GraphicsContext, textures *Textures) {
2014-05-03 08:25:41 +02:00
drawTitleBackground(context, textures, s.count)
drawLogo(context, textures, "BLOCKS")
2013-12-18 19:21:25 +01:00
message := "PRESS SPACE TO START"
2014-01-07 15:29:12 +01:00
x := (ScreenWidth - textWidth(message)) / 2
y := ScreenHeight - 48
2014-05-03 08:25:41 +02:00
drawTextWithShadow(context, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff})
2013-12-18 10:05:28 +01:00
}
2014-12-09 14:09:22 +01:00
func drawTitleBackground(context ebiten.GraphicsContext, textures *Textures, c int) {
2013-12-18 10:05:28 +01:00
const textureWidth = 32
const textureHeight = 32
2014-05-03 08:25:41 +02:00
backgroundTextureId := textures.GetTexture("background")
2014-12-09 14:09:22 +01:00
parts := []ebiten.TexturePart{}
2013-12-18 10:05:28 +01:00
for j := -1; j < ScreenHeight/textureHeight+1; j++ {
for i := 0; i < ScreenWidth/textureWidth+1; i++ {
2014-12-09 14:09:22 +01:00
parts = append(parts, ebiten.TexturePart{
2013-12-18 19:21:25 +01:00
LocationX: i * textureWidth,
LocationY: j * textureHeight,
2014-12-09 14:09:22 +01:00
Source: ebiten.Rect{0, 0, textureWidth, textureHeight},
2013-12-18 10:05:28 +01:00
})
}
}
2014-01-07 15:29:12 +01:00
dx := (-c / 4) % textureWidth
dy := (c / 4) % textureHeight
2014-12-09 14:09:22 +01:00
geo := ebiten.GeometryMatrixI()
2013-12-18 10:05:28 +01:00
geo.Translate(float64(dx), float64(dy))
2014-12-09 14:09:22 +01:00
clr := ebiten.ColorMatrixI()
2014-05-03 06:58:18 +02:00
context.Texture(backgroundTextureId).Draw(parts, geo, clr)
2013-12-18 10:05:28 +01:00
}
2014-12-09 14:09:22 +01:00
func drawLogo(context ebiten.GraphicsContext, textures *Textures, str string) {
2013-12-18 19:21:25 +01:00
scale := 4
textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2
y := 32
2014-05-03 08:25:41 +02:00
drawTextWithShadow(context, textures, str, x, y, scale, color.RGBA{0x00, 0x00, 0x80, 0xff})
2013-12-18 10:05:28 +01:00
}