blocks: Introduce level and gravity

This commit is contained in:
Hajime Hoshi 2014-12-29 15:24:20 +09:00
parent bb0dfcfc3e
commit 0346e81161
2 changed files with 35 additions and 15 deletions

View File

@ -75,22 +75,23 @@ func (f *Field) RotatePieceRight(piece *Piece, x, y int, angle Angle) Angle {
return angle.RotateRight() return angle.RotateRight()
} }
func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) { func (f *Field) AbsorbPiece(piece *Piece, x, y int, angle Angle) int {
piece.AbsorbInto(f, x, y, angle) piece.AbsorbInto(f, x, y, angle)
f.Flush() return f.Flush()
} }
func (f *Field) setBlock(x, y int, blockType BlockType) { func (f *Field) setBlock(x, y int, blockType BlockType) {
f.blocks[x][y] = blockType f.blocks[x][y] = blockType
} }
func (f *Field) Flush() { func (f *Field) Flush() int {
flushedLineNum := 0 flushedLineNum := 0
for j := fieldBlockNumY - 1; 0 <= j; j-- { for j := fieldBlockNumY - 1; 0 <= j; j-- {
if f.flushLine(j + flushedLineNum) { if f.flushLine(j + flushedLineNum) {
flushedLineNum++ flushedLineNum++
} }
} }
return flushedLineNum
} }
func (f *Field) flushLine(j int) bool { func (f *Field) flushLine(j int) bool {

View File

@ -43,14 +43,17 @@ func drawRect(r *ebiten.Image, x, y, width, height int) error {
} }
type GameScene struct { type GameScene struct {
field *Field field *Field
rand *rand.Rand rand *rand.Rand
currentPiece *Piece currentPiece *Piece
currentPieceX int currentPieceX int
currentPieceY int currentPieceY int
currentPieceAngle Angle currentPieceYCarry int
nextPiece *Piece currentPieceAngle Angle
landingCount int nextPiece *Piece
landingCount int
currentFrame int
lines int
} }
func NewGameScene() *GameScene { func NewGameScene() *GameScene {
@ -74,12 +77,18 @@ func (s *GameScene) initCurrentPiece(piece *Piece) {
x, y := s.currentPiece.InitialPosition() x, y := s.currentPiece.InitialPosition()
s.currentPieceX = x s.currentPieceX = x
s.currentPieceY = y s.currentPieceY = y
s.currentPieceYCarry = 0
s.currentPieceAngle = Angle0 s.currentPieceAngle = Angle0
} }
func (s *GameScene) Update(state *GameState) error { func (s *GameScene) level() int {
const maxLandingCount = 60 return s.lines / 10
}
func (s *GameScene) Update(state *GameState) error {
s.currentFrame++
const maxLandingCount = 60
if s.currentPiece == nil { if s.currentPiece == nil {
s.initCurrentPiece(s.choosePiece()) s.initCurrentPiece(s.choosePiece())
} }
@ -107,16 +116,26 @@ func (s *GameScene) Update(state *GameState) error {
s.currentPieceY = s.field.DropPiece(piece, x, y, angle) s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
moved = y != s.currentPieceY moved = y != s.currentPieceY
} }
// Drop the current piece with gravity.
s.currentPieceYCarry += 2*s.level() + 1
for 60 <= s.currentPieceYCarry {
s.currentPieceYCarry -= 60
s.currentPieceY = s.field.DropPiece(piece, s.currentPieceX, s.currentPieceY, angle)
moved = y != s.currentPieceY
}
if moved { if moved {
s.landingCount = 0 s.landingCount = 0
} else if !s.field.PieceDroppable(piece, x, y, angle) { } else if !s.field.PieceDroppable(piece, s.currentPieceX, s.currentPieceY, angle) {
if 0 < state.Input.StateForKey(ebiten.KeyDown) { if 0 < state.Input.StateForKey(ebiten.KeyDown) {
s.landingCount += 10 s.landingCount += 10
} else { } else {
s.landingCount++ s.landingCount++
} }
if maxLandingCount <= s.landingCount { if maxLandingCount <= s.landingCount {
s.field.AbsorbPiece(piece, x, y, angle) lines := s.field.AbsorbPiece(piece, s.currentPieceX, s.currentPieceY, angle)
s.lines += lines
s.initCurrentPiece(s.nextPiece) s.initCurrentPiece(s.nextPiece)
s.nextPiece = nil s.nextPiece = nil
s.landingCount = 0 s.landingCount = 0