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()
}
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)
f.Flush()
return f.Flush()
}
func (f *Field) setBlock(x, y int, blockType BlockType) {
f.blocks[x][y] = blockType
}
func (f *Field) Flush() {
func (f *Field) Flush() int {
flushedLineNum := 0
for j := fieldBlockNumY - 1; 0 <= j; j-- {
if f.flushLine(j + flushedLineNum) {
flushedLineNum++
}
}
return flushedLineNum
}
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 {
field *Field
rand *rand.Rand
currentPiece *Piece
currentPieceX int
currentPieceY int
currentPieceAngle Angle
nextPiece *Piece
landingCount int
field *Field
rand *rand.Rand
currentPiece *Piece
currentPieceX int
currentPieceY int
currentPieceYCarry int
currentPieceAngle Angle
nextPiece *Piece
landingCount int
currentFrame int
lines int
}
func NewGameScene() *GameScene {
@ -74,12 +77,18 @@ func (s *GameScene) initCurrentPiece(piece *Piece) {
x, y := s.currentPiece.InitialPosition()
s.currentPieceX = x
s.currentPieceY = y
s.currentPieceYCarry = 0
s.currentPieceAngle = Angle0
}
func (s *GameScene) Update(state *GameState) error {
const maxLandingCount = 60
func (s *GameScene) level() int {
return s.lines / 10
}
func (s *GameScene) Update(state *GameState) error {
s.currentFrame++
const maxLandingCount = 60
if s.currentPiece == nil {
s.initCurrentPiece(s.choosePiece())
}
@ -107,16 +116,26 @@ func (s *GameScene) Update(state *GameState) error {
s.currentPieceY = s.field.DropPiece(piece, x, y, angle)
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 {
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) {
s.landingCount += 10
} else {
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.nextPiece = nil
s.landingCount = 0