ebiten/example/blocks/game_scene.go

117 lines
2.8 KiB
Go
Raw Normal View History

2013-12-18 19:21:25 +01:00
package blocks
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2013-12-19 17:08:32 +01:00
"github.com/hajimehoshi/go-ebiten/ui"
2013-12-18 19:21:25 +01:00
"image/color"
"math/rand"
"time"
)
func init() {
texturePaths["empty"] = "images/blocks/empty.png"
}
type GameScene struct {
2013-12-19 17:08:32 +01:00
field *Field
rand *rand.Rand
currentPiece *Piece
currentPieceX int
currentPieceY int
currentPieceAngle Angle
nextPiece *Piece
2013-12-19 19:03:35 +01:00
landingCount int
2013-12-18 19:21:25 +01:00
}
func NewGameScene() *GameScene {
return &GameScene{
field: NewField(),
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
const emptyWidth = 16
const emptyHeight = 16
const fieldWidth = blockWidth * fieldBlockNumX
const fieldHeight = blockHeight * fieldBlockNumY
func (s *GameScene) choosePiece() *Piece {
2013-12-19 17:23:00 +01:00
num := NormalBlockTypeNum
blockType := BlockType(s.rand.Intn(num) + 1)
2013-12-18 19:21:25 +01:00
return Pieces[blockType]
}
func (s *GameScene) Update(state *GameState) {
2013-12-19 19:03:35 +01:00
const maxLandingCount = 30
2013-12-18 19:21:25 +01:00
if s.currentPiece == nil {
s.currentPiece = s.choosePiece()
2013-12-19 17:08:32 +01:00
x, y := s.currentPiece.InitialPosition()
s.currentPieceX = x
s.currentPieceY = y
s.currentPieceAngle = Angle0
}
2013-12-19 19:03:35 +01:00
piece := s.currentPiece
x := s.currentPieceX
y := s.currentPieceY
angle := s.currentPieceAngle
moved := false
2013-12-19 17:08:32 +01:00
if state.Input.StateForKey(ui.KeySpace) == 1 {
2013-12-19 19:03:35 +01:00
orig := angle
s.currentPieceAngle = s.field.RotatePieceRight(piece, x, y, angle)
moved = orig != angle
2013-12-19 17:08:32 +01:00
}
2013-12-19 17:23:00 +01:00
if l := state.Input.StateForKey(ui.KeyLeft);
l == 1 || (10 <= l && l % 2 == 0) {
2013-12-19 19:03:35 +01:00
orig := x
s.currentPieceX = s.field.MovePieceToLeft(piece, x, y, angle)
moved = orig != x
2013-12-19 17:08:32 +01:00
}
2013-12-19 17:23:00 +01:00
if r := state.Input.StateForKey(ui.KeyRight);
r == 1 || (10 <= r && r % 2 == 0) {
2013-12-19 19:03:35 +01:00
orig := y
s.currentPieceX = s.field.MovePieceToRight(piece, x, y, angle)
moved = orig != y
2013-12-18 19:21:25 +01:00
}
2013-12-19 17:23:00 +01:00
if d := state.Input.StateForKey(ui.KeyDown);
(d - 1) % 2 == 0 {
2013-12-19 19:03:35 +01:00
orig := y
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
moved = orig != y
}
if moved {
s.landingCount = 0
} else if !s.field.PieceDroppable(piece, x, y, angle) {
s.landingCount++
if maxLandingCount <= s.landingCount {
print("landing!\n")
// TODO: implement
s.currentPiece = nil
s.landingCount = 0
}
2013-12-19 17:23:00 +01:00
}
2013-12-18 19:21:25 +01:00
}
func (s *GameScene) Draw(context graphics.Context) {
context.Fill(0xff, 0xff, 0xff)
field := drawInfo.textures["empty"]
geoMat := matrix.IdentityGeometry()
2013-12-19 17:08:32 +01:00
geoMat.Scale(float64(fieldWidth)/float64(emptyWidth),
float64(fieldHeight)/float64(emptyHeight))
2013-12-18 19:21:25 +01:00
geoMat.Translate(20, 20) // magic number?
colorMat := matrix.IdentityColor()
colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
context.DrawTexture(field, geoMat, colorMat)
geoMat = matrix.IdentityGeometry()
geoMat.Translate(20, 20)
s.field.Draw(context, geoMat)
if s.currentPiece != nil {
2013-12-19 17:08:32 +01:00
s.currentPiece.Draw(context, 20, 20,
s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
2013-12-18 19:21:25 +01:00
}
}